liveBook · Manning

Environment variable

As explained in Kubernetes in Action, Second Edition

An environment variable is a named value supplied through a process environment and accessible to the process and its child processes.

An environment variable is a named string value available to a running process. In a Kubernetes pod, it gives an application configuration without requiring that value to be built into the container image. Variables are set for each container, not once for the whole pod. A Dockerfile’s ENV directive can provide a default; the pod manifest can supply a different value. section 8.1 section 8.1.2

Set a value in a pod

Add an env entry beneath the container definition: env: - name: INITIAL_STATUS_MESSAGE value: This status message is set in the pod spec. The application can read INITIAL_STATUS_MESSAGE and display it in its response. Values must be strings: quote values such as numbers or true so YAML does not interpret them as another type. To inspect the running container, use kubectl exec kiada -- env. By contrast, kubectl set env pod kiada --list shows only variables defined in the pod manifest. section 8.1.2

A value can also refer to another environment variable. Kubernetes examples use $(POD_NAME) in a value or $(LISTEN_PORT) in a container argument. This lets a value supplied through env become a command-line argument when the application expects one. section 8.1.2 section 8.2.1

Instead of writing a literal value in the pod manifest, choose a source that fits the data:

  • ConfigMap: use env.valueFrom.configMapKeyRef to select one key, or envFrom.configMapRef to expose all entries. A separate ConfigMap can supply different configuration in each environment while the pod manifest stays the same. An optional missing reference leaves the variable unset; a required missing key blocks the affected container from starting. section 8.2.1 section 8.2.3
  • Secret: use valueFrom.secretKeyRef for one entry, or envFrom.secretRef for the entire Secret. Take care: logs or error reports may reveal environment variables, and child processes inherit them. Secret files are the suggested safer alternative. section 8.3.3
  • Downward API: use valueFrom.fieldRef for pod or node information, such as the pod name and IP address; use valueFrom.resourceFieldRef for container CPU or memory limits. section 8.4.2
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:
    - ...

The manifest shows the exact valueFrom, ConfigMap name, and key fields for injecting one entry. section 8.2.3

Environment variables are one way to deliver configuration to a container; configuration files are another. ConfigMaps and Secrets can also be mounted as files, an approach especially worth considering for sensitive values. section 8.2.1 section 8.3.3

How Kubernetes in Action, Second Edition covers it

How deep it goes
Works through literal values, references, inspection, and injected sources; defers file-based ConfigMap and Secret delivery.
Examples
Uses pod manifests, kubectl exec output, a status message, ConfigMap and Secret references, and pod metadata.
What it assumes
Builds on running an application process in Kubernetes while introducing container configuration through concrete manifests.
What you can do after
Operate/configure it in a pod manifest, inspect container values, and choose appropriate sources while recognizing Secret exposure risks.
How it connects
Connects environment variables to Dockerfile defaults, command-line arguments, ConfigMaps, Secrets, and the Downward API.

Also explained in

Compare how 19 books explain environment variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.