CKS Cheat Cards

Scan references, not study notes. Commands, tables, the gotcha, the docs path. Teaching happens in drills.

MEMORIZE — drill cold, docs won't save you in time. LOOKUP — drill the navigation: which page, which snippet, which fields to edit. All syntax below is exam-canonical kubeadm. k3s differs where noted.

01 — NetworkPolicy

Memorize

Default deny, both directions

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: prod
spec:
  podSelector: {}          # empty = every pod in the namespace
  policyTypes:
  - Ingress
  - Egress                  # omitting a type = that direction stays open

Allow DNS — required after any default-deny egress

  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53

Selector semantics

Written asMeans
- from: [{podSelector}, {namespaceSelector}]OR — two separate sources
- from: [{podSelector, namespaceSelector}]AND — those pods in those namespaces
podSelector: {}all pods
namespaceSelector: {}all namespaces
ipBlock.cidr + exceptthe only way to match non-pod IPs

Verify

k get netpol -n prod
k describe netpol default-deny -n prod
k run t --image busybox --rm -it --restart Never -- wget -qO- -T 3 svc.ns:80

Gotchas

  • Default-deny egress kills DNS. Symptom reads as "service is down", cause is name resolution.
  • Policies are additive and allow-only — there is no deny rule and no ordering.
  • A pod with zero matching policies is fully open; one matching policy locks it to that policy.
  • namespaceSelector matches namespace labels. kubernetes.io/metadata.name is set automatically on every namespace — use it.
  • The CNI must enforce policy. Flannel alone does not; k3s adds an enforcing controller by default.
Docs: kubernetes.io/docs/concepts/services-networking/network-policies/ — copy the "default deny all" examples at the bottom of the page.

02 — Pod Security Admission

Memorize

The label shape

k label ns prod pod-security.kubernetes.io/enforce=restricted
k label ns prod pod-security.kubernetes.io/warn=restricted
k label ns prod pod-security.kubernetes.io/audit=restricted

# pin the version, overwrite an existing label
k label ns prod pod-security.kubernetes.io/enforce-version=v1.35 --overwrite

Modes × levels

ModeEffectLevelBlocks
enforcerejects the podprivilegednothing
auditaudit log entrybaselineprivileged, hostPath, host namespaces, most capabilities
warnwarning to the clientrestrictedbaseline + must runAsNonRoot, drop ALL, seccomp RuntimeDefault

Verify

k get ns prod --show-labels
k run bad --image nginx -n prod          # should be rejected under restricted
k label ns prod pod-security.kubernetes.io/enforce-

Gotchas

  • Enforce only blocks new pods. Already-running pods keep running — a Deployment fails at its next rollout, not immediately.
  • A pod that passes baseline usually fails restricted: restricted needs runAsNonRoot, allowPrivilegeEscalation: false, capabilities.drop: [ALL], seccompProfile: RuntimeDefault.
  • Trailing - on a label key removes it.
  • Exempt the namespaces that genuinely need privilege (CNI, storage, node agents) or you break the cluster.
Docs: kubernetes.io/docs/concepts/security/pod-security-admission/ · the level field lists are under /docs/concepts/security/pod-security-standards/

03 — RBAC & ServiceAccounts

Memorize

Create, bind, verify

k create sa app -n prod

k create role pod-reader --verb get,list,watch --resource pods -n prod
k create rolebinding app-reads --role pod-reader --serviceaccount prod:app -n prod

k create clusterrole node-reader --verb get,list --resource nodes
k create clusterrolebinding app-nodes --clusterrole node-reader --serviceaccount prod:app

# always confirm by impersonation, not by re-reading the YAML
k auth can-i list pods -n prod --as system:serviceaccount:prod:app
k auth can-i --list -n prod --as system:serviceaccount:prod:app

Turn off token automount

