CodeBlanks Logo Icon CodeBlanks

kubectl create configmap

Learn to create a ConfigMap with kubectl create configmap.

kubectl create configmap

The sources for creating a ConfigMap can be combined: literal values, single files, all files in a folder, and env files. A file's key name can be changed with key=file:

kubectl create configmap web-ayar \
  --from-literal=TEMA=koyu \
  --from-file=nginx.conf \
  --from-file=index=site/index.html \
  --from-file=ayarlar/            # each file in the folder is a key

Updating a ConfigMap

kubectl create errors on an existing object. The clean way to update a ConfigMap produced from files: regenerate the YAML and apply it. Then the deployment is restarted for pods reading the change as environment variables:

kubectl create configmap web-ayar --from-file=nginx.conf --dry-run=client -o yaml | kubectl apply -f -
kubectl rollout restart deployment/web

ConfigMaps written with immutable: true can't be changed; in big clusters this reduces the API server's load.

Practice Generate from files and update

Write two commands: generate the web-ayar ConfigMap with the literal value TEMA=koyu and the nginx.conf file as YAML without creating it, and pipe it straight into kubectl apply -f -; then restart the web deployment.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl create configmap web-ayar --from-literal=TEMA=koyu --from-file=nginx.conf --dry-run=client -o yaml | kubectl apply -f -
kubectl rollout restart deployment/web

Expected output

configmap/web-ayar configured
deployment.apps/web restarted

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which key does --from-file=index=site/index.html create?

  • index.html
  • site/index.html
  • site
  • index Correct answer

Explanation: The key=path form overrides the file name.

2 Apply the YAML coming from standard input.

Question
... --dry-run=client -o yaml | kubectl apply -f ____
Answer
... --dry-run=client -o yaml | kubectl apply -f -

Explanation: -f - reads standard input.

3 Match.

Term Meaning
--from-literal Single value
--from-file=folder/ Each file is a key
| kubectl apply -f - Update
immutable: true Can't be changed

All lessons in Intermediate