Skip to main content
Kubernetes lets you inject configuration data into Pods via environment variables. This declarative approach mirrors Docker’s -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
  • kubectl configured 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:
This makes APP_COLOR=pink available inside the container.
Direct assignment is ideal for non-sensitive, static configuration values.
You can replicate this behavior in Docker with:

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 SourceFieldWhen to Use
DirectvalueStatic, non-sensitive values
ConfigMapvalueFrom.configMapKeyRefApplication settings shared across Pods
SecretvalueFrom.secretKeyRefSensitive data (passwords, tokens, certificates)

4. References

Watch Video