Kustomize is a Kubernetes configuration management tool that lets you customize YAML files for different environments without duplicating them. It solves the problem of maintaining multiple slightly different copies of the same config by allowing you to define a base configuration and overlay environment-specific changes like replica counts, labels, or images, making it easier to scale and manage infrastructure consistently
deployment.yaml for three environments/namespaces, and each environment needs different number of replicas. It’s easy at first, but in the long run when more components are needed, misconfiguration and missing something is very likely, this is where Kustomize comes inFile structure
~/someApp
├── base
│ ├── deployment.yaml
│ ├── kustomization.yaml
│ └── service.yaml
└── overlays
├── development
│ ├── cpu_count.yaml
│ ├── kustomization.yaml
│ └── replica_count.yaml
└── production
├── cpu_count.yaml
├── kustomization.yaml
└── replica_count.yaml
Kustomize comes built-in with kubectl so no other packages need to be installed. May still want to install the Kustomize CLI to get the latest version. It also does not require learning how to use any complex and hard to read templating systems (like helm)
Kustomize is built into kubectl and uses a declarative approach without templating, while Helm uses Go templating for more dynamic, chart-based deployments
kustomization.yaml file defines the base resources and overlays for customizing Kubernetes configurations in a Kustomize setup
Say in a directory you have nginx-depl.yml, nginx-svc.yml, and kustomization.yaml. The kustomization.yaml file would look like this:
# Resources to be managed by Kustomize
resources:
- nginx-depl.yml
- nginx-svc.yml
# A builtin that applies specific label to all your resources, called transformer
commonLabels:
company: KodeKloud
Kustomize looks for a kustomization file which contains a list of all Kubernetes manifests to manage and all of the customizations that should be applied
Check for more built-ins: https://kubectl.docs.kubernetes.io/references/kustomize/builtins/ (also known as transformers)
You can manage directories in two ways
kustomization.yaml file in each of the subdirectories and import only the resources within that directoryExamples: namePrefix and nameSuffix transformers, which modify resource names, images transformer which can replace image or change tag for a container, and namespace transformers, which automatically apply a namespace to all resources

If you want to apply the transformations for all manifests, add them into the root kustomization file, otherwise use subdirectories if you want to add transformations to specified manifests
https://github.com/kubernetes-sigs/kustomize/tree/master/examples/transformerconfigs
To create a patch, 3 parameters must be provided:
If you want to apply a configuration across the board (like adding a label, namespace, etc..), use a common transformer. However, if you wanna apply or change something on one specific object or just a couple of objects, you would use Patch (e.g., updating number of replicas)
Patch example

metadata.name), can also target an index of a list/array (e.g., /spec/template/spec/containers/0). Check this video from Kodekloud’s CKA course on Udemy for moreThere are two ways patches can be applied: Strategic merge patch merges changes based on Kubernetes resource structure, while JSON 6902 patch uses a precise, list-based syntax to apply exact changes at specific paths. It’s a matter of preference

You can write patches inline like in previous examples or in a separate file
# kustomization.yaml
patches:
- path: replica-patch.yaml
target:
kind: Deployment
name: nginx-deployment
---
# replica-patch.yaml
- op: replace
path: /spec/replicas
value: 5
An overlay in Kustomize is a layer that customizes a base configuration for a specific environment by applying changes like patches, transformers, or variable overrides
Structure example
k8s/
├── base/
│ ├── kustomization.yaml
│ ├── nginx-depl.yaml
│ ├── service.yaml
│ ├── redis-depl.yaml
├── overlays/
│ ├── dev/
│ │ ├── kustomization.yaml
│ │ └── config-map.yaml
│ ├── stg/
│ │ ├── kustomization.yaml
│ │ └── config-map.yaml
│ └── prod/
│ ├── kustomization.yaml
│ └── config-map.yaml
Example

resources in the environment’s kustomization file