Table of Contents
- Common Kubernetes Vulnerabilities
- Scanning and Policy Enforcement
- Defining and Applying securityContext
- Additional Kubernetes Security Features
- Demo: Validating Deployments with OPA Conftest
- Links & References
Common Kubernetes Vulnerabilities
Attackers often exploit misconfigurations or unpatched components. Typical risks include:- Privileged Containers
- Images with Known CVEs
- Excessive RBAC Permissions
- Unrestricted Network Access
- Improper Secret Management
Scanning and Policy Enforcement
Leverage automated scanners and policy-as-code to detect risks before deployment. Below is a comparison:| Tool | Use Case | Example Command |
|---|---|---|
| Trivy | Image & filesystem CVE scanning | trivy image my-app:latest |
| Kubesec | Static manifest analysis | kubesec scan deployment.yaml |
| OPA Conftest | Custom policy checks against YAML | conftest test deployment.yaml --policy policies/ |
Integrate these tools into your CI pipelines for continuous assessment. For example, add a GitHub Action step to run
trivy on every push.Defining and Applying securityContext
AsecurityContext sets Linux privileges and filesystem controls for Pods and containers. Enforcing non-root users and read-only filesystems significantly reduces attack surface.
| Field | Description |
|---|---|
runAsUser | Numeric UID the container must run as |
runAsNonRoot | Ensures the container UID is non-zero |
readOnlyRootFilesystem | Mounts the root filesystem read-only |
allowPrivilegeEscalation | Defaults to false to block setuid binaries and escalations |
Running containers as root (UID 0) may allow privilege escalation and lateral movement within your cluster. Always set
runAsNonRoot: true unless absolutely necessary.Example securityContext
Additional Kubernetes Security Features
BeyondsecurityContext, Kubernetes offers:
- AppArmor & SELinux policies for Mandatory Access Control
- Pod Security Admission & legacy PodSecurityPolicy
- NetworkPolicies to isolate traffic at the pod level
- Audit Logging for forensic analysis
- Air-gapped Cluster Deployments for sensitive environments
- TLS Encryption for API and etcd communication
Demo: Validating Deployments with OPA Conftest
-
Write a Rego Policy
Createpolicies/run_as_non_root.rego: -
Create a Sample Deployment
Save asdeployment.yaml: -
Run Conftest
You should see an error indicating the missing
runAsNonRootsetting.