AI Hints
Home›Kubernetes Tutorial›Configuration and Secrets
Kubernetes · Lesson 27 of 86

Using Configuration

Environment variables and files.

⏱ 2 min read▶ Video at 1:06:45🎓 Free
Watch this lesson in the Kubernetes courseWatch on YouTube ↗Full Kubernetes course

Creating a config map is only half the job. Your app must actually receive the settings. Kubernetes offers two ways, environment variables, and files. Each has its place, and an important difference when settings change. In this lesson, you will use both, and see that difference live.

Using Configuration
From the video: Using Configuration
  • Two ways: variables or files
  • Both come from the same objects
  • Updates behave differently

A pod that uses both

Here is a pod that uses both objects. Under env from, we load every key of the config map, as environment variables, with one short line. Under env, we pick a single value from a secret, and call it D B password. Under volume mounts and volumes, we also mount the config map as a folder, at slash etc slash config, where each key becomes a file. The same data, available in two ways.

app.yamlyaml
apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
  - name: app
    image: busybox:1.36
    command: ["sleep", "3600"]
    envFrom:
    - configMapRef:
        name: app-config
    env:
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: db-pass
          key: password
    volumeMounts:
    - name: cfg
      mountPath: /etc/config
  volumes:
  - name: cfg
    configMap:
      name: app-config

Variables and files

Let's see it from the inside. We apply the pod, and list its environment. The greeting and the colour from the config map appear, and the password from the secret. Now look at the folder. There is one file for each key. And reading the greeting file, shows the value hello. Your app can read settings either way, whichever is easier for it.

kubectl
kubectl apply -f app.yaml
pod/app created
kubectl exec app -- env | grep -E 'GREETING|COLOR|DB_PASSWORD'
COLOR=blue
DB_PASSWORD=S3cr3t-demo
GREETING=Hello
kubectl exec app -- ls /etc/config
COLOR
GREETING
kubectl exec app -- cat /etc/config/GREETING
Hello
File used: app.yaml
apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
  - name: app
    image: busybox:1.36
    command: ["sleep", "3600"]
    envFrom:
    - configMapRef:
        name: app-config
    env:
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: db-pass
          key: password
    volumeMounts:
    - name: cfg
      mountPath: /etc/config
  volumes:
  - name: cfg
    configMap:
      name: app-config

When the settings change

Now the important difference. We change the greeting in the config map, from hello to bonjour. After about a minute, the file inside the pod shows bonjour. Mounted files update by themselves. But the environment variable still says hello. Variables are read once, when the container starts, so a restart is needed to see the new value. Remember this rule, files refresh, variables do not, and plan your apps accordingly.

kubectl
kubectl patch configmap app-config -p '{"data":{"GREETING":"Bonjour"}}'
configmap/app-config patched
kubectl exec app -- cat /etc/config/GREETING
Bonjour
kubectl exec app -- env | grep GREETING
GREETING=Hello
File used: app.yaml
apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
  - name: app
    image: busybox:1.36
    command: ["sleep", "3600"]
    envFrom:
    - configMapRef:
        name: app-config
    env:
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: db-pass
          key: password
    volumeMounts:
    - name: cfg
      mountPath: /etc/config
  volumes:
  - name: cfg
    configMap:
      name: app-config

Summary

Wonderful. Settings reach pods as environment variables, or as mounted files. Files update on their own, variables need a restart. With config in place, next we tackle another big topic, keeping data safe when pods disappear, storage.

Key takeaways
  • envFrom and secretKeyRef give variables
  • Volumes give files
  • Files refresh, variables do not
Next up: Volumes — storage that outlives containers