CodeBlanks Logo Icon CodeBlanks

ConfigMap YAML

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.yaml

Practice 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

terminal
# buraya yaz

Sample solution

terminal
apiVersion: v1
kind: ConfigMap
metadata:
  name: api-ayar
data:
  LOG_LEVEL: info
  DB_HOST: db
  ayar.yaml: |
    sayfa_boyutu: 20
    onbellek: true

Expected 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.

Question
data:
  ayar.yaml: ____
    sayfa_boyutu: 20
Answer
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

All lessons in Intermediate