K8s by Example: Kustomize Basics

Kustomize customizes YAML without templates. Compose resources, add labels, and change images, all declaratively. Built into kubectl since v1.14.

kustomization.yaml

Kustomization file lists resources to include. All referenced resources are combined into a single output. Can reference local files or remote URLs.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - deployment.yaml
  - service.yaml
  - configmap.yaml
kustomization-labels.yaml

Add common labels and annotations to all resources. Great for consistent tagging across environments. Labels are also added to selectors automatically.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - deployment.yaml
  - service.yaml

commonLabels:
  app.kubernetes.io/name: my-app
  app.kubernetes.io/managed-by: kustomize

commonAnnotations:
  owner: platform-team
kustomization-images.yaml

Change namespace for all resources. Override image tags without editing manifests. newName changes registry, newTag changes version.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: production

resources:
  - deployment.yaml

images:
  - name: my-app
    newTag: v1.2.3
  - name: nginx
    newName: my-registry/nginx
    newTag: "1.25"
kustomization-generators.yaml

Generate ConfigMaps and Secrets from files or literals. Kustomize adds a hash suffix for automatic rollouts when content changes.

configMapGenerator:
  - name: app-config
    literals:
      - LOG_LEVEL=info
      - DEBUG=false
    files:
      - config.json

secretGenerator:
  - name: app-secrets
    literals:
      - API_KEY=secret123
    files:
      - credentials.json
terminal

Apply with kubectl or preview changes. kubectl kustomize outputs rendered YAML to stdout. -k flag tells kubectl to process kustomization. kubectl diff -k shows what would change.

$ kubectl kustomize ./base

$ kubectl apply -k ./base
configmap/app-config-abc123 created
deployment.apps/my-app created
service/my-app created

$ kubectl diff -k ./base

Index | GitHub | Use arrow keys to navigate |