容器组(pod)安全策略 - Amazon EKS
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

容器组(pod)安全策略

Kubernetes Pod 安全策略准入控制器根据一组规则验证 Pod 创建和更新请求。预设情况下,Amazon EKS 集群附带完全宽松的安全策略(没有任何限制)。有关更多信息,请参阅 Kubernetes 文档中的容器组(pod)安全策略

注意

PodSecurityPolicy(PSP)在 Kubernetes 版本 1.21 中已弃用,且已在 Kubernetes 1.25 中删除。PSPs 将被容器组(pod)安全准入(PSA)取代,其是一种内置的准入控制器,可实施容器组(pod)安全标准(PSS)中列出的安全控制。PSA 和 PSS 都处于测试版功能状态,默认情况下在 Amazon EKS 中处于启用状态。为了解决 1.25 版中的 PSP 删除问题,我们建议您在 Amazon EKS 中实施 PSS。有关更多信息,请参阅 Amazon 博客上的 Implementing Pod Security Standards in Amazon EKS(在 Amazon EKS 中实施容器组(pod)安全标准)。

Amazon EKS 默认 Pod 安全策略

带 Kubernetes 版本 1.13 或更高版本的 Amazon EKS 集群具有名为 eks.privileged 的默认 Pod 安全策略。此策略对于系统中可以接受什么类型的 Pod 没有限制,这相当于在禁用 PodSecurityPolicy 控制器的情况下运行 Kubernetes。

注意

创建此策略是为了与未启用 PodSecurityPolicy 控制器的集群保持向后兼容性。您可以针对集群、各个命名空间和服务账户创建更具有限制性的策略,然后删除默认策略以启用这些更具有限制性的策略。

您可以使用以下命令查看默认策略。

kubectl get psp eks.privileged

示例输出如下。

NAME PRIV CAPS SELINUX RUNASUSER FSGROUP SUPGROUP READONLYROOTFS VOLUMES eks.privileged true * RunAsAny RunAsAny RunAsAny RunAsAny false *

有关更多详细信息,您可以使用以下命令描述此策略。

kubectl describe psp eks.privileged

示例输出如下。

Name:  eks.privileged

Settings:
  Allow Privileged:                       true
  Allow Privilege Escalation:             0xc0004ce5f8
  Default Add Capabilities:               <none>
  Required Drop Capabilities:             <none>
  Allowed Capabilities:                   *
  Allowed Volume Types:                   *
  Allow Host Network:                     true
  Allow Host Ports:                       0-65535
  Allow Host PID:                         true
  Allow Host IPC:                         true
  Read Only Root Filesystem:              false
  SELinux Context Strategy: RunAsAny
    User:                                 <none>
    Role:                                 <none>
    Type:                                 <none>
    Level:                                <none>
  Run As User Strategy: RunAsAny
    Ranges:                               <none>
  FSGroup Strategy: RunAsAny
    Ranges:                               <none>
  Supplemental Groups Strategy: RunAsAny
    Ranges:                               <none>

您可以在完整 YAML 文件中查看 eks.privileged Pod 安全策略、其集群角色以及 安装或恢复默认的 Pod 安全策略 中绑定的集群角色。

删除默认的 Amazon EKS Pod 安全策略

如果为 Pods 创建更加限制的策略,创建后,可以删除默认的 Amazon EKS eks.privileged Pod 安全策略,以启用您的自定义策略。

重要

如果您使用的是版本 1.7.0 或更高版本的 CNI 插件,并将自定义 Pod 安全策略分配给用于 Daemonset 部署的 aws-node Pods 的 aws-node Kubernetes 服务账户,则该策略的 allowedCapabilities 部分必须具有 NET_ADMIN,而且策略的 spec 必须具有 hostNetwork: trueprivileged: true

删除默认的 Pod 安全策略
  1. 创建一个名为 privileged-podsecuritypolicy.yaml 的文件,具有 安装或恢复默认的 Pod 安全策略 中示例文件的内容。

  2. 使用以下命令删除 YAML。这将删除默认的 Pod 安全策略、ClusterRole 以及与其相关联的 ClusterRoleBinding

    kubectl delete -f privileged-podsecuritypolicy.yaml

安装或恢复默认的 Pod 安全策略

如果要从 Kubernetes 的早期版本升级,或者您已修改或删除默认的 Amazon EKS eks.privileged Pod 安全策略,则可按照以下步骤恢复该策略。

安装或恢复默认的 Pod 安全策略
  1. 创建以下内容的名为 privileged-podsecuritypolicy.yaml 的文件。

    apiVersion: policy/v1beta1 kind: PodSecurityPolicy metadata: name: eks.privileged annotations: kubernetes.io/description: 'privileged allows full unrestricted access to Pod features, as if the PodSecurityPolicy controller was not enabled.' seccomp.security.alpha.kubernetes.io/allowedProfileNames: '*' labels: kubernetes.io/cluster-service: "true" eks.amazonaws.com/component: pod-security-policy spec: privileged: true allowPrivilegeEscalation: true allowedCapabilities: - '*' volumes: - '*' hostNetwork: true hostPorts: - min: 0 max: 65535 hostIPC: true hostPID: true runAsUser: rule: 'RunAsAny' seLinux: rule: 'RunAsAny' supplementalGroups: rule: 'RunAsAny' fsGroup: rule: 'RunAsAny' readOnlyRootFilesystem: false --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: eks:podsecuritypolicy:privileged labels: kubernetes.io/cluster-service: "true" eks.amazonaws.com/component: pod-security-policy rules: - apiGroups: - policy resourceNames: - eks.privileged resources: - podsecuritypolicies verbs: - use --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: eks:podsecuritypolicy:authenticated annotations: kubernetes.io/description: 'Allow all authenticated users to create privileged Pods.' labels: kubernetes.io/cluster-service: "true" eks.amazonaws.com/component: pod-security-policy roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: eks:podsecuritypolicy:privileged subjects: - kind: Group apiGroup: rbac.authorization.k8s.io name: system:authenticated
  2. 使用以下命令应用 YAML。

    kubectl apply -f privileged-podsecuritypolicy.yaml