|
| 1 | +--- |
| 2 | +title: Define Dependent Environment Variables |
| 3 | +content_type: task |
| 4 | +weight: 20 |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- overview --> |
| 8 | + |
| 9 | +This page shows how to define dependent environment variables for a container |
| 10 | +in a Kubernetes Pod. |
| 11 | + |
| 12 | + |
| 13 | +## {{% heading "prerequisites" %}} |
| 14 | + |
| 15 | + |
| 16 | +{{< include "task-tutorial-prereqs.md" >}} |
| 17 | + |
| 18 | + |
| 19 | +<!-- steps --> |
| 20 | + |
| 21 | +## Define an environment dependent variable for a container |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +In this exercise, you create a Pod that runs one container. The configuration |
| 26 | +file for the Pod defines an dependent environment variable with common usage defined. Here is the configuration manifest for the |
| 27 | +Pod: |
| 28 | + |
| 29 | +{{< codenew file="pods/inject/dependent-envars.yaml" >}} |
| 30 | + |
| 31 | +1. Create a Pod based on that manifest: |
| 32 | + |
| 33 | + ```shell |
| 34 | + kubectl apply -f https://k8s.io/examples/pods/inject/dependent-envars.yaml |
| 35 | + ``` |
| 36 | + ``` |
| 37 | + pod/dependent-envars-demo created |
| 38 | + ``` |
| 39 | + |
| 40 | +2. List the running Pods: |
| 41 | + |
| 42 | + ```shell |
| 43 | + kubectl get pods dependent-envars-demo |
| 44 | + ``` |
| 45 | + ``` |
| 46 | + NAME READY STATUS RESTARTS AGE |
| 47 | + dependent-envars-demo 1/1 Running 0 9s |
| 48 | + ``` |
| 49 | + |
| 50 | +3. Check the logs for the container running in your Pod: |
| 51 | + |
| 52 | + ```shell |
| 53 | + kubectl logs pod/dependent-envars-demo |
| 54 | + ``` |
| 55 | + ``` |
| 56 | +
|
| 57 | + UNCHANGED_REFERENCE=$(PROTOCOL)://172.17.0.1:80 |
| 58 | + SERVICE_ADDRESS=https://172.17.0.1:80 |
| 59 | + ESCAPED_REFERENCE=$(PROTOCOL)://172.17.0.1:80 |
| 60 | + ``` |
| 61 | + |
| 62 | +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`. |
| 63 | + |
| 64 | +When an environment variable is already defined when being referenced, |
| 65 | +the reference can be correctly resolved, such as in the `SERVICE_ADDRESS` case. |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +The `$(VAR_NAME)` syntax can be escaped with a double `$`, ie: `$$(VAR_NAME)`. |
| 70 | +Escaped references are never expanded, regardless of whether the referenced variable |
| 71 | +is defined or not. This can be seen from the `ESCAPED_REFERENCE` case above. |
| 72 | + |
| 73 | +## {{% heading "whatsnext" %}} |
| 74 | + |
| 75 | + |
| 76 | +* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/). |
| 77 | +* See [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core). |
| 78 | + |
0 commit comments