本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Pod 安全政策
Kubernetes Pod 安全政策許可控制器,會根據一套規則驗證建立和更新 Pod 的請求。根據預設,Amazon EKS 叢集隨附於一套完全寬鬆的安全政策,不受任何限制。如需詳細資訊,請參閱 Kubernetes 文件中的 Pod 安全政策
注意
PodSecurityPolicy
(PSP) 已於 Kubernetes 版本 1.21
中棄用並在 Kubernetes 1.25
中移除。PSPs 正為 Pod 安全許可 (PSA)1.25
中移除的問題,我們建議您在 Amazon EKS 中實作 PSS。如需詳細資訊,請參閱 AWS 部落格上的 Implementing Pod Security Standards in Amazon EKS
Amazon EKS 預設 Pod 安全政策
使用 Kubernetes 版本 1.13
或更新版本的 Amazon EKS 叢集有一個名為 Pod 的預設 eks.privileged
安全政策。此政策針對系統可接受哪些類型的 Pod 不設限制,等同於在停用 Kubernetes 控制器的情況下執行 PodSecurityPolicy
。
注意
此政策的建立,是為了讓未啟用 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>
您可以在 eks.privileged
中檢視 Pod 安裝或還原預設 Pod 安全政策 安全政策、叢集角色和叢集角色連結的完整 YAML 檔案。
刪除預設 Amazon EKS Pod 安全政策
在為 Pods 建立更多限制性政策後,您可以刪除預設的 Amazon EKS eks.privileged
Pod 安全政策,以便啟用您的自訂政策。
重要
如果您使用 CNI 外掛程式版本 1.7.0
或更新版本,並將自訂 Pod 安全政策指派給用於 Daemonset 部署的 aws-node
Pods 的 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