Skip to content

Commit 5513c60

Browse files
author
Weiping Cai
committed
add define interdependent environment variables page
Signed-off-by: Weiping Cai <[email protected]>
1 parent d0e6fd0 commit 5513c60

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
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'; echo SERVICE_ADDRESS;printenv SERVICE_ADDRESS;echo UNCHANGE_REFERENCE;printenv UNCHANGE_REFERENCE;echo ESCAPED_REFERENCE;printenv ESCAPED_REFERENCE; 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: UNCHANGE_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)