Configuration, Secrets, Quotas and Service Accounts

Topics Covered

ConfigMaps: Create Fast, Consume Correctly

Creating ConfigMaps imperatively

Consuming a ConfigMap as environment variables

Consuming a ConfigMap as a volume

What happens when the ConfigMap changes

Immutable ConfigMaps and verification

Updating a ConfigMap from a changed file

Limits and edge cases worth knowing

Configuration questions are some of the most reliable points on both exams. They are short, they have a clear right answer and they can be verified in seconds. They are also easy to lose through small mistakes: a key name typed with the wrong case, an environment variable that never refreshes, or a volume mounted over a directory that the application needed. This lesson treats ConfigMaps, Secrets, quotas, LimitRanges, ServiceAccounts and admission as task patterns. You already know what a ConfigMap is from the containers course. The goal here is to produce one in under a minute, wire it into a pod the way the task asks and prove that it worked.

Every drill in this lesson starts the same way. Read the task's context line, run the kubectl config use-context command it gives you, and note the namespace. Most lost points on configuration tasks come from creating the right object in the wrong namespace. Use -n on every command, or set the namespace on the context for the duration of the task, and switch it back afterwards.

Creating ConfigMaps imperatively

You almost never need to write a ConfigMap by hand. kubectl create configmap has three sources, and you can combine them in one command.

bash
1# literal key value pairs
2kubectl -n shop create configmap app-config \
3  --from-literal=LOG_LEVEL=info --from-literal=FEATURE_X=on
4
5# one file becomes one key; the key defaults to the file name
6kubectl -n shop create configmap nginx-conf --from-file=nginx.conf
7# choose the key name yourself
8kubectl -n shop create configmap nginx-conf2 --from-file=default.conf=./site.conf
9
10# a whole directory: every regular file becomes a key
11kubectl -n shop create configmap web-files --from-file=./conf.d/
12
13# an env file: each KEY=value line becomes its own key
14kubectl -n shop create configmap env-cfg --from-env-file=app.env

The difference between --from-file=app.env and --from-env-file=app.env is a classic trap. The first creates a single key named app.env whose value is the whole file. The second parses the file and creates one key per line. If the task says the pod must see DB_HOST and DB_PORT as separate variables loaded from a file, you want --from-env-file. If the task says the application reads a file called app.env, you want --from-file.

When the task gives you a manifest to fix rather than a command to run, generate a skeleton and edit it. kubectl create configmap x --from-literal=a=b --dry-run=client -o yaml prints valid YAML that you can redirect to a file. This is faster than remembering whether the field is data or spec.data. It is data, at the top level, with no spec at all, which is a common slip for people who write Deployments all day.

Consuming a ConfigMap as environment variables

There are two shapes. env with valueFrom.configMapKeyRef picks individual keys and lets you rename them. envFrom with configMapRef imports every key as a variable of the same name, optionally with a prefix.

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: web
5  namespace: shop
6spec:
7  containers:
8  - name: web
9    image: nginx:1.27
10    env:
11    - name: APP_LOG_LEVEL          # the name the container sees
12      valueFrom:
13        configMapKeyRef:
14          name: app-config
15          key: LOG_LEVEL           # the key in the ConfigMap
16    envFrom:
17    - configMapRef:
18        name: env-cfg
19      prefix: CFG_                 # DB_HOST becomes CFG_DB_HOST

Three behaviours matter for tasks. First, if a referenced ConfigMap or key does not exist, the container does not start: the pod shows CreateContainerConfigError and the events name the missing key. You can mark a reference optional: true when the task says the value may be absent. Second, with envFrom, a key that the API server rejects as an environment variable name is skipped and reported in an event rather than failing the pod, although since v1.34 almost any printable name is accepted, so this is now rare. Third, environment variables are read once, when the container starts. Editing the ConfigMap later does nothing to a running container.

One ConfigMap can reach a container as environment variables or as files in a volume.

Consuming a ConfigMap as a volume

A ConfigMap volume turns each key into a file. This is the right shape for configuration files such as nginx.conf or application.properties.

yaml
1spec:
2  volumes:
3  - name: conf
4    configMap:
5      name: nginx-conf2
6      items:                      # optional: choose keys and file names
7      - key: default.conf
8        path: default.conf
9      defaultMode: 0444
10  containers:
11  - name: web
12    image: nginx:1.27
13    volumeMounts:
14    - name: conf
15      mountPath: /etc/nginx/conf.d
16      readOnly: true

