Skip to main content

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 KyvernoApply essential security policies (best practices)

Step 1: Install Kyverno

Check:
You should see Kyverno pods running.

Step 2: Apply Essential Security Policies

Here are must-have policies for production clusters:

1. Disallow Privileged Containers

Prevent pods from running with privileged: true:
Apply:

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:
It should be blocked.

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 if runAsNonRoot 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
Kyverno policies are written in YAML, making it easier for Kubernetes users compared to writing complex Rego scripts (OPA).

What is Admission Control in Kubernetes?

When a resource is created or modified, the request flows through:
  1. Authentication & Authorization
  2. Admission Controllers (Mutating → Validating)
  3. Persistence in etcd
Kyverno hooks into this flow as an admission controller:
  • 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
Example:

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
Example (Enforce Mode):
Example (Audit Mode):

Enforce vs Audit Mode


Kyverno Operations


Where to See Policy Violations

  • PolicyReports CRD:
  • Kyverno Logs:
Example violation in logs:

Examples for Common Use Cases

Mutate Example: Add Label

Add kyverno=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)