- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud
- Security
Kubernetes with kops for PodSecurityPolicy Lab
This lab guides the student through a step-by-step hands-on example of creating a pod security policy, testing it, and using role bindings to enable it to prevent privileged pods.
Lab Info
Table of Contents
-
Challenge
Use Kops to Create the Cluster
From the bastion host, use kops to create a cluster:
Use the terminal emulator or SSH to gain access to the 'Bastion Host' Cloud Server server instantiated for the lab.
ssh cloud_user@[IP Address of Bastion Host]Once you have access you should be able to do a
ls -land see thek8s-create.sh.ls -lExecute the script to create the cluster configuration files.
. ./k8s-create.shNote: Answer any prompts as needed.
Use kops to edit the cluster configuration. Use
escthen:wq!to save your changes.kops edit clusterUnder the
spec:for the cluster, add the following lines.spec: kubeAPIServer: admissionControl: - NamespaceLifecycle - LimitRanger - ServiceAccount - PersistentVolumeLabel - DefaultStorageClass - ResourceQuota - PodSecurityPolicy - DefaultTolerationSecondsThe display above has tabs, but you should normally use just two spaces to indent lines. The file should remain the same from the next line to the end.
After editing the cluster configuration to add the admission controller, use
kopsto update the cluster and create the nodes.kops update cluster --name=$KOPS_CLUSTER_NAME --yesCopy the command at the bottom to connect to the master node using SSH.
Note: It will take several minutes to validate the cluster, which you can check with:
kops validate cluster. -
Challenge
Create Namespace, Serviceaccount, and Rolebinding
Create the
psp-nsnamespace.kubectl create namespace psp-nsCreate the
psp-saserviceaccount within thepsp-nsnamespace.kubectl create serviceaccount -n psp-ns psp-saCreate a rolebinding binding the cluster role verb edit to the service account psp-sa.
kubectl create rolebinding -n psp-ns rb-id --clusterrole=edit --serviceaccount=psp-ns:psp-saNow for convenience, create an alias for the psp-admin within the namespace.
alias psp-admin='kubectl -n psp-ns'And create an alias for the psp-user within the namespace.
alias psp-user='kubectl --as=system:serviceaccount:psp-ns:psp-sa -n psp-ns' -
Challenge
Create the Pod Security Policy
Use vi or a Linux editor to create the psp-policy YAML file.
vi psp-policy.yamlMake sure the file is as follows.
apiVersion: policy/v1beta1 kind: PodSecurityPolicy metadata: name: psp-policy spec: privileged: false # Don't allow privileged pods! seLinux: rule: RunAsAny supplementalGroups: rule: RunAsAny runAsUser: rule: RunAsAny fsGroup: rule: RunAsAny volumes: - '*'Then create the policy.
psp-admin create -f psp-policy.yaml -
Challenge
Create a YAML File to Deploy a Pod, and Attempt to Create It
Use an editor to create a YAML file to create a pod.
vi pod-pause.yamlEdit the file as follows.
apiVersion: v1 kind: Pod metadata: name: pod-pause spec: containers: - name: pause image: k8s.gcr.io/pauseNow attempt to create the pod.
psp-user create -f pod-pause.yamlTo explain the error, you can use
can-ito see that the policy is not being used by the service account psp-sa.psp-user auth can-i use podsecuritypolicy/psp-policy -
Challenge
Create a Rolebinding to Allow the Psp-Sa Service Account to Use the Policy and Then Re-attempt to Create the Pod
Create a role to use the psp-policy.
psp-admin create role psp-role --verb=use --resource=podsecuritypolicy --resource-name=psp-policyCreate a rolebinding to bind the role to the serviceaccount.
psp-admin create rolebinding rb-id2 --role=psp-role --serviceaccount=psp-ns:psp-saRetry to create the pod as before.
psp-user create -f pod-pause.yamlThe pod should deploy. Check with:
psp-user get pods -
Challenge
Delete the Pod from the Previous Step and Attempt to Deploy a Privileged Pod
Delete the pod previously deployed.
psp-user delete po/pod-pauseUse the editor to create a YAML file for a privileged pod.
vi priv-pod.yamlThe YAML file should contain the following:
apiVersion: v1 kind: Pod metadata: name: privileged spec: containers: - name: pause image: k8s.gcr.io/pause securityContext: privileged: trueNow try to create the pod.
psp-user create -f priv-pod.yamlThe attempt should fail.
-
Challenge
Attempt to Use a Deployment to Create the Unprivileged Pod
Use an editor to create a YAML file for the deployment.
vi psp-deploy.yamlThe file contents should be:
apiVersion: apps/v1 kind: Deployment metadata: name: psp-deploy labels: app: paused spec: replicas: 1 selector: matchLabels: app: paused template: metadata: labels: app: paused spec: containers: - name: paused image: k8s.gcr.io/pauseNow attempt to create the deployment.
psp-user create -f psp-deploy.yamlSee if the pod deployed.
psp-user get podsIt should not have deployed.
Check the events to see what happened.
psp-user get events --sort-by='.metadata.creationTimestamp' -
Challenge
Clean Up the Failed Deployment, Add the Needed Role Binding, and Re-attempt the Deployment
Check if there are any pods running and delete as needed.
psp-user get pods(delete as needed)
psp-user delete po/[pod name]Check if the failed deployment exists and delete as needed.
psp-user get deploy(delete as needed)
psp-user delete deploy/[deployment name]Create a role binding linking the role that allows use of the policy with the default service account.
psp-admin create rolebinding rb-id3 --role=psp-role --serviceaccount=psp-ns:defaultNow reattempt to create the deployment as before.
psp-user create -f psp-deploy.yamlCheck the events to see what happened.
psp-user get events --sort-by='.metadata.creationTimestamp'Check the deployment.
psp-user get deployCheck the pod.
psp-user get podsClean up as needed, and experiment with other namespaces and service accounts until this material is comfortable.
This completes this lab.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.