Introduction
Today we will learn about ConfigMap Resource of Kubernetes. This is used to pass the configs to Kubernetes resources like Pod, Deployment etc.
ConfigMap
We will create a ConfigMap using yaml:
apiVersion: v1 kind: ConfigMap metadata: name: special-config namespace: default data: special.how: very log_level: INFO SPECIAL_LEVEL: very SPECIAL_TYPE: charm example.property.file: |- property.1=value-1 property.2=value-2 property.3=value-3
We will create the config from above yaml:
$ kubectl apply -f config.yaml configmap/special-config created
We will use this config in below pod:
apiVersion: v1 kind: Pod metadata: name: ashok-config-pod spec: containers: - name: ashok-config-pod image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "env" ] env: - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: special-config key: special.how - name: LOG_LEVEL valueFrom: configMapKeyRef: name: special-config key: log_level restartPolicy: Never
Let's apply above yaml:
$ kubectl apply -f pod.yaml pod/ashok-config-pod created
We can check the status of the pod:
$ kubectl get po NAME READY STATUS RESTARTS AGE ashok-config-pod 0/1 Completed 0 48s
We can see the status is completed for our pod. To check our configs passed as environment variables will be visible in the logs.
$ kubectl logs ashok-config-pod KUBERNETES_SERVICE_PORT=443 KUBERNETES_PORT=tcp://10.96.0.1:443 LOG_LEVEL=INFO <--- Our config from the ConfigMap Resource HOSTNAME=ashok-config-pod SHLVL=1 HOME=/root ASHOK_SVC_SERVICE_HOST=10.106.81.159 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 ASHOK_SVC_PORT=tcp://10.106.81.159:80 ASHOK_SVC_SERVICE_PORT=80 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_PROTO=tcp ASHOK_SVC_PORT_80_TCP_ADDR=10.106.81.159 SPECIAL_LEVEL_KEY=very <--- Our config from the ConfigMap Resource ASHOK_SVC_PORT_80_TCP_PORT=80 ASHOK_SVC_PORT_80_TCP_PROTO=tcp KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 PWD=/ KUBERNETES_SERVICE_HOST=10.96.0.1 ASHOK_SVC_PORT_80_TCP=tcp://10.106.81.159:80
We will use another pod yaml to see the example.property.file :
apiVersion: v1 kind: Pod metadata: name: ashok-config-pod spec: containers: - name: ashok-config-pod image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "ls /etc/config/" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: # Provide the name of the ConfigMap containing the files you want # to add to the container name: special-config restartPolicy: Never
Similarly you can check the configs in logs.
Happy Coding !!
No comments:
Post a Comment