Cheatsheet - Kustomize
Kustomize is an open-source tool for customizing Kubernetes configurations without templates. It allows you to define a base configuration and then apply overlays to create environment-specific variations, promoting a "Don't Repeat Yourself" (DRY) principle. Kustomize is integrated directly into kubectl
(since version 1.14), but can also be used as a standalone binary.
Core Concepts
kustomization.yaml
: This is the main configuration file in a Kustomize directory. It declares the base resources, patches, and other customizations to be applied.- Base: A directory containing a
kustomization.yaml
file and the common Kubernetes resource definitions (e.g., Deployments, Services) that will be used across multiple environments. - Overlay: A directory with its own
kustomization.yaml
that refers to a base (or other kustomizations) and applies environment-specific customizations (patches) on top of it. - Patches: YAML files that define modifications to be applied to resources in the base. Kustomize supports strategic merge patches and JSON patches.
- Generators: Functions that create new resources, such as
ConfigMap
orSecret
from files, literals, or environment variables. - Transformers: Functions that modify existing resources, like adding common labels, annotations, or name prefixes.
1. Building Configurations (kustomize build
)
This is the most fundamental Kustomize command, used to render the final Kubernetes manifests after applying all customizations.
kustomize build <PATH>
- Builds and prints the customized Kubernetes manifests to standard output (stdout).
<PATH>
can be a local directory containing akustomization.yaml
or a Git repository URL.- Example:
kustomize build .
(Builds thekustomization.yaml
in the current directory). - Example:
kustomize build overlays/production
(Builds the production overlay). - Example:
kustomize build https://github.com/kubernetes-sigs/kustomize.git/examples/helloWorld?ref=v1.0.6
(Builds from a remote Git repository).
2. Managing kustomization.yaml
(kustomize edit
)
Commands to declaratively modify the kustomization.yaml
file.
kustomize create --autodetect
- Generates a
kustomization.yaml
file in the current directory by automatically detecting Kubernetes resource files.
- Generates a
kustomize edit add resource <FILE_PATH>
- Adds a resource (YAML file) to the
resources
list inkustomization.yaml
. - Example:
kustomize edit add resource deployment.yaml
- Adds a resource (YAML file) to the
kustomize edit add patch <FILE_PATH>
- Adds a patch file to the
patches
orpatchesStrategicMerge
section. - Example:
kustomize edit add patch my-deployment-patch.yaml
- Adds a patch file to the
kustomize edit set image <OLD_IMAGE>=<NEW_IMAGE>:<NEW_TAG>
- Updates the image in the
images
section ofkustomization.yaml
. - Example:
kustomize edit set image nginx=myregistry/nginx:v2
- Example:
kustomize edit set image monopole/hello:1=monopole/hello:latest
- Updates the image in the
kustomize edit add configmap <NAME> --from-literal=<KEY>=<VALUE>
- Generates a
ConfigMap
entry inkustomization.yaml
from literals. - Example:
kustomize edit add configmap my-config --from-literal=key1=value1
- Generates a
kustomize edit add secret <NAME> --from-file=<FILE_PATH>
- Generates a
Secret
entry inkustomization.yaml
from a file. - Example:
kustomize edit add secret my-secret --from-file=./secrets/db-password
- Generates a
kustomize edit set namespace <NAMESPACE_NAME>
- Sets a common namespace for all resources in the kustomization.
- Example:
kustomize edit set namespace dev-ns
kustomize edit set nameprefix <PREFIX>
- Adds a prefix to the names of all resources.
- Example:
kustomize edit set nameprefix dev-
kustomize edit add label <KEY>:<VALUE>
- Adds common labels to all resources.
- Example:
kustomize edit add label app:my-app
kustomize edit add annotation <KEY>:<VALUE>
- Adds common annotations to all resources.
- Example:
kustomize edit add annotation oncallPager:800-555-1212
3. Integration with kubectl
Kustomize functionality is deeply integrated into kubectl
.
kubectl apply -k <DIR>
- Applies the Kustomize-built configuration from the specified directory directly to the Kubernetes cluster. This is equivalent to
kustomize build <DIR> | kubectl apply -f -
. - Example:
kubectl apply -k overlays/production
- Applies the Kustomize-built configuration from the specified directory directly to the Kubernetes cluster. This is equivalent to
kubectl kustomize <DIR>
- This
kubectl
command is an alias forkustomize build <DIR>
. It outputs the rendered YAML. - Example:
kubectl kustomize .
- This
kubectl diff -k <DIR>
- Compares the Kustomize-built configuration with the live state on the cluster.
- Example:
kubectl diff -k overlays/staging
kubectl apply -k <DIR> --dry-run=client -o yaml
- Shows what would be applied to the cluster without actually making changes, useful for testing patches and transformations.
4. Other Useful Commands
kustomize version
- Displays the Kustomize CLI version.
kustomize help [COMMAND]
- Provides help for a specific Kustomize command or group.