Environment Variables
Part of the Introduction to Kubernetes section of Coddy's Terminal journey. Lesson 28 of 45.
A container reads its configuration from environment variables. In a manifest they are listed under the container's env, each with a name and a value:
containers:
- name: app
image: busybox
env:
- name: GREETING
value: helloInstead of a fixed value, a variable can take its value from a ConfigMap key with valueFrom:
env:
- name: COLOR
valueFrom:
configMapKeyRef:
name: app-config
key: COLORThe same works for Secrets with secretKeyRef. The ConfigMap must exist in the same namespace as the Pod.
Check what the container sees by running env inside it:
kubectl exec app -- envBoth GREETING=hello and COLOR=blue appear in the list.
Challenge
MediumCreate a ConfigMap named app-config with COLOR set to blue. Then open pod.yaml from the file tree and add an environment variable COLOR that takes its value from the COLOR key of app-config, save it, apply it, and run env inside the app Pod to confirm COLOR=blue.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction to Kubernetes
Practice on your own: Terminal playground