What is a Pod Environment Variable
An environment variable is just a key-value pair that you pass into a container at runtime, so the application inside can read it without you hardcoding values in code or image.
In Kubernetes, Pods support environment variables through the container spec:
env:
- name: DB_USER
value: admin
echo $DB_USER
# admin
2. Why use environment variables in Pods?
They help you:
-
Make apps configurable without rebuilding the image
→ You can changeDB_HOSTorAPI_KEYby updating YAML, no code change. -
Separate secrets & configs from code
→ Avoid hardcoding credentials in the container image. -
Control behavior for different environments (dev, staging, prod)
→ Same image, different environment variables. -
Read runtime data from Kubernetes
→ Pull Pod metadata (name, namespace, IP, etc.) into your app automatically.
3. Ways to set Environment Variables in Pods
A. Hardcoded values
Good for small static values.
B. From a ConfigMap
Best for non-secret config that you might reuse across multiple Pods.
Example ConfigMap:
C. From a Secret
Best for passwords, API tokens.
Example Secret:
Comments
Post a Comment