Advanced Track Do this after finishing Chapters 01-14.

Estimated Time

  • Reading: 30-40 min
  • Lab: 60-90 min
  • Quiz: 15-20 min

Prerequisites

  • Core track (Chapters 01-14) completed.
  • GitOps promotion and observability workflows available.

Source Code References

  • require-security-context.example.yaml Members

Sign in to view source code.

What You Will Produce

Three Kyverno policies running in Audit, one promoted to Enforce, and a deny -> fix -> admit cycle you can explain.

Chapter 15: Admission Policy Guardrails

Learning Objectives

By the end of this chapter, you will be able to:

  • Deploy Kyverno policy packs in audit mode for safe rollout
  • Graduate policies from audit to enforce using evidence from audit logs
  • Configure break-glass exceptions with expiry and evidence requirements
  • Evaluate policy coverage against the security baseline

Start with the video for the concept overview, then work through each lesson section.

Upstream guardrails (CI, PR reviews) are essential but can be bypassed. In this chapter, we implement a Cluster Gatekeeper using Kyverno. This is our final line of defense, ensuring that only workloads matching our production standards are admitted to the cluster.


1. The Problem: The “Policy Bypass”

A critical configuration error (like running as root or missing resource limits) slips through the CI/CD pipeline. Without a cluster-side check, the non-compliant workload runs in production, violating your security and stability invariants. The system’s safety depends entirely on every previous step being perfect.

2. The Concept: Cluster-Side Enforcement

We move from “hoping” the pipeline is correct to “enforcing” the state at the Kubernetes API boundary.

  1. Admission Control: Every request to create or update a resource is intercepted by a policy engine.
  2. Kyverno: A Kubernetes-native policy engine that uses standard YAML to define rules.
  3. Fail-Closed: If a workload violates a critical policy (e.g., Security Context), the cluster rejects the request entirely.

3. The Code: The Policy Pack

Our sre/ repo includes standard policy packs for security, resources, and best practices. These policies are the definitive contract for what is allowed to run in our cluster.

Security context admission policy

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: require-security-context-example
spec:
  validationFailureAction: Audit
  background: true
  rules:
    - name: require-container-security-context
      match:
        any:
          - resources:
              kinds:
                - Pod
      validate:
        message: "Containers must run as non-root and disallow privilege escalation."
        pattern:
          spec:
            containers:
              - securityContext:
                  runAsNonRoot: true
                  allowPrivilegeEscalation: false

4. The Guardrail: Audit-First Rollout

We never block traffic blindly. Every new policy starts in Audit mode, where violations are recorded in a PolicyReport but not blocked. Only after the audit is clean do we move the policy to Enforce mode.

5. Verification: Did I Get It?

Check your current cluster compliance and test the boundary:

# Compliance reports for the scratch namespace
kubectl -n lab get policyreports
# Deploy a deliberately non-compliant pod there (latest tag, no limits, no securityContext)
kubectl -n lab run policy-test --image=nginx:latest
kubectl -n lab get policyreports -o wide

Expected Output: the pod runs (Kyverno is in Audit) and the report gains fail entries for it. Why lab and not develop? The environment namespaces enforce Pod Security restricted, which rejects this pod before Kyverno ever sees it — you would be observing PSS, not admission policy. lab only warns, so the policy engine is the one talking.


Detailed Lessons

Hands-On Materials

Labs, quizzes, and runbooks — available to course members.

  • Admission Policy Guardrails Scorecard (Template) Members
  • Lab: Admission Guardrails in Audit and Enforce Modes (Advanced) Members
  • Quiz: Chapter 15 (Admission Policy Guardrails) Members
  • Runbook: Admission Policy Operations (Advanced) Members

The Incident: The Policy Bypass

Result: The system’s safety depends entirely on upstream processes being perfect. A single mistake in the pipeline becomes a production vulnerability. Observed Symptoms What the team sees first: Workloads are …

Investigation & Containment

Safe investigation sequence: Identify Violation: Use kubectl get policyreports or check cluster events for denied requests. Trace Origin: Determine how the non-compliant workload reached the cluster (CI bypass, manual …

Workflow & Strategy

Kubernetes Native: Policies are written in YAML, not a specialized language like Rego. Mutation Capable: It can automatically inject sidecars or fix small label errors. Image Aware: Built-in support for verifying …

Lab & Completion

Done When You have completed this chapter when: You can demonstrate the difference between Audit and Enforce policy modes. You have successfully blocked a non-compliant pod at the cluster boundary. You can find and …