すべてのプロダクト
Search
ドキュメントセンター

Container Service for Kubernetes:[非推奨] ポッドのセキュリティポリシーの使用

最終更新日:Dec 13, 2024

ポッドセキュリティポリシーは、ポリシーで定義されているルールに基づいて、クラスター内でポッドを作成および更新する要求を検証するアドミッションコントローラーリソースです。 ポッドを作成または更新する要求がルールを満たさない場合、要求は拒否され、エラーが返されます。 このトピックでは、Container Service for Kubernetes (ACK) クラスターでポッドセキュリティポリシーを使用する方法について説明します。

前提条件

ネットワークポリシーを設定する前に、次の手順を実行していることを確認してください。

デフォルトのポッドセキュリティポリシー

デフォルトでは、ポッドセキュリティポリシーコントロールは、標準マネージドKubernetesクラスター (Kubernetes 1.16.6) および標準専用Kubernetesクラスター (Kubernetes 1.16.6) に対して有効になっています。 ack.privilegedという名前のポッドセキュリティポリシーが自動的に作成されます。 このセキュリティポリシーは、すべてのタイプのポッドを受け入れます。 これにより、クラスターのポッドセキュリティポリシー制御が無効になっている場合と同じ効果が得られます。

デフォルトのポッドセキュリティポリシーの照会

$ kubectl get psp ack.privileged
NAME             PRIV   CAPS   SELINUX    RUNASUSER   FSGROUP    SUPGROUP   READONLYROOTFS   VOLUMES
ack.privileged   true   *      RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            *

デフォルトのポッドセキュリティポリシーに関する詳細の照会

 kubectl describe psp ack.privileged
Name:  ack.privileged

Settings:
  Allow Privileged:                       true
  Allow Privilege Escalation:             true
  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>

クリックすると、ポッドセキュリティポリシーを定義するコードと、関連するClusterRoleおよびClusterRoleBindingリソースが表示されます

---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
 name: ack.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"
 ack.alicloud.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: ack:podsecuritypolicy:privileged
 labels:
 kubernetes.io/cluster-service: "true"
 ack.alicloud.com/component: pod-security-policy
rules:
- apiGroups:
 - policy
 resourceNames:
 - ack.privileged
 resources:
 - podsecuritypolicies
 verbs:
 - use

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
 name: ack:podsecuritypolicy:authenticated
 annotations:
 kubernetes.io/description: 'Allow all authenticated users to create privileged pods.'
 labels:
 kubernetes.io/cluster-service: "true"
 ack.alicloud.com/component: pod-security-policy
roleRef:
 apiGroup: rbac.authorization.k8s.io
 kind: ClusterRole
 name: ack:podsecuritypolicy:privileged
subjects:
 - kind: Group
 apiGroup: rbac.authorization.k8s.io
 name: system:authenticated

デフォルトのポッドセキュリティポリシーに関連するClusterRoleBindingリソースを削除する

警告

ClusterRoleBindingリソースを削除する前に、カスタムポッドのセキュリティポリシーと関連するRBACバインディングを構成する必要があります。 そうしないと、すべてのユーザー、コントローラー、およびサービスアカウントがポッドを作成または更新できなくなります。

カスタムポッドセキュリティポリシーと関連するRBACバインディングを設定した後、デフォルトのポッドセキュリティポリシーack.privilegedのClusterRoleBindingリソースを削除して、カスタムポッドセキュリティポリシーを有効にすることができます。

重要

ack.privilegedおよびack:podsecuritypolicy:privileged ClusterRoleを削除したり、名前を変更したりしないでください。 これら2つのリソースは、クラスターの実行に必要です。

をクリックして、既定のポッドセキュリティポリシーackのClusterRoleBindingリソースを削除するコードを表示します。

$ cat <<EOF | kubectl delete -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: ack:podsecuritypolicy:authenticated
  annotations:
    kubernetes.io/description: 'Allow all authenticated users to create privileged pods.'
  labels:
    kubernetes.io/cluster-service: "true"
    ack.alicloud.com/component: pod-security-policy
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: ack:podsecuritypolicy:privileged
subjects:
  - kind: Group
    apiGroup: rbac.authorization.k8s.io
    name: system:authenticated
EOF

デフォルトのポッドセキュリティポリシーの設定または復元

クリックして、デフォルトのポッドセキュリティポリシーとそのRBACバインディングを構成または復元するコードを表示します

$ cat <<EOF | kubectl apply -f -
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: ack.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"
    ack.alicloud.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: ack:podsecuritypolicy:privileged
  labels:
    kubernetes.io/cluster-service: "true"
    ack.alicloud.com/component: pod-security-policy
rules:
- apiGroups:
  - policy
  resourceNames:
  - ack.privileged
  resources:
  - podsecuritypolicies
  verbs:
  - use

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: ack:podsecuritypolicy:authenticated
  annotations:
    kubernetes.io/description: 'Allow all authenticated users to create privileged pods.'
  labels:
    kubernetes.io/cluster-service: "true"
    ack.alicloud.com/component: pod-security-policy
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: ack:podsecuritypolicy:privileged
subjects:
  - kind: Group
    apiGroup: rbac.authorization.k8s.io
    name: system:authenticated
EOF