Environment variables in Kubernetes

To keep things simple, we'll use pods with a single container, but the same principles apply to pods with multiple containers.

How can I set an env var inside a pod?

You can add the env section to a container in the Pod specification to define a list of environment variables for that container:

---
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
    - name: container1
      image: alpine
      env:
        - name: MY_VAR1
          value: my_value1
        - name: MY_VAR2
          value: my_value2

If you create the pod from the command line, you can use the --env flag to define env vars:

kubectl run pod1 --image alpine --env MY_VAR1=my_value1 --env MY_VAR2=my_value2 -- sleep infinity

This command will create a pod called pod1 with an alpine container and 2 env vars defined: MY_VAR1 and MY_VAR2. The container will run a sleep command indefinitely so that you can inspect it.

How can I see all env vars defined inside a pod?

Their values can be seen by describing the Pod:

kubectl describe pod pod1

Check the Environment section of each container in the Pod to see all env vars defined for that container.

You can also check the values of all env vars from inside the container:

kubectl exec pod1 -- printenv

Is there a more convenient way if I want to set many env vars for a pod?

Yes, you can create a config map and reference it from the pod:

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm1
data:
  MY_VAR1: my_value1
  MY_VAR2: my_value2

You can add all keys from the config map as env vars using the envFrom section and configMapRef inside a container:

---
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
    - image: alpine
      name: container1
      envFrom:
        - configMapRef:
            name: cm1

Can I choose which env vars to add instead of adding the whole config map?

Yes, you can set individual env vars using the env section and configMapKeyRef:

---
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: container1
    image: alpine
    env:
      - name: MY_VAR1
        valueFrom:
          configMapKeyRef:
            name: cm1
            key: MY_VAR1

Can env vars reference other env vars?

Yes, you can use the $(ENV_VAR) syntax to reference other env vars:

---
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
    - name: container1
      image: alpine
      env:
        - name: MY_VAR1
          value: my_value1
        - name: MY_VAR2
          value: "The value of MY_VAR1 is $(MY_VAR1)"

Can I get information from the pod itself as env vars?

Yes, you can get some information from the pod itself as env vars using the downwardAPI:

---
 apiVersion: v1
 kind: Pod
 metadata:
   name: pod1
 spec:
   containers:
     - name: container1
       image: alpine
       env:
         - name: POD_NAME
           valueFrom:
             fieldRef:
               fieldPath: metadata.name
         - name: POD_IP
           valueFrom:
             fieldRef:
               fieldPath: status.podIP

You can check all available fields here.

More info

External links: