$ run as a normal user# run as root (or with sudo)
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 -AapiVersion: 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 & *:*"Step by step
- Install Kyverno with Helm (quick answer; for production use 3 replicas of the admission controller — see the full guide).
- Write the policy (quick answer). Parts:
match(which objects),validate(the rule),failureAction(Audit= report only,Enforce= refuse). - 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}'- 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"}]'- 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.- 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- Exceptions — for one system namespace or tool, use a
PolicyException(reviewed, documented), not a weaker policy for everybody.
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.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Deployments are not blocked, only their Pods fail | Policy 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 Enforce | Policy too strict or matches system namespaces. | Start with Audit; exclude kube-system or use exceptions. |
| Kyverno down → nothing can be created | Webhook failurePolicy: Fail and no healthy Kyverno. | Run 3 replicas; monitor Kyverno; know the emergency procedure. |
| No policy reports | Reports controller off, or background scan off. | Enable background: true; check the reports controller Pods. |
failureAction field unknown | Older Kyverno uses validationFailureAction at spec level. | Use the syntax of your Kyverno version. |
| Teams bypass with another namespace | Policy is a namespaced Policy. | Use ClusterPolicy for company-wide rules. |
Go further
- Command pages: kubectl, helm.
- Next how-tos: Apply Pod Security Standards to a namespace, Sign and verify an image with Cosign, Set resource requests and limits.
- Full guide: Kyverno. Learn it: CKS — Policy engines, CKS — Audit before enforce.
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.