kustomization.yaml file: what it is, why it’s required, and how to configure it for common use cases.
In this example repository we have a k8s/ directory containing two Kubernetes manifests:
nginx-deployment.yamlnginx-service.yaml
kustomization.yaml (some Kustomize versions accept kustomization.yml or Kustomization). Create this file in the directory you want Kustomize to manage.
The
kustomization.yaml file tells Kustomize which resources to include and which transformations (labels, patches, image overrides, etc.) to apply. Without it, Kustomize has no entry point.- Declare the Kubernetes resources (manifests) Kustomize should manage.
- Define the customizations (transformations) to apply to those resources.
kustomization.yaml that includes the two manifests and applies a common label across all resources:
- The
resourceslist enumerates each manifest file that Kustomize will read and include in the build. - The
commonLabelsentry instructs Kustomize to addcompany: KodeKloudto every resource included in the generated output.
Note: When including examples with braces or other special characters in tables or inline examples, wrap them in backticks (e.g.,
[{ "object": "person" }]) so MDX parsers don’t interpret them.
Running Kustomize
To build the final manifest YAML from a kustomization directory, run:
- Kustomize reads
k8s/kustomization.yaml. - It imports the files listed in
resources. - It applies the transformations declared in the kustomization file.
- It prints the final YAML to stdout.
kustomize build generates the manifests but does not apply them to a Kubernetes cluster. To apply the generated manifests, pipe the output into kubectl:
kubectl perform the kustomize build and apply in one step:
kustomize build k8s, the output will include both the Service and the Deployment manifests with the added label. For example:
- Keep your
kustomization.yamlfocused: list only the resources you intend to manage in that kustomization directory. - Use
namePrefixornameSuffixto avoid naming collisions across environments. - Prefer
patchesStrategicMergeor JSON patches for environment-specific modifications rather than maintaining duplicate manifests. - For complex setups, break overlays into separate directories (base + overlays) and reference them via
resourcesorbases.
- Kustomize official docs: https://kustomize.io/
- Kubernetes documentation: https://kubernetes.io/docs/
- Kustomize looks for
kustomization.yamlin the directory you point it to. - That file declares (a) the list of resource manifests to include and (b) the customizations/transformations to apply.
- Use
kustomize build <dir>to produce the final YAML and pipe it tokubectl apply -f -or usekubectl apply -k <dir>to apply directly.