Kyverno PSP Guide
Kyverno is more flexible and can Enforce security and compliance standards beyond what PSA provides. It installs as an admission webhook in your cluster. It checks every pod/deployment before creation. If your pod breaks a rule → Kyverno warns or blocks it. ✅ Install Kyverno ✅ Apply essential security policies (best practices)✅ Step 1: Install Kyverno
✅ Step 2: Apply Essential Security Policies
Here are must-have policies for production clusters:1. Disallow Privileged Containers
Prevent pods from running withprivileged: true:
2. Require runAsNonRoot
Enforce running containers as non-root:
3. Disallow Privilege Escalation
4. Restrict HostPath Volumes
Block access to host file system:5. Enforce Image Registry Policy
Allow only trusted image registries:✅ How to Apply All Policies
Save these YAMLs in a folder (e.g.,kyverno-policies/) and apply:
Keep Note: validationFailureAction: Enforce/Audit Enforce blocks the creation of resources that violate the policy, while Audit allows them but logs violations.
- Mutating Admission Controller → Can add/change things (like injecting a sidecar or setting defaults).
- Validating Admission Controller → Can allow or deny the object based on rules.
✅ Step 3: Verify
Try to create a privileged pod:✅ Essential Kyverno Policies for Production
1. Block latest Image Tag
Reason: Using latest can cause unpredictable deployments.
2. Enforce Read-Only Root Filesystem
Prevents tampering inside containers.3. Limit Container Capabilities
Drop all Linux capabilities except required ones.4. Require CPU & Memory Limits
Guarantee resource allocation to prevent noisy neighbor issues.5. Block Containers Running as Root (UID 0)
Even ifrunAsNonRoot is missed, Enforce UID check.
DOCS ------------------------------------------------------------------------------------
Kyverno Admission Control: Mutate, Validate, Enforce, and Audit
Overview
Kyverno is a Kubernetes-native policy engine that manages and secures Kubernetes resources using admission control. It acts as a validating and mutating admission webhook, enabling:- Validation – Ensure configurations follow policies (block or warn)
- Mutation – Add or modify resource configuration dynamically
- Generation – Create new resources when needed
- Verification – Check image signatures for security
What is Admission Control in Kubernetes?
When a resource is created or modified, the request flows through:- Authentication & Authorization
- Admission Controllers (Mutating → Validating)
- Persistence in etcd
- Mutating Webhook – Modify resource before creation (e.g., add labels)
- Validating Webhook – Accept or reject resource (enforce policies)
Kyverno Policy Types
✅ 1. Mutate Policies
- Automatically add, modify, or remove fields in resource manifests.
-
Example use cases:
- Add labels/annotations
- Inject sidecar containers
- Enforce default security settings
✅ 2. Validate Policies
- Ensure that resources meet security and compliance requirements.
-
Two modes:
- enforce → Block non-compliant resources
- audit → Report violations, but do not block
Enforce vs Audit Mode
| Mode | Behavior |
|---|---|
| enforce | Denies non-compliant resources (fails admission) |
| audit | Logs violation in PolicyReport, resource is allowed |
Kyverno Operations
| Operation | Purpose |
|---|---|
| Mutate | Add or modify resource configuration |
| Validate | Ensure resources follow rules (block or allow) |
| Generate | Create resources automatically |
| VerifyImages | Enforce signed images |
Where to See Policy Violations
- PolicyReports CRD:
- Kyverno Logs:
Examples for Common Use Cases
Mutate Example: Add Label
Addkyverno=true label if missing:
Validate Example: Disallow Privileged Pods
Key Benefits
- Kubernetes-native policies (YAML)
- Admission control without coding
- Easy enforce/audit switch
- Supports mutate + validate together
✅ Next Steps
- Install Kyverno:
-
Apply policies for:
- Security (No privileged containers, Run as non-root)
- Governance (Label enforcement, Registry restrictions)
- Best Practices (Resource limits, Probes)
