Define Dependent 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:

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

    1. Create a Pod based on that manifest:

      1. kubectl apply -f https://k8s.io/examples/pods/inject/dependent-envars.yaml
    2. Check the logs for the container running in your Pod:

    When an environment variable is already defined when being referenced, the reference can be correctly resolved, such as in the 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: . 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.