Expose Pod Information to Containers Through Environment Variables
In Kubernetes, there are two ways to expose Pod and container fields to a running container:
- Environment variables, as explained in this task
- Volume files
Together, these two ways of exposing Pod and container fields are called the downward API.
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:
In this part of exercise, you create a Pod that has one container, and you project Pod-level fields into the running container as environment variables.
pods/inject/dapi-envars-pod.yaml
In that manifest, you can see five environment variables. The field is an array of environment variable definitions. The first element in the array specifies that the MY_NODE_NAME
environment variable gets its value from the Pod’s spec.nodeName
field. Similarly, the other environment variables get their names from Pod fields.
Note: The fields in this example are Pod fields. They are not fields of the container in the Pod.
kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-pod.yaml
Verify that the container in the Pod is running:
# If the new Pod isn't yet healthy, rerun this command a few times.
kubectl get pods
View the container’s logs:
The output shows the values of selected environment variables:
To see why these values are in the log, look at the command
and args
fields in the configuration file. When the container starts, it writes the values of five environment variables to stdout. It repeats this every ten seconds.
Next, get a shell into the container that is running in your Pod:
In your shell, view the environment variables:
# Run this in a shell inside the container
printenv
The output shows that certain environment variables have been assigned the values of Pod fields:
MY_POD_SERVICE_ACCOUNT=default
...
MY_POD_NAMESPACE=default
MY_POD_IP=172.17.0.4
...
MY_NODE_NAME=minikube
MY_POD_NAME=dapi-envars-fieldref
Here is a manifest for another Pod that again has just one container:
pods/inject/dapi-envars-container.yaml
In this manifest, you can see four environment variables. The env
field is an array of environment variable definitions. The first element in the array specifies that the MY_CPU_REQUEST
environment variable gets its value from the requests.cpu
field of a container named test-container
. Similarly, the other environment variables get their values from fields that are specific to this container.
Create the Pod:
Verify that the container in the Pod is running:
# If the new Pod isn't yet healthy, rerun this command a few times.
kubectl get pods
View the container’s logs:
The output shows the values of selected environment variables:
- Read Defining Environment Variables for a Container
- Read the API definition for Pod. This includes the definition of Container (part of Pod).
- Read the list of available fields that you can expose using the downward API.