Kubernetes on configuration and storage
ConfigMaps keep settings out of the image
- A ConfigMap holds non-secret settings as key-value pairs, up to 1 MiB.
- A Pod reads them as environment variables, or as files in a mounted volume.
- Mounted files update after a change. Environment variables need a Pod restart.
Secrets are not secret by default
- A Secret holds passwords, tokens and keys, and is used the same way as a ConfigMap.
- Values are base64-encoded. That is not encryption, and anyone can decode it.
- By default etcd stores Secrets unencrypted. Turn on encryption at rest.
- Limit who can read Secrets with access rules, or load them from an external secret store.
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef: {name: db, key: password}Files written inside a container do not last
- When a container restarts, it starts from a clean state. Files it wrote are gone.
- A volume is a directory mounted into the Pod. Its type decides how long the data lives.
- An emptyDir volume is scratch space that survives container restarts, but not the Pod.
Persistent volumes outlive Pods
- A PersistentVolumeClaim (PVC) is a request for storage: 20 GiB, read-write.
- A StorageClass says how to create storage, such as a fast cloud disk.
- The cluster creates a PersistentVolume, the real disk, and binds it to the claim.
- The Pod mounts the claim. When the Pod is replaced, the data is still there.
- Podmounts the claim
- PersistentVolumeClaimasks for 20 GiB
- StorageClassknows how to make a disk
- PersistentVolumethe real disk
Access mode and reclaim policy protect your data
- ReadWriteOnce lets one node mount the volume read-write. ReadWriteMany lets many nodes.
- ReadWriteOncePod allows only a single Pod in the whole cluster to use the volume.
- Reclaim policy Delete removes the disk when the claim is deleted. Retain keeps it.
- A StorageClass defaults to Delete. Choose Retain for data you cannot lose.
A StatefulSet gives each Pod its own volume
- Its volumeClaimTemplates field creates one claim per Pod, not one shared claim.
- Pod db-0 keeps the same name and gets the same volume back after every restart.
- Deleting the StatefulSet or its Pods does not delete the claims.