Define Environment Variables for a Container
You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using or you can use one of these Kubernetes playgrounds:
When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the or envFrom
field in the configuration file.
The env
and envFrom
fields have different effects.
env
allows you to set environment variables for a container, specifying a value directly for each variable that you name.
envFrom
You can read more about and Secret.
This page explains how to use env
.
In this exercise, you create a Pod that runs one container. The configuration file for the Pod defines an environment variable with name DEMO_GREETING
and value . Here is the configuration manifest for the Pod:
List the running Pods:
NAME READY STATUS RESTARTS AGE
envar-demo 1/1 Running 0 9s
List the Pod’s container environment variables:
The output is similar to this:
NODE_VERSION=4.4.2
HOSTNAME=envar-demo
...
DEMO_GREETING=Hello from the environment
Note: The environment variables set using the env
or envFrom
field override any environment variables specified in the container image.
Note: Environment variables may reference each other, however ordering is important. Variables making use of others defined in the same context must come later in the list. Similarly, avoid circular references.
Environment variables that you define in a Pod’s configuration can be used elsewhere in the configuration, for example in commands and arguments that you set for the Pod’s containers. In the example configuration below, the GREETING
, HONORIFIC
, and NAME
environment variables are set to Warm greetings to
, The Most Honorable
, and Kubernetes
, respectively. Those environment variables are then used in the CLI arguments passed to the container.
Upon creation, the command echo Warm greetings to The Most Honorable Kubernetes
is run on the container.
- Learn more about environment variables.
- Learn about .