Cluster Security & Policy Audit
DevOps IaC Part 2 β Day 13
Audit K8s manifests for non-root security context (runAsNonRoot), Trivy container scanning, and Checkov static analysis rules
Default Kubernetes workloads are notoriously permissive: containers run as the root user (UID 0), possess Linux capabilities that allow container breakouts, and read/write to the host root filesystem. On Day 13, you will enforce enterprise Pod Security Standards (Restricted Profile) directly in Terraform HCL, scan your infrastructure code with Checkov static analysis, and inspect container images with Trivy to ensure zero critical CVEs before deployment.
An interactive 20 min video walkthrough for Day 13 is currently in production. Follow the step-by-step interactive playbook below!
The official reference codebase for Day 13 is hosted in the repository. Clone the repository and navigate to day-13-cluster-security to run the working solution.
git clone https://github.com/777abhi/devops-terraform-roadmap.git
cd devops-terraform-roadmap/day-13-cluster-securitySecurity Standards: Baseline vs Restricted PSS
Understand Kubernetes Pod Security Standards (Privileged, Baseline, Restricted) and the CIS Benchmark.
Identify common container misconfigurations that expose host nodes to privilege escalation.
Kubernetes Threat Vectors: 1. Root Execution (UID 0): An attacker exploiting an app vulnerability gets host root privileges. 2. Writable Root Filesystem: Allows attackers to install rootkits and download malicious binaries. 3. Excessive Capabilities: Defaults grant CAP_CHOWN, CAP_KILL, CAP_NET_BIND_SERVICE, etc. 4. Host IPC/PID/Network Sharing: Exposes socket communication between processes on the node. Restricted Profile Enforcement: β’ runAsNonRoot: true (UID >= 1000) β’ readOnlyRootFilesystem: true β’ allowPrivilegeEscalation: false β’ capabilities.drop: ["ALL"]
HCL Manifest Hardening: Security Contexts
Express the Restricted Pod Security profile directly in Terraform HCL.
Configure pod and container security contexts to satisfy enterprise compliance.
# main.tf
resource "kubernetes_namespace" "secure_ns" {
metadata {
name = "secure-production"
labels = {
"pod-security.kubernetes.io/enforce" = "restricted"
"pod-security.kubernetes.io/audit" = "restricted"
}
}
}
resource "kubernetes_deployment" "hardened_app" {
metadata {
name = "secure-microservice"
namespace = kubernetes_namespace.secure_ns.metadata[0].name
}
spec {
replicas = 2
selector {
match_labels = {
app = "secure-microservice"
}
}
template {
metadata {
labels = {
app = "secure-microservice"
}
}
spec {
# Pod-level security context
security_context {
run_as_non_root = true
run_as_user = 10001
run_as_group = 10001
fs_group = 10001
}
container {
name = "web"
image = "nginxinc/nginx-unprivileged:alpine"
security_context {
allow_privilege_escalation = false
read_only_root_filesystem = true
privileged = false
capabilities {
drop = ["ALL"]
}
}
port {
container_port = 8080
}
}
}
}
}
}Automated Security Scanning: Checkov & Trivy
Run static policy checks against Terraform code and container image vulnerability scans.
Execute Checkov to verify all CIS benchmark and Kubernetes rules pass.
cd /Users/abhinavsharma/Downloads/devops-terraform-roadmap/day-13-cluster-security
checkov -d . --framework terraform,kubernetesAudit container image for known CVEs before deployment.
trivy image --severity HIGH,CRITICAL nginxinc/nginx-unprivileged:alpineProduction Quality Gates & Compliance
Integrate security scans into PR quality gates with automated build rejection.
In production pipelines, configure the security scanner step to fail the build whenever a policy violation or Critical CVE is detected:
Passed checks: 14, Failed checks: 0, Skipped checks: 0 Status: PASSED - Manifest conforms to Restricted Pod Security Standard.
- Understood Pod Security Standards (Baseline vs Restricted) and common container threat vectors.
- Hardened Kubernetes Deployment in HCL with runAsNonRoot, readOnlyRootFilesystem, and drop ALL.
- Used unprivileged container images (nginxinc/nginx-unprivileged) capable of running non-root.
- Audited Terraform HCL with Checkov static policy engine achieving zero failures.
- Executed Trivy vulnerability scanning to block HIGH/CRITICAL CVEs at the pipeline boundary.