Kustomize Basics
Kustomize starts from the opposite end to Helm: your manifests stay plain, valid Kubernetes YAML, and a kustomization.yaml file next to them lists the files and the transformations to apply: set a namespace, add a name prefix, add labels, change an image tag. kustomize build <dir> prints the result. There is no template language and no install step; the output goes to kubectl apply, a GitOps controller, or a file.
Kustomize is a standalone binary and is also built into kubectl: kubectl kustomize <dir> builds, kubectl apply -k <dir> builds and applies. kubectl embeds a specific Kustomize version, which can lag behind the standalone release:
kustomize version
kubectl version --client | grep -i kustomizev5.8.3
Kustomize Version: v5.8.1
A first kustomization
The session takes linkly's plain manifests from The Running Example (shortened) and lets Kustomize place them in a namespace, prefix the names, label everything, and pin the image:
mkdir -p linkly && cd linkly
cat > deployment.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: linkly
spec:
replicas: 2
selector:
matchLabels: {app.kubernetes.io/name: linkly}
template:
metadata:
labels: {app.kubernetes.io/name: linkly}
spec:
containers:
- name: linkly
image: ghcr.io/acme/linkly
envFrom: [{configMapRef: {name: linkly-config}}]
EOF
cat > service.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: linkly
spec:
selector: {app.kubernetes.io/name: linkly}
ports: [{name: http, port: 80, targetPort: 8080}]
EOF
cat > config.yaml <<'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
name: linkly-config
data: {LOG_LEVEL: info}
EOF
cat > kustomization.yaml <<'EOF'
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: links
namePrefix: team-
labels:
- pairs: {app.kubernetes.io/part-of: linkly}
includeSelectors: false
- pairs: {acme.example.com/team: links}
includeSelectors: true
images:
- name: ghcr.io/acme/linkly
newTag: "1.4.0"
resources:
- deployment.yaml
- service.yaml
- config.yaml
EOF
kustomize build .apiVersion: v1
data:
LOG_LEVEL: info
kind: ConfigMap
metadata:
labels:
acme.example.com/team: links
app.kubernetes.io/part-of: linkly
name: team-linkly-config
namespace: links
---
apiVersion: v1
kind: Service
metadata:
labels:
acme.example.com/team: links
app.kubernetes.io/part-of: linkly
name: team-linkly
namespace: links
spec:
ports:
- name: http
port: 80
targetPort: 8080
selector:
acme.example.com/team: links
app.kubernetes.io/name: linkly
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
acme.example.com/team: links
app.kubernetes.io/part-of: linkly
name: team-linkly
namespace: links
spec:
replicas: 2
selector:
matchLabels:
acme.example.com/team: links
app.kubernetes.io/name: linkly
template:
metadata:
labels:
acme.example.com/team: links
app.kubernetes.io/name: linkly
spec:
containers:
- envFrom:
- configMapRef:
name: team-linkly-config
image: ghcr.io/acme/linkly:1.4.0
name: linkly
Look at what Kustomize did beyond the obvious:
- Order. Output is sorted by kind (ConfigMap, Service, Deployment), so objects that others depend on come first, whatever the order in
resources. - References follow renames.
namePrefixrenamed the ConfigMap toteam-linkly-config, and the Deployment'sconfigMapRefwas updated to match. Kustomize knows the reference fields of built-in kinds (ConfigMap and Secret refs, Service names in Ingress backends, ServiceAccount names, and more). For custom resources it needs aconfigurationsentry to learn them. - Labels and selectors.
includeSelectors: trueadds the label to selectors and Pod templates too;falseonly to metadata, andincludeTemplates: true(withincludeSelectors: false) to metadata and Pod templates but not selectors. Adding to selectors is convenient on day one and dangerous later: a Deployment's selector is immutable, so changing such a label on a live app fails. - Images.
imagesmatched by image name and set the tag;newNamechanges the repository anddigestpins by digest.
Note: Older examples use
commonLabels(always added to selectors),patchesStrategicMergeandpatchesJson6902. All three are deprecated in Kustomize 5 and print a warning;kustomize edit fixrewrites them aslabelsandpatches. Careful:kustomize edit add label k:vwithout--without-selectorstill writescommonLabels.
Common fields
| Field | Effect |
|---|---|
resources |
files, directories with a kustomization, or remote bases (https://github.com/org/repo//path?ref=v1.2.0) |
namespace |
sets metadata.namespace on every namespaced object (and in some references, like RoleBinding subjects) |
namePrefix, nameSuffix |
rename every object, updating references |
labels, commonAnnotations |
add labels / annotations everywhere |
images |
change image names, tags or digests |
replicas |
set replica counts by workload name |
patches |
strategic-merge or JSON patches (next lessons) |
configMapGenerator, secretGenerator |
create ConfigMaps/Secrets with hashed names (Generators) |
components |
reusable optional features (Components) |
replacements |
copy a field's value into other fields (Replacements) |
helmCharts |
render a Helm chart as a resource (Helm Rendered Through Kustomize) |
Editing from scripts: kustomize edit
CI pipelines shouldn't sed YAML. kustomize edit changes kustomization.yaml safely, which is how a pipeline bumps the image after a build:
mkdir -p app && cd app
printf 'apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: a\n' > cm.yaml
printf 'apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: linkly\nspec:\n selector: {matchLabels: {app: l}}\n template:\n metadata: {labels: {app: l}}\n spec: {containers: [{name: l, image: ghcr.io/acme/linkly}]}\n' > deploy.yaml
kustomize create --autodetect
kustomize edit set namespace linkly-staging
kustomize edit set image ghcr.io/acme/linkly:1.5.0-rc.2
kustomize edit add label env:staging --without-selector --include-templates
cat kustomization.yaml
kustomize build . | grep -E 'namespace:|image:|env:' | sort | uniq -c
diff <(kustomize build .) <(kubectl kustomize .) && echo "kubectl kustomize: identical"apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- cm.yaml
- deploy.yaml
namespace: linkly-staging
images:
- name: ghcr.io/acme/linkly
newTag: 1.5.0-rc.2
labels:
- includeTemplates: true
pairs:
env: staging
1 env: staging
1 - image: ghcr.io/acme/linkly:1.5.0-rc.2
2 env: staging
2 namespace: linkly-staging
kubectl kustomize: identical
The counts say: both objects got the namespace and the label, the Pod template got the label too (includeTemplates), the selector did not, and the image tag is set. kubectl kustomize produced byte-identical output here; with features added after v5.8.1 the embedded version could differ, so pin one Kustomize version across CI and laptops.
Test yourself
A Deployment refers to ConfigMap linkly-config, and the kustomization sets namePrefix: team-. What does the reference become?
- Unchanged, so the Pod fails to start
- team-linkly-config, updated automatically
- An error
- linkly-config-team
Why is includeSelectors: true risky on an existing app?
- It is slower
- Selectors of Deployments are immutable, so adding or changing a selector label on a live app fails
- It removes other labels
- It only works with Helm
How should a CI job set a new image tag in a kustomization?
- sed on deployment.yaml
- kustomize edit set image ghcr.io/acme/linkly:1.5.0
- kubectl set image
- helm upgrade