Pod 安全策略
Kubernetes pod 安全策略准入控制器针对一系列规则验证 pod 创建和更新请求。预设情况下,Amazon EKS 集群附带完全宽松的安全策略(没有任何限制)。有关更多信息,请参阅 Kubernetes 文档中的 Pod 安全策略
从 Kubernetes v1.21 开始弃用此功能。根据 Kubernetes 弃用指南,PodSecurityPolicy 将可用于多个 Kubernetes 版本。要了解详情,请参阅 PodSecurityPolicy 弃用:过去、现在和将来
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 安全策略
如果为集群创建更加限制的策略,创建后,可以删除默认 Amazon EKS eks.privileged
Pod 安全策略以启用您的自定义策略。
如果您使用的是版本 1.7.0 或更高版本的 CNI 插件,并将自定义 Pod 安全策略分配给用于 Daemonset 部署的 aws-node
Pod 的 aws-node
Kubernetes 服务账户,则该策略的 allowedCapabilities
部分必须具有 NET_ADMIN
,而且策略的 spec
必须具有 hostNetwork: true
和 privileged: true
。
删除默认 pod 安全策略
-
创建一个名为
的文件,具有 安装或恢复默认 Pod 安全策略 中示例文件的内容。privileged-podsecuritypolicy.yaml
-
使用以下命令删除 YAML。这将删除默认 Pod 安全策略、
ClusterRole
以及相关ClusterRoleBinding
。kubectl delete -f
privileged-podsecuritypolicy.yaml
安装或恢复默认 Pod 安全策略
如果要从 Kubernetes 的早期版本升级,或者您已修改或删除默认 Amazon EKS eks.privileged
Pod 安全策略,则可按照以下步骤还原它。
安装或恢复默认 Pod 安全策略
-
创建以下内容的名为
的文件。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
-
使用以下命令应用 YAML。
kubectl apply -f
privileged-podsecuritypolicy.yaml