Directly Defining Environment Variables
When creating a Pod, you can directly assign environment variables using theenv property in the container specification. The env property is an array where each variable is defined with a name and value. For example, if you would run a Docker container with an environment variable like this:
APP_COLOR is directly set to pink.
Direct assignment is a straightforward approach, ideal for simple scenarios or development environments.
Managing Environment Variables with ConfigMaps and Secrets
For more dynamic configuration management, you can externalize environment variable data using ConfigMaps or Secrets. Instead of hardcoding a value, you reference the value using thevalueFrom field. This helps decouple configuration from application code and allows a more secure and manageable configuration.
Consider the previous direct definition:
APP_COLOR from a ConfigMap named my-config instead of hardcoding it in the manifest.
Using ConfigMaps or Secrets is recommended in production environments to manage sensitive data and configuration changes without modifying the application code.
Comparison of Environment Variable Methods
| Method | Description | Use Case |
|---|---|---|
| Direct Assignment | Environment variable is hardcoded in the Pod manifest. | Simple setups or development. |
| ConfigMap Reference | Environment variable value is sourced from a ConfigMap. | Dynamic configuration management. |
| Secret Reference | Environment variable value is sourced from a Secret. | Managing sensitive data securely. |