Kubernetes hardening
Kubernetes orchestrates containers at scale and its attack surface is huge: API server, RBAC, pod networking, secrets, and the runtime. A misconfigured cluster lets you escalate from a compromised pod to full control. This card covers hardening the critical points. (For K8s cloud fundamentals see Kubernetes.)
Threat model
Section titled “Threat model”The attacker’s goal is usually the same: from execution in a pod to cluster control. The path runs through the classic weak points:
- Exposed API server: the control plane; if reachable without strong auth, it’s game over.
- Lax RBAC: service accounts with
cluster-adminpermissions or dangerous verbs (create pods,get secrets,escalate). - Flat network: without NetworkPolicies, any pod talks to any other and to internal services.
- Weak secrets: K8s
Secrets are only base64, not encryption; without etcd encryption, anyone with store access reads them. - Over-privileged pods:
privileged,hostPID,hostNetwork,hostPathmounts → node escape.
flowchart TD
POD["Compromised pod (RCE in the app)"] --> SA["Mounted ServiceAccount token"]
SA -->|lax RBAC| API["API server"]
API -->|get secrets| SEC["Namespace secrets"]
API -->|create privileged pod| ESC["Pod with hostPath / privileged"]
ESC --> NODE["Node (kubelet, host)"]
NODE --> CLUSTER["Cluster control"]
POD -. flat network .-> OTHER["Other pods / internal services"]
style CLUSTER fill:#c0392b,color:#fff
Least-privilege RBAC
Section titled “Least-privilege RBAC”The most important control. Each service account should have only the permissions it needs; avoid wildcards and escalation verbs.
# Read-only Role over a specific resource, in a namespaceapiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: app name: configmap-readerrules: - apiGroups: [""] resources: ["configmaps"] verbs: ["get", "list"] # no create/update/delete, no "*"Rules: no cluster-admin for workloads; beware get/list secrets, create pods, bind, escalate, and impersonate; unmount the SA token (automountServiceAccountToken: false) in pods that don’t call the API.
Pod Security and security context
Section titled “Pod Security and security context”The Pod Security Standards (replacing the old PodSecurityPolicies) define three levels: privileged, baseline, and restricted. Apply restricted per namespace via Pod Security Admission:
apiVersion: v1kind: Namespacemetadata: name: app labels: pod-security.kubernetes.io/enforce: restricted pod-security.kubernetes.io/enforce-version: latestAnd harden each pod’s securityContext, consistent with container hardening:
securityContext: runAsNonRoot: true runAsUser: 65532 allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: ["ALL"] seccompProfile: type: RuntimeDefaultNetworkPolicies: zero-trust networking
Section titled “NetworkPolicies: zero-trust networking”By default, in K8s all pods can talk to each other. A deny-all NetworkPolicy as the baseline, then allowing only what’s needed, cuts lateral movement:
apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: default-deny-all namespace: appspec: podSelector: {} # all pods in the namespace policyTypes: ["Ingress", "Egress"] # no ingress/egress rules = everything deniedFrom there you add policies that allow, for example, only the frontend to talk to the API and only the API to the database.
Secrets, etcd, and admission
Section titled “Secrets, etcd, and admission”- etcd encryption at rest:
Secrets are base64, not encryption. You must enable encryption at rest in the API server and, better, integrate an external KMS or a secrets operator (Vault, External Secrets). - Admission control: an admission controller (OPA Gatekeeper, Kyverno) rejects at ingress whatever breaks policy — unsigned images, privileged pods, missing limits. It’s the cluster’s runtime gate.
- Image signature verification at admission, linking to the CI/CD chain.
Red Team
Section titled “Red Team”- Enumerate the token’s permissions: from a pod,
kubectl auth can-i --list(or the API directly with the token mounted at/var/run/secrets/...) reveals what the compromised SA can do. - Hunt RBAC escalations: an SA with
create podslets you launch a privileged pod mounting the host; withget secrets, steal credentials; withescalate, grant yourself more permissions. - Node escape: pods with
hostPath,hostPID,hostNetwork, orprivilegedgrant access to the node and the kubelet, and from there to other pods’ tokens. - Node metadata: from a pod, reach the node’s cloud metadata endpoint to steal the worker’s IAM role.
- Exposed API/dashboard: an API server or dashboard reachable without strong auth is a direct cluster takeover.
Blue Team
Section titled “Blue Team”- Least-privilege RBAC, audited; no
cluster-adminfor workloads; unmount unused tokens. - Pod Security Admission at
restrictedby default and a hardenedsecurityContext. - Deny-all NetworkPolicies + allow what’s needed (microsegmentation).
- etcd encryption at rest and an external secrets manager.
- Admission control (Kyverno/Gatekeeper) blocking unsigned images and insecure pods.
- API server not exposed to the internet; access via bastion/VPN and strong authn.
- Auditing and runtime detection (API server audit log + Falco).
- CIS Kubernetes Benchmark (kube-bench) as a baseline.
Checklist
Section titled “Checklist”- Least-privilege RBAC; no
cluster-adminon workloads. -
automountServiceAccountToken: falsewhere the API isn’t used. - Pod Security Admission at
restrictedper namespace. - Hardened
securityContext(non-root,drop ALL, read-only FS, seccomp). - Deny-all NetworkPolicy baseline + explicit rules.
- etcd encryption at rest / KMS for secrets.
- Admission controller verifying signature and pod policy.
- API server and dashboard not exposed to the internet.
- API server audit log enabled and monitored.
- kube-bench (CIS) run periodically.