1. Several Ways for a Kubernetes Pod to Reference Environment Variables
1.1 Directly Key/Value
You can set the Value directly, and you can also use the current Pod’s information as the Value.
| |
1.2 Referencing from a Secret
There are two ways to reference variables in a Secret:
- Reference all variables in the Secret via envFrom
- Reference a specific variable in the Secret via valueFrom
| |
1.3 Referencing from a ConfigMap
There are two ways to reference variables in a ConfigMap:
- Reference all variables in the ConfigMap via envFrom
- Reference a specific variable in the ConfigMap via valueFrom
| |
2. Priority of Variable References
From the source code, the implementation logic is that a Map is initialized to hold the environment variables, and then processing proceeds as follows:
- Iterate in order over the Key/Value pairs of the ConfigMaps and Secrets referenced by envFrom
- Iterate in order over the Key/Value pairs set in env
- Since Pods enable EnableServiceLinks by default, Service-related variables must also be injected at the end
The priority is: Service variables > Env > EnvFrom, where for EnvFrom the later ones override the earlier ones.
Let me talk separately about the Service-related variables injected into the environment:
- Injection scope. All Services in the namespace where the Pod resides.
- Injected content. All service addresses, ports, and protocols under the same namespace.
- Injection format. Uppercase letters with underscores, for example
ADMIN_WEB_PC_TEST_PORT_80_TCP=tcp://10.233.45.183:80,ADMIN_WEB_PC_TEST_PORT=tcp://10.233.45.183:80,ADMIN_WEB_PC_TEST_PORT_80_TCP_ADDR=10.233.45.183,ADMIN_WEB_PC_TEST_PORT_80_TCP_PORT=80,ADMIN_WEB_PC_TEST_PORT_80_TCP_PROTO=tcp. If a large number of services are deployed under the same namespace, each Pod may accumulate several hundred such variables.
