Files
doc-exports/docs/ucs/umn/ucs_01_0150.html
qiujiandong1 0a674cd795 UCS UMN initial update 20250523 version
Reviewed-by: Eotvos, Oliver <oliver.eotvos@t-systems.com>
Co-authored-by: qiujiandong1 <qiujiandong1@huawei.com>
Co-committed-by: qiujiandong1 <qiujiandong1@huawei.com>
2026-01-13 13:39:08 +00:00

108 lines
9.5 KiB
HTML

<a name="ucs_01_0150"></a><a name="ucs_01_0150"></a>
<h1 class="topictitle1">Setting Environment Variables</h1>
<div id="body8662426"><div class="section" id="ucs_01_0150__en-us_topic_0000001199341154_section91908379912"><h4 class="sectiontitle">Scenario</h4><p id="ucs_01_0150__en-us_topic_0000001199341154_p1566654220910">An environment variable is a variable whose value can affect the way a running container will behave. You can modify environment variables even after workloads are deployed, increasing flexibility in workload configuration.</p>
<p id="ucs_01_0150__en-us_topic_0000001199341154_p33421347172917">The function of setting environment variables on UCS is the same as that of specifying <span class="parmname" id="ucs_01_0150__en-us_topic_0000001199341154_parmname1630339175319"><b>ENV</b></span> in a Dockerfile.</p>
<div class="notice" id="ucs_01_0150__en-us_topic_0000001199341154_note1262611218201"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="ucs_01_0150__en-us_topic_0000001199341154_p103084565278">After a container is started, do not modify configurations in the container. If configurations in the container are modified (for example, passwords, certificates, and environment variables of a containerized application are added to the container), the configurations will be lost after the container restarts and container services will become abnormal. An example scenario of container restart is pod rescheduling due to node anomalies.</p>
<p id="ucs_01_0150__en-us_topic_0000001199341154_p26271321192016">Configurations must be imported to a container as arguments. Otherwise, configurations will be lost after the container restarts.</p>
</div></div>
<p id="ucs_01_0150__en-us_topic_0000001199341154_p78261119155911">Environment variables can be set in the following modes:</p>
<ul id="ucs_01_0150__en-us_topic_0000001199341154_ul1669104610598"><li id="ucs_01_0150__en-us_topic_0000001199341154_li266913468594"><strong id="ucs_01_0150__b1539395315590">Custom</strong>: Enter a variable name and value.</li><li id="ucs_01_0150__en-us_topic_0000001199341154_li13148164912599"><strong id="ucs_01_0150__en-us_topic_0000001199341154_b151552136536">Added from ConfigMap</strong>: Import all keys in a ConfigMap as environment variables.</li><li id="ucs_01_0150__en-us_topic_0000001199341154_li1855315291026"><strong id="ucs_01_0150__en-us_topic_0000001199341154_b5398577535">Added from ConfigMap key</strong>: Import a key in a ConfigMap as the value of an environment variable. For example, if you import <strong id="ucs_01_0150__b47601957162410">configmap_value</strong> of <strong id="ucs_01_0150__b1476015717240">configmap_key</strong> in ConfigMap <strong id="ucs_01_0150__b47611557192417">configmap-example</strong> as the value of environment variable <strong id="ucs_01_0150__b07619573246">key1</strong>, an environment variable named <strong id="ucs_01_0150__b137613571242">key1</strong> with its value <strong id="ucs_01_0150__b18761657162414">is configmap_value</strong> exists in the container.</li><li id="ucs_01_0150__en-us_topic_0000001199341154_li1727795616592"><strong id="ucs_01_0150__en-us_topic_0000001199341154_b675162614437">Added from secret</strong>: Import all keys in a secret as environment variables.</li><li id="ucs_01_0150__en-us_topic_0000001199341154_li93353201773"><strong id="ucs_01_0150__en-us_topic_0000001199341154_b0483141614480">Added from secret key</strong>: Import the value of a key in a secret as the value of an environment variable. For example, if you import <strong id="ucs_01_0150__en-us_topic_0000001199341154_b20412138105018">secret_value</strong> of <strong id="ucs_01_0150__en-us_topic_0000001199341154_b1248714112506">secret_key</strong> in secret <strong id="ucs_01_0150__en-us_topic_0000001199341154_b2010675411500">secret-example</strong> as the value of environment variable <strong id="ucs_01_0150__en-us_topic_0000001199341154_b1260612005113">key2</strong>, an environment variable named <strong id="ucs_01_0150__en-us_topic_0000001199341154_b2906162410511">key2</strong> with its value <strong id="ucs_01_0150__en-us_topic_0000001199341154_b26293438519">secret_value</strong> exists in the container.</li><li id="ucs_01_0150__en-us_topic_0000001199341154_li1749760535"><strong id="ucs_01_0150__en-us_topic_0000001199341154_b44091721105314">Variable Value/Reference</strong>: Use the field defined by a pod as the value of the environment variable, for example, the pod name.</li><li id="ucs_01_0150__en-us_topic_0000001199341154_li16129071317"><strong id="ucs_01_0150__en-us_topic_0000001199341154_b11429919184010">Resource Reference</strong>: Use the field defined by a container as the value of the environment variable, for example, the CPU limit of the container.</li></ul>
</div>
<div class="section" id="ucs_01_0150__en-us_topic_0000001199341154_section13829152011595"><h4 class="sectiontitle">Adding Environment Variables</h4><ol id="ucs_01_0150__en-us_topic_0000001199341154_ol4904646935"><li id="ucs_01_0150__en-us_topic_0000001199341154_li190412461831"><span>When creating a workload, select <strong id="ucs_01_0150__en-us_topic_0000001199341154_b23218253516">Environment Variables</strong> under <strong id="ucs_01_0150__en-us_topic_0000001199341154_b1763818524318">Container Settings</strong>.</span></li><li id="ucs_01_0150__en-us_topic_0000001199341154_li468251942720"><span>Set environment variables.</span></li></ol>
</div>
<div class="section" id="ucs_01_0150__en-us_topic_0000001199341154_section19591158201313"><h4 class="sectiontitle">YAML Example</h4><pre class="screen" id="ucs_01_0150__en-us_topic_0000001199341154_screen1034117614147">apiVersion: apps/v1
kind: Deployment
metadata:
name: env-example
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: env-example
template:
metadata:
labels:
app: env-example
spec:
containers:
- name: container-1
image: nginx:alpine
imagePullPolicy: Always
resources:
requests:
cpu: 250m
memory: 512Mi
limits:
cpu: 250m
memory: 512Mi
env:
- name: key # Custom
value: value
- name: key1 # Added from ConfigMap key
valueFrom:
configMapKeyRef:
name: configmap-example
key: key1
- name: key2 # Added from secret key
valueFrom:
secretKeyRef:
name: secret-example
key: key2
- name: key3 # Variable reference, which uses the field defined by a pod as the value of the environment variable.
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.name
- name: key4 # Resource reference, which uses the field defined by a container as the value of the environment variable.
valueFrom:
resourceFieldRef:
containerName: container1
resource: limits.cpu
divisor: 1
envFrom:
- configMapRef: # Added from ConfigMap
name: configmap-example
- secretRef: # Added from secret
name: secret-example
imagePullSecrets:
- name: default-secret</pre>
</div>
<div class="section" id="ucs_01_0150__en-us_topic_0000001199341154_section74514162911"><h4 class="sectiontitle">Viewing Environment Variables</h4><p id="ucs_01_0150__en-us_topic_0000001199341154_p67543412911">If the contents of <strong id="ucs_01_0150__en-us_topic_0000001199341154_b173111319716">configmap-example</strong> and <strong id="ucs_01_0150__en-us_topic_0000001199341154_b1469452310710">secret-example</strong> are as follows:</p>
<pre class="screen" id="ucs_01_0150__en-us_topic_0000001199341154_screen11127515162414">$ kubectl get configmap configmap-example -oyaml
apiVersion: v1
data:
configmap_key: configmap_value
kind: ConfigMap
...
$ kubectl get secret secret-example -oyaml
apiVersion: v1
data:
secret_key: c2VjcmV0X3ZhbHVl # <strong id="ucs_01_0150__en-us_topic_0000001199341154_b197355321293">c2VjcmV0X3ZhbHVl</strong> is the value of secret_value in Base64 mode.
kind: Secret
...</pre>
<p id="ucs_01_0150__en-us_topic_0000001199341154_p1573082017305">The environment variables in the pod are as follows:</p>
<pre class="screen" id="ucs_01_0150__en-us_topic_0000001199341154_screen21636141912">$ kubectl get pod
NAME READY STATUS RESTARTS AGE
env-example-695b759569-lx9jp 1/1 Running 0 17m
$ kubectl exec env-example-695b759569-lx9jp -- printenv
/ # env
key=value # Custom environment variable
key1=configmap_value # Added from ConfigMap key
key2=secret_value # Added from secret key
key3=env-example-695b759569-lx9jp # metadata.name defined by the pod
key4=1 # limits.cpu defined by container1. The value is rounded up, in unit of cores.
configmap_key=configmap_value # Added from ConfigMap. The key value in the original ConfigMap key is directly imported.
secret_key=secret_value # Added from key. The key value in the original secret is directly imported.</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="ucs_01_0105.html">Workload Management</a></div>
</div>
</div>