Skip to main content
OPA Gatekeeper is a powerful policy engine, but it requires learning the Rego language. Kyverno offers an alternative: policies are written as Kubernetes-native YAML, so you can author validate, mutate, and generate rules without learning a new language. This guide verifies Kyverno is running and demonstrates three common policy patterns:
  • validate — allow or deny resources
  • mutate — automatically modify resources
  • generate — create resources in response to events
You’ll also learn how to use Audit mode and PolicyReports to safely roll out policies.

At a glance — Kyverno rule types

Quick links:

Prerequisites

  • A running Kubernetes cluster with Kyverno installed in the kyverno namespace.
  • kubectl configured for the target cluster.

Verify Kyverno is running

Check Kyverno pods in the kyverno namespace:
Example output:
If any Kyverno pods are not Running, check pod logs and events to diagnose installation issues.

1) Validate rule example — restrict image registries

This example enforces that Pod container images must come from docker.io. The policy uses Kyverno YAML pattern overlays — no Rego required. File: validate-registry.yaml
Notes:
  • validationFailureAction: Enforce blocks requests that violate the policy. Switch to Audit to only record violations while allowing creation.
  • Kyverno uses YAML pattern overlays so policies are readable and Kubernetes-native.
Apply the policy:
Test with an untrusted image from quay.io: File: test-pod-untrusted.yaml
Attempt to create the Pod in namespace team-dev:
If enforcement is enabled, the admission webhook will deny the request. Example error:
Now try a trusted image from docker.io: File: test-pod-trusted.yaml
Apply it:
The trusted pod is admitted because it matches the policy pattern.
You can switch validationFailureAction to Audit to collect violations without blocking resources.

2) Mutate rule example — auto-inject labels

Use a mutate rule to automatically inject labels into Pods created in the team-dev namespace using a strategic merge patch. File: mutate-labels.yaml
Apply the policy:
Create a pod (use a docker.io image so the validate policy does not block it):
Check the labels injected by Kyverno:
Example output:
The labels managed-by=kyverno and environment=dev were added automatically by the mutate rule.

3) Generate rule example — auto-create NetworkPolicy on Namespace creation

A generate rule can create a default-deny NetworkPolicy whenever a Namespace is created. File: generate-netpol.yaml
Notes:
  • The namespace field uses the template {{request.object.metadata.name}} so the generated NetworkPolicy is created inside the Namespace that triggered the rule.
Apply the policy:
Create a namespace to trigger generation:
Verify the generated NetworkPolicy:
Example output:
Kyverno automatically created default-deny in auto-np-test.

4) Audit mode and PolicyReports

Audit mode allows resources that violate policies to be created while Kyverno records the violations in PolicyReport resources. This is useful for observing policy impact before enforcing. Patch the restrict-image-registries policy to Audit:
Now apply the untrusted pod again:
The Pod is created (not blocked), and Kyverno records the policy results as PolicyReport resources. List policy reports in the namespace:
Example output (summary):
Describe a PolicyReport for the untrusted pod to view details:
Example (truncated) output from the PolicyReport:
This output shows:
  • The mutate rule passed and injected labels.
  • The validate rule failed (image not from docker.io), but because the policy is in Audit mode, the Pod was admitted and the violation was captured for visibility.
Use Audit mode to evaluate policy impact and blast radius. Once you are confident, switch policies back to Enforce to block violating resources.

Summary and best practices

  • Kyverno policies are Kubernetes-native YAML — no Rego required.
  • Use the three complementary rule types:
    • validate: block or audit resources
    • mutate: change resources at admission (strategic merge, JSON patches, etc.)
    • generate: create resources in response to events (e.g., default-deny NetworkPolicy)
  • Start in Audit mode and review PolicyReports to safely roll out policies.
  • Combine mutate + validate rules to auto-fix common issues and enforce desired state.

Watch Video

Practice Lab