This page shows how to define environment variables for a container in a Kubernetes Pod.
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 minikube 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 env
or envFrom
field in the configuration file.
The env
and envFrom
fields have different effects.
env
envFrom
envFrom
, all the key-value pairs in the referenced ConfigMap or Secret are set as environment variables for the container. You can also specify a common prefix string.You can read more about ConfigMap 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 "Hello from the environment"
. Here is the configuration manifest for the Pod:
apiVersion:v1kind:Podmetadata:name:envar-demolabels:purpose:demonstrate-envarsspec:containers:- name:envar-demo-containerimage:gcr.io/google-samples/hello-app:2.0env:- name:DEMO_GREETINGvalue:"Hello from the environment"- name:DEMO_FAREWELLvalue:"Such a sweet sorrow"
Create a Pod based on that manifest:
kubectl apply -f https://k8s.io/examples/pods/inject/envars.yaml
List the running Pods:
kubectl get pods -l purpose=demonstrate-envars
The output is similar to:
NAME READY STATUS RESTARTS AGE envar-demo 1/1 Running 0 9s
List the Pod's container environment variables:
kubectl exec envar-demo -- printenv
The output is similar to this:
NODE_VERSION=4.4.2 EXAMPLE_SERVICE_PORT_8080_TCP_ADDR=10.3.245.237 HOSTNAME=envar-demo ... DEMO_GREETING=Hello from the environment DEMO_FAREWELL=Such a sweet sorrow
env
or envFrom
field override any environment variables specified in the container image.Environment variables that you define in a Pod's configuration under .spec.containers[*].env[*]
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. The environment variable MESSAGE
combines the set of all these environment variables and then uses it as a CLI argument passed to the env-print-demo
container.
Environment variable names consist of letters, numbers, underscores, dots, or hyphens, but the first character cannot be a digit. If the RelaxedEnvironmentVariableValidation
feature gate is enabled, all printable ASCII characters except "=" may be used for environment variable names.
apiVersion:v1kind:Podmetadata:name:print-greetingspec:containers:- name:env-print-demoimage:bashenv:- name:GREETINGvalue:"Warm greetings to"- name:HONORIFICvalue:"The Most Honorable"- name:NAMEvalue:"Kubernetes"- name:MESSAGEvalue:"$(GREETING) $(HONORIFIC) $(NAME)"command:["echo"]args:["$(MESSAGE)"]
Upon creation, the command echo Warm greetings to The Most Honorable Kubernetes
is run on the container.