DEV Community

loading...

Kubernetes Config Map

Deepak Sabhrawal
The code!!!
・1 min read

Kubernetes ConfigMap resource is a piece of simple key-value pair information that can be passed to any application running on K8S.

Sample file to create config map on a running cluster:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config-map #name of the config map
data:
  myKey1: myValue1
  myKey2: myValue2
Enter fullscreen mode Exit fullscreen mode

Save this file as my-config-map.yaml and create config map on a cluster:

kubectl create -f my-config-map.yaml

Enter fullscreen mode Exit fullscreen mode

Now, ref the values from config map inside a pod.
Lets create a sample busybox pod.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-pod
    image: busybox
    command: ["sh", "-c", "echo $MY_VAR"]
    env:
    - name: MY_VAR
      valueFrom:
        configMapKeyRef:
          name: my-config-map 
          key: myKey1
Enter fullscreen mode Exit fullscreen mode

Save this as my-pod.yaml and create a pod.

kubectl create -f my-pod.yaml
Enter fullscreen mode Exit fullscreen mode

check the logs to see what is printed.

kubectl logs my-pod.   #myValue1
Enter fullscreen mode Exit fullscreen mode

Discussion (0)