Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
When someone creates or changes an object, the API server asks the policy engine (an admission webhook). A policy can validate (allow or deny), mutate (add or change fields), generate (create other objects) or verify images (check signatures). Audit mode only reports; Enforce blocks. Background scans write PolicyReports for objects that already exist.
| Item | Kyverno | OPA Gatekeeper |
|---|---|---|
| Rules written in | YAML (with patterns, CEL and JMESPath) | Rego (ConstraintTemplate) + YAML (Constraint) |
| Install | Helm chart kyverno/kyverno | Helm chart gatekeeper/gatekeeper |
| Namespace | kyverno | gatekeeper-system |
| Objects | ClusterPolicy, Policy, PolicyException, PolicyReport | ConstraintTemplate, constraints |
| Image signatures | built in (verifyImages, Cosign/Notary) | with extra tools |
Also built into Kubernetes (GA since 1.30): ValidatingAdmissionPolicy with CEL — good for simple rules without an extra engine.
Install Kyverno with Helm
Use at least 3 replicas of the admission controller in production (the webhook must always answer):
helm repo add kyverno https://kyverno.github.io/kyverno/
helm repo update
helm upgrade --install kyverno kyverno/kyverno -n kyverno --create-namespace --set admissionController.replicas=3 --set backgroundController.replicas=2 --set cleanupController.replicas=2 --set reportsController.replicas=2 --wait
kubectl -n kyverno get pods
kubectl get validatingwebhookconfigurations | grep kyvernoOptional: the ready Pod Security policy set (baseline/restricted) in Audit mode:
helm upgrade --install kyverno-policies kyverno/kyverno-policies -n kyverno --set podSecurityStandard=restricted --set validationFailureAction=AuditThe Kyverno CLI tests policies without a cluster: download kyverno from the GitHub releases (or kubectl krew install kyverno).
First configuration: your first policies (Audit)
Three rules every cluster needs — no latest tag, resource limits, and a team label:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: basics
annotations:
policies.kyverno.io/description: "No latest tag, limits required, team label required."
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 & *:*"
- name: require-limits
match:
any:
- resources: { kinds: [Pod] }
validate:
failureAction: Audit
message: "CPU and memory limits are required."
pattern:
spec:
containers:
- resources:
limits:
memory: "?*"
cpu: "?*"
- name: require-team-label
match:
any:
- resources: { kinds: [Deployment, StatefulSet] }
exclude:
any:
- resources: { namespaces: [kube-system, kyverno] }
validate:
failureAction: Audit
message: "The label 'team' is required."
pattern:
metadata:
labels:
team: "?*"A mutate policy (add a safe default) and an image signature check (only Cosign-signed images from your registry):
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: default-no-privilege-escalation
spec:
rules:
- name: add-allowPrivilegeEscalation-false
match:
any:
- resources: { kinds: [Pod] }
mutate:
patchStrategicMerge:
spec:
containers:
- (name): "*"
securityContext:
+(allowPrivilegeEscalation): false
---
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: verify-signed-images
spec:
webhookTimeoutSeconds: 30
rules:
- name: cosign-keyless
match:
any:
- resources: { kinds: [Pod] }
verifyImages:
- imageReferences: ["harbor.example.com/shop/*"]
failureAction: Audit
attestors:
- entries:
- keyless:
subject: "https://github.com/ORG/shop/.github/workflows/release.yml@refs/heads/main"
issuer: "https://token.actions.githubusercontent.com"kubectl apply -f policies/
kubectl get clusterpolicy
NAME ADMISSION BACKGROUND READY
basics true true TrueTest a policy locally first (also in CI for your YAML):
kyverno apply policies/basics.yaml --resource k8s/deployment.yaml
kyverno test ./policy-tests/Read reports, then enforce
kubectl get policyreport -A
kubectl get clusterpolicyreport
kubectl -n shop get policyreport -o yaml | grep -B2 -A6 'result: fail'When the reports show (almost) no failures, switch the rules to failureAction: Enforce and apply again. For a real, approved exception, use a PolicyException for that workload instead of weakening the policy.
kubectl -n shop run test --image=nginx:latest
Error from server: admission webhook "validate.kyverno.svc-fail" denied the request: … no-latest-tag: Use a fixed image tag …OPA Gatekeeper in short
Gatekeeper splits a rule into a ConstraintTemplate (Rego logic) and a Constraint (where and with which parameters). Install with helm repo add gatekeeper https://open-policy-agent.github.io/gatekeeper/charts and helm install gatekeeper gatekeeper/gatekeeper -n gatekeeper-system --create-namespace. A constraint that uses a template from the Gatekeeper library:
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: deployments-need-team
spec:
enforcementAction: dryrun
match:
kinds:
- apiGroups: ["apps"]
kinds: ["Deployment"]
parameters:
labels: ["team"]enforcementAction: dryrun = audit only; deny = block. Results: kubectl get k8srequiredlabels deployments-need-team -o yaml (status.violations). Choose Gatekeeper when your company already uses OPA/Rego elsewhere; Kyverno when you want pure YAML and image verification built in.
Daily use: the commands and the real uses
kubectl get clusterpolicy,policy -A
kubectl get policyreport -A -o wide
kubectl get policyexception -A
kubectl -n kyverno logs deploy/kyverno-admission-controller --tail 50
kyverno apply policies/ --resource rendered.yaml
helm template ./chart | kyverno apply policies/ --resource -Real uses at work:
- Guardrails for all teams: Pod Security, limits, labels, allowed registries.
- Supply-chain gate: only signed images (Cosign) and images with an SBOM attestation.
- Defaults by mutation (security context, labels); generate a default NetworkPolicy for each new namespace.
Make it safe
Security checklist:
- Start in Audit, read reports, then Enforce — never surprise teams with blocks.
- Run 3 admission replicas with a PodDisruptionBudget; the webhook must stay up.
- Exclude system namespaces carefully; keep exceptions small, named and reviewed (PolicyException).
- Policies in Git (GitOps) with tests (
kyverno test) in CI. - Limit who may change policies and exceptions (RBAC).
- Keep Kyverno/Gatekeeper updated and check the webhook
failurePolicy(Fail = safe, Ignore = available).
Update and remove
helm repo update
helm upgrade kyverno kyverno/kyverno -n kyverno --reuse-values --wait
helm uninstall kyverno -n kyvernoRemove policies before you uninstall, and check that no webhook configuration is left (kubectl get validatingwebhookconfigurations) — a dead webhook can block the whole cluster.
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
All creates fail: failed calling webhook "validate.kyverno.svc…": context deadline exceeded | Kyverno Pods down or not reachable. | Check kubectl -n kyverno get pods; network policies to the webhook; more replicas. |
Policy is READY False | Error in the policy. | kubectl describe clusterpolicy NAME; test with kyverno apply. |
| A valid Pod is blocked | Pattern too strict (for example init containers, sidecars). | Fix the pattern; add a PolicyException; check with kyverno apply. |
| No PolicyReports | Background scans off or reports controller down. | background: true; check the reports controller Pod. |
verifyImages denies a signed image | Wrong subject/issuer or the image is referenced by a different name. | Compare with cosign verify output; use digests. |
| Gatekeeper constraint shows no violations | Wrong match or still syncing. | Check match.kinds; wait for audit interval; kubectl get constrainttemplates. |
Next steps
- Quick how-tos: Enforce a policy with Kyverno, Apply Pod Security Standards.
- Learn it for the CKS: Know how Gatekeeper works, Template and constraint, Kyverno and ValidatingAdmissionPolicy.
- Practise: Lab: enforce the restricted Pod Security Standard.
- Sign the images the policy checks: Install and set up Cosign. Charts: Helm.
1Why start Kyverno policies with failureAction: Audit?
To see in PolicyReports what would be blocked before blocking anything.
2What is the difference between Kyverno and Gatekeeper rules?
Kyverno rules are YAML patterns; Gatekeeper rules are written in Rego (ConstraintTemplates) with YAML constraints.