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.
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 "Hello from the environment"
. Here is the configuration manifest for the Pod:
List the running Pods:
The output is similar to:
NAME READY STATUS RESTARTS AGE
envar-demo 1/1 Running 0 9s
-
The output is similar to this:
HOSTNAME=envar-demo
...
DEMO_GREETING=Hello from the environment
DEMO_FAREWELL=Such a sweet sorrow
Note: The environment variables set using the env
or 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.
Upon creation, the command echo Warm greetings to The Most Honorable Kubernetes
is run on the container.
- Learn more about .
- Learn about using secrets as environment variables.