Skip to content
Learni
View all tutorials
Kubernetes

How to Master Kustomize for Kubernetes in 2026

Lire en français

Introduction

In the Kubernetes ecosystem in 2026, configuration management remains a major challenge for DevOps teams. Kustomize, natively integrated into kubectl since version 1.14, stands out as the premier declarative solution for customizing YAML manifests without Helm's complex templates or fragile Bash scripts.

Why Kustomize? Imagine a base manifest for a PostgreSQL Deployment that you need to adapt for dev (limited resources), staging (3x replicas), and prod (rotating secrets). Instead of copy-pasting and maintaining 15 identical files, Kustomize applies overlays: a shared base + targeted transformations. This cuts errors by 70% per CNCF benchmarks, enhances GitOps traceability, and scales horizontally for microservices.

This intermediate, code-free tutorial focuses on theory: structures, transformations, and advanced patterns. By the end, you'll design production-ready Kustomize pipelines, as if a mentor with 15 years of experience were guiding you step-by-step from foundations to expertise.

Prerequisites

  • Solid Kubernetes knowledge: Pods, Deployments, Services, ConfigMaps.
  • Familiarity with YAML and Git for GitOps.
  • Experience with kubectl apply and Helm (to compare limitations).
  • Environment: Accessible K8s cluster (Minikube or EKS for testing).

Kustomize Foundations: The Declarative Model

Kustomize is built on a base + overlay paradigm, like Photoshop layers applied to a source image. At its core: the kustomization.yaml file, a declarative manifest listing resources (YAML files to include), patches (JSONPatch/StrategicMergePatch modifications), and transformers (generators like namespace or labels).

Real-world example: For a web app, base/ contains deployment.yaml and service.yaml. The dev/ overlay adds a patch to reduce CPU to 100m and injects a ConfigMap with DEBUG=true. Result: kustomize build dev/ generates a single YAML ready for kubectl apply.

Theoretical advantage: No runtime evaluation (unlike Helm), everything is static and auditable. Analogy: A chef assembling base ingredients + custom seasonings for varied dishes without duplicating recipes.

Kustomize Project Structure: Hierarchy and Modularity

A Kustomize project organizes into a Git-friendly tree:

  • base/: Shared resources (Deployment, HPA, etc.).
  • overlays//: Environment-specific kustomization.yaml (dev/staging/prod).
  • patches/: Reusable files (.yaml or .json).
  • configs/: Shared ConfigMaps/Secrets.
Case study: At a fintech, the base for an API Gateway includes an Istio VirtualService. The eu-west1/ overlay patches the namespace and adds a region=eu label. The us-east1/ overlay merges a patch for replicas=5. Build: kustomize build overlays/prod | kubectl apply -k -.

Progression: Start with a single-resource base, scale to multi-bases via resources: [- ../base1, - ../base2]. This promotes composition, avoiding monolithic Helm charts.

ComponentRoleExample Usage
--------------------------------
resourcesInclude YAML- deployment.yaml
patchesModifyChange image tag
generatorsCreateConfigMap from secrets

Advanced Transformations: Patches and Generators

Patches are the magic core: StrategicMergePatch for recursive merges (perfect for Deployments), JSONPatch for precise ops (e.g., op: replace /spec/replicas 3).

Real-world case: Patch an Ingress to add TLS: { target: { kind: Ingress }, patch: | tls: [{ secretName: prod-tls }] }. Generators like configMapGenerator automatically hash values for immutability (e.g., env: [{name: DB_PASS, value: secret123}] → hashed name).

Images and labels: images: [{name: app, newTag: v1.2.3}] updated via CI/CD. commonLabels: {team: backend} propagates everywhere.

Analogy: Patches like sticky notes on a blueprint; generators like automatic stamps. For complexity: Chain overlays (bases: [- ../../base]) for inheritance, like a YAML ontology.

Multi-Environment Management and GitOps

In 2026, Kustomize shines in GitOps with ArgoCD/Flux: Each branch/env has its overlay. CI pipeline: kustomize build overlays/$ENV > k8s-manifest.yaml, push to GitOps repo.

Advanced pattern: Overlay matrix (overlays/prod/kustomization.yaml references ../common + region/eu). For dynamic namespaces: namespaceTransformer: { target: { kind: * }, namespace: dynamic-ns }.

E-commerce case study: Microservices base + tenant overlays (B2B/B2C). Result: 50+ envs managed by 3 devs, zero config drift.

Progression checklist:

  • Level 1: Simple base.
  • Level 2: Image/label patches.
  • Level 3: Overlay inheritance + secret generators.

Essential Best Practices

  • Strict modularity: One responsibility per base (one service), max 5 overlays per env for readability.
  • Naming conventions: patch-deployment-replicas.yaml; use targetSelector for precision (e.g., matchLabel: app=myapp).
  • Secrets management: Always use secretGenerator with literalSources + sops/SealedSecrets; avoid plaintext.
  • CI validation: Integrate kustomize build --enable-helm --validate + kubeval for dry-run.
  • Versioning: Pure Kustomize uses vars like vars: [{name: APP_TAG, objref: {kind: ConfigMap}}], but prefer CI injection.

Common Pitfalls to Avoid

  • Patch conflicts: StrategicMerge fails on non-associative lists; use patchStrategicMerge with $patch: merge annotations or switch to JSONPatch.
  • Over-inheritance: Too many chained bases slow builds (limit to 3 levels); test with kustomize build --enable-exec.
  • Forgotten namespaces: Without namespace: myns, resources pollute default; enforce via transformer.
  • Hash collisions: Generators change names on every build if values vary; fix with behavior: merge on existing ConfigMaps.

Next Steps

Dive deeper with the official Kustomize docs and CNCF patterns. Integrate with ArgoCD for advanced GitOps.

Check out our Learni Kubernetes and GitOps training: hands-on Kustomize workshops + Helm migration.

Resources:

  • Book: "Kubernetes Patterns" (Config Management chapter).
  • Example repo: kubernetes-sigs/kustomize.
  • Tools: kustomize-controller for Flux v2.