Config, secrets and storage in Kubernetes
About this lesson
A good container image is the same in testing and in production. What changes between them lives outside the image. Kubernetes keeps plain settings in a ConfigMap, passwords and keys in a Secret, and data that must outlive a Pod on a PersistentVolume, reached through a PersistentVolumeClaim. Follow a six-line Python app as it reads a setting, reads a password, writes to a volume, loses its Pod without losing its data, and picks up a changed setting after a restart. Each new Pod adds one line to the same file.
Keep the image the same
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.
one image, everywhere
settings → ConfigMap
passwords, keys → Secret
data that lasts → PersistentVolume
(through a claim)Read the lesson step by step
Keep the image the same
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.
one image, everywhere settings → ConfigMap passwords, keys → Secret data that lasts → PersistentVolume (through a claim)A ConfigMap holds settings
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.
kubectl create configmap app-config --from-literal=GREETING=Hello ConfigMap app-config GREETING: Hello plain, non-secret settings · up to 1 MiBSettings arrive as variables
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.
env: - name: GREETING valueFrom: configMapKeyRef: name: app-config key: GREETING greeting = os.environ["GREETING"] → greeting = "Hello"A Secret holds a password
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.
kubectl create secret generic db --from-literal=password=s3cr3t Secret db password: czNjcjN0 (base64 of "s3cr3t") base64 hides nothing: anyone can decode it by default stored unencrypted in etcdThe Secret becomes a file
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.
volumes: - name: db secret: secretName: db volumeMounts: - name: db mountPath: /etc/db readOnly: true password = open("/etc/db/password").read() → password = "s3cr3t" (decoded for you)Claim a lasting volume
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.
kind: PersistentVolumeClaim spec: accessModes: [ReadWriteOnce] resources: requests: storage: 1Gi claim data ⇄ PersistentVolume (1Gi) ReadWriteOnce: read-write from one nodeWrite to the volume
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.
volumes: - name: data persistentVolumeClaim: claimName: data volumeMounts: - name: data mountPath: /data with open("/data/visits.txt", "a") as f: f.write(greeting + "\n") /data/visits.txt: HelloThe Pod goes, the data stays
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.
kubectl delete pod web-1 web-1 deleted (its container files are lost) web-2 started by the Deployment claim data → the same volume runs the app once: appends a line /data/visits.txt: Hello ✓ still there Hello (from web-2)Change a setting, restart
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.
kubectl edit configmap app-config (GREETING: Salaam) kubectl rollout restart deployment/web env vars are not updated in a running Pod (a ConfigMap mounted as files is, after a delay) web-3: greeting = "Salaam" /data/visits.txt: Hello / Hello / SalaamThree kinds of outside
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.
ConfigMap env vars or files · plain Secret env vars or files · sensitive PVC → PV a mounted folder · lasts the image stays the same everywhere
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Which Kubernetes object should hold a plain, non-secret setting such as GREETING=Hello?
A ConfigMap stores non-confidential settings as key-value pairs, so the same image can run with different settings in different places.
02A Secret stores the password s3cr3t as czNjcjN0. What does that encoding give you?
Base64 only turns bytes into safe text. By default Secrets are stored unencrypted in etcd, so you protect them with encryption at rest and access rules (RBAC).
03You edit a ConfigMap that a running Pod uses as an environment variable. What happens?
Environment variables are set when a container starts. ConfigMaps used as environment variables are not updated automatically and need a Pod restart. ConfigMaps mounted as files are updated after a delay.
04A Pod wrote a file to /data, which is a PersistentVolume mounted through a claim. The Pod is deleted and its Deployment starts a new one. Where is the file that the old Pod wrote?
Files inside the container itself are lost, but the volume lives outside the Pod. The claim binds to the same PersistentVolume, so the new Pod finds the old file (and can add to it).
05What does the ReadWriteOnce access mode mean?
ReadWriteOnce limits read-write mounting to one node. Several Pods on that same node can still use it; ReadWriteOncePod limits it to a single Pod.
Glossary
- ConfigMap
- A Kubernetes object that stores non-confidential settings as key-value pairs, up to 1 MiB. Pods use it as environment variables, command-line arguments or files.
- Secret
- A Kubernetes object for small sensitive values such as passwords, tokens and keys. Its data values are base64-encoded, and by default it is stored unencrypted in etcd.
- Environment variable
- A named value given to a program when it starts. In Python you read one with os.environ["NAME"].
- Base64
- A way to write any bytes as plain text characters. It is an encoding, not encryption: anyone can decode it.
- etcd
- The key-value database where the Kubernetes API server keeps every object, including ConfigMaps and Secrets.
- PersistentVolume (PV)
- A piece of storage in the cluster, created by an administrator or automatically by a StorageClass, that lives independently of any Pod.
- PersistentVolumeClaim (PVC)
- A request for storage (a size and an access mode). Kubernetes binds it one to one to a matching PersistentVolume, which a Pod then mounts.
- Rollout restart
- kubectl rollout restart deployment/<name> replaces a Deployment's Pods with new ones, which read their settings afresh.
Notes and sources
- 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.
- 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.
- A ConfigMap mounted as files is updated in a running Pod after a delay (the kubelet sync period plus cache delay); one used as environment variables is not, and needs a Pod restart.
- ReadWriteOnce means read-write from one node, not one Pod. If the replacement Pod lands on another node, the volume must detach and attach there first, which can take a while; during a rollout the new Pod can even be stuck on a Multi-Attach error while the old Pod still holds the volume. The Recreate strategy avoids that by stopping the old Pod before starting the new one.
- The code card is real Python 3. In the lesson test it runs once per Pod start, with GREETING set and the two paths pointed at a temporary folder (the last line, which keeps the app running, is left out there so the test ends); the password would be used to connect to a database.
- The app runs its lines once when the container starts, then waits forever on line 6. That matters: a Deployment's Pods use restartPolicy Always, so a script that exited, even successfully, would be restarted again and again, adding another line each time. A real app would serve requests here instead of sleeping.
- In most clusters a StorageClass creates the PersistentVolume automatically when the claim is made (dynamic provisioning); an administrator can also create volumes by hand.
- Kubernetes docs: ConfigMaps
- Kubernetes docs: Secrets
- Kubernetes API reference: Secret (v1)
- Kubernetes docs: Managing Secrets using kubectl
- Kubernetes docs: Volumes
- Kubernetes docs: Persistent Volumes
- kubectl reference: kubectl create configmap
- kubectl reference: kubectl rollout restart
- Python docs: os.environ
- RFC 4648: The Base16, Base32, and Base64 Data Encodings
Quiz Unlocks after the last step.