|
| 1 | +--- |
| 2 | +title: Apply Pod Security Standards at the Namespace Level |
| 3 | +content_type: tutorial |
| 4 | +weight: 20 |
| 5 | +--- |
| 6 | + |
| 7 | +{{% alert title="Note" %}} |
| 8 | +This tutorial applies only for new clusters. |
| 9 | +{{% /alert %}} |
| 10 | + |
| 11 | +Pod Security admission (PSA) is enabled by default in v1.23 and later, as it |
| 12 | +[graduated to beta](/blog/2021/12/09/pod-security-admission-beta/). Pod Security Admission |
| 13 | +is an admission controller that applies |
| 14 | +[Pod Security Standards](/docs/concepts/security/pod-security-standards/) |
| 15 | +when pods are created. In this tutorial, you will enforce the `baseline` Pod Security Standard, |
| 16 | +one namespace at a time. |
| 17 | + |
| 18 | +You can also apply Pod Security Standards to multiple namespaces at once at the cluster |
| 19 | +level. For instructions, refer to |
| 20 | +[Apply Pod Security Standards at the cluster level](/docs/tutorials/security/cluster-level-pss/). |
| 21 | + |
| 22 | +## {{% heading "prerequisites" %}} |
| 23 | + |
| 24 | +Install the following on your workstation: |
| 25 | + |
| 26 | +- [KinD](https://kind.sigs.k8s.io/docs/user/quick-start/#installation) |
| 27 | +- [kubectl](/docs/tasks/tools/) |
| 28 | + |
| 29 | +## Create cluster |
| 30 | + |
| 31 | +1. Create a `KinD` cluster as follows: |
| 32 | + |
| 33 | + ```shell |
| 34 | + kind create cluster --name psa-ns-level |
| 35 | + ``` |
| 36 | + |
| 37 | + The output is similar to this: |
| 38 | + |
| 39 | + ``` |
| 40 | + Creating cluster "psa-ns-level" ... |
| 41 | + ✓ Ensuring node image (kindest/node:v{{< skew currentVersion >}}.0) 🖼 |
| 42 | + ✓ Preparing nodes 📦 |
| 43 | + ✓ Writing configuration 📜 |
| 44 | + ✓ Starting control-plane 🕹️ |
| 45 | + ✓ Installing CNI 🔌 |
| 46 | + ✓ Installing StorageClass 💾 |
| 47 | + Set kubectl context to "kind-psa-ns-level" |
| 48 | + You can now use your cluster with: |
| 49 | + |
| 50 | + kubectl cluster-info --context kind-psa-ns-level |
| 51 | + |
| 52 | + Not sure what to do next? 😅 Check out https://kind.sigs.k8s.io/docs/user/quick-start/ |
| 53 | + ``` |
| 54 | + |
| 55 | +1. Set the kubectl context to the new cluster: |
| 56 | + |
| 57 | + ```shell |
| 58 | + kubectl cluster-info --context kind-psa-ns-level |
| 59 | + ``` |
| 60 | + The output is similar to this: |
| 61 | + |
| 62 | + ``` |
| 63 | + Kubernetes control plane is running at https://127.0.0.1:50996 |
| 64 | + CoreDNS is running at https://127.0.0.1:50996/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy |
| 65 | + |
| 66 | + To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. |
| 67 | + ``` |
| 68 | + |
| 69 | +## Create a namespace |
| 70 | + |
| 71 | +Create a new namespace called `example`: |
| 72 | + |
| 73 | +```shell |
| 74 | +kubectl create ns example |
| 75 | +``` |
| 76 | + |
| 77 | +The output is similar to this: |
| 78 | + |
| 79 | +``` |
| 80 | +namespace/example created |
| 81 | +``` |
| 82 | + |
| 83 | +## Enable Pod Security Standards checking for that namespace |
| 84 | + |
| 85 | +1. Enable Pod Security Standards on this namespace using labels supported by |
| 86 | + built-in Pod Security Admission. In this step you will configure a check to |
| 87 | + warn on Pods that don't meet the latest version of the _baseline_ pod |
| 88 | + security standard. |
| 89 | + |
| 90 | + ```shell |
| 91 | + kubectl label --overwrite ns example \ |
| 92 | + pod-security.kubernetes.io/warn=baseline \ |
| 93 | + pod-security.kubernetes.io/warn-version=latest |
| 94 | + ``` |
| 95 | + |
| 96 | +2. You can configure multiple pod security standard checks on any namespace, using labels. |
| 97 | + The following command will `enforce` the `baseline` Pod Security Standard, but |
| 98 | + `warn` and `audit` for `restricted` Pod Security Standards as per the latest |
| 99 | + version (default value) |
| 100 | + |
| 101 | + ```shell |
| 102 | + kubectl label --overwrite ns example \ |
| 103 | + pod-security.kubernetes.io/enforce=baseline \ |
| 104 | + pod-security.kubernetes.io/enforce-version=latest \ |
| 105 | + pod-security.kubernetes.io/warn=restricted \ |
| 106 | + pod-security.kubernetes.io/warn-version=latest \ |
| 107 | + pod-security.kubernetes.io/audit=restricted \ |
| 108 | + pod-security.kubernetes.io/audit-version=latest |
| 109 | + ``` |
| 110 | + |
| 111 | +## Verify the Pod Security Standard enforcement |
| 112 | + |
| 113 | +1. Create a baseline Pod in the `example` namespace: |
| 114 | + |
| 115 | + ```shell |
| 116 | + kubectl apply -n example -f https://k8s.io/examples/security/example-baseline-pod.yaml |
| 117 | + ``` |
| 118 | + The Pod does start OK; the output includes a warning. For example: |
| 119 | + |
| 120 | + ``` |
| 121 | + 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") |
| 122 | + pod/nginx created |
| 123 | + ``` |
| 124 | + |
| 125 | +1. Create a baseline Pod in the `default` namespace: |
| 126 | + |
| 127 | + ```shell |
| 128 | + kubectl apply -n default -f https://k8s.io/examples/security/example-baseline-pod.yaml |
| 129 | + ``` |
| 130 | + Output is similar to this: |
| 131 | + |
| 132 | + ``` |
| 133 | + pod/nginx created |
| 134 | + ``` |
| 135 | + |
| 136 | +The Pod Security Standards enforcement and warning settings were applied only |
| 137 | +to the `example` namespace. You could create the same Pod in the `default` |
| 138 | +namespace with no warnings. |
| 139 | + |
| 140 | +## Clean up |
| 141 | + |
| 142 | +Now delete the cluster which you created above by running the following command: |
| 143 | + |
| 144 | +```shell |
| 145 | +kind delete cluster --name psa-ns-level |
| 146 | +``` |
| 147 | + |
| 148 | +## {{% heading "whatsnext" %}} |
| 149 | + |
| 150 | +- Run a |
| 151 | + [shell script](/examples/security/kind-with-namespace-level-baseline-pod-security.sh) |
| 152 | + to perform all the preceding steps all at once. |
| 153 | + |
| 154 | + 1. Create KinD cluster |
| 155 | + 2. Create new namespace |
| 156 | + 3. Apply `baseline` Pod Security Standard in `enforce` mode while applying |
| 157 | + `restricted` Pod Security Standard also in `warn` and `audit` mode. |
| 158 | + 4. Create a new pod with the following pod security standards applied |
| 159 | + |
| 160 | +- [Pod Security Admission](/docs/concepts/security/pod-security-admission/) |
| 161 | +- [Pod Security Standards](/docs/concepts/security/pod-security-standards/) |
| 162 | +- [Apply Pod Security Standards at the cluster level](/docs/tutorials/security/cluster-level-pss/) |
0 commit comments