Marcio Cunha

How to audit and block Kubernetes failures with Kyverno

Learn how to create manifest validation and mutation policies directly inside your Kubernetes cluster using Kyverno for automated security.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • Kubernetes manages containerized software at scale, but its native flexibility allows insecure configurations without automated barriers.
  • Kyverno acts as an intelligent traffic guard inside the cluster, intercepting resource creation requests before they cause damage.
  • Validation policies block the entry of code that ignores essential security standards, such as using root privileges.
  • Mutation rules adjust manifests at runtime to inject missing labels or parameters without requiring manual intervention.
  • Adopting declarative policies replaces complex scripts with simple rules integrated directly into the native ecosystem.

The challenge of maintaining a secure Kubernetes cluster in practice

Managing an environment with multiple software containers can feel like piloting an airplane full of passengers without a proper instrument panel. Kubernetes revolutionized how we distribute applications, but its immense flexibility brings a dangerous side effect: developers can accidentally push insecure configurations to production. In practice, this means a single forgotten manifest can open severe security gaps, allowing applications to run with unnecessary administrative privileges or without proper resource limits.

To avoid operational disasters, engineering teams typically implement security barriers in continuous integration tools. However, checking code solely in the repository is often not enough, as users with advanced permissions can apply files directly to the cluster using the command line. It is precisely in this critical scenario that specialized governance tools enter the scene, acting as an active defense mechanism within the infrastructure's own foundation. Before moving on to automated rules, it is worth noting that layered security begins with base images, a topic we cover in depth in our guide on how to audit vulnerabilities in Docker containers with Trivy.

Understanding Kyverno and its native approach

Kyverno was designed specifically for the Kubernetes ecosystem, acting as an Admission Controller—a component that intercepts requests sent to the cluster API before any object is actually created or modified. Unlike other market solutions that require learning complex programming languages or obscure syntaxes, Kyverno uses the same YAML format that engineers already use every day. In practice, this means writing a security rule with Kyverno is nearly identical to writing a standard Deployment configuration.

This simplicity drastically reduces the entry barrier for system administrators and developers who need to collaborate on infrastructure security. Furthermore, policies are treated as first-class resources inside the cluster itself, which facilitates versioning and continuous auditing of the desired state. When we combine this runtime governance with an observable ecosystem, such as how to Scale Kubernetes Logs with Grafana Loki and Promtail, we can track exactly which policy blocked a specific change and at what moment.

Creating validation policies to block errors

Validation policies are designed to examine manifests trying to enter the cluster and decide whether they should be approved or rejected based on predefined criteria. Imagine your company requires that no container runs as the root user—the super-powered account that can compromise the entire operating system if breached. With Kyverno, we can write a rule that checks this condition and prevents resource creation if the guideline is violated. Below is a practical example of a policy blocking pods running with excessive privileges:

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: restrict-root-user
spec:
  validationFailureAction: Enforce
  rules:
    - name: check-run-as-non-root
      match:
        any:
          - resources:
              kinds:
                - Pod
      validate:
        message: "Running containers as root is prohibited. Set runAsNonRoot to true."
        pattern:
          spec:
            securityContext:
              runAsNonRoot: true

In practice, when a developer tries to submit a manifest without this configuration, the cluster immediately rejects the command and displays the friendly message defined in the policy. This instant feedback educates the team and prevents flaws from silently reaching staging or production environments.

Mutating manifests automatically to ensure compliance

While validation simply says 'no' to what is wrong, mutation has the superpower to seamlessly correct or enrich manifests before they are saved in the cluster database. Think of this as an automatic spell checker that fixes typos in text while you type, without requiring you to stop what you are doing. In modern software engineering, this is extremely useful for injecting mandatory traceability labels, automatically configuring network policies, or adding standard fault tolerances.

Below, we present a mutation policy that ensures every created namespace automatically receives a label indicating the responsible department, making financial organization and subsequent access control much easier:

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: add-default-department-label
spec:
  rules:
    - name: mutate-namespace
      match:
        any:
          - resources:
              kinds:
                - Namespace
      mutate:
        patchStrategicMerge:
          metadata:
            labels:
              department: engineering

With this automated approach, we eliminate repetitive manual work and ensure that no resource is left orphaned or outside organizational standards established by IT governance.

Best practices for implementing governance without slowing down business

Deploying strict security rules overnight can turn a governance tool into the company's biggest bottleneck, breeding frustration among development teams. The strategy most recommended by experts is to start operating policies in audit mode, known in Kyverno as 'Audit', rather than immediate enforcement. In this initial phase, the system merely logs violations and reports them, allowing engineering to map the real impact of new rules without interrupting critical software deliveries.

Once all teams adjust their repositories and manifests to the new standards, the administrator can change the policy behavior to actively block infractions. Another fundamental practice is keeping rules modular, creating small and specific policies for each business domain rather than trying to solve all security in a single giant file. Monitoring cluster performance and periodically reviewing exceptions ensures that security walks hand in hand with operational agility demanded by the market. To explore these architectural concepts further in production, see our related technical analysis on Slow database? How to speed up queries with partial indexes.

Final thoughts on automation and security

Ensuring the integrity of a container-based infrastructure is no longer an operational luxury; it has become a basic requirement for digital survival in any organization. Tools like Kyverno transform abstract compliance concepts into executable code, allowing security to be treated declaratively and integrated into the natural workflow. By combining rigorous validations with intelligent mutations, teams gain speed without sacrificing control and predictability in their productive environments.

The secret to success lies in continuous evolution: start by applying simple rules in test environments, educate the team with clear feedback, and gradually move toward broader enforcement in production. With a solid, automated foundation, your infrastructure will be ready to scale with stability, resilience, and complete peace of mind against modern threats.