0%
Kubernetes CKA and CKAD Exam Prep: Hands On Task Drills and Mock Exams
How the Exams Work
Administrator (CKA) Skills
Mock Exams
Configuration, Secrets, Quotas and Service Accounts
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.