The env field is a list inside a Kubernetes container definition. Each entry gives an environment variable a name and a value, allowing an application to receive configuration such as a pod name or status message. section 8.1.2
Use the env field to define variables individually:
- Set a literal value with name and value fields. section 8.1.2
- Quote non-string YAML values, including numbers and boolean-like strings, so they remain environment-variable strings. section 8.1.2
- Reference another environment variable with $(VARIABLE), including from the args field. section 8.1.2
- Obtain a value from a ConfigMap key through valueFrom and configMapKeyRef; mark the reference optional when the application can run without it. section 8.2.3
- Define variables separately for each container; Kubernetes does not provide one env list inherited by every container in a pod. section 8.1.2
Define application settings in a pod
spec: containers: - name: kiada image: luksa/kiada:0.4 env: - name: POD_NAME value: kiada - name: INITIAL_STATUS_MESSAGE value: This status message is set in the pod spec. The container can use $(POD_NAME) in another env value or in args. Inspect manifest-defined variables with kubectl set env pod <pod-name> --list, or inspect the running container with kubectl exec <pod-name> -- env.
Listing 8.10 Setting an environment variable from a ConfigMap entry
kind: Pod
...
spec:
containers:
- name: kiada
env: #1
- name: INITIAL_STATUS_MESSAGE #1
valueFrom: #2
configMapKeyRef: #2
name: kiada-config #3
key: status-message #4
optional: true #5
volumeMounts:
- ...
Browse a complete ConfigMap-backed env entry showing valueFrom, configMapKeyRef, key selection, and optional behavior. section 8.2.3
