11.04 The kustomization.yaml File
Abstract
The kustomization.yaml file is the main configuration file used by Kustomize.
It tells Kustomize:
- Which Kubernetes YAML files to manage
- Which customizations or transformations to apply
- How to generate the final Kubernetes manifests
What Is kustomization.yaml?
Kustomize does not directly look for every YAML file in a folder.
Instead, it looks for a file named:
This file acts as the entry point for Kustomize.
Note
The file must be named kustomization.yaml, kustomization.yml, or Kustomization.
The most common and recommended name is kustomization.yaml.
Example Folder Structure
Example Kubernetes manifest folder:
| File | Purpose |
|---|---|
nginx-depl.yml |
Deployment manifest |
nginx-service.yml |
Service manifest |
kustomization.yaml |
Kustomize configuration file |
Basic kustomization.yaml Example
This file has two important sections:
| Section | Purpose |
|---|---|
resources |
Lists Kubernetes manifests managed by Kustomize |
commonLabels |
Adds labels to all listed resources |
Tip
Think of resources as what Kustomize should manage, and transformations as what Kustomize should change.
Resources Section
The resources section lists all Kubernetes manifests that Kustomize should include.
Kustomize reads these files and combines them into one final rendered output.
Warning
If a manifest is not listed under resources, Kustomize will not include it in the final output.
Customization Section
Customizations define changes that should be applied to the resources.
Example:
This adds the label to every resource generated by Kustomize.
Example rendered output:
Example
If both a Deployment and Service are listed under resources, the company: KodeKloud label will be added to both.
Kustomize Build
To generate the final manifest output:
This command:
- Finds the
kustomization.yamlfile - Loads all files listed under
resources - Applies the defined transformations
- Prints the final YAML output to the terminal
Note
kustomize build only renders YAML.
It does not deploy resources to the Kubernetes cluster.
Build Output Example
If commonLabels is configured, the final output includes the label in each resource.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
company: KodeKloud
spec:
ports:
- port: 80
targetPort: 360
selector:
component: nginx
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
company: KodeKloud
spec:
replicas: 1
selector:
matchLabels:
component: nginx
template:
metadata:
labels:
component: nginx
company: KodeKloud
spec:
containers:
- name: nginx
image: nginx
Applying Kustomize Output
kustomize build only displays the final manifest.
To apply it to a cluster, pipe the output to kubectl apply:
Or use the built-in Kustomize support in kubectl:
Success
In real projects, kubectl apply -k is the most common way to apply a Kustomize directory.
kustomize build vs kubectl apply -k
| Command | What It Does |
|---|---|
kustomize build k8s/ |
Renders final YAML only |
kustomize build k8s/ \| kubectl apply -f - |
Renders and applies YAML |
kubectl apply -k k8s/ |
Builds and applies directly |
Tip
Use kustomize build first when you want to review the final manifest before applying it.
Common Fields in kustomization.yaml
| Field | Purpose |
|---|---|
resources |
Includes Kubernetes YAML files |
commonLabels |
Adds labels to all resources |
commonAnnotations |
Adds annotations to all resources |
namePrefix |
Adds a prefix to resource names |
nameSuffix |
Adds a suffix to resource names |
namespace |
Sets namespace for resources |
images |
Updates container images |
replicas |
Updates replica count |
configMapGenerator |
Generates ConfigMaps |
secretGenerator |
Generates Secrets |
patches |
Applies targeted changes |
Example with Multiple Customizations
resources:
- nginx-depl.yml
- nginx-service.yml
namespace: dev
namePrefix: dev-
commonLabels:
company: KodeKloud
environment: dev
commonAnnotations:
managed-by: kustomize
Result:
- Resources are deployed to the
devnamespace - Resource names get the
dev-prefix - Labels are added to all resources
- Annotations are added to all resources
Production Use Case
In production, kustomization.yaml is commonly used to define environment-specific overlays.
Example:
k8s/
├── base/
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
│
└── overlays/
├── dev/
│ └── kustomization.yaml
├── stg/
│ └── kustomization.yaml
└── prod/
└── kustomization.yaml
Production overlay example:
Production Recommendation
Keep shared manifests in base/ and only production-specific changes in overlays/prod/.
Best Practices
Recommended
- Keep
kustomization.yamlsmall and readable - Always list resources explicitly
- Use
base/andoverlays/for multi-environment projects - Run
kustomize buildbefore applying changes - Store Kustomize manifests in Git
- Use labels for ownership, environment, and application tracking
- Validate rendered YAML in CI/CD pipelines
Do's
- Use
resourcesto clearly list managed manifests - Use
commonLabelsfor shared labels - Use overlays for environment-specific changes
- Preview output before applying
- Keep base manifests reusable
- Use Git review for production changes
Don'ts
- Don't assume Kustomize automatically reads all YAML files
- Don't put environment-specific changes directly in the base
- Don't apply manifests without checking the final build output
- Don't duplicate full manifests across environments
- Don't store plain-text secrets in
kustomization.yaml
Troubleshooting
Resource not included
Check whether the file is listed under resources.
Build works but apply fails
Validate the rendered YAML:
Then check API versions, namespaces, and required CRDs.
Wrong labels applied
Check global transformations such as:
These apply to all managed resources.
Useful Commands
| Task | Command |
|---|---|
| Build final manifests | kustomize build k8s/ |
| Apply using pipe | kustomize build k8s/ \| kubectl apply -f - |
| Apply using kubectl | kubectl apply -k k8s/ |
| Preview using kubectl | kubectl kustomize k8s/ |
| Delete using kustomize | kubectl delete -k k8s/ |
Kustomize ApiVersion & Kind
Abstract
A kustomization.yaml file can include apiVersion and kind, just like other Kubernetes-style configuration files.
These fields are technically optional, but adding them makes the file clearer, safer, and more future-proof.
Recommended Fields
Use the following values in kustomization.yaml:
Full example:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- nginx-depl.yaml
- nginx-service.yaml
commonLabels:
company: KodeKloud
Note
Kustomize can still work without these fields because it can assume default values.
However, explicitly defining them is better for readability and long-term maintenance.
Why Add ApiVersion and Kind?
| Field | Purpose |
|---|---|
apiVersion |
Defines the Kustomize config API version |
kind |
Identifies this file as a Kustomization config |
Tip
Treat kustomization.yaml like a normal Kubernetes manifest: define the API version, kind, metadata when needed, and the desired configuration clearly.
Production Recommendation
In production repositories, always include:
This helps avoid confusion when:
- multiple teams maintain manifests
- Kustomize versions change
- CI/CD tools validate YAML
- GitOps tools render manifests
- new engineers review the repository
Recommended
Hard-code apiVersion and kind in every kustomization.yaml file, especially for shared, production, or GitOps-managed environments.
What Happens If You Omit Them?
Kustomize may still process the file:
But this is less explicit.
Warning
Relying on defaults can become risky if tool behavior changes in the future.
Explicit values make your configuration easier to understand and safer to maintain.
Do's
- Add
apiVersionandkindto everykustomization.yaml - Use the supported Kustomize API version
- Keep the file simple and readable
- Use consistent structure across
baseandoverlays - Validate output with
kustomize build
Don'ts
- Don't rely on implicit defaults in production
- Don't mix Kustomize config fields with normal Kubernetes resource fields incorrectly
- Don't rename
kustomization.yamlrandomly - Don't use unsupported or outdated API versions without checking compatibility
Quick Example: Base Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- nginx-depl.yaml
- nginx-service.yaml
commonLabels:
app: nginx
company: KodeKloud
Quick Example: Overlay Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
patches:
- path: replica-patch.yaml
commonLabels:
environment: prod
Example
Base kustomizations usually contain shared resources.
Overlay kustomizations usually reference the base and apply environment-specific changes.
Summary
Quote
kustomization.yamlcan includeapiVersionandkind- These fields are optional but strongly recommended
- Use
apiVersion: kustomize.config.k8s.io/v1beta1 - Use
kind: Kustomization - Explicit fields make production manifests safer and easier to maintain
Kustomize Output
Abstract
kustomize build generates the final Kubernetes YAML output, but it does not create anything in the cluster by itself.
To deploy the generated output, you must pass it to kubectl apply.
Build Does Not Deploy
When you run:
Kustomize:
- Reads
kustomization.yaml - Loads all listed resources
- Applies transformations
- Prints the final manifest to the terminal
It does not create Deployments, Services, Pods, ConfigMaps, or any other resources.
Warning
kustomize build only renders YAML.
If you run kubectl get pods, kubectl get deployments, or kubectl get services after only running kustomize build, nothing new will be created.
Apply Kustomize Configs
To apply the generated manifests, redirect the output of kustomize build into kubectl apply.
What this means:
| Part | Meaning |
|---|---|
kustomize build k8s/ |
Generates final YAML |
| |
Pipes output from the first command to the second command |
kubectl apply -f - |
Applies YAML from standard input |
Tip
The pipe | is a Linux shell feature.
It sends the output of the command on the left into the command on the right.
Native Kubectl Apply
Kubectl has built-in Kustomize support.
Instead of using a pipe, you can run:
Here, -k means kubectl should treat the directory as a Kustomize directory.
Success
kubectl apply -k k8s/ is shorter and commonly used in real projects.
Apply Command Comparison
Example Apply Output
Example output:
This means Kubernetes created the resources generated by Kustomize.
Note
The final objects are still normal Kubernetes objects.
Kustomize only helps generate the manifests before they are applied.
Delete Kustomize Resources
Deleting works almost the same way as applying.
Use:
This deletes the resources generated by the Kustomize build output.
Example output:
Native Kubectl Delete
You can also delete using kubectl directly:
This tells kubectl to build the Kustomize directory and delete the generated resources.
Danger
Be careful with kubectl delete -k.
It deletes all resources generated from that Kustomize directory.
Delete Command Comparison
Production Workflow
A safe production workflow is:
Review the output.
Then apply:
Verify:
Production Recommendation
Always preview production manifests before applying them, especially when overlays modify replicas, images, namespaces, labels, or security settings.
CI/CD Usage
In CI/CD pipelines, Kustomize is commonly used like this:
This gives you a saved copy of the rendered manifest.
Tip
Saving rendered YAML is useful for auditing, approvals, troubleshooting, and rollback investigation.
Best Practices for Kustomize Output
Recommended
- Run
kustomize buildbefore applying changes - Use
kubectl apply -kfor normal deployment - Use
kubectl delete -kcarefully - Keep separate overlays for
dev,stg, andprod - Validate rendered manifests in CI/CD
- Store Kustomize configs in Git
- Review production output before applying
Do's
- Use
kustomize buildto preview generated YAML - Use
kubectl apply -kfor clean deployment - Use
kubectl delete -konly when you are sure - Review resource names before deleting
- Validate manifests before production rollout
Don'ts
- Don't assume
kustomize builddeploys resources - Don't delete using
-kwithout reviewing the target directory - Don't apply production overlays without checking rendered output
- Don't mix unrelated apps in the same Kustomize directory
- Don't store secrets directly in plain YAML
Common Mistakes
Mistake: Running only build
This only prints YAML. It does not deploy anything.
Mistake: Wrong directory
Make sure the directory contains a valid kustomization.yaml.
Mistake: Deleting the wrong overlay
This deletes production resources generated by that overlay.
Quick Reference
| Task | Command |
|---|---|
| Preview final YAML | kustomize build k8s/ |
| Apply using pipe | kustomize build k8s/ \| kubectl apply -f - |
| Apply directly | kubectl apply -k k8s/ |
| Delete using pipe | kustomize build k8s/ \| kubectl delete -f - |
| Delete directly | kubectl delete -k k8s/ |
Summary
Quote
kustomize buildgenerates final manifests only- It does not deploy anything by itself
- Use
kubectl apply -kto deploy Kustomize configs - Use
kubectl delete -kto delete generated resources - In production, always preview and validate rendered YAML before applying
Summary
Quote
kustomization.yamlis the main Kustomize configuration file- It lists the resources Kustomize should manage
- It defines transformations that should be applied
kustomize buildrenders final YAML but does not deploy it- Use
kubectl apply -kto apply Kustomize manifests directly to a cluster