add define interdependent environment variables page

Signed-off-by: Weiping Cai <weiping.cai@daocloud.io>
This commit is contained in:
Weiping Cai 2020-06-16 09:50:06 +08:00
parent d0e6fd0aa3
commit 5513c60b54
No known key found for this signature in database
GPG Key ID: 97CBE6D7C8EBC386
2 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,82 @@
---
title: Define Dependent Environment Variables
content_type: task
weight: 20
---
<!-- overview -->
This page shows how to define dependent environment variables for a container
in a Kubernetes Pod.
## {{% heading "prerequisites" %}}
{{< include "task-tutorial-prereqs.md" >}}
<!-- steps -->
## Define an environment dependent variable for a container
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.
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:
{{< codenew file="pods/inject/dependent-envars.yaml" >}}
1. Create a Pod based on that manifest:
```shell
kubectl apply -f https://k8s.io/examples/pods/inject/dependent-envars.yaml
```
```
pod/dependent-envars-demo created
```
2. List the running Pods:
```shell
kubectl get pods dependent-envars-demo
```
```
NAME READY STATUS RESTARTS AGE
dependent-envars-demo 1/1 Running 0 9s
```
3. Get log to the container running in your Pod:
```shell
kubectl logs pod/dependent-envars-demo
```
```
SERVICE_ADDRESS
https://172.17.0.1:80
UNCHANGE_REFERENCE
$(PROTOCOL)://172.17.0.1:80
ESCAPED_REFERENCE
$(PROTOCOL)://172.17.0.1:80
```
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`.
When the environment variable is defined, you can use it directly. You can use it after the definition is completed, such as `SERVICE_ADDRESS`.
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.
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.
## {{% heading "whatsnext" %}}
* Learn more about [environment variables](/docs/tasks/inject-data-application/environment-variable-expose-pod-information/).
* See [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core).

View File

@ -0,0 +1,26 @@
apiVersion: v1
kind: Pod
metadata:
name: dependent-envars-demo
spec:
containers:
- name: dependent-envars-demo
args:
- 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;
command:
- sh
- -c
image: busybox
env:
- name: SERVICE_PORT
value: "80"
- name: SERVICE_IP
value: "172.17.0.1"
- name: UNCHANGE_REFERENCE
value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"
- name: PROTOCOL
value: "https"
- name: SERVICE_ADDRESS
value: "$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"
- name: ESCAPED_REFERENCE
value: "$$(PROTOCOL)://$(SERVICE_IP):$(SERVICE_PORT)"