You edit a ConfigMap, apply it, and nothing happens. The pods keep running with the old values because Kubernetes does not restart a Deployment when a ConfigMap it reads changes. The Kubernetes docs say it plainly: ConfigMaps consumed as environment variables are not updated automatically and require a pod restart.
Kustomize solves this with generators. A configMapGenerator builds the ConfigMap for you and appends a hash of its content to the name. When the content changes, the name changes, Kustomize rewrites every reference in your workloads, and the changed pod template makes the Deployment roll out new pods. This guide moves a hand-written ConfigMap and Secret into generators, adds a per-environment override, and covers the errors you are likely to hit.
How the hash suffix triggers a rollout
For a generated ConfigMap, Kustomize hashes the kind, the name, data and binaryData. For a Secret it hashes the kind, the name, type, data and stringData. It takes the first 10 hex characters of a SHA-256 digest, replaces 0, 1, 3, a and e with g, h, k, m and t, and appends the result to the name, for example web-config-ktbffhtdh7.
Kustomize also knows which fields in a Deployment, StatefulSet, DaemonSet, Job, CronJob or Pod refer to a ConfigMap or Secret by name: volumes, envFrom, env[].valueFrom and others. During the build it replaces the plain name in those fields with the suffixed one. A new name in the pod template is a change to the template, and a Deployment answers that with a rolling update.
Labels and annotations are not part of the hash. Changing only the options.labels of a generator leaves the name as it was, so no rollout happens.
Step 1: Lay out a base and an overlay
Start with a base that holds the Deployment and the generators, and a production overlay that changes one value. The layout looks like this.
app/
base/
kustomization.yaml
deployment.yaml
config.env
overlays/
prod/
kustomization.yaml
Put the generator in the base, not the overlay. The Kustomize docs point out that an overlay generator that merges into an existing ConfigMap does not add a suffix by itself. The suffix comes from the base generator, and the overlay changes the content that the suffix is computed from.
Step 2: Write the config as an env file
An env file holds one KEY=value pair per line. Kustomize skips empty lines and lines that start with #, splits each other line at the first =, and turns every pair into its own key in the ConfigMap. That makes it a good fit for settings the app reads as environment variables.
# app/base/config.env
LOG_LEVEL=info
CACHE_TTL=60
Kustomize keeps the value exactly as written after the =. If you write LOG_LEVEL="info", the stored value includes the quotes. The literals field behaves differently: it strips one pair of matching surrounding quotes.
Step 3: Add the generator and the Deployment
The base kustomization lists the Deployment under resources and builds the ConfigMap from config.env with the envs field. The generatorOptions block adds a label to every generated object in this kustomization, which you will use later to find old ConfigMaps.
# app/base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
generatorOptions:
labels:
app.kubernetes.io/name: web
configMapGenerator:
- name: web-config
envs:
- config.env
In the Deployment, refer to the ConfigMap by the name you gave the generator, web-config, without any suffix. Kustomize fills in the suffix during the build.
# app/base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
envFrom:
- configMapRef:
name: web-config
Step 4: Build the base and read the output
Render the base without applying it. Either the standalone CLI or kubectl works.
kustomize build app/base
# or
kubectl kustomize app/base
The ConfigMap in the output carries the suffix, and the configMapRef in the Deployment points at the same name.
apiVersion: v1
data:
CACHE_TTL: "60"
LOG_LEVEL: info
kind: ConfigMap
metadata:
labels:
app.kubernetes.io/name: web
name: web-config-ktbffhtdh7
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- envFrom:
- configMapRef:
name: web-config-ktbffhtdh7
image: nginx:1.27
name: web
Now change LOG_LEVEL=info to LOG_LEVEL=debug in config.env and build again. The ConfigMap becomes web-config-fdtbb7524c and the configMapRef follows it. That one changed line in the pod template is what makes Kubernetes start new pods. Change it back to info before you continue.
Step 5: Override a value in the overlay with behavior: merge
Production should log at warn. Instead of copying the whole env file, add a generator in the overlay with the same name and behavior: merge. Merge keeps the keys from the base and adds or overwrites the ones you list.
# app/overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
configMapGenerator:
- name: web-config
behavior: merge
literals:
- LOG_LEVEL=warn
The built ConfigMap holds CACHE_TTL: "60" and LOG_LEVEL: warn, and its name is web-config-hmc267d6bc. The hash is computed from the merged data, so the production name differs from the base name and changes whenever either file changes.
The other choice, behavior: replace, throws away the base data and keeps only what the overlay lists. Leaving behavior out means create, and with a ConfigMap of the same name already coming from the base, the build fails because Kustomize will not create a second one.
The name and namespace in the overlay generator must match the base generator exactly. Generators run before the namespace transformer, so if the base generator sets no namespace, the overlay generator must not set one either, even when the overlay has a namespace field.
Step 6: Generate the Secret the same way
Secrets work the same way through secretGenerator. Kustomize base64-encodes every value into data and sets type: Opaque unless you give a type. Keep the source file out of Git, because base64 is an encoding, not encryption.
# app/base/secrets.env
DB_PASSWORD=change-me
Add the generator to the base kustomization next to the ConfigMap generator.
# app/base/kustomization.yaml (secretGenerator added)
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
generatorOptions:
labels:
app.kubernetes.io/name: web
configMapGenerator:
- name: web-config
envs:
- config.env
secretGenerator:
- name: web-secrets
envs:
- secrets.env
Then reference it from the container by its plain name, next to the ConfigMap.
envFrom:
- configMapRef:
name: web-config
- secretRef:
name: web-secrets
The built Secret is named web-secrets-dc27fg8gbg with DB_PASSWORD: Y2hhbmdlLW1l in data, and the secretRef is rewritten to match. Rotating the password now rolls the Deployment as well.
Step 7: Apply and watch the rollout
Apply the overlay and wait for the rollout to finish. kubectl diff -k first shows you the new ConfigMap and the changed reference in the pod template, which is a quick way to confirm a config edit will restart pods.
kubectl diff -k app/overlays/prod
kubectl apply -k app/overlays/prod
kubectl rollout status deployment/web
If the new config breaks the app, kubectl rollout undo deployment/web goes back to the previous ReplicaSet. That works because the previous ReplicaSet still points at the old ConfigMap name, and that ConfigMap still exists in the cluster.
Clean up old ConfigMaps
Every config change leaves the previous ConfigMap and Secret behind, since kubectl apply creates the new names and does not delete objects that are no longer in the build. That is what makes rollback possible, but the objects pile up over time. The label from generatorOptions lets you list them.
kubectl get configmaps,secrets -l app.kubernetes.io/name=web
Compare the list with the names in kubectl kustomize app/overlays/prod and delete the ones nothing references once you no longer need them for rollback. Keep at least the one used by the previous ReplicaSet.
Troubleshooting
The name suffix did not change
Check what you changed. Only the name and the data fields feed the hash, so edits to options.labels or options.annotations produce the same name. Also check that disableNameSuffixHash: true is not set, either on the generator or in generatorOptions. A true in generatorOptions wins over the entry's own options.
"does not exist; cannot merge or replace"
The overlay generator uses behavior: merge or behavior: replace, but Kustomize found no ConfigMap with the same name and namespace in the resources it already loaded. Look for a typo in the name, a namespace set on one generator and not the other, or a base that is missing from resources.
"illegally repeats the key"
One generator produced the same key twice. Kustomize collects keys from envs, literals and files into one map and stops at the first duplicate. A common cause is a key that appears both in an env file and in literals of the same entry. Move the override to an overlay with behavior: merge, or remove one of the two.
Values arrive with quotes
If the app reads "info" with the quote characters, the value came from an env file written as LOG_LEVEL="info". Env files keep the quotes. Remove them from the file, or move the value to literals, which strips one matching pair.