1. 程式人生 > >Kubernetes(k8s)中文文件 名詞解釋:Security Context和PSP_Kubernetes中文社群

Kubernetes(k8s)中文文件 名詞解釋:Security Context和PSP_Kubernetes中文社群

Security Context

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

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

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

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,並且配置PodSecurityPolicyadmission控制器。

支援的控制項

控制項 說明
privileged 執行特權容器
defaultAddCapabilities 可新增到容器的Capabilities
requiredDropCapabilities 會從容器中刪除的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 只讀根檔案系統

示例

限制容器的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:
  - '*'

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

參考:https://feisky.gitbooks.io/kubernetes/concepts/security-context.html25

K8S中文社群微信公眾號