Kustomize โ kustomization.yaml
If you like this project, consider supporting me on Buy Me a Coffee โ๏ธ
tags:
- kubernetes
- containers
- docker
-
sysadmin
โธ๏ธ Kustomize โ Template-Free Kubernetes Configuration
Description / ะะฟะธัะฐะฝะธะต:
Kustomize is a template-free configuration management tool for Kubernetes, built directly into kubectl. It uses a base and overlay pattern to customize Kubernetes manifests without modifying the originals. Instead of Go templates (like Helm), Kustomize applies declarative patches, strategic merges, and generators to produce environment-specific configurations. It is ideal for managing multi-environment deployments (dev/staging/prod) from a single set of base manifests.
[!NOTE] Current Status: Kustomize is a CNCF project and is integrated into
kubectl(viakubectl apply -k). It is widely used as a simpler alternative to Helm when templating is not needed. StandalonekustomizeCLI provides additional features over thekubectlbuilt-in version. Alternatives: Helm (full package management with templating), Timoni (CUE-based), cdk8s (programmatic). / ะขะตะบััะธะน ััะฐััั: Kustomize โ ะฟัะพะตะบั CNCF, ะฒัััะพะตะฝ ะฒkubectl. ะะปััะตัะฝะฐัะธะฒั: Helm, Timoni, cdk8s.
Basic Structure
# Minimal kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
commonLabels:
app: demo
Base & Overlay Pattern
Directory Structure
base/
โโโ kustomization.yaml
โโโ deployment.yaml
โโโ service.yaml
overlays/
โโโ dev/
โ โโโ kustomization.yaml
โโโ staging/
โ โโโ kustomization.yaml
โโโ prod/
โโโ kustomization.yaml
[!TIP] The base contains shared resources. Each overlay customizes the base for a specific environment without modifying the originals. / Base ัะพะดะตัะถะธั ะพะฑัะธะต ัะตััััั. ะะฐะถะดัะน overlay ะฝะฐัััะฐะธะฒะฐะตั base ะฟะพะด ะบะพะฝะบัะตัะฝะพะต ะพะบััะถะตะฝะธะต ะฑะตะท ะธะทะผะตะฝะตะฝะธั ะพัะธะณะธะฝะฐะปะพะฒ.
base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
commonLabels:
app: myapp
overlays/dev/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: dev-
replicas:
- name: myapp
count: 1
images:
- name: myapp
newTag: dev
overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: prod-
replicas:
- name: myapp
count: 3
images:
- name: myapp
newTag: v1.2.3
configMapGenerator:
- name: app-config
literals:
- MODE=production
- LOG_LEVEL=info
[!IMPORTANT] The
basesfield is deprecated in newer Kustomize versions. Useresourcesinstead to reference base directories. / ะะพะปะตbasesัััะฐัะตะปะพ. ะัะฟะพะปัะทัะนัะตresourcesะดะปั ัััะปะบะธ ะฝะฐ base-ะดะธัะตะบัะพัะธะธ.
Strategic Merge Patches
# overlays/prod/kustomization.yaml
patchesStrategicMerge:
- patch-resources.yaml
- patch-env.yaml
patch-resources.yaml (Strategic Merge)
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
template:
spec:
containers:
- name: myapp
resources:
requests:
cpu: 500m
memory: 512Mi
limits:
cpu: 1000m
memory: 1Gi
JSON Patches
# overlays/prod/kustomization.yaml
patchesJson6902:
- target:
group: apps
version: v1
kind: Deployment
name: myapp
patch: |-
- op: replace
path: /spec/replicas
value: 5
- op: add
path: /spec/template/spec/containers/0/env/-
value:
name: NEW_VAR
value: "production"
ConfigMap & Secret Generators
ConfigMap from literals / ConfigMap
configMapGenerator:
- name: app-config
literals:
- MODE=prod
- LOG_LEVEL=info
- MAX_CONNECTIONS=100
ConfigMap from files / ConfigMap
configMapGenerator:
- name: app-config
files:
- config.properties
- application.yaml
Secret from literals / Secret
secretGenerator:
- name: app-secret
literals:
- DB_PASSWORD=<PASSWORD>
- API_KEY=<SECRET_KEY>
Secret from files / Secret
secretGenerator:
- name: tls-secret
files:
- tls.crt=cert.pem
- tls.key=key.pem
type: kubernetes.io/tls
[!NOTE] Kustomize automatically appends a hash suffix to generated ConfigMaps and Secrets, ensuring rolling updates on content changes. Use
generatorOptions.disableNameSuffixHash: trueto disable this. / Kustomize ะฐะฒัะพะผะฐัะธัะตัะบะธ ะดะพะฑะฐะฒะปัะตั ั ะตั-ััััะธะบั ะบ ัะณะตะฝะตัะธัะพะฒะฐะฝะฝัะผ ConfigMap/Secret. ะัะฟะพะปัะทัะนัะตgeneratorOptions.disableNameSuffixHash: trueะดะปั ะพัะบะปััะตะฝะธั.
Image Tag Replacement
# Replace image tags
images:
- name: nginx
newName: nginx
newTag: 1.21.0
- name: myapp
newName: <REGISTRY_URL>/myapp
newTag: v1.2.3
# Digest replacement (immutable)
images:
- name: myapp
digest: sha256:1234567890abcdef...
[!TIP] Using
digestinstead ofnewTagensures immutable deployments โ the exact image is pinned regardless of tag changes. / ะัะฟะพะปัะทะพะฒะฐะฝะธะตdigestะฒะผะตััะพnewTagะณะฐัะฐะฝัะธััะตั ะฝะตะธะทะผะตะฝัะตะผัะต ัะฐะทะฒััััะฒะฐะฝะธั.
Namespace & Labels
# Set namespace for all resources
namespace: production
# Common labels
commonLabels:
app: myapp
env: prod
managed-by: kustomize
# Common annotations
commonAnnotations:
monitoring: "true"
team: platform
# Name prefix/suffix
namePrefix: prod-
nameSuffix: -v2
Common Commands
# Render kustomization
kubectl kustomize ./overlays/prod
# Apply kustomization
kubectl apply -k ./overlays/prod
# Diff before apply
kubectl diff -k ./overlays/prod
# Delete resources
kubectl delete -k ./overlays/prod
# Build to file
kubectl kustomize ./overlays/prod > rendered.yaml
[!WARNING]
kubectl delete -kwill delete all resources defined in the kustomization. Double-check the overlay path before running in production. /kubectl delete -kัะดะฐะปะธั ะฒัะต ัะตััััั. ะัะพะฒะตััะนัะต ะฟััั overlay ะฟะตัะตะด ะทะฐะฟััะบะพะผ ะฒ ะฟัะพะดะฐะบัะตะฝะต.
Troubleshooting
# Validate kustomization.yaml
kubectl kustomize ./overlays/prod --enable-alpha-plugins
# Debug resource generation
kubectl kustomize ./overlays/prod --load-restrictor=LoadRestrictionsNone
# Common errors
# - "accumulating resources: ..." โ Check base path is correct
# - "no matches for kind ..." โ Ensure resources exist in base
# - "multiple matches for ..." โ Patches target must be unique
# View all resources
kubectl kustomize ./overlays/prod | grep -E "^(kind|metadata):"
Common Issues Quick Reference
| Error / ะัะธะฑะบะฐ | Cause / ะัะธัะธะฝะฐ | Fix / ะ ะตัะตะฝะธะต |
|---|---|---|
accumulating resources |
Incorrect base path | Verify relative path to base directory / ะัะพะฒะตัะธัั ะพัะฝะพัะธัะตะปัะฝัะน ะฟััั |
no matches for kind |
Resource missing in base | Add resource to base kustomization.yaml / ะะพะฑะฐะฒะธัั ะฒ base |
multiple matches |
Ambiguous patch target | Ensure unique name + kind in patch target / ะฃะฝะธะบะฐะปัะฝัะน name + kind |
must be a file |
Wrong path format | Use relative paths, not absolute / ะัะฝะพัะธัะตะปัะฝัะต ะฟััะธ |
Documentation Links
- Kustomize Official Documentation: https://kustomize.io/
- Kustomize GitHub Repository: https://github.com/kubernetes-sigs/kustomize
- Kustomize API Reference: https://kubectl.docs.kubernetes.io/references/kustomize/
- kubectl apply -k Documentation: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/
- Kustomize Examples: https://github.com/kubernetes-sigs/kustomize/tree/master/examples