[k8s] ConfigMap

tags: kubernetes application lifecycle management

1⃣ ConfigMap 特性

ConfigMap 通常都是用來存放偏向部署面的設定檔,像是資料庫的初始化設定、nginx 設定檔等等

# game.properties enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 # ui.properties color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice

⭐ ConfigMaps are used as environment variables

🔆 how to create

from property file

kubectl create configmap game-config \ --from-file=./game.properties \ --from-file=./ui.properties \ -n playground

from literal value

kubectl create configmap special-config \ --from-literal=special.how=very \ --from-literal=special.type=charm \ -n playground

from yaml

apiVersion: v1 kind: ConfigMap metadata: name: special-config namespace: default data: special.how: very --- apiVersion: v1 kind: ConfigMap metadata: name: env-config namespace: default data: LOG_LEVEL: INFO SPECIAL_LEVEL: very SPECIAL_TYPE: charm
kubectl get configmap -n playground kubectl describe configmap game-config -n playground kubectl delete configmap game-config -n playground

🔆 how to use

fetch value of specific key

apiVersion: v1 kind: Pod metadata: namespace: playground name: dapi-test-pod spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "env" ] env: - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: special-config key: special.how

fetch values of all keys

apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "env" ] envFrom: - configMapRef: name: special-config

⭐ ConfigMaps are used as files

🔆 how to create

from yaml

apiVersion: v1 kind: ConfigMap metadata: name: mosquitto-config-file data: mosquitto.conf: | log_des stdout log_type all log_timestamp true listener 9001

🔆 how to use

  1. volumes mounted into the pod
  2. mount volumes into container
apiVersion: v1 kind: Pod metadata: name: mosquitto spec: containers: - name: mosquitto image: eclipse-mosquitto:1.6.2 ports: - containerPort: 1883 volumeMounts: - name: mosquitto-config mountPath: /mosquitto/config volumes: - name: mosquitto-config configMap: name: mosquitto-config-file

resource