-e flag but offers tighter integration with Kubernetes primitives like ConfigMaps and Secrets.
Why Use Environment Variables?
- Decouple configuration from container images
- Simplify application customization across environments
- Secure sensitive data using Secrets
Prerequisites
- A running Kubernetes cluster
kubectlconfigured against your cluster- Basic knowledge of Pods and YAML manifests
1. Direct Assignment with value
To set an environment variable directly in your Pod spec, use the env array under the container definition:
APP_COLOR=pink available inside the container.
Direct assignment is ideal for non-sensitive, static configuration values.
2. Decoupling Config with valueFrom
Rather than embedding values in your Pod spec, you can reference external sources:
Secrets are only base64-encoded by default and not encrypted at rest. Use encryption providers or external secret stores for stronger security.
3. Comparison at a Glance
| Configuration Source | Field | When to Use |
|---|---|---|
| Direct | value | Static, non-sensitive values |
| ConfigMap | valueFrom.configMapKeyRef | Application settings shared across Pods |
| Secret | valueFrom.secretKeyRef | Sensitive data (passwords, tokens, certificates) |