Sub topic 3
Creating ConfigMaps
This chapter assumes you can use kubectl, read YAML manifests, and work within a namespace. It covers three ways to create a ConfigMap: from a manifest, from literal values, and from files.
What you are creating
A ConfigMap stores non-secret configuration as key-value pairs. Every value under data is a string. A key can hold a short value such as a log level, or a whole file such as a properties file.
From a manifest
A manifest is the declarative approach. You can keep it in version control and apply it repeatedly.
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
LOG_LEVEL: "info"
app.properties: |
cache.ttl=60
cache.size=100Quote values like "info", and especially numbers and booleans, so YAML does not turn them into non-string types. The | after app.properties: starts a multi-line string. The key then holds the full text, with line breaks preserved.
Apply it with kubectl apply -f configmap.yaml.
From the command line
kubectl create configmap builds the object without writing YAML. This is useful for quick experiments and for turning existing files into ConfigMaps.
cat > app.properties <<'EOF'
cache.ttl=60
cache.size=100
EOF
kubectl create configmap app-config \
--from-literal=LOG_LEVEL=info \
--from-file=app.propertiesThe two flags behave differently:
| Flag | Key | Value |
|---|---|---|
| `--from-literal=K=V` | `K` | `V` |
| `--from-file=path` | The file's base name | The file's contents |
| `--from-file=key=path` | `key` | The file's contents |
| `--from-env-file=path` | Each `KEY=value` line | The value on that line |
You can repeat any flag, and you can mix them in one command. Use --from-env-file when the file is a list of environment variables. Use --from-file when the application reads the file as a whole.
Inspecting the result
Check what the API server stored:
kubectl get configmap app-config -o yamlThe output matches the manifest above, plus metadata such as uid and creationTimestamp. If the key names are not what you expected, fix them now. Pods refer to these names later.
Rules to remember
- Key names may contain only letters, digits,
-,_, and.. A file namedmy config.txtfails as a key. - The total size of a ConfigMap is limited to 1 MiB. Larger data belongs in a volume or an external store.
- A ConfigMap is namespaced. A Pod can only reference ConfigMaps in its own namespace.
- Binary content goes under
binaryDataas base64.--from-fileplaces it there automatically when the file is not valid UTF-8.
Creating versus updating
kubectl create fails if the name already exists. To update from files, pipe a dry run into apply:
kubectl create configmap app-config \
--from-literal=LOG_LEVEL=debug \
--from-file=app.properties \
--dry-run=client -o yaml | kubectl apply -f -This replaces the stored data with the data you gave. Running Pods do not all pick up the change in the same way, which a later chapter covers.
Exercise
You run kubectl create configmap web --from-file=conf/nginx.conf. What is the key name in the ConfigMap, and how would you make the key site.conf instead?
Show answer
The key is nginx.conf, the file's base name. Use --from-file=site.conf=conf/nginx.conf to set the key explicitly.