Configuring Environment Variables
Scenario
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.
The function of setting environment variables on CCE is the same as that of specifying ENV in a Dockerfile.
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.
Configurations must be imported to a container as arguments. Otherwise, configurations will be lost after the container restarts.
Environment variables can be set in the following modes:
- Custom: Enter the environment variable name and parameter value.
- Added from ConfigMap: Import all key values in a ConfigMap as environment variables.
- Added from ConfigMap key: Import the value of a key in a ConfigMap as the value of an environment variable. As shown in Figure 1, if you import configmap_value of configmap_key in configmap-example as the value of environment variable key1, an environment variable named key1 whose value is configmap_value is available in the container.
- Added from secret: Import all key values in a secret as environment variables.
- Added from secret key: Import the value of a key in a secret as the value of an environment variable. As shown in Figure 1, if you import secret_value of secret_key in secret-example as the value of environment variable key2, an environment variable named key2 whose value is secret_value is available in the container.
- Variable Value/Reference: Use the field defined by a pod as the value of the environment variable. As shown in Figure 1, if the pod name is imported as the value of environment variable key3, an environment variable named key3 whose value is the pod name is available in the container.
- Resource Reference: The value of Request or Limit defined by the container is used as the value of the environment variable. As shown in Figure 1, if you import the CPU limit of container-1 as the value of environment variable key4, an environment variable named key4 whose value is the CPU limit of container-1 is available in the container.
Adding Environment Variables
- Log in to the CCE console.
- Click the cluster name to go to the cluster console, choose Workloads in the navigation pane, and click the Create Workload in the upper right corner.
- When creating a workload, modify the container information in Container Settings and click the Environment Variables tab.
- Configure environment variables.
- To add environment variables one by one, click Adding a Variable and configure its parameters.
- To add environment variables in batches, click Editing Custom Variables in Batches. Then, in the displayed dialog box, enter environment variables in the format of "Variable name=Variable or variable reference".
Figure 1 Configuring environment variables
YAML Example
apiVersion: apps/v1kind: Deploymentmetadata:name: env-examplenamespace: defaultspec:replicas: 1selector:matchLabels:app: env-exampletemplate:metadata:labels:app: env-examplespec:containers:- name: container-1image: nginx:alpineimagePullPolicy: Alwaysresources:requests:cpu: 250mmemory: 512Milimits:cpu: 250mmemory: 512Mienv:- name: key # Customvalue: value- name: key1 # Added from ConfigMap keyvalueFrom:configMapKeyRef:name: configmap-examplekey: configmap_key- name: key2 # Added from secret keyvalueFrom:secretKeyRef:name: secret-examplekey: secret_key- name: key3 # Variable reference, which uses the field defined by a pod as the value of the environment variable.valueFrom:fieldRef:apiVersion: v1fieldPath: metadata.name- name: key4 # Resource reference, which uses the field defined by a container as the value of the environment variable.valueFrom:resourceFieldRef:containerName: container1resource: limits.cpudivisor: 1envFrom:- configMapRef: # Added from ConfigMapname: configmap-example- secretRef: # Added from secretname: secret-exampleimagePullSecrets:- name: default-secret
Viewing Environment Variables
If the contents of configmap-example and secret-example are as follows:
$ kubectl get configmap configmap-example -oyamlapiVersion: v1data:configmap_key: configmap_valuekind: ConfigMap...$ kubectl get secret secret-example -oyamlapiVersion: v1data:secret_key: c2VjcmV0X3ZhbHVl # c2VjcmV0X3ZhbHVl is the value of secret_value in Base64 mode.kind: Secret...
The environment variables in the pod are as follows:
$ kubectl get podNAME READY STATUS RESTARTS AGEenv-example-695b759569-lx9jp 1/1 Running 0 17m$ kubectl exec env-example-695b759569-lx9jp -- printenv/ # envkey=value # Custom environment variablekey1=configmap_value # Added from ConfigMap keykey2=secret_value # Added from secret keykey3=env-example-695b759569-lx9jp # metadata.name defined by the podkey4=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.
- Scenario
- Adding Environment Variables
- YAML Example
- Viewing Environment Variables