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.

    1. kubectl apply -f https://k8s.io/examples/pods/inject/dapi-envars-pod.yaml

    Verify that the container in the Pod is running:

    1. # If the new Pod isn't yet healthy, rerun this command a few times.
    2. 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:

      1. # Run this in a shell inside the container
      2. printenv

      The output shows that certain environment variables have been assigned the values of Pod fields:

      1. MY_POD_SERVICE_ACCOUNT=default
      2. ...
      3. MY_POD_NAMESPACE=default
      4. MY_POD_IP=172.17.0.4
      5. ...
      6. MY_NODE_NAME=minikube
      7. 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 Expose Pod Information to Containers Through Environment Variables - 图2

      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:

        1. # If the new Pod isn't yet healthy, rerun this command a few times.
        2. kubectl get pods

        View the container’s logs:

        The output shows the values of selected environment variables: