Kustomize — Kubernetes Config. Management
Kustomize1 — Customizing Kubernetes application configurations
kubectl
native …no separate dependency- Plain YAML …does not use template/DSL (like Helm)
- …compose/customize collections of resources
- …generate resources from other sources (for example simple files)
- …patches to introduce environment specific changes
Terminology:
- Generator — Makes a resource
- Patch — General instructions to modify a resource
- Overlay — Kustomization that depends on another kustomization
- Base — Kustomization referred to by some other kustomization
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.yaml
3
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
- literals — Key/value as data specified in
- 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
#...
Footnotes
Kustomize Project
https://kustomize.io
https://github.com/kubernetes-sigs/kustomize↩︎Kustomize Reference Documentation
https://kubectl.docs.kubernetes.io/references/kustomize↩︎Content of a kustomization file
https://kubectl.docs.kubernetes.io/references/kustomize/kustomization↩︎configMapGenerator
, Kuberntes Documentation
https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/configmapgenerator/↩︎