TExcelTAD

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:

bash · try it yourself
kustomize version
kubectl version --client | grep -i kustomize
v5.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:

bash · try it yourself
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. namePrefix renamed the ConfigMap to team-linkly-config, and the Deployment's configMapRef was 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 a configurations entry to learn them.
  • Labels and selectors. includeSelectors: true adds the label to selectors and Pod templates too; false only to metadata, and includeTemplates: true (with includeSelectors: 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. images matched by image name and set the tag; newName changes the repository and digest pins by digest.

Note: Older examples use commonLabels (always added to selectors), patchesStrategicMerge and patchesJson6902. All three are deprecated in Kustomize 5 and print a warning; kustomize edit fix rewrites them as labels and patches. Careful: kustomize edit add label k:v without --without-selector still writes commonLabels.

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:

bash · try it yourself
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?

  1. Unchanged, so the Pod fails to start
  2. team-linkly-config, updated automatically
  3. An error
  4. linkly-config-team

Why is includeSelectors: true risky on an existing app?

  1. It is slower
  2. Selectors of Deployments are immutable, so adding or changing a selector label on a live app fails
  3. It removes other labels
  4. It only works with Helm

How should a CI job set a new image tag in a kustomization?

  1. sed on deployment.yaml
  2. kustomize edit set image ghcr.io/acme/linkly:1.5.0
  3. kubectl set image
  4. helm upgrade