Kustomize Template-free manifests

Version 5.8 · source

Start here

kustomize create --autodetect
Generate kustomization.yaml
kustomize build <dir>
Render the manifests
kubectl apply -k <dir>
Build and apply
kubectl diff -k <dir>
Preview the change
kustomize edit add resource <file>
Add a resource
kustomize edit set image <name>=<image>:<tag>
Pin an image
kustomize edit set namespace <ns>
Set the namespace
kustomize build <dir> | kubectl apply -f -
Pipe a build

Concepts

  1. Base plain manifests
  2. Overlay patches, images
  3. Rendered YAML stdout
  4. Cluster

Build and apply

kustomize build <dir>
Render a kustomization to stdout
kustomize build <dir> -o <path>
Write output to a file or directory
kubectl apply -k <dir>
Build and apply to the cluster
kubectl kustomize <dir>
Build using kubectl's bundled kustomize
kubectl diff -k <dir>
Show what apply would change
kubectl delete -k <dir>
Delete everything the build produces
kubectl get -k <dir>
List live objects of the build
kustomize build --enable-helm <dir>
Inflate helmCharts entries
Needs the helm binary on PATH.
kustomize build --load-restrictor LoadRestrictionsNone <dir>
Allow files outside the root
kustomize build <git-url>//<path>?ref=<tag>
Build straight from a git repo
kustomize version
Print the version

Create and edit

kustomize create
Write an empty kustomization.yaml
kustomize create --autodetect
Create it listing YAML files found
kustomize edit add resource <path>
Add a file or directory to resources
kustomize edit add patch --path <file> --kind <kind>
Add a patch entry
kustomize edit add configmap <name> --from-literal=<k>=<v>
Add a generated ConfigMap
kustomize edit add label <k>:<v>
Add a label to all resources
kustomize edit add annotation <k>:<v>
Add an annotation to all resources
kustomize edit set image <name>=<newname>:<tag>
Change an image name and tag
kustomize edit set namespace <ns>
Set the namespace
kustomize edit set nameprefix <prefix>
Set the name prefix
kustomize edit set replicas <name>=<n>
Set a replica count
kustomize edit fix
Migrate deprecated fields
Rewrites patchesStrategicMerge, commonLabels and bases to current fields.

kustomization.yaml

Skeleton
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- ../../base- extra.yaml
resources:
Files, directories or URLs to include
namespace: <ns>
Set namespace on every resource
namePrefix: <p> / nameSuffix: <s>
Prefix or suffix all names
References between resources are updated too.
labels:
Add labels, optionally to selectors
Prefer over commonLabels: selectors are only touched with includeSelectors: true.
commonAnnotations:
Add annotations to all resources
replicas:
Override replica counts
components:
Include reusable Component directories
generatorOptions:
Defaults for generated ConfigMaps/Secrets
helmCharts:
Render Helm charts in place
buildMetadata:
Record origin in annotations or labels

Patches

Strategic merge from a file
patches:- path: patch.yaml  target:    kind: Deployment    name: web
Inline JSON6902
patches:- target: {kind: Deployment, name: web}  patch: |-    - op: replace      path: /spec/replicas      value: 3
target: {kind, name, labelSelector}
Select what a patch applies to
name and namespace are regexes, anchored automatically.
op: add / replace / remove
JSON6902 operations
path: /metadata/labels/a~1b
Escape / as ~1 in JSON paths
options: {allowNameChange: true}
Let a patch rename a resource

Images and generators

Images
images:- name: nginx  newName: registry.example/nginx  newTag: "1.27"
digest: sha256:<hash>
Pin an image by digest instead of tag
ConfigMap from literals and files
configMapGenerator:- name: app-config  literals:  - LOG_LEVEL=debug  files:  - app.properties
envs: [<file>.env]
Load KEY=value lines as entries
secretGenerator:
Same fields as configMapGenerator, makes Secrets
behavior: create / replace / merge
How an overlay treats a base's generator
options: {disableNameSuffixHash: true}
Keep the plain name without a hash
The hash suffix triggers rollouts when config changes; disabling it loses that.
files: [<key>=<file>]
Choose the data key for a file

Replacements and components

Copy a value into other fields
replacements:- source:    kind: ConfigMap    name: app-config    fieldPath: data.HOST  targets:  - select:      kind: Deployment    fieldPaths:    - spec.template.spec.containers.[name=web].env.[name=HOST].value
sourceValue: <literal>
Use a literal instead of a source object
options: {delimiter: ":", index: 1}
Replace only part of a string
reject:
Exclude matches from a target select
A component
apiVersion: kustomize.config.k8s.io/v1alpha1kind: Componentpatches:- path: add-sidecar.yaml

Recipes

Base plus overlay layout
base/  kustomization.yaml   # resources: [deployment.yaml, service.yaml]overlays/prod/  kustomization.yaml   # resources: [../../base]kubectl apply -k overlays/prod
Prod overlay
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationnamespace: prodresources:- ../../basereplicas:- name: web  count: 5images:- name: web  newTag: v2.3.1
Preview before applying
kustomize build overlays/prod > /tmp/prod.yamlkubectl diff -k overlays/prodkubectl apply -k overlays/prod
Bump an image in CI
cd overlays/prodkustomize edit set image web=registry.example/web:$TAGkustomize build . | kubectl apply -f -