Kubernetes Security & Hardening›Detect & Harden · Cheat sheet & self-check

Detect & Harden · wrap-up

Cheat sheet & self-check

Every command from this section on one page.

13 · Audit logging & runtime detection

API audit (kube-apiserver flags)

--audit-policy-file=/etc/kubernetes/audit/policy.yamlWhich events to record, at which level
--audit-log-path=/var/log/kubernetes/audit/audit.logWhere to write (or use a webhook backend)
--audit-log-maxage=30 --audit-log-maxbackup=10 --audit-log-maxsize=200Rotation: days, files, MB
jq 'select(.objectRef.resource=="secrets") | {user: .user.username, verb, ns: .objectRef.namespace}' audit.logWho touched Secrets

Falco

helm repo add falcosecurity https://falcosecurity.github.io/chartsChart repository
helm install falco falcosecurity/falco -n falco --create-namespaceInstall Falco
kubectl -n falco logs -l app.kubernetes.io/name=falco -fWatch alerts

14 · CIS benchmark hardening

Run kube-bench

kubectl apply -f https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job.yamlRun kube-bench as a Job on a node
kubectl logs job/kube-benchRead the report
kube-bench run --targets nodeRun only node checks (binary on a node)

Kubelet settings to check (KubeletConfiguration)

authentication.anonymous.enabled: falseNo anonymous kubelet API access
authorization.mode: WebhookKubelet asks the API server who may do what
readOnlyPort: 0Disable the unauthenticated read-only port
protectKernelDefaults: trueFail rather than silently changing kernel settings
rotateCertificates: trueRotate the kubelet client certificate

15 · Capstone: hardened cluster from scratch

Verification commands

kubectl auth can-i --list --as=system:serviceaccount:shop:default -n shopWhat the default ServiceAccount may do (should be nothing useful)
kubectl run test --image=nginx:1.27 -n shopShould be rejected under restricted PSA
kubectl -n shop exec deploy/web -- wget -qO- -T 3 http://api.payments:8080Cross-namespace call (should time out)
kubectl logs job/kube-benchBenchmark result