|
| 1 | +--- |
| 2 | +title: クラスターレベルでのPodセキュリティの標準の適用 |
| 3 | +content_type: tutorial |
| 4 | +weight: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +{{% alert title="Note" %}} |
| 8 | +このチュートリアルは、新しいクラスターにのみ適用されます。 |
| 9 | +{{% /alert %}} |
| 10 | + |
| 11 | +Podセキュリティアドミッション(PSA)は、[ベータへ進み](/blog/2021/12/09/pod-security-admission-beta/)、v1.23以降でデフォルトで有効になっています。 |
| 12 | +Podセキュリティアドミッションは、Podが作成される際に、[Podセキュリティの標準](/ja/docs/concepts/security/pod-security-standards/)の適用の認可を制御するものです。 |
| 13 | +このチュートリアルでは、クラスター内の全ての名前空間に標準設定を適用することで、クラスターレベルで`baseline` Podセキュリティの標準を強制する方法を示します。 |
| 14 | + |
| 15 | +Podセキュリティの標準を特定の名前空間に適用するには、[名前空間レベルでのPodセキュリティの標準の適用](/ja/docs/tutorials/security/ns-level-pss/)を参照してください。 |
| 16 | + |
| 17 | +v{{< skew currentVersion >}}以外のKubernetesバージョンを実行している場合は、そのバージョンのドキュメントを確認してください。 |
| 18 | + |
| 19 | +## {{% heading "prerequisites" %}} |
| 20 | + |
| 21 | +ワークステーションに以下をインストールしてください: |
| 22 | + |
| 23 | +- [KinD](https://kind.sigs.k8s.io/docs/user/quick-start/#installation) |
| 24 | +- [kubectl](/ja/docs/tasks/tools/) |
| 25 | + |
| 26 | +このチュートリアルでは、完全な制御下にあるKubernetesクラスターの何を設定できるかをデモンストレーションします。 |
| 27 | +コントロールプレーンを設定できない管理されたクラスターのPodセキュリティアドミッションに対しての設定方法を知りたいのであれば、[名前空間レベルでのPodセキュリティの標準の適用](/ja/docs/tutorials/security/ns-level-pss/)を参照してください。 |
| 28 | + |
| 29 | +## 適用する正しいPodセキュリティの標準の選択 |
| 30 | + |
| 31 | +[Podのセキュリティアドミッション](/ja/docs/concepts/security/pod-security-admission/)は、以下のモードでビルトインの[Podセキュリティの標準](/ja/docs/concepts/security/pod-security-standards/)の適用を促します: `enforce`、`audit`、`warn`。 |
| 32 | +設定に最適なPodセキュリティの標準を選択するにあたって助けになる情報を収集するために、以下を行ってください: |
| 33 | + |
| 34 | +1. Podセキュリティの標準を適用していないクラスターを作成します: |
| 35 | + |
| 36 | + ```shell |
| 37 | + kind create cluster --name psa-wo-cluster-pss |
| 38 | + ``` |
| 39 | + 出力は次のようになります: |
| 40 | + ``` |
| 41 | + Creating cluster "psa-wo-cluster-pss" ... |
| 42 | + ✓ Ensuring node image (kindest/node:v{{< skew currentVersion >}}.0) 🖼 |
| 43 | + ✓ Preparing nodes 📦 |
| 44 | + ✓ Writing configuration 📜 |
| 45 | + ✓ Starting control-plane 🕹️ |
| 46 | + ✓ Installing CNI 🔌 |
| 47 | + ✓ Installing StorageClass 💾 |
| 48 | + Set kubectl context to "kind-psa-wo-cluster-pss" |
| 49 | + You can now use your cluster with: |
| 50 | +
|
| 51 | + kubectl cluster-info --context kind-psa-wo-cluster-pss |
| 52 | +
|
| 53 | + Thanks for using kind! 😊 |
| 54 | + ``` |
| 55 | + |
| 56 | +1. kubectl contextを新しいクラスターにセットします: |
| 57 | + |
| 58 | + ```shell |
| 59 | + kubectl cluster-info --context kind-psa-wo-cluster-pss |
| 60 | + ``` |
| 61 | + 出力は次のようになります: |
| 62 | + |
| 63 | + ``` |
| 64 | + Kubernetes control plane is running at https://127.0.0.1:61350 |
| 65 | +
|
| 66 | + CoreDNS is running at https://127.0.0.1:61350/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy |
| 67 | +
|
| 68 | + To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. |
| 69 | + ``` |
| 70 | + |
| 71 | +1. クラスター内の名前空間の一覧を取得します: |
| 72 | + |
| 73 | + ```shell |
| 74 | + kubectl get ns |
| 75 | + ``` |
| 76 | + 出力は次のようになります: |
| 77 | + ``` |
| 78 | + NAME STATUS AGE |
| 79 | + default Active 9m30s |
| 80 | + kube-node-lease Active 9m32s |
| 81 | + kube-public Active 9m32s |
| 82 | + kube-system Active 9m32s |
| 83 | + local-path-storage Active 9m26s |
| 84 | + ``` |
| 85 | + |
| 86 | +1. 異なるPodセキュリティの標準が適用されたときに何が起きるかを理解するために、`-dry-run=server`を使います: |
| 87 | + |
| 88 | + 1. privileged |
| 89 | + ```shell |
| 90 | + kubectl label --dry-run=server --overwrite ns --all \ |
| 91 | + pod-security.kubernetes.io/enforce=privileged |
| 92 | + ``` |
| 93 | + |
| 94 | + 出力は次のようになります: |
| 95 | + ``` |
| 96 | + namespace/default labeled |
| 97 | + namespace/kube-node-lease labeled |
| 98 | + namespace/kube-public labeled |
| 99 | + namespace/kube-system labeled |
| 100 | + namespace/local-path-storage labeled |
| 101 | + ``` |
| 102 | + 2. baseline |
| 103 | + ```shell |
| 104 | + kubectl label --dry-run=server --overwrite ns --all \ |
| 105 | + pod-security.kubernetes.io/enforce=baseline |
| 106 | + ``` |
| 107 | + |
| 108 | + 出力は次のようになります: |
| 109 | + ``` |
| 110 | + namespace/default labeled |
| 111 | + namespace/kube-node-lease labeled |
| 112 | + namespace/kube-public labeled |
| 113 | + Warning: existing pods in namespace "kube-system" violate the new PodSecurity enforce level "baseline:latest" |
| 114 | + Warning: etcd-psa-wo-cluster-pss-control-plane (and 3 other pods): host namespaces, hostPath volumes |
| 115 | + Warning: kindnet-vzj42: non-default capabilities, host namespaces, hostPath volumes |
| 116 | + Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged |
| 117 | + namespace/kube-system labeled |
| 118 | + namespace/local-path-storage labeled |
| 119 | + ``` |
| 120 | + |
| 121 | + 3. restricted |
| 122 | + ```shell |
| 123 | + kubectl label --dry-run=server --overwrite ns --all \ |
| 124 | + pod-security.kubernetes.io/enforce=restricted |
| 125 | + ``` |
| 126 | + |
| 127 | + 出力は次のようになります: |
| 128 | + ``` |
| 129 | + namespace/default labeled |
| 130 | + namespace/kube-node-lease labeled |
| 131 | + namespace/kube-public labeled |
| 132 | + Warning: existing pods in namespace "kube-system" violate the new PodSecurity enforce level "restricted:latest" |
| 133 | + Warning: coredns-7bb9c7b568-hsptc (and 1 other pod): unrestricted capabilities, runAsNonRoot != true, seccompProfile |
| 134 | + Warning: etcd-psa-wo-cluster-pss-control-plane (and 3 other pods): host namespaces, hostPath volumes, allowPrivilegeEscalation != false, unrestricted capabilities, restricted volume types, runAsNonRoot != true |
| 135 | + Warning: kindnet-vzj42: non-default capabilities, host namespaces, hostPath volumes, allowPrivilegeEscalation != false, unrestricted capabilities, restricted volume types, runAsNonRoot != true, seccompProfile |
| 136 | + Warning: kube-proxy-m6hwf: host namespaces, hostPath volumes, privileged, allowPrivilegeEscalation != false, unrestricted capabilities, restricted volume types, runAsNonRoot != true, seccompProfile |
| 137 | + namespace/kube-system labeled |
| 138 | + Warning: existing pods in namespace "local-path-storage" violate the new PodSecurity enforce level "restricted:latest" |
| 139 | + Warning: local-path-provisioner-d6d9f7ffc-lw9lh: allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile |
| 140 | + namespace/local-path-storage labeled |
| 141 | + ``` |
| 142 | + |
| 143 | +この出力から、`privileged` Podセキュリティの標準を適用すると、名前空間のどれにも警告が示されないことに気付くでしょう。 |
| 144 | +これに対し、`baseline`と`restrict`の標準ではどちらも、とりわけ`kube-system`名前空間に対して警告が示されています。 |
| 145 | + |
| 146 | +## モード、バージョン、標準のセット |
| 147 | + |
| 148 | +このセクションでは、`latest`バージョンに以下のPodセキュリティの標準を適用します: |
| 149 | + |
| 150 | +* `enforce`モードで`baseline`標準。 |
| 151 | +* `warn`および`audit`モードで`restricted`標準。 |
| 152 | + |
| 153 | +`baseline` Podセキュリティの標準は、免除リストを短く保てて、かつ既知の特権昇格を防ぐような、利便性のある中庸を提供します。 |
| 154 | + |
| 155 | +加えて、`kube-system`内の失敗からPodを守るために、適用されるPodセキュリティの標準の対象から名前空間を免除します。 |
| 156 | + |
| 157 | +環境にPodセキュリティアドミッションを実装する際には、以下の点を考慮してください: |
| 158 | + |
| 159 | +1. クラスターに適用されるリスク状況に基づくと、`restricted`のようにより厳格なPodセキュリティの標準のほうが、より良い選択肢かもしれません。 |
| 160 | +1. `kube-ssytem`名前空間の免除は、Podがその名前空間で`privileged`として実行するのを許容することになります。 |
| 161 | + 実世界で使うにあたっては、以下の最小権限の原則に従って`kube-system`へのアクセスを制限する厳格なRBACポリシーを適用することを、Kubernetesプロジェクトは強く推奨します。 |
| 162 | + 上記の標準を実装するには、次のようにします: |
| 163 | +1. 目的のPodセキュリティの標準を実装するために、Podセキュリティアドミッションコントローラーで利用可能な設定ファイルを作成します: |
| 164 | + |
| 165 | + ``` |
| 166 | + mkdir -p /tmp/pss |
| 167 | + cat <<EOF > /tmp/pss/cluster-level-pss.yaml |
| 168 | + apiVersion: apiserver.config.k8s.io/v1 |
| 169 | + kind: AdmissionConfiguration |
| 170 | + plugins: |
| 171 | + - name: PodSecurity |
| 172 | + configuration: |
| 173 | + apiVersion: pod-security.admission.config.k8s.io/v1 |
| 174 | + kind: PodSecurityConfiguration |
| 175 | + defaults: |
| 176 | + enforce: "baseline" |
| 177 | + enforce-version: "latest" |
| 178 | + audit: "restricted" |
| 179 | + audit-version: "latest" |
| 180 | + warn: "restricted" |
| 181 | + warn-version: "latest" |
| 182 | + exemptions: |
| 183 | + usernames: [] |
| 184 | + runtimeClasses: [] |
| 185 | + namespaces: [kube-system] |
| 186 | + EOF |
| 187 | + ``` |
| 188 | +
|
| 189 | + {{< note >}} |
| 190 | + `pod-security.admission.config.k8s.io/v1`設定はv1.25+での対応です。 |
| 191 | + v1.23とv1.24では[v1beta1](https://v1-24.docs.kubernetes.io/docs/tasks/configure-pod-container/enforce-standards-admission-controller/)を使用してください。 |
| 192 | + v1.22では[v1alpha1](https://v1-22.docs.kubernetes.io/docs/tasks/configure-pod-container/enforce-standards-admission-controller/)を使用してください。 |
| 193 | + {{< /note >}} |
| 194 | +
|
| 195 | +
|
| 196 | +1. クラスターの作成中にこのファイルを取り込むAPIサーバーを設定します: |
| 197 | +
|
| 198 | + ``` |
| 199 | + cat <<EOF > /tmp/pss/cluster-config.yaml |
| 200 | + kind: Cluster |
| 201 | + apiVersion: kind.x-k8s.io/v1alpha4 |
| 202 | + nodes: |
| 203 | + - role: control-plane |
| 204 | + kubeadmConfigPatches: |
| 205 | + - | |
| 206 | + kind: ClusterConfiguration |
| 207 | + apiServer: |
| 208 | + extraArgs: |
| 209 | + admission-control-config-file: /etc/config/cluster-level-pss.yaml |
| 210 | + extraVolumes: |
| 211 | + - name: accf |
| 212 | + hostPath: /etc/config |
| 213 | + mountPath: /etc/config |
| 214 | + readOnly: false |
| 215 | + pathType: "DirectoryOrCreate" |
| 216 | + extraMounts: |
| 217 | + - hostPath: /tmp/pss |
| 218 | + containerPath: /etc/config |
| 219 | + # optional: if set, the mount is read-only. |
| 220 | + # default false |
| 221 | + readOnly: false |
| 222 | + # optional: if set, the mount needs SELinux relabeling. |
| 223 | + # default false |
| 224 | + selinuxRelabel: false |
| 225 | + # optional: set propagation mode (None, HostToContainer or Bidirectional) |
| 226 | + # see https://kubernetes.io/docs/concepts/storage/volumes/#mount-propagation |
| 227 | + # default None |
| 228 | + propagation: None |
| 229 | + EOF |
| 230 | + ``` |
| 231 | +
|
| 232 | + {{<note>}} |
| 233 | + macOSでDocker DesktopとKinDを利用している場合は、**Preferences > Resources > File Sharing**のメニュー項目からShared Directoryとして`/tmp`を追加できます。 |
| 234 | + {{</note>}} |
| 235 | +
|
| 236 | +1. 目的のPodセキュリティの標準を適用するために、Podセキュリティアドミッションを使うクラスターを作成します: |
| 237 | +
|
| 238 | + ```shell |
| 239 | + kind create cluster --name psa-with-cluster-pss --config /tmp/pss/cluster-config.yaml |
| 240 | + ``` |
| 241 | + 出力は次のようになります: |
| 242 | + ``` |
| 243 | + Creating cluster "psa-with-cluster-pss" ... |
| 244 | + ✓ Ensuring node image (kindest/node:v{{< skew currentVersion >}}.0) 🖼 |
| 245 | + ✓ Preparing nodes 📦 |
| 246 | + ✓ Writing configuration 📜 |
| 247 | + ✓ Starting control-plane 🕹️ |
| 248 | + ✓ Installing CNI 🔌 |
| 249 | + ✓ Installing StorageClass 💾 |
| 250 | + Set kubectl context to "kind-psa-with-cluster-pss" |
| 251 | + You can now use your cluster with: |
| 252 | +
|
| 253 | + kubectl cluster-info --context kind-psa-with-cluster-pss |
| 254 | +
|
| 255 | + Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community 🙂 |
| 256 | + ``` |
| 257 | + |
| 258 | +1. kubectlをこのクラスターに向けます: |
| 259 | + ```shell |
| 260 | + kubectl cluster-info --context kind-psa-with-cluster-pss |
| 261 | + ``` |
| 262 | + 出力は次のようになります: |
| 263 | + ``` |
| 264 | + Kubernetes control plane is running at https://127.0.0.1:63855 |
| 265 | + CoreDNS is running at https://127.0.0.1:63855/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy |
| 266 | +
|
| 267 | + To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. |
| 268 | + ``` |
| 269 | + |
| 270 | +1. デフォルトの名前空間にPodを作成します: |
| 271 | + |
| 272 | + ```shell |
| 273 | + kubectl apply -f https://k8s.io/examples/security/example-baseline-pod.yaml |
| 274 | + ``` |
| 275 | + |
| 276 | + Podは正常に開始されますが、出力には警告が含まれます: |
| 277 | + ``` |
| 278 | + Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "nginx" must set securityContext.allowPrivilegeEscalation=false), unrestricted capabilities (container "nginx" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "nginx" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "nginx" must set securityContext.seccompProfile.type to "RuntimeDefault" or "Localhost") |
| 279 | + pod/nginx created |
| 280 | + ``` |
| 281 | + |
| 282 | +## 後片付け |
| 283 | + |
| 284 | +では、上記で作成したクラスターを、以下のコマンドを実行して削除します: |
| 285 | + |
| 286 | +```shell |
| 287 | +kind delete cluster --name psa-with-cluster-pss |
| 288 | +``` |
| 289 | +```shell |
| 290 | +kind delete cluster --name psa-wo-cluster-pss |
| 291 | +``` |
| 292 | + |
| 293 | +## {{% heading "whatsnext" %}} |
| 294 | + |
| 295 | +- 前出の一連の手順を一度に全て行うために[シェルスクリプト](/examples/security/kind-with-cluster-level-baseline-pod-security.sh)を実行します: |
| 296 | + 1. クラスターレベルの設定に基づきPodセキュリティの標準を作成します。 |
| 297 | + 2. APIサーバーでこの設定を取り込むようにファイルを作成します。 |
| 298 | + 3. この設定のAPIサーバーを立てるクラスターを作成します。 |
| 299 | + 4. この新しいクラスターにkubectl contextをセットします。 |
| 300 | + 5. 最小限のPod YAMLファイルを作成します。 |
| 301 | + 6. 新しいクラスター内でPodを作成するために、このファイルを適用します。 |
| 302 | +- [Podのセキュリティアドミッション](/ja/docs/concepts/security/pod-security-admission/) |
| 303 | +- [Podセキュリティの標準](/ja/docs/concepts/security/pod-security-standards/) |
| 304 | +- [名前空間レベルでのPodセキュリティの標準の適用](/ja/docs/tutorials/security/ns-level-pss/) |
0 commit comments