Skip to content

Commit 9c196f9

Browse files
authored
Merge pull request #21811 from Cweiping/feature/add_interdependent_envvars
add define interdependent environment variables page
2 parents c452e65 + 4490fe0 commit 9c196f9

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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)"

0 commit comments

Comments
 (0)