Downward API Volume
Someone relabels a running Pod, and a file in its container picks up the new labels while an environment variable keeps the old value. Why does only the file follow, and which fields reach the app each way?
Play the animation 6 steps, animated, plays on a computer or a tablet
Downward API Volume, step by step
-
Pod api-0 asks for its labels two ways. The labels file in a downwardAPI volume takes fieldRef metadata.labels, and an env var ZONE takes the zone key alone. Another env var, NODE_NAME, takes spec.nodeName. Kubelet on Node-1 gets the Pod object bound to that Node.
Play from step 1 -
Before the container starts, Kubelet writes the volume. /etc/podinfo/labels holds the whole labels map, one key="value" line per label. An env var fieldRef, by contrast, cannot take the whole map, only one key.
Play from step 2 -
Kubelet starts the app container and hands it the environment variables ZONE=east and NODE_NAME=Node-1. Their values are resolved once, as the container is created. The app now holds the zone twice, in its environment and in the labels file.
Play from step 3 -
Someone changes the label zone to west on the Pod object. A label change does not restart the container. The new object reaches Kubelet, because Kubelet watches the Pods bound to Node-1.
Play from step 4 -
Kubelet rewrites the labels file and swaps the new version in through a ..data symlink, the way a ConfigMap volume is updated. So a read after the swap returns zone="west". ZONE in the environment still says east, and only a container restart can change it. A labels file mounted through subPath would keep zone="east" until its container restarts.
Play from step 5 -
Not every field goes both ways, though. Fields such as spec.nodeName, status.podIP, status.hostIP and spec.serviceAccountName reach a container only as env, so no file in the volume can hold them. The whole labels or annotations map, by contrast, reaches it only as a file, and a single key goes either way.
Play from step 6