Skip to content

KCSA: Kubernetes Security Fundamentals

22% of the KCSA exam, 7 competencies. Back to the KCSA overview.

Each competency lists what to know and the official documentation to read, then a demo you can run. Demos run on the demo lab, a throwaway kind cluster on your own machine; set it up once before you start.

Know

  • Three profiles: privileged, baseline, restricted.

Read

Demo: Check existing pods against a stricter level before you enforce it

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-pss
    kubectl config set-context --current --namespace=kcsa-pss
  2. Some pods that meet baseline but not restricted.

    Terminal window
    kubectl create deployment web --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 --replicas=2 -- /agnhost netexec --http-port=8080
    kubectl rollout status deployment/web
  3. Dry-run the label change: the API server evaluates every existing pod.

    Terminal window
    kubectl label --dry-run=server --overwrite namespace kcsa-pss pod-security.kubernetes.io/enforce=baseline
    kubectl label --dry-run=server --overwrite namespace kcsa-pss pod-security.kubernetes.io/enforce=restricted

What you should see: baseline passes silently. restricted prints warnings naming each existing pod and what it is missing (allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile). The dry run changes nothing, so it is the safe first step before tightening a namespace in production.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-pss

Know

  • Namespace labels set enforce, audit, and warn modes per profile.

Read

Demo: Enforce, warn, and audit at different levels

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-psa
    kubectl config set-context --current --namespace=kcsa-psa
  2. Enforce baseline, warn and audit at restricted, and pin the enforce version.

    Terminal window
    kubectl label namespace kcsa-psa \
    pod-security.kubernetes.io/enforce=baseline \
    pod-security.kubernetes.io/enforce-version=v1.37 \
    pod-security.kubernetes.io/warn=restricted \
    pod-security.kubernetes.io/audit=restricted
    kubectl get namespace kcsa-psa --show-labels
  3. A pod that passes baseline but not restricted, and one that fails both.

    Terminal window
    kubectl run ok-ish --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 -- pause
    kubectl run bad --image=registry.k8s.io/e2e-test-images/agnhost:2.66.1 --overrides='{"spec":{"hostNetwork":true}}' -- pause
    kubectl get pods

What you should see: ok-ish is created with a warning listing what restricted would require. bad is rejected outright for host namespaces. Running warn and audit one level above enforce shows you what would break before you raise enforcement. Pinning enforce-version keeps the rules from changing under you when the cluster is upgraded.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-psa

On this site

Know

  • X.509 client certificates, bearer tokens, OIDC, and webhook authentication. Kubernetes has no user objects; identities come from outside.

Read

Demo: Issue a user certificate through the API

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-authn
    kubectl config set-context --current --namespace=kcsa-authn
  2. Generate a key and a certificate signing request for user jane in group devs.

    Terminal window
    openssl genrsa -out jane.key 2048
    openssl req -new -key jane.key -out jane.csr -subj "/CN=jane/O=devs"
    kubectl apply -f - <<EOF
    apiVersion: certificates.k8s.io/v1
    kind: CertificateSigningRequest
    metadata:
    name: jane
    spec:
    request: $(base64 < jane.csr | tr -d '\n')
    signerName: kubernetes.io/kube-apiserver-client
    expirationSeconds: 3600
    usages: ["client auth"]
    EOF
    kubectl certificate approve jane
    kubectl get csr jane -o jsonpath='{.status.certificate}' | base64 -d > jane.crt
    openssl x509 -in jane.crt -noout -subject -enddate
  3. Log in as jane. Authenticated, but not yet authorized.

    Terminal window
    kubectl config set-credentials jane --client-certificate=jane.crt --client-key=jane.key --embed-certs=true
    kubectl config set-context jane --cluster=kind-kind --user=jane --namespace=kcsa-authn
    kubectl --context=jane auth whoami
    kubectl --context=jane get pods

