A pod named 'web-app' is running but has no environment variables. The developer wants to inject a variable 'DB_URL=postgres://db:5432' from a ConfigMap named 'db-config'. Which pod spec snippet correctly achieves this?
Trap 1: env: - name: DB_URL value: "postgres://db:5432"
This assignment hardcodes a literal string as the value for DB_URL, completely bypassing the db-config ConfigMap. Even if the string matches the intended value, the environment variable is not sourced from Kubernetes configuration data, so any future updates to the ConfigMap will not be reflected in this pod. It also violates the principle of decoupling configuration from the pod definition, making the manifest less flexible and portable.
Trap 2: envFrom: - configMapRef: name: db-config key: DB_URL
The envFrom field is specifically designed to import all key-value pairs from a ConfigMap as environment variables, not to select a single key. Furthermore, the syntax is invalid: configMapRef only accepts a name field, not a key field, so this manifest would fail Kubernetes API validation. Even if the key field were removed, this approach would inject every entry from db-config into the pod's environment, which is not the intended behavior of setting only DB_URL.
Trap 3: env: - name: DB_URL valueFrom: secretKeyRef: name:…
This snippet incorrectly uses secretKeyRef, which is meant to reference a Kubernetes Secret, not a ConfigMap. When Kubernetes processes this, it looks for a Secret named db-config, not a ConfigMap, so the reference will fail or inject nothing unless a Secret with that name exists. To pull a value from a ConfigMap, you must use configMapKeyRef instead, as the two resource types are distinct and handled by different API fields.
- A
env: - name: DB_URL value: "postgres://db:5432"
Why wrong: This assignment hardcodes a literal string as the value for DB_URL, completely bypassing the db-config ConfigMap. Even if the string matches the intended value, the environment variable is not sourced from Kubernetes configuration data, so any future updates to the ConfigMap will not be reflected in this pod. It also violates the principle of decoupling configuration from the pod definition, making the manifest less flexible and portable.
- B
envFrom: - configMapRef: name: db-config key: DB_URL
Why wrong: The envFrom field is specifically designed to import all key-value pairs from a ConfigMap as environment variables, not to select a single key. Furthermore, the syntax is invalid: configMapRef only accepts a name field, not a key field, so this manifest would fail Kubernetes API validation. Even if the key field were removed, this approach would inject every entry from db-config into the pod's environment, which is not the intended behavior of setting only DB_URL.
- C
env: - name: DB_URL valueFrom: secretKeyRef: name: db-config key: DB_URL
Why wrong: This snippet incorrectly uses secretKeyRef, which is meant to reference a Kubernetes Secret, not a ConfigMap. When Kubernetes processes this, it looks for a Secret named db-config, not a ConfigMap, so the reference will fail or inject nothing unless a Secret with that name exists. To pull a value from a ConfigMap, you must use configMapKeyRef instead, as the two resource types are distinct and handled by different API fields.
- D
env: - name: DB_URL valueFrom: configMapKeyRef: name: db-config key: DB_URL
This is the correct and idiomatic way to inject a single value from a ConfigMap into an environment variable. The configMapKeyRef field explicitly names the ConfigMap (db-config) and the specific key (DB_URL), and Kubernetes resolves this reference when the pod is created. Unlike envFrom, it gives precise control over which entry gets injected, and unlike a literal value, it keeps the configuration externalized so the same manifest can be reused with different ConfigMaps.