Skip to content

官档,https://kubernetes.io/zh-cn/docs/concepts/security/pod-security-standards/

1. 禁止root

#帮助
kubectl explain pod.spec.securityContext
#帮助
kubectl explain pod.spec.securityContext
yaml
apiVersion: v1
kind: Pod
metadata:
  name: <name>
spec:
  securityContext:
    runAsUser: <UID higher than 1000>
    runAsGroup: <UID higher than 3000>
apiVersion: v1
kind: Pod
metadata:
  name: <name>
spec:
  securityContext:
    runAsUser: <UID higher than 1000>
    runAsGroup: <UID higher than 3000>

2. 禁用allowPrivilegeEscalation

allowPrivilegeEscalation=true 表示容器的任何子进程都可以获得比父进程更多的权限。最好将其设置为 false,以确保 RunAsUser 命令不能绕过其现有的权限集。

yaml
apiVersion: v1
kind: Pod
metadata:
  name: <Pod name>
spec:
  containers:
  - name: <container name>
  image: <image>
    securityContext:
      allowPrivilegeEscalation: false
apiVersion: v1
kind: Pod
metadata:
  name: <Pod name>
spec:
  containers:
  - name: <container name>
  image: <image>
    securityContext:
      allowPrivilegeEscalation: false

3.Pod安全上下文

cat 18-pod-securityContext.yaml 
apiVersion: v1
kind: Pod
metadata:
   name: securitycontext-004
spec:
  containers:
  - name: c1
    image: harbor.oldboyedu.com/tools/centos7-iptabls:v0.1
    # args:
    # - tail
    # - -f
    # - /etc/hosts
    # 配置Pod的安全相关属性
    securityContext:
      # 配置容器为特权容器,若配置了特权容器,可能对capabilities测试有影响哟!
      #privileged: true
      # 自定义LINUX内核特性
      # 推荐阅读:
      #   https://man7.org/linux/man-pages/man7/capabilities.7.html
      #   https://docs.docker.com/compose/compose-file/compose-file-v3/#cap_add-cap_drop
      capabilities:
        # 添加所有的Linux内核功能
        add:
        - ALL
        # 移除指定Linux内核特性
        drop:
        # 代表禁用网络管理的配置,
        # - NET_ADMIN
        # 代表禁用UID和GID,表示你无法使用chown命令哟
        # 比如执行"useradd oldboyedu"时会创建"/home/oldboyedu"目录,并执行chown修改目录权限为"oldboyedu"用户,此时你会发现可以创建用户成功,但无法修改"/home/oldboyedu"目录的属主和属组。
        - CHOWN
        # # 代表禁用chroot命令
        - SYS_CHROOT
      # 如果容器的进程以root身份运行,则禁止容器启动!
      # runAsNonRoot: true
      # 指定运行程序的用户UID,注意,该用户的UID必须存在!
      # runAsUser: 666
cat 18-pod-securityContext.yaml 
apiVersion: v1
kind: Pod
metadata:
   name: securitycontext-004
spec:
  containers:
  - name: c1
    image: harbor.oldboyedu.com/tools/centos7-iptabls:v0.1
    # args:
    # - tail
    # - -f
    # - /etc/hosts
    # 配置Pod的安全相关属性
    securityContext:
      # 配置容器为特权容器,若配置了特权容器,可能对capabilities测试有影响哟!
      #privileged: true
      # 自定义LINUX内核特性
      # 推荐阅读:
      #   https://man7.org/linux/man-pages/man7/capabilities.7.html
      #   https://docs.docker.com/compose/compose-file/compose-file-v3/#cap_add-cap_drop
      capabilities:
        # 添加所有的Linux内核功能
        add:
        - ALL
        # 移除指定Linux内核特性
        drop:
        # 代表禁用网络管理的配置,
        # - NET_ADMIN
        # 代表禁用UID和GID,表示你无法使用chown命令哟
        # 比如执行"useradd oldboyedu"时会创建"/home/oldboyedu"目录,并执行chown修改目录权限为"oldboyedu"用户,此时你会发现可以创建用户成功,但无法修改"/home/oldboyedu"目录的属主和属组。
        - CHOWN
        # # 代表禁用chroot命令
        - SYS_CHROOT
      # 如果容器的进程以root身份运行,则禁止容器启动!
      # runAsNonRoot: true
      # 指定运行程序的用户UID,注意,该用户的UID必须存在!
      # runAsUser: 666

https://www.elastic.co/cn/support/matrix#matrix_compatibility