k edit sa default -n prod        # add: automountServiceAccountToken: false
k edit deploy web -n prod        # same field under spec.template.spec
k get sa default -n prod -o yaml | grep automount

Find what to reduce

k get clusterrolebindings -o wide | grep cluster-admin
k describe clusterrole edit | head -30
k get rolebindings,roles -A

Gotchas

  • Pod spec beats ServiceAccount: if both set automountServiceAccountToken, the pod wins.
  • Security tasks say reduce, not create. Read the existing Role first, then remove verbs — don't author a fresh one.
  • RoleBinding + ClusterRole = those permissions in that one namespace. Common and frequently misread.
  • --as checks your authorization to impersonate too. A "no" can mean you lack impersonate rights, not that the SA lacks the verb.
  • Namespace on every create. Missing -n is the single most expensive habit.
Docs: kubernetes.io/docs/reference/access-authn-authz/rbac/ · SA tokens: /docs/tasks/configure-pod-container/configure-service-account/

04 — apiserver hardening

Memorize the ritual

The ritual — every single time

# 1. back up OUTSIDE the manifests directory
cp /etc/kubernetes/manifests/kube-apiserver.yaml /tmp/apiserver.bak

# 2. edit
vim /etc/kubernetes/manifests/kube-apiserver.yaml

# 3. watch it come back — kubelet reconciles, do not restart anything
crictl ps | grep apiserver
watch crictl ps

# 4. if the API never returns
cp /tmp/apiserver.bak /etc/kubernetes/manifests/kube-apiserver.yaml

Flags that get asked

FlagSet to
--anonymous-authfalse
--authorization-modeNode,RBAC — never AlwaysAllow
--profilingfalse
--tls-min-versionVersionTLS12 or VersionTLS13
--enable-admission-pluginsNodeRestriction, PodSecurity, …
--kubelet-certificate-authorityCA path
--audit-log-pathsee card 05

Gotchas

  • A .bak left inside /etc/kubernetes/manifests/ is loaded as a second static pod. Back up to /tmp.
  • Any file referenced by a flag needs a hostPath volume and a volumeMount. Flag alone = apiserver never starts.
  • Don't restart kubelet or containerd. Kubelet picks up the file change on its own; if you must kick it, move the manifest out and back.
  • While the apiserver is down kubectl is dead — crictl ps is your only view.
  • k3s: no static manifests. Flags go in /etc/rancher/k3s/config.yaml under kube-apiserver-arg, then restart the k3s service.
Docs: kubernetes.io/docs/reference/command-line-tools-reference/kube-apiserver/

05 — Audit logging

Lookup

Policy file — /etc/kubernetes/audit-policy.yaml

apiVersion: audit.k8s.io/v1
kind: Policy
rules:
# specific rules FIRST — first match wins
- level: None
  users: ["system:kube-proxy"]
- level: RequestResponse
  resources:
  - group: ""
    resources: ["secrets"]
- level: Metadata            # catch-all LAST

Four flags on the apiserver

--audit-policy-file=/etc/kubernetes/audit-policy.yaml
--audit-log-path=/var/log/kubernetes/audit.log
--audit-log-maxage=7
--audit-log-maxbackup=2

Both mounts — this is what people forget

volumes:
- name: audit-policy
  hostPath: {path: /etc/kubernetes/audit-policy.yaml, type: File}
- name: audit-log
  hostPath: {path: /var/log/kubernetes, type: DirectoryOrCreate}
volumeMounts:
- name: audit-policy
  mountPath: /etc/kubernetes/audit-policy.yaml
  readOnly: true
- name: audit-log
  mountPath: /var/log/kubernetes

Levels

LevelRecords
Nonenothing
Metadatawho, what, when — no bodies
Request+ request body
RequestResponse+ response body

Gotchas

  • First match wins. A catch-all above a specific rule makes the specific rule dead code.
  • The key is verbs, plural — and resources nests group + resources. Copy the docs example, don't type it from memory.
  • Log path is a directory mount; the policy is a File mount. Wrong type: and the apiserver won't start.
  • Read it back with tail -f /var/log/kubernetes/audit.log | grep secrets before calling it done.