Without items, every key in the ConfigMap becomes a file. With items, only the listed keys appear, under the paths you give. Watch the mount path. Mounting a volume at a directory hides whatever the image had there. If you mount a ConfigMap at /etc/nginx, nginx loses its main nginx.conf and the container may crash. Mount at /etc/nginx/conf.d, or use subPath to place a single file without hiding its neighbours.

yaml
1    volumeMounts:
2    - name: conf
3      mountPath: /etc/nginx/conf.d/default.conf
4      subPath: default.conf

subPath has a cost: files mounted with it never receive updates. That leads to the next point, which the exam likes to probe.

What happens when the ConfigMap changes

A normal ConfigMap volume is updated in place by the kubelet after you edit the object. The delay is the kubelet sync period plus its cache propagation delay, so expect up to about a minute or two, not an instant change. The update is atomic: the kubelet writes the new files into a fresh hidden directory and swaps a symlink, so the application never sees a half written file. Whether the application notices is a separate question. nginx, for example, does not reload its configuration just because the file changed.

Environment variables and subPath mounts do not update at all. If the task says "the pods must use the new value", the reliable answer is to edit the ConfigMap and then restart the workload with kubectl rollout restart deployment/web -n shop. Then verify inside a new pod.

After an edit, only a normal volume mount picks up the change without a restart.

Immutable ConfigMaps and verification

Setting immutable: true at the top level of a ConfigMap or Secret prevents any change to its data. The kubelet stops watching immutable objects, which reduces API server load in large clusters. To change one you delete and recreate it, usually under a new name, and roll the workload to the new name. You cannot set the field back to false.

Verification is quick and should become a habit, because a configuration task is only worth points if the pod actually sees the value.

bash
1kubectl -n shop get configmap app-config -o yaml
2kubectl -n shop exec web -- printenv APP_LOG_LEVEL
3kubectl -n shop exec web -- env | grep CFG_
4kubectl -n shop exec web -- cat /etc/nginx/conf.d/default.conf
5kubectl -n shop describe pod web | grep -A5 Events

If exec fails because the image has no shell or no printenv, use kubectl debug with an ephemeral container, or check the rendered pod spec with kubectl get pod web -o yaml and confirm the reference is correct. For a quick graded check, the pod being Running and the variable being visible through exec is the evidence the grader looks for, so show it to yourself before you move to the next task.

Updating a ConfigMap from a changed file

A frequent task variant gives you an existing ConfigMap and a new version of the file it was built from. kubectl edit works for a one line change, but for a whole file it is error prone: you are editing a YAML block scalar in vim and one wrong indent breaks it. The idiomatic approach regenerates the object and applies it over the old one.

bash
1kubectl -n shop create configmap nginx-conf2 \
2  --from-file=default.conf=./site.conf \
3  --dry-run=client -o yaml | kubectl apply -f -
4kubectl -n shop rollout restart deployment/site   # if the app needs a reload

kubectl apply may print a warning that the object was missing the last applied configuration annotation, because it was originally created imperatively. The warning is harmless; apply patches the annotation in and the update succeeds. kubectl replace -f - also works and avoids the warning, but it fails if the object does not exist yet.

Limits and edge cases worth knowing

A ConfigMap is limited to about 1 MiB of data in total, because it is stored as a single object in etcd. Larger configuration belongs in an image, a volume or an external store. Values must be UTF-8 text under data; binary content goes under binaryData as base64, and --from-file picks the right field automatically when the file is not valid UTF-8.

ConfigMaps are namespaced, and a pod can only reference ConfigMaps in its own namespace. If a task creates the ConfigMap in default and the pod in shop, the pod fails with a not found error even though kubectl get configmap appears to show the object. Always include -n when you check, or you will be looking in the wrong namespace and convince yourself the object exists.

Key names are case sensitive and may contain only letters, digits, dashes, underscores and dots. A key such as log-level is a valid ConfigMap key, and envFrom injects it under that exact name, but most shells cannot reference a variable with a dash in it, so a script that reads $LOG_LEVEL sees nothing. If the application really expects that name, deliver it as a file instead.

Generate, then edit

For any ConfigMap or Secret with more than one source, run the imperative create with --dry-run=client -o yaml into a file, check the keys, then apply. It costs ten seconds and catches a wrong key name before a pod fails on it.