ConfigMap YAML
- Docker & Kubernetes course
- Intermediate
- Lesson 44/82
- 4 min
Learn the ConfigMap YAML structure.
ConfigMap YAML
A ConfigMap YAML has no spec; the data is written directly under data. Values are always text; numbers and values like true may need quoting. Multi-line file content is written with |:
apiVersion: v1
kind: ConfigMap
metadata:
name: api-ayar
data:
LOG_LEVEL: info
DB_HOST: db
ayar.yaml: |
sayfa_boyutu: 20
onbellek: true
A ConfigMap from files
To produce a ConfigMap from existing config files, --from-file is used; the key becomes the file name and the value its contents. --from-env-file turns NAME=value lines into separate keys:
kubectl create configmap nginx-ayar --from-file=nginx.conf
kubectl create configmap api-ayar --from-env-file=api.env
kubectl create configmap api-ayar --from-env-file=api.env --dry-run=client -o yaml > cm.yamlPractice Write the ConfigMap YAML
Write a ConfigMap YAML: name api-ayar; under data, LOG_LEVEL: info, DB_HOST: db and a multi-line ayar.yaml key with | (containing the line sayfa_boyutu: 20).
Starter code
# buraya yazSample solution
apiVersion: v1
kind: ConfigMap
metadata:
name: api-ayar
data:
LOG_LEVEL: info
DB_HOST: db
ayar.yaml: |
sayfa_boyutu: 20
onbellek: trueExpected output
configmap/api-ayar created
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why can writing PORT: 8080 under data cause an error?
- Keys must be lowercase
- It doesn't error
- YAML reads it as a number; ConfigMap values must be strings Correct answer
- 8080 is a forbidden port
Explanation: You write PORT: "8080".
2 Write multi-line content.
data:
ayar.yaml: ____
sayfa_boyutu: 20
data:
ayar.yaml: |
sayfa_boyutu: 20
Explanation: | keeps the line breaks.
3 Match.
| Term | Meaning |
|---|---|
data |
String values |
| |
Multi-line block |
--from-file |
File name becomes the key |
--from-env-file |
NAME=value lines |