Skip to main content
Built-in Kubernetes admission controllers like LimitRanger and ResourceQuota help govern cluster resources, but they are limited to resource-specific checks. For richer, custom policy enforcement — for example:
  • requiring specific Kubernetes labels on every Pod,
  • restricting container images to approved registries, or
  • forbidding use of the latest tag —
you can extend the admission pipeline using Open Policy Agent (OPA) Gatekeeper. This guide walks through a concise, safe workflow to author and roll out policies with Gatekeeper:
  1. Verify Gatekeeper is running.
  2. Create a ConstraintTemplate (policy definition containing Rego logic).
  3. Apply a Constraint (policy instance) that enforces required labels on Pods.
  4. Demonstrate deny and dryrun enforcement modes and how to audit violations safely.

Prerequisites

  • A Kubernetes cluster with Gatekeeper installed and reachable via kubectl.
  • kubectl configured to the target cluster context.

1) Verify Gatekeeper components

Confirm the Gatekeeper pods are running:
Example output:
If Gatekeeper pods are not running, install or redeploy Gatekeeper before continuing. Gatekeeper integrates with the Kubernetes admission webhook to enforce policies at request time.

2) Create a ConstraintTemplate (policy definition)

A ConstraintTemplate serves two roles:
  • It defines a new CRD so Gatekeeper can register a custom resource kind.
  • It contains the Rego policy logic that Gatekeeper evaluates at admission time.
The following ConstraintTemplate defines a RequiredLabels kind and enforces that an object has a set of labels provided by the matching Constraint:
Apply the template:
When applied, Gatekeeper creates a new CRD for the RequiredLabels kind:
Example output:
This indicates the API now exposes a RequiredLabels resource you can instantiate.

3) Create a Constraint (policy instance)

A Constraint is an instance of the CRD created by the ConstraintTemplate. It configures:
  • which resources the policy targets (match),
  • the enforcement mode (spec.enforcementAction), and
  • any parameters the template requires (parameters).
Example RequiredLabels constraint that enforces team and environment labels on Pod resources:
Key fields:
  • spec.enforcementAction: one of deny, dryrun, or warn.
    • deny blocks requests that violate the constraint.
    • dryrun records violations but allows requests to succeed.
    • warn allows the request and emits a warning.
  • match.kinds: restricts the constraint to specific API groups/kinds (here, core Pod).
  • parameters.labels: available in Rego as input.parameters.labels.
Apply the constraint:
Inspect the constraint status and violations:

Enforcement modes quick reference

4) Test the constraint

Create a namespace for testing if needed:
  1. Pod without required labels (expected to be rejected when enforcementAction: deny)
File: test-pod-no-labels.yaml
Apply it:
Expected rejection when the constraint is in deny mode:
  1. Pod with required labels (allowed)
File: test-pod-with-labels.yaml
Apply it:
Expected success:

Dryrun — audit without breaking workloads

A safe rollout strategy is to start in dryrun mode so you can discover which resources would be blocked, then remediate them before switching to deny. Switch the constraint to dryrun:
Now retry creating the unlabeled pod:
It should be admitted:
List pods in the test namespace:
Example output:
View recorded violations (example excerpt) Use kubectl describe on the Constraint to review violations recorded while in dryrun. Example messages:
Violations may include other cluster resources (including system Pods) that lack the labels. Use this information to:
  • identify which resources would be blocked,
  • remediate or patch existing resources, or
  • narrow the constraint match scope to avoid unintended impact.
Safe rollout pattern: start with dryrun to collect violations, remediate or adjust scope for failing resources, and then switch enforcementAction to deny to fully enforce the policy.

Summary

  • ConstraintTemplate: defines a new CRD and contains the Rego logic evaluated at admission.
  • Constraint: an instance of that CRD that scopes the policy, supplies parameters, and sets enforcement behavior.
  • Enforcement modes:
    • deny — block violating requests
    • dryrun — record violations but allow requests (use for auditing)
    • warn — allow and emit warnings
  • Best practice: audit with dryrun first, remediate, then move to deny.
For more policies and examples:

Watch Video

Practice Lab