Kustomize — Kubernetes Config. Management

Kubernetes
Published

April 16, 2025

Modified

April 23, 2025

Kustomize1 — Customizing Kubernetes application configurations

Terminology:

kustomization.yaml

Term “kustomization” refers to akustomization.yaml file…

  • …aka a directory containing this file
  • …describes how to generate or transform resources
  • Containers fields in the following categories:
    • resources …what existing resources are to be customized.
    • generators …what new resources should be created
    • transformers …what to do to the aforementioned resources
    • meta …fields which may influence all or some of the above

Examples:

# create a simple resource
cat > volume.yaml <<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: volume
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: rook-ceph-block
EOF

# create a kustomization
cat > kustomization.yaml <<EOF
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: example-prefix-
resources:
- volume.yaml
EOF

$target must be a path or a URL to a kustomization (can be a base/overlay)

kubectl kustomize $target

# …send it to the apply operation
kubectl kustomize $target | kubectl apply -f -

Kustomization

Reference the docuemntation for Kustomize’s built-in transformers and generators2 and the content of kustomization.yaml3

Names

Resource names…

namePrefix: example-prefix-            # add prefix to names
nameSuffix: -suffix                    # add suffix to names
namespace: example-namespace           # add a namespace
labels:                                # add labels
- pairs:
    service: example
    environment: development 

Create and use a namespace

kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: example
resources:
- ./namespace.yml
#…
namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: example

Resources

Each entry in this list must be a path to a file (or URL):

  • …files container Kubernetes API resources
  • …paths should be specified relative to the kustomization file
  • …processed in depth-first order
resources:
# Local files
- volume.yaml
- deployment.yaml
- path/to/secret.yaml
# Local directories
- ../../path/
# Remote URLs
- https://....

ConfigMaps

Generate ConfigMap resources4:

  • Generation sources…
    • literals — Key/value as data specified in kustomization.yaml
    • env. file — File with key-value pairs relative to kustomization.yaml
    • file — Complete configuration file relative to kustomization.yaml
  • Name of the generated resource automatically identified & referenced
  • Changes to content (will change the name suffix) …recreates of the ConfigMap

Generate a ConfigMap with a data item containing the contents of a file

kustomization.yaml
# create a ConfigMap from a file (relative path)
configMapGenerator:
- name: nginx-conf
  files:
    - etc/nginx.conf
pod.yaml
#...
spec:
  volumes:
  - name: nginx-conf
    configMap:
      name: nginx-conf
      items:
      - key: nginx.conf
        path: nginx.conf
#...
  containers:
  - name: httpd-server
    image: nginx:alpine
    # mount the configuration into a container
    volumeMounts:
    - name: nginx-conf
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf
      readOnly: true
#...