The idea
Build one container image and run it everywhere. What differs between test and production, the settings, the passwords and the saved data, lives outside the image, in Kubernetes objects that the Pod plugs into.
Settings: ConfigMap
A ConfigMap is a Kubernetes object that stores plain settings as key-value pairs. The kubectl create configmap command makes one, named app-config, with one key: GREETING=Hello. It is not secret: anyone allowed to read it sees the values.
When the Pod starts, Kubernetes copies GREETING into the container as an environment variable. Line 2 reads it with os.environ. The image never mentions Hello, so the same image can greet differently elsewhere.
Passwords: Secret
A Secret holds small sensitive values, such as a database password. Its values are stored base64-encoded: s3cr3t becomes czNjcjN0. Base64 is not encryption. By default Secrets sit unencrypted in etcd, the cluster's database, so limit who can read them.
The Secret is mounted into the Pod as a file, /etc/db/password, held in memory (tmpfs) and never written to the node's disk. Line 3 opens it like any file. The password is never baked into the image.
Data that lasts
Files written inside a container are lost when it stops. For data that must last, the Pod uses a PersistentVolumeClaim asking for 1Gi, ReadWriteOnce. Kubernetes binds it, one to one, to a PersistentVolume: a piece of real storage.
The volume is mounted in the Pod at /data. Lines 4 and 5 open /data/visits.txt for appending and add the greeting. The file lives on the PersistentVolume, not inside the container, and now holds one line: Hello.
Now delete the Pod. Its Deployment starts a replacement, web-2, and the same claim mounts the same volume again. Files inside the old container are gone, but line 1 is still there, and web-2 runs the app and adds its own Hello.
Changing a setting
Edit the ConfigMap to GREETING=Salaam. Environment variables are read only when a container starts, so run a rollout restart. The new Pod, web-3, reads Salaam and appends it: the file holds all three lines. The image never changed.
Settings go in a ConfigMap, passwords and keys in a Secret, and data that must outlive a Pod on a PersistentVolume, reached through a claim. You change any of them without rebuilding the image.
What happens
;; step 1/10 · outside
one image, everywhere
settings → ConfigMap
passwords, keys → Secret
data that lasts → PersistentVolume
(through a claim)
Secrets are only safer than ConfigMaps if you set them up that way: turn on encryption at rest for etcd and limit with RBAC who can read Secrets. Anyone who can create a Pod in a namespace can read the Secrets in it. Pods made by a Deployment get generated names such as web-7c9d8f6b5-x2x7q. The figure calls them web-1, web-2 and web-3 to keep them readable. Strictly, the Deployment manages a ReplicaSet, and it is the ReplicaSet that starts a replacement Pod.