How to Use Kustomize in Kubernetes: Simple Guide
Use
kustomize to customize Kubernetes YAML files by creating a kustomization.yaml file that lists resources and patches. Run kustomize build to generate the final YAML with your customizations applied, then apply it with kubectl apply -k.Syntax
The main file for Kustomize is kustomization.yaml. It lists resources, patches, and configurations to customize Kubernetes manifests.
Key parts include:
resources: List of base YAML files or directories.patchesStrategicMerge: YAML snippets to modify existing resources.configMapGenerator: To create ConfigMaps from literals or files.
Use kustomize build [path] to generate the customized YAML output.
yaml
resources: - deployment.yaml - service.yaml patchesStrategicMerge: - patch.yaml configMapGenerator: - name: example-config literals: - key1=value1 - key2=value2
Example
This example shows how to customize a Deployment by changing the image and adding environment variables using Kustomize.
yaml
# deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 2 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp-container image: nginx:1.14.2 ports: - containerPort: 80 --- # kustomization.yaml resources: - deployment.yaml patchesStrategicMerge: - patch.yaml --- # patch.yaml apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: template: spec: containers: - name: myapp-container image: nginx:1.16.0 env: - name: ENV_VAR value: "production"
Output
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: nginx:1.16.0
ports:
- containerPort: 80
env:
- name: ENV_VAR
value: "production"
Common Pitfalls
Common mistakes when using Kustomize include:
- Not matching
metadata.nameexactly in patches, so changes don't apply. - Using
kubectl apply -finstead ofkubectl apply -kto apply Kustomize directories. - Forgetting to include all resources in
kustomization.yaml. - Incorrect indentation or YAML syntax causing build errors.
yaml
Wrong patch name:
apiVersion: apps/v1
kind: Deployment
metadata:
name: wrongname # This won't patch the original deployment
Right patch name:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp # Must match the original resource nameQuick Reference
Remember these commands and files when using Kustomize:
kubernetes
kustomization.yaml - main config file listing resources and patches kustomize build [dir] - generates customized YAML kubectl apply -k [dir] - applies customized resources to cluster
| Command/File | Purpose |
|---|---|
| kustomization.yaml | Defines resources and customizations |
| kustomize build [dir] | Generates final YAML with customizations |
| kubectl apply -k [dir] | Applies customized YAML to Kubernetes cluster |
Key Takeaways
Create a kustomization.yaml file listing your base resources and patches.
Use kustomize build to generate customized YAML before applying.
Always apply with kubectl apply -k to use Kustomize directories.
Ensure patch metadata names exactly match the resources to modify.
Check YAML syntax carefully to avoid build errors.