|
| 1 | +--- |
| 2 | +title: 在名字空间级别应用 Pod 安全标准 |
| 3 | +content_type: tutorial |
| 4 | +weight: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- |
| 8 | +title: Apply Pod Security Standards at the Namespace Level |
| 9 | +content_type: tutorial |
| 10 | +weight: 10 |
| 11 | +--> |
| 12 | + |
| 13 | +{{% alert title="Note" %}} |
| 14 | +<!-- This tutorial applies only for new clusters. --> |
| 15 | +本教程仅适用于新集群。 |
| 16 | +{{% /alert %}} |
| 17 | + |
| 18 | +<!-- |
| 19 | +Pod Security admission (PSA) is enabled by default in v1.23 and later, as it [graduated |
| 20 | +to beta](/blog/2021/12/09/pod-security-admission-beta/). Pod Security Admission |
| 21 | +is an admission controller that applies |
| 22 | +[Pod Security Standards](docs/concepts/security/pod-security-standards/) |
| 23 | +when pods are created. In this tutorial, you will enforce the `baseline` Pod Security Standard, |
| 24 | +one namespace at a time. |
| 25 | +
|
| 26 | +You can also apply Pod Security Standards to multiple namespaces at once at the cluster |
| 27 | +level. For instructions, refer to [Apply Pod Security Standards at the cluster level](/docs/tutorials/security/cluster-level-pss). |
| 28 | +--> |
| 29 | +Pod 安全准入(PSA)在 v1.23 及更高版本默认启用, |
| 30 | +因为它[升级到测试版(beta)](/blog/2021/12/09/pod-security-admission-beta/)。 |
| 31 | +Pod 安全准入是在创建 Pod 时应用 |
| 32 | +[Pod 安全标准](/zh/docs/concepts/security/pod-security-standards/)的准入控制器。 |
| 33 | +在本教程中,你将应用 `baseline` Pod 安全标准,每次一个名字空间。 |
| 34 | + |
| 35 | +你还可以在集群级别一次将 Pod 安全标准应用于多个名称空间。 |
| 36 | +有关说明,请参阅[在集群级别应用 Pod 安全标准](/zh/docs/tutorials/security/cluster-level-pss)。 |
| 37 | + |
| 38 | +## {{% heading "prerequisites" %}} |
| 39 | + |
| 40 | +<!-- |
| 41 | +Install the following on your workstation: |
| 42 | +
|
| 43 | +- [KinD](https://kind.sigs.k8s.io/docs/user/quick-start/#installation) |
| 44 | +- [kubectl](https://kubernetes.io/docs/tasks/tools/) |
| 45 | +--> |
| 46 | +在你的工作站中安装以下内容: |
| 47 | + |
| 48 | +- [KinD](https://kind.sigs.k8s.io/docs/user/quick-start/#installation) |
| 49 | +- [kubectl](https://kubernetes.io/docs/tasks/tools/) |
| 50 | + |
| 51 | +<!-- |
| 52 | +## Create cluster |
| 53 | +
|
| 54 | +1. Create a `KinD` cluster as follows: |
| 55 | +--> |
| 56 | +## 创建集群 {#create-cluster} |
| 57 | + |
| 58 | +2. 按照如下方式创建一个 `KinD` 集群: |
| 59 | + |
| 60 | + ```shell |
| 61 | + kind create cluster --name psa-ns-level --image kindest/node:v1.23.0 |
| 62 | + ``` |
| 63 | + <!-- The output is similar to this: --> |
| 64 | + 输入类似于: |
| 65 | + ``` |
| 66 | + Creating cluster "psa-ns-level" ... |
| 67 | + ✓ Ensuring node image (kindest/node:v1.23.0) 🖼 |
| 68 | + ✓ Preparing nodes 📦 |
| 69 | + ✓ Writing configuration 📜 |
| 70 | + ✓ Starting control-plane 🕹️ |
| 71 | + ✓ Installing CNI 🔌 |
| 72 | + ✓ Installing StorageClass 💾 |
| 73 | + Set kubectl context to "kind-psa-ns-level" |
| 74 | + You can now use your cluster with: |
| 75 | + |
| 76 | + kubectl cluster-info --context kind-psa-ns-level |
| 77 | + |
| 78 | + Not sure what to do next? 😅 Check out https://kind.sigs.k8s.io/docs/user/quick-start/ |
| 79 | + ``` |
| 80 | + |
| 81 | +<!-- 1. Set the kubectl context to the new cluster: --> |
| 82 | +1. 将 kubectl 上下文设置为新集群: |
| 83 | + ```shell |
| 84 | + kubectl cluster-info --context kind-psa-ns-level |
| 85 | + ``` |
| 86 | + <!-- The output is similar to this: --> |
| 87 | + 输入类似于: |
| 88 | + ``` |
| 89 | + Kubernetes control plane is running at https://127.0.0.1:50996 |
| 90 | + CoreDNS is running at https://127.0.0.1:50996/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy |
| 91 | + |
| 92 | + To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. |
| 93 | + ``` |
| 94 | + |
| 95 | +<!-- |
| 96 | +## Create a namespace |
| 97 | +
|
| 98 | +Create a new namespace called `example`: |
| 99 | +--> |
| 100 | +## 创建名字空间 {#create-a-namespace} |
| 101 | + |
| 102 | +创建一个名为 `example` 的新名字空间: |
| 103 | + |
| 104 | +```shell |
| 105 | +kubectl create ns example |
| 106 | +``` |
| 107 | +<!-- The output is similar to this: --> |
| 108 | +输出类似于: |
| 109 | +``` |
| 110 | +namespace/example created |
| 111 | +``` |
| 112 | + |
| 113 | +<!-- |
| 114 | +## Apply Pod Security Standards |
| 115 | +
|
| 116 | +1. Enable Pod Security Standards on this namespace using labels supported by |
| 117 | + built-in Pod Security Admission. In this step we will warn on baseline pod |
| 118 | + security standard as per the latest version (default value) |
| 119 | +--> |
| 120 | +## 应用 Pod 安全标准 {#apply-pod-security-standards} |
| 121 | + |
| 122 | +1. 使用内置 Pod 安全准入所支持的标签在此名字空间上启用 Pod 安全标准。 |
| 123 | + 在这一步中,我们将根据最新版本(默认值)对基线 Pod 安全标准发出警告。 |
| 124 | + |
| 125 | + ```shell |
| 126 | + kubectl label --overwrite ns example \ |
| 127 | + pod-security.kubernetes.io/warn=baseline \ |
| 128 | + pod-security.kubernetes.io/warn-version=latest |
| 129 | + ``` |
| 130 | + |
| 131 | +<!-- |
| 132 | +2. Multiple pod security standards can be enabled on any namespace, using labels. |
| 133 | + Following command will `enforce` the `baseline` Pod Security Standard, but |
| 134 | + `warn` and `audit` for `restricted` Pod Security Standards as per the latest |
| 135 | + version (default value) |
| 136 | +--> |
| 137 | +2. 可以使用标签在任何名字空间上启用多个 Pod 安全标准。 |
| 138 | + 以下命令将强制(`enforce`) 执行基线(`baseline`)Pod 安全标准, |
| 139 | + 但根据最新版本(默认值)对受限(`restricted`)Pod 安全标准执行警告(`warn`)和审核(`audit`)。 |
| 140 | + |
| 141 | + ``` |
| 142 | + kubectl label --overwrite ns example \ |
| 143 | + pod-security.kubernetes.io/enforce=baseline \ |
| 144 | + pod-security.kubernetes.io/enforce-version=latest \ |
| 145 | + pod-security.kubernetes.io/warn=restricted \ |
| 146 | + pod-security.kubernetes.io/warn-version=latest \ |
| 147 | + pod-security.kubernetes.io/audit=restricted \ |
| 148 | + pod-security.kubernetes.io/audit-version=latest |
| 149 | + ``` |
| 150 | + |
| 151 | +<!-- |
| 152 | +## Verify the Pod Security Standards |
| 153 | +
|
| 154 | +1. Create a minimal pod in `example` namespace: |
| 155 | +--> |
| 156 | +## 验证 Pod 安全标准 {#verify-the-pod-security-standards} |
| 157 | + |
| 158 | +1. 在 `example` 名字空间中创建一个最小的 pod: |
| 159 | + |
| 160 | + ```shell |
| 161 | + cat <<EOF > /tmp/pss/nginx-pod.yaml |
| 162 | + apiVersion: v1 |
| 163 | + kind: Pod |
| 164 | + metadata: |
| 165 | + name: nginx |
| 166 | + spec: |
| 167 | + containers: |
| 168 | + - image: nginx |
| 169 | + name: nginx |
| 170 | + ports: |
| 171 | + - containerPort: 80 |
| 172 | + EOF |
| 173 | + ``` |
| 174 | +
|
| 175 | +<!-- |
| 176 | +2. Apply the pod spec to the cluster in `example` namespace: |
| 177 | +--> |
| 178 | +1. 将 Pod 规约应用到集群中的 `example` 名字空间中: |
| 179 | + ```shell |
| 180 | + kubectl apply -n example -f /tmp/pss/nginx-pod.yaml |
| 181 | + ``` |
| 182 | + <!-- The output is similar to this: --> |
| 183 | + 输出类似于: |
| 184 | + ``` |
| 185 | + 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") |
| 186 | + pod/nginx created |
| 187 | + ``` |
| 188 | +
|
| 189 | +<!-- |
| 190 | +1. Apply the pod spec to the cluster in `default` namespace: |
| 191 | +--> |
| 192 | +3. 将 Pod 规约应用到集群中的 `default` 名字空间中: |
| 193 | + ```shell |
| 194 | + kubectl apply -n default -f /tmp/pss/nginx-pod.yaml |
| 195 | + ``` |
| 196 | + <!-- Output is similar to this: --> |
| 197 | + 输入类似于: |
| 198 | + ``` |
| 199 | + pod/nginx created |
| 200 | + ``` |
| 201 | +
|
| 202 | +<!-- |
| 203 | +The Pod Security Standards were applied only to the `example` |
| 204 | +namespace. You could create the same Pod in the `default` namespace |
| 205 | +with no warnings. |
| 206 | +--> |
| 207 | +以上 Pod 安全标准仅被应用到 `example` 名字空间。 |
| 208 | +你可以在没有警告的情况下在 `default` 名字空间中创建相同的 Pod。 |
| 209 | +
|
| 210 | +<!-- |
| 211 | +## Clean up |
| 212 | +
|
| 213 | +Run `kind delete cluster -name psa-ns-level` to delete the cluster created. |
| 214 | +--> |
| 215 | +## 清理 {#clean-up} |
| 216 | +
|
| 217 | +运行 `kind delete cluster -name psa-ns-level` 删除创建的集群。 |
| 218 | +
|
| 219 | +## {{% heading "whatsnext" %}} |
| 220 | +
|
| 221 | +<!-- |
| 222 | +- Run a |
| 223 | + [shell script](/examples/security/kind-with-namespace-level-baseline-pod-security.sh) |
| 224 | + to perform all the preceding steps all at once. |
| 225 | + 1. Create KinD cluster |
| 226 | + 2. Create new namespace |
| 227 | + 3. Apply `baseline` Pod Security Standard in `enforce` mode while applying |
| 228 | + `restricted` Pod Security Standard also in `warn` and `audit` mode. |
| 229 | + 4. Create a new pod with the following pod security standards applied |
| 230 | +- [Pod Security Admission](/docs/concepts/security/pod-security-admission/) |
| 231 | +- [Pod Security Standards](/docs/concepts/security/pod-security-standards/) |
| 232 | +- [Apply Pod Security Standards at the cluster level](/docs/tutorials/security/cluster-level-pss/) |
| 233 | +--> |
| 234 | +- 运行一个 [shell 脚本](/examples/security/kind-with-namespace-level-baseline-pod-security.sh) |
| 235 | + 一次执行所有前面的步骤。 |
| 236 | + 1. 创建 KinD 集群 |
| 237 | + 2. 创建新的名字空间 |
| 238 | + 3. 在 `enforce` 模式下应用 `baseline` Pod 安全标准, |
| 239 | + 同时在 `warn` 和 `audit` 模式下应用 `restricted` Pod 安全标准。 |
| 240 | + 4. 创建一个应用以下 Pod 安全标准的新 Pod |
| 241 | +- [Pod 安全准入](/zh/docs/concepts/security/pod-security-admission/) |
| 242 | +- [Pod 安全标准](/zh/docs/concepts/security/pod-security-standards/) |
| 243 | +- [在集群级别应用 Pod 安全标准](/zh/docs/tutorials/security/cluster-level-pss/) |
0 commit comments