πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Configuration beginner ⏱ 15 minutes K8s 1.28+

Kubernetes Environment Variables Guide

Set environment variables in Kubernetes pods from literals, ConfigMaps, Secrets, and the Downward API. Covers variable ordering, references, and best practices.

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: Set environment variables in Kubernetes pods from literals, ConfigMaps, Secrets, and the Downward API. Covers variable ordering, references, and best practices.

The Problem

This is one of the most searched Kubernetes topics. Having a comprehensive, well-structured guide helps both beginners and experienced users quickly find what they need.

The Solution

Set Environment Variables

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
    - name: app
      image: my-app:v1
      env:
        # Literal value
        - name: APP_ENV
          value: "production"
        
        # From ConfigMap
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: database_host
        
        # From Secret
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: password
        
        # From Downward API (pod metadata)
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        
        # Resource limits
        - name: MEMORY_LIMIT
          valueFrom:
            resourceFieldRef:
              containerName: app
              resource: limits.memory

      # Import ALL keys from ConfigMap/Secret
      envFrom:
        - configMapRef:
            name: app-config
        - secretRef:
            name: app-secrets
            optional: true    # Don't fail if secret doesn't exist

Variable References

env:
  - name: BASE_URL
    value: "https://api.example.com"
  - name: CALLBACK_URL
    value: "$(BASE_URL)/callback"    # References BASE_URL
# Check environment variables in a running pod
kubectl exec <pod> -- env | sort
kubectl exec <pod> -- printenv DB_HOST
graph TD
    A[Environment Variables] --> B[Literal values]
    A --> C[ConfigMap keys]
    A --> D[Secret keys]
    A --> E[Downward API]
    E --> F[Pod name, namespace, IP]
    E --> G[Node name]
    E --> H[Resource limits]

Frequently Asked Questions

Do environment variables update when ConfigMaps change?

No. Environment variables are set at pod startup and don’t change. You must restart the pod. Use volume mounts if you need auto-updating config.

What’s the order of env vs envFrom?

env entries override envFrom entries with the same key. Variables defined later in env override earlier ones.

Best Practices

  • Start simple β€” use the basic form first, add complexity as needed
  • Be consistent β€” follow naming conventions across your cluster
  • Document your choices β€” add annotations explaining why, not just what
  • Monitor and iterate β€” review configurations regularly

Key Takeaways

  • This is fundamental Kubernetes knowledge every engineer needs
  • Start with the simplest approach that solves your problem
  • Use kubectl explain and kubectl describe when unsure
  • Practice in a test cluster before applying to production
#environment-variables #env #configmap #secrets #downward-api #kubernetes
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens