Master Kubernetes security auditing by automating Trivy scans with admission controllers. Prevent vulnerable container images from deploying to your cluster.
I’ve seen too many teams treat Kubernetes security as a post-deployment checklist. They run a scan, find a mountain of CVEs, and then spend three days patching things that shouldn't have been deployed in the first place. If you’re relying on your developers to manually run Trivy scans before pushing to the registry, you’re already losing.
To build a robust DevSecOps pipeline, you need to shift security left—all the way to the cluster gatekeeper. By integrating container scanning directly into the deployment workflow using Admission Controllers, you ensure that no image hits your nodes unless it meets your security threshold.
Kubernetes Admission Controllers are the secret sauce here. Specifically, we'll use a Validating Admission Webhook. When a kubectl apply hits the API server, the webhook intercepts the request, checks the image against a Trivy scan report, and denies the request if it finds critical vulnerabilities.
We aren't going to build a custom webhook from scratch—that’s a maintenance nightmare. Instead, we’ll use Kyverno to trigger Trivy-based security checks.
The Trivy Operator automatically scans your cluster resources. Install it via Helm:
Bashhelm repo add aqua https://aquasecurity.github.io/helm-charts helm install trivy-operator aqua/trivy-operator \ --namespace trivy-system \ --create-namespace
Once installed, the operator creates VulnerabilityReport custom resources for every pod in your cluster. This is the data we'll use to make our "go/no-go" decisions.
Now, we create a Kyverno ClusterPolicy that blocks deployments containing images with "CRITICAL" vulnerabilities.
YAMLapiVersion: kyverno.io/v1 kind: ClusterPolicy metadata: name: block-critical-vulnerabilities spec: validationFailureAction: Enforce background: false rules: - name: check-trivy-vulnerabilities match: resources: kinds: - Pod validate: message: "Deployment blocked: Image contains CRITICAL vulnerabilities." pattern: metadata: annotations: trivy-operator.aquasecurity.github.io/vulnerability-report-status: "scanned" trivy-operator.aquasecurity.github.io/critical-vulnerabilities: "0"
Try deploying a known vulnerable image, like nginx:1.14.2.
Bashkubectl run vulnerable-pod --image=nginx:1.14.2
If your policy is active, the API server will reject the request with a clean, descriptive error message. You've just stopped a vulnerability before it became a runtime risk.
validationFailureAction: Audit. This logs violations without breaking your current deployments. Once your teams fix the backlog, flip it to Enforce.exclude rules based on specific namespaces or labels.By automating Kubernetes security, you remove the human element from the compliance process. Developers get instant feedback in their CI/CD logs, and you get the peace of mind that your production environment is hardened by default.
Container scanning isn't just about finding bugs; it's about enforcing a quality standard. When you combine the power of Trivy with the strict enforcement of admission controllers, you create a "secure by default" platform that scales with your organization.
Stop chasing CVEs in production. Start blocking them at the gate.
Master Kubernetes Secret Management by syncing HashiCorp Vault with External Secrets Operator. Learn how to automate secure, GitOps-friendly secret injection.
Read moreMaster Kubernetes networking by implementing Zero-Trust security with Cilium and Hubble. Learn to secure pod-to-pod communication with identity-based policies.