Skip to main content
In this lesson you’ll learn how to set environment variables for containers running in Kubernetes. We’ll compare the familiar Docker runtime approach with the Kubernetes manifest approach, show how to provide literal values, and explain how to reference external sources such as ConfigMaps and Secrets.
A presentation slide titled "Environment Variables" with a code/HTML icon set against a dark blue curved background on the right. A small "© Copyright KodeKloud" appears in the bottom-left.
Quick comparison — Docker vs Kubernetes:
In Kubernetes, environment variables are declared under the container spec using the env field. The env field is an array where each item is an object containing:
  • name: — the environment variable name
  • either value: — a literal string, or valueFrom: — reference to an external source
Here is a simple Pod manifest that sets APP_COLOR as a literal string using value:
Key structure notes:
  • env is a list under the container definition.
  • Each list item must include name: and either value: or valueFrom:.
  • Values provided with value: are literal strings that appear in the container’s environment.
When not hard-coding values in the manifest, you can reference cluster objects using valueFrom. The most common options are:
  • configMapKeyRef — reads a key from an existing ConfigMap
  • secretKeyRef — reads a key from an existing Secret
Example: reference a key from a ConfigMap
Example: reference a key from a Secret
Quick reference: env options
When using configMapKeyRef or secretKeyRef, the referenced ConfigMap or Secret must already exist in the same namespace as the Pod. Use kubectl create configmap or kubectl create secret (or your CI/CD tooling) to provision these resources before deploying the Pod.
Do not store sensitive data in ConfigMaps. Use Kubernetes Secrets for sensitive information and follow your cluster’s secret management best practices (encryption at rest, RBAC, and restricted access).
Additional resources and references: That’s it for this lesson — you now know how to set environment variables in Kubernetes using literal values, ConfigMaps, and Secrets.

Watch Video