Sub topic 1
Why configuration lives outside the image
Prerequisites
This chapter assumes you can:
- Run basic
kubectlcommands. - Read and write YAML manifests.
- Describe what a Pod and a Deployment are.
- Work with namespaces.
- Explain how environment variables and files work inside a container.
The problem with baked-in settings
A container image is a snapshot of your application and its dependencies. If you put settings inside that snapshot, such as a database host, a log level, or a feature flag, three problems follow.
- One image per environment. Development, staging, and production each need different values. Baking values in means you build and test a different image for each one. The image you tested is then not the image you ship.
- Slow changes. Changing a log level means rebuilding, pushing, and redeploying an image.
- Leaked secrets. Anyone who can pull the image can read what is inside its layers. Deleting a password in a later layer does not remove it from earlier layers.
The fix is to build the image once and supply the settings when the Pod starts.
Where Kubernetes keeps configuration
Kubernetes provides two API objects for this purpose. Both live in a namespace and are created and read through the API server, like any other resource.
| Object | Holds | Typical content |
|---|---|---|
| ConfigMap | Non-sensitive settings | Log level, feature flags, config files |
| Secret | Sensitive values | Passwords, tokens, TLS keys |
A Pod references these objects by name. The kubelet (the agent on each node) reads them and hands the values to the container at start time. The image stays the same in every environment. Only the referenced objects differ.
Benefits you get
- Portability. The same image runs on a laptop cluster and in production.
- Independent lifecycles. You can change configuration without rebuilding code. Some changes still need a Pod restart, which later chapters cover.
- Separate permissions. RBAC (role-based access control) can let a team edit ConfigMaps but not read Secrets.
- Reviewable changes. Manifests live in version control, so configuration changes get the same review as code.
A quick look
The pattern is the same for every setting. This Pod reads its log level from a ConfigMap named app-config instead of from the image.
apiVersion: v1
kind: Pod
metadata:
name: demo
spec:
containers:
- name: demo
image: busybox:1.36
command: ["sh", "-c", "echo level=$LOG_LEVEL && sleep 3600"]
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVELThe image has no LOG_LEVEL value of its own. If app-config does not exist in the same namespace, the Pod fails to start with a CreateContainerConfigError. Later chapters cover how to diagnose that.
What externalizing does not solve
Moving a value out of the image does not make it safe. A Secret is still only base64-encoded by default, and anyone with read access to it can decode it. Later chapters cover RBAC, encryption at rest, and external secret managers, which close that gap.
Exercise
A team builds one image per environment, each with its own database host compiled in. Name two problems with this approach and the Kubernetes feature that addresses them.
Show answer
The image you test is not the image you deploy, and every settings change needs a rebuild and redeploy. Anyone who can pull the image can also read any credentials inside it. ConfigMaps (for non-sensitive settings) and Secrets (for sensitive values) let you build one image and supply the settings per environment at Pod start.