Environment Variables
Inject configuration into containers with environment variables from literals, ConfigMaps, or Secrets.
Syntax
env:
- name: KEY
value: "literal"
- name: KEY2
valueFrom:
configMapKeyRef: { name: cm, key: k }Containers commonly read their settings from environment variables. Kubernetes gives you several sources for them under a container's env and envFrom fields.
Sources
- Literal values — a fixed string written right in the manifest.
- ConfigMap keys — via
configMapKeyRef. - Secret keys — via
secretKeyRef. - Pod fields — via
fieldRef(e.g. the Pod's own name or namespace).
Bulk import
Use envFrom to pull every key from a ConfigMap or Secret at once, instead of listing them one by one.
Example
apiVersion: v1
kind: Pod
metadata:
name: env-demo
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "env; sleep 3600"]
env:
- name: GREETING
value: "hello"
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVELWhen to use it
- A twelve-factor app reads all external configuration from environment variables injected by Kubernetes, keeping the image environment-agnostic.
- A developer overrides a single environment variable in a pod spec to enable debug mode without changing the shared ConfigMap.
- A platform team injects pod metadata like the node name and pod IP as environment variables using the Downward API so the app can include them in logs.
More examples
Static and ConfigMap env vars
Shows three env var sources in one container spec: a literal value, a ConfigMap key, and a Secret key.
spec:
containers:
- name: app
image: myapp:1.0
env:
- name: APP_ENV
value: production
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVEL
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-creds
key: passwordDownward API env vars
Uses the Downward API to inject pod metadata into the container at runtime, enabling the app to self-identify.
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeNameInspect env vars inside a pod
Runs env inside the pod to list all injected variables, and printenv to retrieve a specific one for debugging.
kubectl exec -it my-pod -- env | sort
# Shows all environment variables available inside the container
kubectl exec -it my-pod -- printenv DB_PASSWORD
Discussion