kubectl create configmap: Usage, Options & Common CI Errors
Build a ConfigMap from literals or files without writing YAML.
kubectl create configmap turns flags into a ConfigMap, sourcing keys from inline literals, files, directories, or an env file. It is the fastest way to inject non-secret config into a workload from CI.
What it does
kubectl create configmap NAME populates data from --from-literal=key=value, --from-file=path (file name becomes the key, or dir contents become many keys), or --from-env-file=.env (each KEY=VAL line becomes a key). Add --dry-run=client -o yaml to scaffold the manifest instead of creating it.
Common usage
kubectl create configmap app-cfg --from-literal=LOG_LEVEL=info
kubectl create configmap app-cfg --from-file=./config/
kubectl create configmap app-cfg --from-env-file=./.env
kubectl create configmap app-cfg --from-literal=k=v \
--dry-run=client -o yaml | kubectl apply -f -Common errors in CI
"Error from server (AlreadyExists): configmaps \"app-cfg\" already exists" is the re-run trap - create is not idempotent. The idiom that fixes it is to generate-then-apply: kubectl create configmap ... --dry-run=client -o yaml | kubectl apply -f -. Also note a ConfigMap key must be a valid env-var-ish name ([-._a-zA-Z0-9]); a --from-file whose filename has odd characters fails with "is not a valid key name". And a mounted ConfigMap updates pods lazily - restart the workload (rollout restart) if pods read the value only at start.