Pod Security
Kubernetes pods are the smallest deployable units, but their security configuration is critical to preventing vulnerabilities and ensuring system integrity. Misconfigured pods can expose the cluster to privilege escalation, data leaks, and unauthorized access. This section covers how to secure pods using security contexts, prevent privilege escalation, and follow best practices for robust pod configurations.
Pod Security Context¶
The securityContext field in a pod's spec defines how the container processes run, including user permissions, capabilities, and privilege settings. Key parameters include:
runAsUser: Specifies the user ID (UID) the container runs as. Avoid usingroot(UID 0) unless absolutely necessary.runAsGroup: Defines the group ID (GID) for the container.capabilities: Grants or restricts Linux capabilities (e.g.,CAP_NET_BIND_SERVICEfor binding to privileged ports).privileged: Whentrue, the container has full access to the host’s devices. Set this tofalseby default.allowPrivilegeEscalation: Prevents processes from escalating privileges (e.g., viasudo). Set tofalseto block this.
Example: Non-root user with limited capabilities
spec:
containers:
- name: secure-app
image: your-image
securityContext:
runAsUser: 1000
runAsGroup: 3000
capabilities:
drop:
- ALL
allowPrivilegeEscalation: false
Privilege Escalation Prevention¶
Privilege escalation occurs when a process gains higher permissions than intended, often via exploiting misconfigurations. To mitigate this:
- Avoid running as root: Use non-root users (e.g.,
runAsUser: 1000) andrunAsNonRoot: trueto enforce this. - Disable privileged mode: Set
privileged: falseunless explicitly required for specific workloads. - Restrict capabilities: Drop unnecessary capabilities using the
capabilities.dropfield. - Use PodSecurity Admission Controller: Enforce security policies via Kubernetes' built-in admission controllers (e.g.,
PodSecurity), which validate pod specs against predefined policies likeRestrictedorPrivileged.
Example: Enforce non-root execution
spec:
containers:
- name: secure-app
image: your-image
securityContext:
runAsNonRoot: true
runAsUser: 1000
Best Practices for Secure Pod Configuration¶
- Run as non-root users: Always set
runAsUserto a non-root UID and enablerunAsNonRoot. - Limit capabilities: Drop unused capabilities to minimize attack surfaces.
- Disable privileged mode: Avoid
privileged: trueunless absolutely necessary. - Use read-only file systems: Set
readOnlyRootFilesystem: trueto prevent container escape attacks. - Secure secrets: Use Kubernetes Secrets or Vault instead of hardcoding credentials in images.
- Leverage admission controllers: Enable
PodSecurityto enforce security policies across the cluster.
Example: Full security context configuration
spec:
containers:
- name: secure-app
image: your-image
securityContext:
runAsUser: 1000
runAsGroup: 3000
capabilities:
drop:
- ALL
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
Key takeaways¶
- Always run containers as non-root users to reduce exploitation risks.
- Disable privileged mode and restrict capabilities to minimize attack surfaces.
- Use
securityContextto enforce user, group, and capability settings. - Leverage Kubernetes admission controllers to enforce security policies.
- Keep file systems read-only and avoid hardcoding sensitive data in pod specs.