An essential DevSecOps guide to securing Kubernetes clusters in production. Master Pod Security Standards (PSS), eBPF NetworkPolicies, and container supply chain validation.
The Expanded Attack Surface of Containerized Orchestration
Kubernetes (K8s) is the undeniable standard for deploying distributed microservices at enterprise scale. However, out-of-the-box Kubernetes configurations favor developer convenience over defensive security: pods can run as the root superuser, containers have write access to their underlying host root filesystems, and cluster internal networking is completely open (any pod can communicate with any other pod across any namespace).
If an attacker exploits a remote code execution (RCE) flaw in a publicly facing web container, default configurations permit the attacker to read the pod's service account token, probe internal databases, and potentially execute a container breakout directly onto the Linux host kernel. Production hardening requires establishing layered defenses across Admission Control, Linux Kernel namespaces, and NetworkPolicies.
1. Pod Security Standards (PSS): Enforcing Least Privilege
With the deprecation of PodSecurityPolicy (PSP), Kubernetes formalized Pod Security Standards (PSS), enforced natively through the built-in Pod Security Admission controller. PSS categorizes workload security into three distinct tiers:
- Privileged: Completely open; allows unrestricted container capabilities (only permitted for system CNI plugins and storage drivers).
- Baseline: Prevents known privilege escalations with minimal configuration friction.
- Restricted: Hardened enterprise tier; requires non-root execution, drops all Linux capabilities, and enforces immutable read-only filesystems.
# Enforcing the Restricted Security Profile on an Entire Production Namespace
apiVersion: v1
kind: Namespace
metadata:
name: production-tools
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/audit: restricted
Hardening the Container SecurityContext Manifest:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hifi-api-gateway
namespace: production-tools
spec:
template:
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
runAsGroup: 10001
fsGroup: 10001
seccompProfile:
type: RuntimeDefault
containers:
- name: gateway
image: hifitoolkit/gateway:v2.4.0
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
volumeMounts:
- name: ephemeral-tmp
mountPath: /tmp
volumes:
- name: ephemeral-tmp
emptyDir: {}
2. Zero-Trust NetworkPolicies: Eradicating Lateral Movement
By default, Kubernetes implements a flat network topology. A compromised frontend pod can open TCP connections directly to production database instances or the internal Kubernetes API server. Enforcing NetworkPolicies (backed by Calico, Cilium, or cloud CNI providers) establishes a default-deny posture.
# Production Default-Deny Ingress and Egress Policy
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: production-tools
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
---
# Explicitly Permitting Frontend to Query API Service Only
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-api
namespace: production-tools
spec:
podSelector:
matchLabels:
app: api-service
ingress:
- from:
- podSelector:
matchLabels:
app: frontend-ui
ports:
- protocol: TCP
port: 8080
Frequently Asked Questions (FAQs)
Why is readOnlyRootFilesystem: true so critical for container security?
When attackers exploit a vulnerability in a web application, their first action is typically downloading malicious exploitation scripts, reverse shells, or cryptocurrency miners into writable directories (like /tmp or /app). Locking the root filesystem as read-only blocks malware execution immediately.
What is the risk of keeping the default Kubernetes ServiceAccount token mounted?
Every pod mounts an auto-generated token at /var/run/secrets/kubernetes.io/serviceaccount/token. If RBAC permissions are loosely configured, an attacker compromising the pod can use this token to query the Kubernetes API server, list cluster secrets, or spin up unauthorized containers. Always set automountServiceAccountToken: false if the pod does not need to speak to the K8s API.
Was this technical guide helpful?
Your feedback helps our engineering team write higher quality tutorials.