What you should see: auth whoami reports jane in group devs: the API server trusts any certificate its CA signed. get pods is forbidden until you add a RoleBinding. The certificate lasts one hour because of expirationSeconds; there is no way to revoke it earlier, which is why short lifetimes matter.

Clean up:

Terminal window
kubectl delete csr jane
kubectl config delete-context jane; kubectl config delete-user jane
rm -f jane.key jane.csr jane.crt
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-authn

Know

  • Base64 is not encryption. Enable encryption at rest, limit get/list on Secrets, and prefer external secret stores.

Read

Demo: The list verb reveals Secret values

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-secrets
    kubectl config set-context --current --namespace=kcsa-secrets
  2. A role that may only list Secrets, not get them.

    Terminal window
    kubectl create secret generic api-key --from-literal=key=TopSecretValue
    kubectl create serviceaccount lister
    kubectl create role list-secrets --verb=list --resource=secrets
    kubectl create rolebinding lister --role=list-secrets --serviceaccount=kcsa-secrets:lister
    kubectl auth can-i get secret/api-key --as=system:serviceaccount:kcsa-secrets:lister
  3. List them as that account and read the output.

    Terminal window
    kubectl get secrets -o jsonpath='{.items[?(@.metadata.name=="api-key")].data.key}' --as=system:serviceaccount:kcsa-secrets:lister | base64 -d; echo

What you should see: can-i get says no, yet the list response contains the full Secret data and the value decodes to TopSecretValue. list and watch on Secrets are as sensitive as get. Grant none of them unless the workload truly needs them.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-secrets

On this site

Know

  • Namespaces plus RBAC, NetworkPolicies, quotas, and dedicated nodes for sensitive workloads.

Read

Demo: A tenant namespace in four objects

  1. Create a namespace for this demo and make it the default for your context.

    Terminal window
    kubectl create namespace kcsa-tenant
    kubectl config set-context --current --namespace=kcsa-tenant
  2. Pod Security, a quota, a default limit, and default-deny networking.

    Terminal window
    kubectl label namespace kcsa-tenant pod-security.kubernetes.io/enforce=restricted
    kubectl create quota tenant --hard=pods=10,requests.cpu=2,requests.memory=2Gi,limits.cpu=4,limits.memory=4Gi
    kubectl apply -f - <<'EOF'
    apiVersion: v1
    kind: LimitRange
    metadata:
    name: tenant-defaults
    spec:
    limits:
    - type: Container
    default: {cpu: 250m, memory: 256Mi}
    defaultRequest: {cpu: 100m, memory: 128Mi}
    ---
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
    name: default-deny
    spec:
    podSelector: {}
    policyTypes: ["Ingress", "Egress"]
    EOF
    kubectl get quota,limitrange,networkpolicy
  3. Grant the tenant team edit rights in its namespace only.

    Terminal window
    kubectl create rolebinding tenant-edit --clusterrole=edit --group=tenant-a
    kubectl auth can-i create deployments -n kcsa-tenant --as=someone --as-group=tenant-a
    kubectl auth can-i create deployments -n default --as=someone --as-group=tenant-a

What you should see: Namespaces separate names; the four objects are what turn one into a boundary. The tenant group can deploy in its namespace and not in default. Namespaces still share nodes and the kernel, so for hostile tenants add node pools or sandboxed runtimes.

Clean up:

Terminal window
kubectl config set-context --current --namespace=default
kubectl delete namespace kcsa-tenant

Know

  • An audit policy decides which requests are logged and at what level (None, Metadata, Request, RequestResponse).

Read

Demo: use the CKS demo for “Use Kubernetes audit logs to monitor access”. It creates a kind cluster with an audit policy and queries the log with jq.

Know

  • Default-deny plus explicit allows; enforcement depends on the CNI.

Read

Demo: use the CKA demo for “Define and enforce Network Policies”. It sets a default deny and allows a single client, then tests both.


Domain and competency names are from the CNCF KCSA curriculum (current curriculum (unversioned)), cncf/curriculum, licensed CC BY 4.0. Summaries, demos, and exercises are original to this site.