CodeBlanks Logo Icon CodeBlanks

What is a ConfigMap?

Learn to separate configuration data with a ConfigMap.

What is a ConfigMap?

A ConfigMap is an object that keeps configuration data (key-value pairs or whole files) separate from the image. The same image runs in development, test and production with different ConfigMaps. Pods use a ConfigMap as environment variables or as files.

kubectl create configmap api-ayar --from-literal=LOG_LEVEL=info --from-literal=DB_HOST=db
kubectl get configmap api-ayar -o yaml

ConfigMaps are for non-secret data; Secrets are used for passwords and keys. A ConfigMap can be at most 1 MiB.

Inspecting a ConfigMap

ConfigMap data is in the data field. describe shows the keys and values readably; a single value is taken with jsonpath:

$ kubectl describe configmap api-ayar
Data
====
DB_HOST:
----
db
LOG_LEVEL:
----
info
$ kubectl get cm api-ayar -o jsonpath='{.data.LOG_LEVEL}'
info

Practice The first ConfigMap

Write two commands: create the api-ayar ConfigMap with the values LOG_LEVEL=info and DB_HOST=db; then print the LOG_LEVEL value with jsonpath='{.data.LOG_LEVEL}'.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl create configmap api-ayar --from-literal=LOG_LEVEL=info --from-literal=DB_HOST=db
kubectl get configmap api-ayar -o jsonpath='{.data.LOG_LEVEL}'

Expected output

configmap/api-ayar created
info

Questions and answers

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

1 What is the benefit of keeping configuration in a ConfigMap instead of baking it into the image?

  • The image shrinks
  • Passwords are encrypted
  • Pods get faster
  • The same image is used in different environments without rebuilding Correct answer

Explanation: The build is done once; the configuration changes per environment.

2 Add a key-value from the command line.

Question
kubectl create configmap api-ayar ____=LOG_LEVEL=info
Answer
kubectl create configmap api-ayar --from-literal=LOG_LEVEL=info

Explanation: --from-literal takes NAME=value.

3 Match.

Term Meaning
ConfigMap Non-secret config
Secret Secret value
data Key-value field
1 MiB Size limit

All lessons in Intermediate