Expose Pod Information to Containers Through Environment Variables

    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:

    To check the version, enter .

    There are two ways to expose Pod and Container fields to a running Container:

    • Environment variables

    Together, these two ways of exposing Pod and Container fields are called the Downward API.

    In this exercise, you create a Pod that has one Container. Here is the configuration file for the Pod:

    pods/inject/dapi-envars-pod.yaml

    In the configuration file, you can see five environment variables. The env field is an array of . 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.

    Create 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:

      View the Container’s logs:

      1. kubectl logs dapi-envars-fieldref

      The output shows the values of selected environment variables:

      To see why these values are in the log, look at the command and 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:

      1. kubectl exec -it dapi-envars-fieldref -- sh

      In your shell, view the environment variables:

      1. /# printenv
      1. MY_POD_SERVICE_ACCOUNT=default
      2. ...
      3. MY_POD_NAMESPACE=default
      4. MY_POD_IP=172.17.0.4
      5. ...
      6. ...

      In the preceding exercise, you used Pod fields as the values for environment variables. In this next exercise, you use Container fields as the values for environment variables. Here is the configuration file for a Pod that has one container:

      pods/inject/dapi-envars-container.yaml Expose Pod Information to Containers Through Environment Variables - 图2

      In the configuration file, you can see four environment variables. The env field is an array of . 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 Container fields.

      Create the Pod:

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

      Verify that the Container in the Pod is running:

      View the Container’s logs:

      1. kubectl logs dapi-envars-resourcefieldref

      The output shows the values of selected environment variables: