Kubernetes
  • 序言
  • 基礎入門
    • Kubernetes 簡介
    • Kubernetes 基本概念
    • Kubernetes 101
    • Kubernetes 201
    • Kubernetes 集群
  • 核心原理
    • 核心原理
    • 架構原理
    • 設計理念
    • 核心組件
      • etcd
      • kube-apiserver
      • kube-scheduler
      • kube-controller-manager
      • kubelet
      • kube-proxy
      • kube-dns
      • Federation
      • kubeadm
      • hyperkube
      • kubectl
    • 資源對象
      • Autoscaling
      • ConfigMap
      • CronJob
      • CustomResourceDefinition
      • DaemonSet
      • Deployment
      • Ingress
      • Job
      • LocalVolume
      • Namespace
      • NetworkPolicy
      • Node
      • PersistentVolume
      • Pod
      • PodPreset
      • ReplicaSet
      • Resource Quota
      • Secret
      • SecurityContext
      • Service
      • ServiceAccount
      • StatefulSet
      • Volume
  • 部署配置
    • 部署指南
    • kubectl 安裝
    • 單機部署
    • 特性開關
    • 最佳配置
    • 版本支持
    • 集群部署
      • kubeadm
      • kops
      • Kubespray
      • Azure
      • Windows
      • LinuxKit
      • kubeasz
    • 附加組件
      • Addon-manager
      • DNS
      • Dashboard
      • 監控
      • 日誌
      • Metrics
      • GPU
      • Cluster Autoscaler
      • ip-masq-agent
    • Kubernetes-The-Hard-Way
      • 準備部署環境
      • 安裝必要工具
      • 創建計算資源
      • 配置創建證書
      • 配置生成配置
      • 配置生成密鑰
      • 部署 Etcd 群集
      • 部署控制節點
      • 部署計算節點
      • 配置 Kubectl
      • 配置網絡路由
      • 部署 DNS 擴展
      • 煙霧測試
      • 刪除集群
  • 插件擴展
    • API 擴展
      • Aggregation
      • CustomResourceDefinition
    • 訪問控制
      • 認證
      • RBAC 授權
      • 准入控制
    • Scheduler 擴展
    • 網絡插件
      • CNI
      • Flannel
      • Calico
      • Weave
      • Cilium
      • OVN
      • Contiv
      • SR-IOV
      • Romana
      • OpenContrail
      • Kuryr
    • 運行時插件 CRI
      • CRI-tools
      • Frakti
    • 存儲插件
      • 容器存儲接口 CSI
      • FlexVolume
      • glusterfs
    • 網絡策略
    • Ingress Controller
      • Ingress + Letsencrypt
      • minikube Ingress
      • Traefik Ingress
      • Keepalived-VIP
    • Cloud Provider 擴展
    • Device 插件
  • 服務治理
    • 服務治理
      • 一般準則
      • 滾動升級
      • Helm
      • Operator
      • Service Mesh
      • Linkerd
      • Linkerd2
    • Istio
      • 安裝
      • 流量管理
      • 安全管理
      • 策略管理
      • 度量管理
      • 排錯
      • 社區
    • Devops
      • Draft
      • Jenkins X
      • Spinnaker
      • Kompose
      • Skaffold
      • Argo
      • Flux GitOps
  • 實踐案例
    • 實踐概覽
    • 資源控制
    • 集群高可用
    • 應用高可用
    • 調試
    • 端口映射
    • 端口轉發
    • 用戶管理
    • GPU
    • HugePage
    • 安全
    • 審計
    • 備份恢復
    • 證書輪換
    • 大規模集群
    • 大數據與機器學習
      • Spark
      • Tensorflow
    • Serverless
  • 排錯指南
    • 排錯概覽
    • 集群排錯
    • Pod 排錯
    • 網絡排錯
    • PV 排錯
      • AzureDisk
      • AzureFile
    • Windows 排錯
    • 雲平臺排錯
      • Azure
    • 排錯工具
  • 社區貢獻
    • 開發指南
    • 單元測試和集成測試
    • 社區貢獻
  • 附錄
    • 生態圈
    • 學習資源
    • 國內鏡像
    • 如何貢獻
    • 參考文檔
Powered by GitBook
On this page
  • Container-level Security Context
  • Pod-level Security Context
  • Pod Security Policies(PSP)
  • API 版本對照表
  • 支持的控制項
  • 示例
  • SELinux
  • 開啓與關閉 SELinux
  • 示例
  • 參考文檔
  1. 核心原理
  2. 資源對象

SecurityContext

PreviousSecretNextService

Last updated 1 year ago

Security Context 的目的是限制不可信容器的行爲,保護系統和其他容器不受其影響。

Kubernetes 提供了三種配置 Security Context 的方法:

  • Container-level Security Context:僅應用到指定的容器

  • Pod-level Security Context:應用到 Pod 內所有容器以及 Volume

  • Pod Security Policies(PSP):應用到集群內部所有 Pod 以及 Volume

Container-level Security Context

僅應用到指定的容器上,並且不會影響 Volume。比如設置容器運行在特權模式:

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
    - name: hello-world-container
      # The container definition
      # ...
      securityContext:
        privileged: true

