Expose Pod Information to Containers Through Environment Variables

This page shows how a Pod can use environment variables to expose information about itself to Containers running in the Pod. Environment variables can expose Pod fields and Container fields.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. 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 kubectl version.

The Downward API

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

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

Use Pod fields as values for environment variables

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 Expose Pod Information to Containers Through Environment Variables - 图1

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: dapi-envars-fieldref
  5. spec:
  6. containers:
  7. - name: test-container
  8. image: k8s.gcr.io/busybox
  9. command: [ "sh", "-c"]
  10. args:
  11. - while true; do
  12. echo -en '\n';
  13. printenv MY_NODE_NAME MY_POD_NAME MY_POD_NAMESPACE;
  14. printenv MY_POD_IP MY_POD_SERVICE_ACCOUNT;
  15. sleep 10;
  16. done;
  17. env:
  18. - name: MY_NODE_NAME
  19. valueFrom:
  20. fieldRef:
  21. fieldPath: spec.nodeName
  22. - name: MY_POD_NAME
  23. valueFrom:
  24. fieldRef:
  25. fieldPath: metadata.name
  26. - name: MY_POD_NAMESPACE
  27. valueFrom:
  28. fieldRef:
  29. fieldPath: metadata.namespace
  30. - name: MY_POD_IP
  31. valueFrom:
  32. fieldRef:
  33. fieldPath: status.podIP
  34. - name: MY_POD_SERVICE_ACCOUNT
  35. valueFrom:
  36. fieldRef:
  37. fieldPath: spec.serviceAccountName
  38. restartPolicy: Never

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

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:

  1. kubectl get pods

View the Container’s logs:

  1. kubectl logs dapi-envars-fieldref

The output shows the values of selected environment variables:

  1. minikube
  2. dapi-envars-fieldref
  3. default
  4. 172.17.0.4
  5. default

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:

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

In your shell, view the environment variables:

  1. /# 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. ...
  8. MY_POD_NAME=dapi-envars-fieldref

Use Container fields as values for environment variables

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

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: dapi-envars-resourcefieldref
  5. spec:
  6. containers:
  7. - name: test-container
  8. image: k8s.gcr.io/busybox:1.24
  9. command: [ "sh", "-c"]
  10. args:
  11. - while true; do
  12. echo -en '\n';
  13. printenv MY_CPU_REQUEST MY_CPU_LIMIT;
  14. printenv MY_MEM_REQUEST MY_MEM_LIMIT;
  15. sleep 10;
  16. done;
  17. resources:
  18. requests:
  19. memory: "32Mi"
  20. cpu: "125m"
  21. limits:
  22. memory: "64Mi"
  23. cpu: "250m"
  24. env:
  25. - name: MY_CPU_REQUEST
  26. valueFrom:
  27. resourceFieldRef:
  28. containerName: test-container
  29. resource: requests.cpu
  30. - name: MY_CPU_LIMIT
  31. valueFrom:
  32. resourceFieldRef:
  33. containerName: test-container
  34. resource: limits.cpu
  35. - name: MY_MEM_REQUEST
  36. valueFrom:
  37. resourceFieldRef:
  38. containerName: test-container
  39. resource: requests.memory
  40. - name: MY_MEM_LIMIT
  41. valueFrom:
  42. resourceFieldRef:
  43. containerName: test-container
  44. resource: limits.memory
  45. restartPolicy: Never

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

  1. kubectl get pods

View the Container’s logs:

  1. kubectl logs dapi-envars-resourcefieldref

The output shows the values of selected environment variables:

  1. 1
  2. 1
  3. 33554432
  4. 67108864

What’s next