Define Dependent Environment Variables

This page shows how to define dependent environment variables for a container in a Kubernetes Pod.

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:

Define an environment dependent variable for a container

When you create a Pod, you can set dependent environment variables for the containers that run in the Pod. To set dependent environment variables, you can use $(VAR_NAME) in the value of env in the configuration file.

In this exercise, you create a Pod that runs one container. The configuration file for the Pod defines an dependent environment variable with common usage defined. Here is the configuration manifest for the Pod:

pods/inject/dependent-envars.yaml Define Dependent Environment Variables - 图1

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: dependent-envars-demo
  5. spec:
  6. containers:
  7. - name: dependent-envars-demo
  8. args:
  9. - while true; do echo -en '\n'; printf UNCHANGED_REFERENCE=$UNCHANGED_REFERENCE'\n'; printf SERVICE_ADDRESS=$SERVICE_ADDRESS'\n';printf ESCAPED_REFERENCE=$ESCAPED_REFERENCE'\n'; sleep 30; done;
  10. command:
  11. - sh
  12. - -c
  13. image: busybox
  14. env:
  15. - name: SERVICE_PORT
  16. value: "80"
  17. - name: SERVICE_IP
  18. value: "172.17.0.1"
  19. - name: UNCHANGED_REFERENCE
  20. value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"
  21. - name: PROTOCOL
  22. value: "https"
  23. - name: SERVICE_ADDRESS
  24. value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"
  25. - name: ESCAPED_REFERENCE
  26. value: "$$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"
  1. Create a Pod based on that manifest:

    1. kubectl apply -f https://k8s.io/examples/pods/inject/dependent-envars.yaml
    1. pod/dependent-envars-demo created
  2. List the running Pods:

    1. kubectl get pods dependent-envars-demo
    1. NAME READY STATUS RESTARTS AGE
    2. dependent-envars-demo 1/1 Running 0 9s
  3. Check the logs for the container running in your Pod:

    1. kubectl logs pod/dependent-envars-demo
    1. UNCHANGED_REFERENCE=$(PROTOCOL)://172.17.0.1:80
    2. SERVICE_ADDRESS=https://172.17.0.1:80
    3. ESCAPED_REFERENCE=$(PROTOCOL)://172.17.0.1:80

As shown above, you have defined the correct dependency reference of SERVICE_ADDRESS, bad dependency reference of UNCHANGED_REFERENCE and skip dependent references of ESCAPED_REFERENCE.

When an environment variable is already defined when being referenced, the reference can be correctly resolved, such as in the SERVICE_ADDRESS case.

When the environment variable is undefined or only includes some variables, the undefined environment variable is treated as a normal string, such as UNCHANGED_REFERENCE. Note that incorrectly parsed environment variables, in general, will not block the container from starting.

The $(VAR_NAME) syntax can be escaped with a double $, ie: $$(VAR_NAME). Escaped references are never expanded, regardless of whether the referenced variable is defined or not. This can be seen from the ESCAPED_REFERENCE case above.

What’s next