Pod-level Security Context

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
  # specification of the pod's containers
  # ...
  securityContext:
    fsGroup: 1234
    supplementalGroups: [5678]
    seLinuxOptions:
      level: "s0:c123,c456"

Pod Security Policies(PSP)

Pod Security Policies(PSP)是集群級的 Pod 安全策略,自動爲集群內的 Pod 和 Volume 設置 Security Context。

使用 PSP 需要 API Server 開啓 extensions/v1beta1/podsecuritypolicy,並且配置 PodSecurityPolicy admission 控制器。

API 版本對照表

Kubernetes 版本
Extension 版本

v1.5-v1.15

extensions/v1beta1

v1.10+

policy/v1beta1

v1.21

deprecated

支持的控制項

控制項
說明

privileged

運行特權容器

defaultAddCapabilities

可添加到容器的 Capabilities

requiredDropCapabilities

會從容器中刪除的 Capabilities

allowedCapabilities

允許使用的 Capabilities 列表

volumes

控制容器可以使用哪些 volume

hostNetwork

允許使用 host 網絡

hostPorts

允許的 host 端口列表

hostPID

使用 host PID namespace

hostIPC

使用 host IPC namespace

seLinux

SELinux Context

runAsUser

user ID

supplementalGroups

允許的補充用戶組

fsGroup

volume FSGroup

readOnlyRootFilesystem

只讀根文件系統

allowedHostPaths

允許 hostPath 插件使用的路徑列表

allowedFlexVolumes

允許使用的 flexVolume 插件列表

allowPrivilegeEscalation

defaultAllowPrivilegeEscalation

默認是否允許特權升級

示例

限制容器的 host 端口範圍爲 8000-8080:

apiVersion: extensions/v1beta1
kind: PodSecurityPolicy
metadata:
  name: permissive
spec:
  seLinux:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  runAsUser:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  hostPorts:
  - min: 8000
    max: 8080
  volumes:
  - '*'

限制只允許使用 lvm 和 cifs 等 flexVolume 插件:

apiVersion: extensions/v1beta1
kind: PodSecurityPolicy
metadata:
  name: allow-flex-volumes
spec:
  fsGroup:
    rule: RunAsAny
  runAsUser:
    rule: RunAsAny
  seLinux:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  volumes:
    - flexVolume
  allowedFlexVolumes:
    - driver: example/lvm
    - driver: example/cifs

SELinux

SELinux (Security-Enhanced Linux) 是一種強制訪問控制(mandatory access control)的實現。它的作法是以最小權限原則(principle of least privilege)爲基礎,在 Linux 核心中使用 Linux 安全模塊(Linux Security Modules)。SELinux 主要由美國國家安全局開發,並於 2000 年 12 月 22 日發行給開放源代碼的開發社區。

可以通過 runcon 來爲進程設置安全策略,ls 和 ps 的 - Z 參數可以查看文件或進程的安全策略。

開啓與關閉 SELinux

修改 / etc/selinux/config 文件方法:

  • 開啓:SELINUX=enforcing

  • 關閉:SELINUX=disabled

通過命令臨時修改:

  • 開啓:setenforce 1

  • 關閉:setenforce 0

查詢 SELinux 狀態:

$ getenforce

示例

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
  - image: gcr.io/google_containers/busybox:1.24
    name: test-container
    command:
    - sleep
    - "6000"
    volumeMounts:
    - mountPath: /mounted_volume
      name: test-volume
  restartPolicy: Never
  hostPID: false
  hostIPC: false
  securityContext:
    seLinuxOptions:
      level: "s0:c2,c3"
  volumes:
  - name: test-volume
    emptyDir: {}

這會自動給 docker 容器生成如下的 HostConfig.Binds:

/var/lib/kubelet/pods/f734678c-95de-11e6-89b0-42010a8c0002/volumes/kubernetes.io~empty-dir/test-volume:/mounted_volume:Z
/var/lib/kubelet/pods/f734678c-95de-11e6-89b0-42010a8c0002/volumes/kubernetes.io~secret/default-token-88xxa:/var/run/secrets/kubernetes.io/serviceaccount:ro,Z
/var/lib/kubelet/pods/f734678c-95de-11e6-89b0-42010a8c0002/etc-hosts:/etc/hosts

對應的 volume 也都會正確設置 SELinux:

$ ls -Z /var/lib/kubelet/pods/f734678c-95de-11e6-89b0-42010a8c0002/volumes
drwxr-xr-x. root root unconfined_u:object_r:svirt_sandbox_file_t:s0:c2,c3 kubernetes.io~empty-dir
drwxr-xr-x. root root unconfined_u:object_r:svirt_sandbox_file_t:s0:c2,c3 kubernetes.io~secret

參考文檔

應用到 Pod 內所有容器,並且還會影響 Volume(包括 fsGroup 和 selinuxOptions)。

由於 中從代碼庫中刪除。PodSecurityPolicy API 不夠靈活、認證模型不夠完善且配置更新繁瑣等缺陷,PodSecurityPolicy 已在 v1.21 正式,並將在 v1.25 中從代碼庫中刪除。已經使用 PodSecurityPolicy 的用戶推薦遷移到 。

允許容器進程設置

Container-level Security Context
Pod-level Security Context
棄用
Open Policy Agent
Kubernetes Pod Security Policies
no_new_privs