DEV Community

Cheedge Lee
Cheedge Lee

Posted on • Originally published at Medium

Kubernetes ConfigMap

1. ConfigMap type

  1. property-like key
apiVersion: v1 kind: ConfigMap metadata: name: app-config data: api-url: "https://api.example.com" log-level: "debug" 
Enter fullscreen mode Exit fullscreen mode
  1. file-like key
apiVersion: v1 kind: ConfigMap metadata: name: app-config data: config.yaml: | apiVersion: v1 data: app-name: "MyApp" app-version: "1.0" 
Enter fullscreen mode Exit fullscreen mode

2. ConfigMap consume type

  1. Args
    • Inside a container command and args
    • samilar with the env var comsuming method
  2. Env var
    • Use it as environment variables for a container
    • normally used property-like key ConfigMap.
  3. Vol Mount
    • Add a file in read-only volume, for the application to read
    • can be used both property-like key and file-like ConfigMap
  4. Write code to run inside the Pod that uses the Kubernetes API to read a ConfigMap (not discuss it here)

3.1 Args

apiVersion: v1 kind: ConfigMap metadata: name: app-config data: my-param: "Hello, Kubernetes!" --- apiVersion: v1 kind: Pod metadata: name: app-pod spec: containers: - name: app-container image: busybox command: ["/bin/sh"] args: ["-c", "sleep 3600", "echo $(MY_PARAM)> log.txt"] env: # use it here same as env - name: MY_PARAM valueFrom: configMapKeyRef: name: app-config key: my-param 
Enter fullscreen mode Exit fullscreen mode

2.2 Env var

2.2.1 Use whole data

apiVersion: v1 kind: ConfigMap metadata: name: app-config data: api-url: "https://api.example.com" log-level: "debug" --- apiVersion: v1 kind: Pod metadata: name: app-pod spec: containers: - name: app-container image: busybox command: ["bin/sh", "-c", "sleep 3600"] envFrom: # use whole data value - configMapRef: name: app-config 
Enter fullscreen mode Exit fullscreen mode

2.2.2 Use some data

apiVersion: v1 kind: ConfigMap metadata: name: app-config data: api-url: "https://api.example.com" log-level: "debug" --- apiVersion: v1 kind: Pod metadata: name: app-pod spec: containers: - name: app-container image: busybox command: ["bin/sh", "-c", "sleep 3600"] env: - name: API_URL valueFrom: configMapKeyRef: name: app-config key: api-url - name: LOG_LEVEL valueFrom: # use some data value configMapKeyRef: name: app-config key: log-level 
Enter fullscreen mode Exit fullscreen mode

2.3 Vol Mount

2.3.1 use whole file

apiVersion: v1 kind: ConfigMap metadata: name: app-config data: config.yaml: | apiVersion: v1 data: app-name: "MyApp" app-version: "1.0" --- apiVersion: v1 kind: Pod metadata: name: app-pod spec: containers: - name: app-container image: busybox command: ["/bin/sh", "-c", "sleep 3600"] volumeMounts: - name: config-volume mountPath: /etc/config readOnly: true volumes: - name: config-volume configMap: name: app-config 
Enter fullscreen mode Exit fullscreen mode

2.3.2 Use part data items from file

apiVersion: v1 kind: ConfigMap metadata: name: app-config data: app-name: "MyApp" app-version-file: | appVersion "1.0" --- apiVersion: v1 kind: Pod metadata: name: app-pod spec: containers: - name: app-container image: busybox command: ["/bin/sh", "-c", "sleep 3600"] volumeMounts: - name: config-volume mountPath: /etc/config readOnly: true volumes: - name: config-volume configMap: name: app-config items: - key: app-name path: app-name - key: app-version-file path: app-version-file 
Enter fullscreen mode Exit fullscreen mode

we can see the files inside the container:

/ # ls /etc/config/ app-name app-version-file / # cat /etc/config/app-name / # cat /etc/config/app-version-file appVersion "1.0" / # cat /etc/config/app-name MyApp / # tree /etc/config/ /etc/config/ ├── app-name -> ..data/app-name └── app-version-file -> ..data/app-version-file 0 directories, 2 files 
Enter fullscreen mode Exit fullscreen mode

Above we can see it according to property-like key and file-like create different files.

Top comments (0)