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
latesttag —
- Verify Gatekeeper is running.
- Create a
ConstraintTemplate(policy definition containing Rego logic). - Apply a
Constraint(policy instance) that enforces required labels on Pods. - Demonstrate
denyanddryrunenforcement modes and how to audit violations safely.
Prerequisites
- A Kubernetes cluster with Gatekeeper installed and reachable via
kubectl. kubectlconfigured to the target cluster context.
1) Verify Gatekeeper components
Confirm the Gatekeeper pods are running: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)
AConstraintTemplate 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.
ConstraintTemplate defines a RequiredLabels kind and enforces that an object has a set of labels provided by the matching Constraint:
RequiredLabels kind:
RequiredLabels resource you can instantiate.
3) Create a Constraint (policy instance)
AConstraint 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).
RequiredLabels constraint that enforces team and environment labels on Pod resources:
spec.enforcementAction: one ofdeny,dryrun, orwarn.denyblocks requests that violate the constraint.dryrunrecords violations but allows requests to succeed.warnallows the request and emits a warning.
match.kinds: restricts the constraint to specific API groups/kinds (here, corePod).parameters.labels: available in Rego asinput.parameters.labels.
Enforcement modes quick reference
4) Test the constraint
Create a namespace for testing if needed:- Pod without required labels (expected to be rejected when enforcementAction: deny)
test-pod-no-labels.yaml
deny mode:
- Pod with required labels (allowed)
test-pod-with-labels.yaml
Dryrun — audit without breaking workloads
A safe rollout strategy is to start indryrun mode so you can discover which resources would be blocked, then remediate them before switching to deny.
Switch the constraint to dryrun:
kubectl describe on the Constraint to review violations recorded while in dryrun. Example messages:
- identify which resources would be blocked,
- remediate or patch existing resources, or
- narrow the constraint
matchscope 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 requestsdryrun— record violations but allow requests (use for auditing)warn— allow and emit warnings
- Best practice: audit with
dryrunfirst, remediate, then move todeny.
- Gatekeeper policy library: https://github.com/open-policy-agent/gatekeeper/tree/master/library
- Gatekeeper documentation: https://open-policy-agent.github.io/gatekeeper/