Docs: kubernetes.io/docs/tasks/debug/debug-cluster/audit/ — copy the policy block verbatim, then edit.

06 — Falco rules

Lookup

Where things live

PathWhat
/etc/falco/falco.yamlconfig — output format, which rule files load
/etc/falco/falco_rules.yamlshipped rules — read, don't edit
/etc/falco/falco_rules.local.yamlyour rules and overrides

Rule shape

- rule: Shell in container
  desc: Detect a shell spawned inside a container
  condition: container.id != host and proc.name in (bash, sh, zsh)
  output: "%evt.time,%user.name,%container.id,%proc.name"
  priority: WARNING
  tags: [container, shell]

Common condition fields

FieldMatches
container.id / container.namecontainer, != host excludes the host
proc.name / proc.cmdlinethe process
evt.typesyscall: execve, open, connect
fd.name / fd.directoryfile or socket touched
k8s.ns.name / k8s.pod.nameKubernetes metadata

Apply and read

falco -r /etc/falco/falco_rules.local.yaml # validate the file
systemctl restart falco
journalctl -u falco -f
tail -f /var/log/syslog | grep falco

Gotchas

  • Tasks usually say modify an existing rule, not write one. Find it in falco_rules.yaml, copy it into .local.yaml, keep the same rule: name to override.
  • The output field list is graded literally — paste the requested fields in the requested order.
  • Rule changes need a restart. Editing the file alone changes nothing.
  • % prefixes fields in output, never in condition.
Docs: falco.org/docs/rules/ — on the allowed list. Drill navigating it like kubernetes.io.

07 — kube-bench / CIS

Lookup

Run it as a Job, not a binary on the node

curl -sLo job-master.yaml \
  https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job-master.yaml
k apply -f job-master.yaml
k wait --for=condition=complete job/kube-bench-master --timeout 180s
k logs job/kube-bench-master | tee /tmp/kb.txt
k get pod -l job-name=kube-bench-master -o wide   # WHICH node did it scan?

Neither upstream manifest is pinned. With more than one control-plane node, add nodeName: under spec.template.spec or it scans whichever it lands on.

Read the output

MarkerMeans
[PASS]control satisfied
[FAIL]automated check failed — verify before acting
[WARN]manual control, the tool can't judge it

Sections: 1 control plane · 2 etcd · 3 control-plane config · 4 worker and kubelet · 5 policies. The remediation text prints after each section and names the exact file and command.

Fix only what was asked, then prove it

grep '^\[FAIL\]' /tmp/kb.txt
grep -A3 '^\[FAIL\] 4.1.3' /tmp/kb.txt   # the remediation block

Re-run the Job afterwards. FAIL becoming PASS is the proof — not the fact that you ran a command.

Gotchas

  • A FAIL is a claim, not a fact. Verify against the live process arguments before changing anything.
  • On k3s and RKE2, sections 1.2, 1.3 and 1.4 are all false FAILs. Those distributions run the apiserver, controller-manager and scheduler as goroutines inside one process, so kube-bench finds no flags and records FAIL rather than N/A. Ground truth: journalctl -u k3s | grep -m1 "Running kube-apiserver" | tr ' ' '\n' | grep '^--' | sort
  • Fixing findings beyond the asked control scores nothing and costs real time.
  • File-permission controls (4.1.x) get reset by some upgrades. Script them; don't chmod once.
  • Write down what you deliberately won't fix as accepted deviations, with the evidence. A clean score is not the goal.
Docs: not on the allowed-docs list — the task supplies what's needed. kube-bench --help and the remediation text in the output are the in-exam reference.

Related: CKA cheat cards · AWS & Terraform cheat cards · CI/CD cheat cards · DevOps interview bank