What this covers and who it is for
Kubernetes security is the work of closing six gaps around a workload you did not write and cannot fully see: the identity the pod runs as, the network paths it can reach, the capabilities it can call, and the image it came from. Do them in order — cut pod identities, deny the network by default, harden the pod spec, enforce at admission, control the image supply chain, and watch the API server. Skip the first two and the rest is decoration.
This hub gives you the order, the manifests to start from, and the failure modes behind false assurance. It is for the people who own this in practice: platform and DevOps engineers running a cluster, SREs who inherited one, developers who ship Deployments and want to know what the platform team keeps asking for, and small-business owners on a managed cluster without dedicated security staff. It assumes you can read YAML and run kubectl, not that you have a security team, a service mesh, or a policy budget.
Out of scope: cloud account security beyond the cluster boundary, bugs in your own application code, and fleet management across clusters. This page stays on the cluster and the workloads inside it.
The short version
If you read nothing else: treat every pod as a process that will eventually be compromised, and spend your effort on what it can still do afterwards. Six defaults, in order.
- Identity first. Stop mounting service account tokens into pods that never call the API, create one service account per workload, and bind it to a Role that lists specific verbs and resources.
- Network default-deny. Deny ingress and egress in every namespace, then add back only the paths you can name.
- Harden the pod spec. Non-root, no privilege escalation, all capabilities dropped, read-only root filesystem, runtime default seccomp profile, resource limits.
- Enforce, do not suggest. Put new namespaces in restricted Pod Security Admission mode, and use a policy engine for what admission does not cover.
- Control the supply chain. Pin images by digest, scan them, sign them, and refuse unsigned ones at admission.
- Watch the API and the runtime. Enable audit logging, ship it off-cluster, and alert on the few events that matter.
The order is deliberate. Identity and network changes limit blast radius when a container is popped, and they are the two teams skip most often, because neither produces a scan report to point at.
Key concepts you need first
A handful of ideas carry most of the weight. Once they are clear, the steps below read as obvious rather than arbitrary.
The pod is a trust boundary that does not exist. Containers in one pod share a network namespace and can reach each other’s volumes unless you configure otherwise. There is no isolation between them. Treat the pod as one trust unit and split workloads that should not share one.
Service accounts are identities, and they are the ones that matter. People authenticate through your identity provider; pods authenticate as service accounts. By default a token is mounted into every container at /var/run/secrets/kubernetes.io/serviceaccount, so anything with code execution there can call the API server. A pod allowed to read Secrets can usually read every Secret in its namespace.
Admission is where policy becomes real. Anything written in a wiki is a suggestion; admission controllers decide whether an object is stored. Pod Security Admission applies the built-in Pod Security Standards — privileged, baseline, restricted — per namespace. For rules it does not cover, engines such as Kyverno or Gatekeeper enforce your own.
Namespaces are labels, not walls. With no NetworkPolicy in place, every pod can reach every other pod in the cluster, because absence of a policy means allow. A namespace becomes isolated only when a policy selects its pods and your CNI implements it. Verify the second half; not every plugin does.
The node is shared and the host is reachable. Privileged containers, hostPath, hostNetwork, hostPID, and the runtime socket all bridge a pod into the node. One privileged pod is usually equivalent to root on that node, and from there to every pod scheduled on it.
Provenance is a security property. A tag is mutable; a digest is not. If the cluster cannot say who built an image and what is in it, nothing else here counts.
How to apply this
Work through these steps on one namespace before going cluster-wide. Pick a low-risk but real workload — an internal API, not the ingress controller — so breakage surfaces early and cheaply.
Step 1: Find the pods that mount tokens. Start with the ones you did not write:
kubectl get pods -A -o json | jq -r '.items[] | select(.spec.automountServiceAccountToken != false) | "\(.metadata.namespace)/\(.metadata.name)"'
Every line is a workload holding an identity nobody chose, and most of them do not need it.
Step 2: Fix identity before anything else. Set automountServiceAccountToken: false unless the workload genuinely calls the API, create one service account per workload, and bind it to a namespaced Role with named verbs and resources. Never bind cluster-admin to a workload account and never leave a workload on default. Check what the account can do by impersonating it:
kubectl auth can-i list secrets --as=system:serviceaccount:payments:invoice-api -n payments
The answer should be no. If it is yes, find the binding that granted it before changing anything else.
Step 3: Enforce pod security at the namespace. Label with warn first, read the warnings your Deployments generate, then switch to enforce.
apiVersion: v1
kind: Namespace
metadata:
name: payments
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/warn: restricted
Restricted mode rejects the shortcuts that matter: privileged containers, hostPath, host namespaces, added capabilities. Expect real remediation, not a cosmetic change.
Step 4: Default-deny the network, then re-open DNS. One policy denies ingress and egress for the whole namespace, then adds back the path that always breaks first.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-plus-dns
namespace: payments
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
The API server labels every namespace with its own name, so that selector stays stable without hand-maintained labels. Add rules for the database and queue next, and block the cloud metadata address 169.254.169.254 from application namespaces — on cloud nodes it can hand a pod the node’s own credentials.
Step 5: Harden the pod spec. This is the worked example. An internal API talks to a database and a queue. The version most teams ship has a single container, no security context, the default service account, a tag instead of a digest, and no limits. The hardened version runs the same application with almost none of that:
apiVersion: apps/v1
kind: Deployment
metadata:
name: invoice-api
namespace: payments
spec:
replicas: 2
selector:
matchLabels:
app: invoice-api
template:
metadata:
labels:
app: invoice-api
spec:
automountServiceAccountToken: false
serviceAccountName: invoice-api
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: api
image: registry.example.com/payments/invoice-api@sha256:REPLACE_WITH_DIGEST
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
volumeMounts:
- name: scratch
mountPath: /tmp
volumes:
- name: scratch
emptyDir: {}
Three settings carry the weight. runAsNonRoot means a container escape starts as an unprivileged user, not root. readOnlyRootFilesystem means an attacker with code execution cannot drop a binary or edit the application’s own files. drop: [“ALL”] removes the capabilities the runtime grants for historical reasons, including mounting filesystems and using raw sockets.
The emptyDir at /tmp is not decoration. Many frameworks write to temporary paths and will crash on a read-only root filesystem without a writable scratch volume. Add the scratch space deliberately instead of switching the flag back off, then verify from inside the running container:
kubectl -n payments exec deploy/invoice-api -- id
kubectl -n payments exec deploy/invoice-api -- touch /etc/should-fail
kubectl -n payments get pod -l app=invoice-api -o jsonpath='{.items[0].spec.containers[0].securityContext}'
Expect an unprivileged user, a read-only filesystem error on the write, and the security context echoed back as applied.
Step 6: Control supply and watch the API. Pin images by digest, scan in CI, sign with a tool such as Cosign, and enforce signature and scan status at admission. Enable audit logging and forward it off-cluster. The events worth alerting on are narrow: RBAC patched, Secrets read in bulk, RoleBindings created to cluster-admin, pods appearing where none should. Runtime detection with Falco or Tetragon covers the other half — a shell spawned inside a container that has no reason to own one.
Where people get this wrong
They harden the main container and forget the rest. Init containers and sidecars run with the privileges the pod gives them, so a locked-down application container next to a sidecar with a writable filesystem is not hardened. Set the security context at pod level, then tighten per container.
They apply NetworkPolicy to a CNI that ignores it. This is the most expensive false assurance in Kubernetes: the object is accepted, kubectl describe shows it exists, and traffic still flows. Test it by running a throwaway pod and trying to reach a service the policy should block. If the connection succeeds, you have policy objects, not policy.
They turn token mounting back on because something broke. Disabling automatic mounting is the highest-value single line in most manifests, and it gets reversed the first time a workload fails because an SDK expected a token. The fix is a dedicated service account with a small Role, not restoring the default. A token a pod requests explicitly is not covered by the automount setting anyway.
They treat namespaces as tenant isolation. Namespaces give you RBAC scoping and policy grouping. They do not stop network traffic or stop a pod landing on a node shared with another tenant, and cluster-scoped objects — custom resource definitions, PersistentVolumes, StorageClasses — are visible across them. For hostile tenants, use separate clusters.
They secure the cluster and ignore the way in. Long-lived kubeconfigs with cluster-admin rights, copied between laptops and CI systems, undo everything above. Prefer short-lived credentials from an OIDC issuer, route human access through an identity provider with MFA, and give CI a scoped identity rather than a kubeconfig in a repository secret.
They mistake scanning for security. A clean scan means no known-vulnerable package at scan time. It says nothing about the manifest, the RBAC, the network, or whether the running image is the one you approved. Scanning is one of six controls and the easiest to buy.
Where to go next
This page is the hub. These articles go deeper on each layer, in the order you would work through them.
- Docker Unveiled: Understanding and Installing Docker — the namespaces and cgroups underneath every pod, which is what Kubernetes security is really constraining.
- Exploiting and Securing Virtualization: From VM Escape to Container Vulnerabilities — what an escape looks like, and the failure the hardening above is built to blunt.
- Enhancing DevOps Security: Strategies for Securing CI/CD Pipelines — where images are built and signed, and where the credentials that deploy to your cluster live.
- Understanding DevSecOps — moving these checks into the delivery process instead of a quarterly review.
- Secure Software Development Life Cycle — the upstream controls that decide how much you must catch at admission.
- Zero to Patch: A Practical Patch Management Checklist — because most clusters rot through stale base images, not through clever attacks.
- Beyond CVSS: Contextual Vulnerability Prioritization — triaging scanner output when a cluster produces more findings than you can read.
- Zero Trust Architecture — the principle behind default-deny policy, applied beyond one cluster.
- Mastering Cloud Security Audits and Mastering AWS Config — the account-level controls around a managed cluster.
Start with two changes that cost nothing and break nothing: turn off automatic service account token mounting, and run one namespace through warn mode of the restricted Pod Security Standard to see what your manifests would fail. Both take an afternoon and say more about posture than any scanner report.
Image credit: nayukim, licensed under CC BY 2.0.