liveBook · Manning

Env field

As explained in Kubernetes in Action, Second Edition

The env field is a Kubernetes container specification field containing individually defined environment variable name–value entries.

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

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains literal values, YAML quoting, variable references, container scope, and ConfigMap lookup while deferring bulk injection to envFrom.
Examples
Demonstrates pod-name and status-message variables, ConfigMap-backed values, variable expansion, and kubectl environment inspection.
What you can do after
Apply it via Kubernetes manifests to define, reference, inspect, and selectively source container environment variables.
How it connects
Connects environment variables with containers, pods, ConfigMaps, envFrom, args, and shell commands.

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.