|
| 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 | + |
| 14 | + |
| 15 | +## {{% heading "prerequisites" %}} |
| 16 | + |
| 17 | + |
| 18 | +{{< include "task-tutorial-prereqs.md" >}} |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +<!-- steps --> |
| 24 | + |
| 25 | +## Define an environment dependent variable for a container |
| 26 | + |
| 27 | +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. |
| 28 | + |
| 29 | +In this exercise, you create a Pod that runs one container. The configuration |
| 30 | +file for the Pod defines an dependent environment variable with common usage defined. Here is the configuration manifest for the |
| 31 | +Pod: |
| 32 | + |
| 33 | +{{< codenew file="pods/inject/dependent-envars.yaml" >}} |
| 34 | + |
| 35 | +1. Create a Pod based on that manifest: |
| 36 | + |
| 37 | + ```shell |
| 38 | + kubectl apply -f https://k8s.io/examples/pods/inject/dependent-envars.yaml |
| 39 | + ``` |
| 40 | + ``` |
| 41 | + pod/dependent-envars-demo created |
| 42 | + ``` |
| 43 | + |
| 44 | +2. List the running Pods: |
| 45 | + |
| 46 | + ```shell |
| 47 | + kubectl get pods dependent-envars-demo |
| 48 | + ``` |
| 49 | + ``` |
| 50 | + NAME READY STATUS RESTARTS AGE |
| 51 | + dependent-envars-demo 1/1 Running 0 9s |
| 52 | + ``` |
| 53 | + |
| 54 | +3. Get log to the container running in your Pod: |
| 55 | + |
| 56 | + ```shell |
| 57 | + kubectl logs pod/dependent-envars-demo |
| 58 | + ``` |
| 59 | + ``` |
| 60 | + SERVICE_ADDRESS |
| 61 | + https://172.17.0.1:80 |
| 62 | + UNCHANGE_REFERENCE |
| 63 | + $(PROTOCOL)://172.17.0.1:80 |
| 64 | + ESCAPED_REFERENCE |
| 65 | + $(PROTOCOL)://172.17.0.1:80 |
| 66 | + ``` |
| 67 | + |
| 68 | +As shown above, we have defined the correct dependency reference of `SERVICE_ADDRESS`, bad dependency reference of `UNCHANGE_REFERENCE` and skip dependent references of `ESCAPED_REFERENCE`. |
| 69 | + |
| 70 | +When the environment variable is defined, you can use it directly. You can use it after the definition is completed, such as `SERVICE_ADDRESS`. |
| 71 | + |
| 72 | +When the environment variable is undefined or only includes some variables, the undefined environment variable is treated as a normal string, such as `UNCHANGE_REFERENCE`. A bad reference does not interfere with the operation of the container. |
| 73 | + |
| 74 | +The $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).Escaped references will never be expanded, regardless of whether the variable exists or not. |
| 75 | + |
| 76 | +## {{% heading "whatsnext" %}} |
| 77 | + |
| 78 | + |
| 79 | +* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/). |
| 80 | +* See [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core). |
| 81 | + |
| 82 | + |
0 commit comments