Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 125Kubernetes · Security

How to enforce a Kubernetes policy with Kyverno

Teams should not deploy :latest images, Pods without limits, or containers that run as root — but rules in a wiki are not enforced. Kyverno checks every object the API server receives against policies written in plain YAML, and can warn (Audit) or refuse (Enforce). This how-to installs it, writes a policy, rolls it out safely (Audit first), and reads the reports.

  • 5 Lessons
  • 2 Commands

$ run as a normal user# run as root (or with sudo)

125.1

The quick answer

helm repo add kyverno https://kyverno.github.io/kyverno/
helm upgrade --install kyverno kyverno/kyverno -n kyverno --create-namespace
kubectl apply -f policies/no-latest.yaml
kubectl get policyreport -A
policies/no-latest.yaml
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: no-latest-tag
spec:
  background: true
  rules:
    - name: no-latest-tag
      match:
        any:
          - resources: { kinds: [Pod] }
      validate:
        failureAction: Audit
        message: "Use a fixed image tag, not ':latest' or no tag."
        pattern:
          spec:
            containers:
              - image: "!*:latest & *:*"

125.2

Step by step

  1. Install Kyverno with Helm (quick answer; for production use 3 replicas of the admission controller — see the full guide).
  2. Write the policy (quick answer). Parts: match (which objects), validate (the rule), failureAction (Audit = report only, Enforce = refuse).
  3. Start with Audit — see who would be blocked, without breaking anything:
kubectl get policyreport -A
kubectl get policyreport -n shop -o jsonpath='{range .items[*].results[?(@.result=="fail")]}{.policy}{"  "}{.resources[0].name}{"\n"}{end}'
  1. Fix the findings with the teams, then switch to Enforce:
kubectl patch clusterpolicy no-latest-tag --type=json -p '[{"op":"replace","path":"/spec/rules/0/validate/failureAction","value":"Enforce"}]'
  1. Test that it blocks:
kubectl run test --image=nginx:latest -n shop
Error from server: admission webhook "validate.kyverno.svc-fail" denied the request:
resource Pod/shop/test was blocked due to the following policies
no-latest-tag: …Use a fixed image tag, not ':latest' or no tag.
  1. Ready-made policies — the Pod Security Standards as Kyverno policies:
helm upgrade --install kyverno-policies kyverno/kyverno-policies -n kyverno --set podSecurityStandard=restricted --set validationFailureAction=Audit
  1. Exceptions — for one system namespace or tool, use a PolicyException (reviewed, documented), not a weaker policy for everybody.

125.3

Check that it worked

kubectl get clusterpolicy shows the policy READY, policy reports show results, and in Enforce mode a bad Pod is refused with your message while good Pods start normally.

125.4

Common problems

ProblemCauseFix
Deployments are not blocked, only their Pods failPolicy matches Pods; Kyverno auto-generates rules for controllers.Check the generated rules (kubectl get clusterpolicy -o yaml); the Deployment's ReplicaSet shows the error.
Whole cluster blocked after EnforcePolicy too strict or matches system namespaces.Start with Audit; exclude kube-system or use exceptions.
Kyverno down → nothing can be createdWebhook failurePolicy: Fail and no healthy Kyverno.Run 3 replicas; monitor Kyverno; know the emergency procedure.
No policy reportsReports controller off, or background scan off.Enable background: true; check the reports controller Pods.
failureAction field unknownOlder Kyverno uses validationFailureAction at spec level.Use the syntax of your Kyverno version.
Teams bypass with another namespacePolicy is a namespaced Policy.Use ClusterPolicy for company-wide rules.

125.5

Go further

1Why start a new policy with failureAction: Audit?

You see who would be blocked without breaking deployments; switch to Enforce after the fixes.

2Where do you see which resources fail a policy?

In the PolicyReports: kubectl get policyreport -A.

All commands in this chapter