One of the best practices in Kubernetes is to separate application code from configuration information. One way to do this is to use the configMap concept introduced in Kubernetes 1.2.

https://kubernetes.io/blog/20…

A configMap is essentially a series of key-value pairs stored in an ETCD. The etCD website says:

etcd is a distributed key-value store designed to reliably and quickly preserve and provide access to critical data.

https://github.com/etcd-io/et…

Etcd is a high-performance distributed key-value pair repository for storing and accessing critical data.

Create a Kubernetes config map using the following command line:

kubectl create configmap test-config –from-literal=test.type=unit –from-literal=test.exec=always

Create a key-value pair named test-config with key as test.type, value as unit, key as test.exec, and value as always.

Next I’m going to create a POD to consume this configMap named test-config.

Create a YAML file with the following contents:

apiVersion: v1 kind: Pod metadata: name: test-configmap spec: containers: - name: test-container image: Alpine :3.8 command: ["/bin/sh", "-c", "env"] env: -name: TEST_TYPE valueFrom: configMapKeyRef: name: TEST_TYPE valueFrom: configMapKeyRef: name: test-config key: test.type - name: TEST_EXEC valueFrom: configMapKeyRef: name: test-config key: test.exec restartPolicy: Never

This YAMl file defines pod based on the Docker image alpine and runs the shell command /bin/sh -c env to view environment variables.

In the env area, I inject the POD with an environment variable named TEST_TYPE, which is taken from the configMap key-value pair’s value named test.type.

Kubectl create -f creates this pod:

Kubectl logs test-configmap: TEST_TYPE=unit (TEST_TYPE=unit) Unit comes from the test-config value unit in configMap.

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: