StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 6 · ConfigMaps

Env From ConfigMaps

easyEnvironment InjectionConfigMap References—No data yet

Problem statement

The service reads twelve settings out of environment variables, and right now every one of them is typed into the pod by hand. The values already live in the cluster: namespace appconf-env holds app-settings (three settings, dotted keys) and feature-flags (four switches, already named like environment variables). Nothing consumes either of them yet.

About this exercise

A ConfigMap only becomes configuration when something reads it. The first way in is the process environment, and Kubernetes offers two shapes of it:

  • One key, one variable. A container's environment entry can take its value from somewhere else instead of a literal - naming a ConfigMap and a key inside it. You choose the variable's name, so the map's key and the variable can differ; this is what lets a key like log.level, which is not a legal variable name, arrive as LOG_LEVEL.
  • A whole map at once. A container can also pull in every key of a ConfigMap, each key becoming a variable of the same name. Because the key names become variable names directly, only keys that are already legal variable names survive - and an optional prefix is prepended to all of them, which is how you keep a map's variables distinguishable from everything else in the environment.

Both forms resolve when the container starts, not when you write them. That has two consequences. A reference to a ConfigMap or a key that does not exist stops the container from starting at all: the pod is created, the kubelet cannot build the container's environment, and it reports why. And a reference can instead be marked optional, which tells the kubelet to skip it silently - the container starts and the variable is simply not there.

kubectl explain pod.spec.containers.env, kubectl explain pod.spec.containers.envFrom and kubectl describe pod are the reference.

How this project is graded

The grader asserts from inside the running containers - it reads each pod's actual process environment rather than your manifest, so a variable only counts if the container really has it. For the pod that cannot start it reads the status the cluster recorded, and compares it against the reason you saved. Pods take a moment to schedule; the grader waits for them.

What you will build

Three pods in namespace appconf-env, each running busybox:1.36 and staying alive, plus one answer file:

Pod app-runtime         container name: app
    LOG_LEVEL           from key log.level   of ConfigMap app-settings
    RETRY_LIMIT         from key max.retries of ConfigMap app-settings
    every key of ConfigMap feature-flags, each prefixed FLAG_
                        (so BETA_CHECKOUT arrives as FLAG_BETA_CHECKOUT)

Pod strict-runtime container name: app AUDIT_MODE from key audit.mode of ConfigMap absent-settings absent-settings does not exist and you must not create it

Pod optional-runtime container name: app AUDIT_MODE the same reference, marked optional

answers/missing-reason.txt the reason the cluster records for strict-runtime's container, exactly that word and nothing else

Step 1 - named settings and a prefixed map

Bring app-runtime up with the two named variables and the whole flag map behind its prefix. Both flavours of injection live in the same container.

single_key_reaches_the_container_environment and whole_map_arrives_under_the_defined_prefix should pass

Step 2 - a reference that cannot resolve

Create strict-runtime pointing at a ConfigMap that is not in the namespace. It will not start. Find what the cluster says about it and save that reason into answers/missing-reason.txt.

missing_required_reference_holds_the_container_back and recorded_reason_matches_what_the_cluster_reports should pass

Step 3 - the same reference, made optional

Create optional-runtime with the identical reference marked optional. This one runs, and AUDIT_MODE is not in its environment at all.

optional_reference_lets_the_pod_run and optional_reference_leaves_the_variable_unset should pass

When every test passes

You have:

  • Settings reaching a process as environment variables, sourced from the cluster
  • A whole map injected in one entry, namespaced by a prefix instead of hand-listed
  • The failure mode of an unresolvable reference, seen and named
  • The escape hatch that makes a reference tolerable when it is missing

You do not have yet:

  • Files. Environment variables are fixed for the life of the container, and plenty of programs read a config file rather than the environment - that is the next exercise.

Verify

Click Test after every step.

More in ConfigMaps

See the module
Literal & File ConfigMapsMove settings and whole configuration files out of the image into ConfigMaps - built from literals and from files, read back, updated one key at a time, and deleted.—No data yeteasyConfigMap VolumesMount ConfigMaps as files - the whole map as a directory, selected keys at custom paths and modes - and watch an update reach the mounted files while the environment stays frozen.—No data yeteasyExternalized App ConfigurationTake a service's whole configuration out of its image into ConfigMaps, feed one container from all of them at once - named variable, prefixed map, mounted directory and a selected file - and run the drill that shows which half a live edit can reach.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.