#Why this matters
Hardening a single Docker container is not enough once workloads run inside a Kubernetes cluster. A single compromised Pod sits on a shared cluster network and, by default, receives a mounted ServiceAccount token that lets it talk directly to the Kubernetes API server.
If that Pod also runs with privileged container settings, a wildcard RBAC role, and no network segmentation, an application bug turns into a cluster-wide breach. Kubernetes security rests on three core controls: RBAC for the control plane, Pod Security for the node runtime, and NetworkPolicies for east-west traffic.
#Kubernetes RBAC and ServiceAccount least privilege
Kubernetes authorizes API requests using Role-Based Access Control (RBAC). Two scopes exist:
RoleandRoleBindinggrant permissions inside one namespace (for exampleapp-prod).ClusterRoleandClusterRoleBindinggrant permissions across every namespace or on cluster-scoped objects likenodes.
By default, Kubernetes mounts a ServiceAccount token at /var/run/secrets/kubernetes.io/serviceaccount/token inside every Pod. Applications that never query the Kubernetes API should disable this in the Pod spec with automountServiceAccountToken: false.
Watch closely for dangerous RBAC patterns during audits:
- Wildcard verbs or resources (
verbs: ["*"],resources: ["*"]). - Broad
getorlistaccess onsecrets(exposes credentials across the namespace or cluster). createonpods/exec(allows running commands inside other Pods) orpods(allows launching a privileged Pod).escalate,bind, orimpersonateverbs, which allow a principal to grant itself higher roles.
Use kubectl auth can-i to verify effective permissions for a ServiceAccount before deploying:
$ kubectl auth can-i list secrets --all-namespaces --as=system:serviceaccount:app-prod:web-sa
no
$ kubectl auth can-i get configmaps -n app-prod --as=system:serviceaccount:app-prod:web-sa
yes#Pod Security Standards and securityContext hardening
Kubernetes enforces runtime safety through Pod Security Admission (PSA), which replaced the deprecated PodSecurityPolicy in Kubernetes 1.25. Administrators label each namespace with enforce, audit, or warn modes targeting one of three Pod Security Standards:
| Profile | Intent | Key restrictions |
|---|---|---|
Privileged |
Unrestricted | Allows host namespaces, root, and privileged containers (system daemons only) |
Baseline |
Stops known escapes | Blocks privileged: true, hostNetwork, hostPID, hostIPC, and dangerous hostPath mounts |
Restricted |
Hardened best practice | Enforces Baseline plus runAsNonRoot: true, allowPrivilegeEscalation: false, capabilities.drop: ["ALL"], and RuntimeDefault seccomp |
Label a namespace to test policies safely with warn and audit before enforcing:
$ kubectl label namespace app-prod pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/warn=restrictedTo pass the Restricted profile, configure securityContext on both the Pod and container, alongside readOnlyRootFilesystem: true for defense in depth.
#Isolating east-west traffic with NetworkPolicies
Without a NetworkPolicy, Kubernetes allows every Pod in every namespace to connect to every other Pod over the network. A NetworkPolicy is namespace-scoped and selects target Pods via podSelector. Once any NetworkPolicy selects a Pod for Ingress or Egress, all unselected traffic of that direction is denied.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress-allow-web
namespace: app-prod
spec:
podSelector:
matchLabels:
app: payment-api
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080#Defending and common mistakes
- Granting
ClusterRoleout of convenience: Always prefer a namespace-scopedRoleandRoleBindingunless cluster-wide access is genuinely required. - Forgetting
automountServiceAccountToken: false: Most application Pods never talk to the Kubernetes API; leaving tokens mounted hands attackers free API credentials on RCE. - Writing egress policies that block DNS: If you add
policyTypes: ["Egress"]without explicitly allowing UDP/TCP port53tokube-dns/coredns, service discovery breaks immediately. - Turning on
enforce=restrictedblindly: Rolling outenforce=restrictedon an active namespace blocks non-compliant Pods at admission time; label withwarnandauditfirst and fix manifests withkubescapeortrivy config. - Assuming
NetworkPolicyworks without verifying the CNI: Test blocked ports between two lab Pods after applying a policy to confirm your CNI actually enforces rules.
#Summary
Securing Kubernetes requires defense in depth across identity, runtime, and network boundaries. Replace wildcard ClusterRole rules with namespace-scoped Role permissions and disable unnecessary ServiceAccount token mounting. Enforce the Restricted Pod Security Standard through hardened securityContext fields, segment east-west Pod traffic with default-deny NetworkPolicy manifests on a policy-aware CNI, and validate YAML continuously with kubescape and trivy config.