The downward API and projected volumes
How a Pod learns its own name, labels, node and limits through the downward API, and how projected volumes combine config, secrets and short-lived tokens.
The downward API and projected volumes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The downward API exposes a Pod's own fields (name, namespace, UID, labels, annotations, node, IPs, service account) and its containers' requests and limits, as env vars or files.
- Labels and annotations as a whole map are only available as files, and those files update when the labels change. Env vars are fixed at start.
- A resourceFieldRef for a limit that isn't set reports the node's allocatable amount instead, and a divisor sets the unit.
- A projected volume merges several sources (configMap, secret, downwardAPI, serviceAccountToken, clusterTrustBundle, podCertificate) into one directory.
- Every Pod already gets one: kube-api-access-*, which projects a service account token, the cluster CA and the namespace. The token is short-lived, audience-scoped and bound to the Pod, and the kubelet rotates it.
A Pod that doesn’t know who it is
Your app logs to a central system, and every line should say which Pod and node it came from. A JVM wants to size its heap to the container’s memory limit. A sidecar needs to tag metrics with the Pod’s labels. All of that information exists, in the Pod object in the API server. The process inside doesn’t have it.
You could give every Pod API credentials and have it look itself up. That’s slow, needs RBAC, and couples your app to Kubernetes. The downward API is the cheaper answer: you declare which fields of the Pod you want, and the kubelet hands them to the container as environment variables or files. Information flows down from the object to the process, hence the name.
Fields as environment variables
env:
- name: POD_NAME
valueFrom: {fieldRef: {fieldPath: metadata.name}}
- name: NODE_NAME
valueFrom: {fieldRef: {fieldPath: spec.nodeName}}
- name: MEM_LIMIT_MI
valueFrom: {resourceFieldRef: {resource: limits.memory, divisor: 1Mi}}
fieldRef reads Pod fields. As env vars you can have metadata.name, metadata.namespace, metadata.uid, a single metadata.labels['app'] or annotation, spec.nodeName, spec.serviceAccountName, status.hostIP(s) and status.podIP(s). resourceFieldRef reads the container’s own requests and limits for CPU, memory and ephemeral storage, and divisor picks the unit. With 1Mi, a 256Mi limit arrives as 256.
One trap is easy to miss. If the limit you ask for isn’t set, the downward API reports the node’s allocatable amount instead. A container with no CPU limit reading limits.cpu sees every core on the node, and an app that spawns one worker per reported CPU will spawn far too many.
Fields as files
The same fields can be written into a downwardAPI volume:
volumes:
- name: podinfo
downwardAPI:
items:
- {path: labels, fieldRef: {fieldPath: metadata.labels}}
- {path: annotations, fieldRef: {fieldPath: metadata.annotations}}
Two differences from env vars matter. The whole metadata.labels or metadata.annotations map is only available this way, written one key="value" per line. And the files update: the kubelet rewrites them, with the same atomic symlink swap it uses for ConfigMap volumes, when you relabel the Pod. Env vars are a snapshot from container start. The volume takes fewer fields, though: only name, namespace, UID, labels and annotations (whole or one key) and the resource fields. The node name, service account and IPs are env-only, as the API server’s validation lists show.
Projected volumes: one directory, many sources
Sometimes an app wants its config file, its TLS certificate and its Pod name all in /etc/app. Three volumes means three mount points. A projected volume merges several sources into one directory:
volumes:
- name: app
projected:
sources:
- configMap: {name: app-config}
- secret:
name: app-tls
items: [{key: tls.crt, path: certs/tls.crt}]
- downwardAPI:
items: [{path: name, fieldRef: {fieldPath: metadata.name}}]
- serviceAccountToken: {audience: vault, expirationSeconds: 600, path: vault-token}
The allowed sources are configMap, secret, downwardAPI, serviceAccountToken, and two newer ones that went GA in 1.37: clusterTrustBundle, which projects CA bundles published cluster-wide, and podCertificate, which has the kubelet request a certificate and key for the Pod. Think of a projected volume as a mail slot that several senders deliver into. The app reads one folder and doesn’t care who wrote which file.
The volume you already have
Create any Pod and look at it afterwards. We applied a Pod with no service account volume of its own, and the stored object had this added:
- name: kube-api-access-8crm9
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token
- configMap:
name: kube-root-ca.crt
items: [{key: ca.crt, path: ca.crt}]
- downwardAPI:
items:
- path: namespace
fieldRef: {apiVersion: v1, fieldPath: metadata.namespace}
The ServiceAccount admission plugin injects it and mounts it at /var/run/secrets/kubernetes.io/serviceaccount. That’s how every client library finds credentials in-cluster. Set automountServiceAccountToken: false on the Pod or ServiceAccount when the app never talks to the API.
The token is the interesting part. It replaced the long-lived token Secrets of old, and it differs in three ways. It is short-lived: the kubelet requests it through the TokenRequest API and replaces it once 80 percent of its lifetime has passed, or after 24 hours. (The odd 3607 seconds is a marker. The API server quietly extends tokens with exactly that lifetime to a year so old client libraries that never reread the file keep working, and it can log their use.) It is audience-scoped: a token with audience: vault is useless against the API server. And it is bound to the Pod: its claims carry the Pod’s name and UID.
We checked all three with kubectl create token. A token minted for audience vault and bound to a Pod decoded to a 600-second lifetime with kubernetes.io.pod.uid set. A TokenReview against the API server’s default audience was refused with token audiences ["vault"] is invalid. After we deleted the Pod, the next TokenReview still passed, because the API server caches successful authentications for about ten seconds, and the one after returned service account token has been invalidated. The minimum lifetime is 10 minutes. A Pod asking for 300 seconds was refused at admission.
This is the mechanism under workload identity in the clouds and under Vault’s Kubernetes auth: an external system trusts the cluster’s token issuer and accepts tokens with its own audience. The service accounts lesson picks it up from the authentication side.
About the video
The embed is a 15-minute clip, from 20:03 to 35:22, of Cloud With VarJosh’s “Day 26: Kubernetes Volumes | Ephemeral Storage | emptyDir & downwardAPI DEMO” from April 2025’s CKA course. The clip covers the introduction to the downward API and a demo exposing Pod metadata through it. The earlier emptyDir half belongs to the volumes lesson. It doesn’t cover projected volumes or service account tokens, which is why most of the second half of this lesson comes from docs and the lab. Engineering with Morris’s short video in deeper covers tokens and the TokenRequest API. Judged from chapter titles and description, since transcripts can’t be fetched here, and from its “English (US) original” audio track.
What was run: against a local v1.37.3 API server whose nodes run no containers, we applied the env and volume Pod above and read back the injected kube-api-access volume, minted bound and audience-scoped tokens with kubectl create token, decoded their claims, ran TokenReviews before and after deleting the Pod, and saw the 10-minute minimum enforced. Nothing ran in a container, so the env values and files themselves weren’t seen. The fallback to node allocatable, the update behaviour and the rotation rule are from the docs and the v1.37.3 kubelet source (token_manager.go), checked 2026-11-28.
Where this fits
Projected tokens are how a Pod proves who it is to an outside secret store. Next: external secrets and sealed secrets.
Check yourself
Your app needs the Pod's labels, and an operator relabels Pods at runtime. Should you use an env var or a downwardAPI volume?
A volume with fieldPath metadata.labels. The kubelet rewrites the file when labels change, and the whole map is only available as a file anyway. An env var holds one label's value as it was when the container started.
A container has a memory request but no limit, and reads limits.memory through resourceFieldRef. What value does it see?
The node's allocatable memory. When a limit isn't set the downward API falls back to node allocatable, so code that sizes a heap from that value can badly overcommit.
You project a service account token with audience vault and give it to Vault. Why is it safer than the old token Secret?
It expires (here within the hour, and the kubelet refreshes it), it is only valid for the vault audience so it can't be replayed against the API server, and it is bound to the Pod's UID so it stops working once the Pod is deleted. The legacy Secret token never expired and worked everywhere.
Why does kubectl get pod -o yaml show a kube-api-access volume you never wrote?
The ServiceAccount admission plugin injects it at creation: a projected volume with a token, the kube-root-ca.crt ConfigMap's ca.crt and the namespace, mounted at /var/run/secrets/kubernetes.io/serviceaccount. Set automountServiceAccountToken false to leave it out.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.