Environment variables precedence in Kubernetes Pod


Context

The Kubernetes Pod specification allows defining environment variables through the use of the env and envFrom fields. These variables are then accessible at runtime to the processes running in the container, alongside the variables declared in the image. If the same variable is defined in multiple sources, then the order of precedence determines what the environment variable value will be during container execution.

How to

The order of precedence for environment variables is as follows:

  1. Keys in env field
  2. Keys in envFrom field
  3. Keys defined in the container image

If there are multiple occurrences of a variable in either env or envFrom fields, then the last key occurrence within each block takes precedence.

References

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.