ConfigMaps in Kubernetes

A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume.

https://kubernetes.io/docs/concepts/configuration/configmap/

We can take the environment information out of the pod definition file and manage it centrally using ConfigMap. ConfigMap is used to pass configuration data in the form of key-value pairs in Kubernetes. When it pod is created inject the configMap into the pod. So the key-value pairs are available as environment variables for the application hosted inside the container in the pod.

  1. Create and Check ConfigMap
  2. Inject data into the pod from ConfigMap.
  3. Create NodePort Service for checking ConfigMap data in a pod.

Create ConfigMap

$ kubectl create configmap \
  phonenumbers --from-literal=jay=+82-123-1456-1234 \
               --from-literal=tim=+1-647-1203-1230

Create ConfigMap by YAML file

apiVersion: v1
kind: ConfigMap
metadata:
  name: phonenumbers
data:
  jay: +82-010-5010-1020
  tim: +1-647-1023-1023
$ kubectl create -f phonenumbers-configmap.yaml 
configmap/first-config created

$ kubectl describe configmaps phonenumbers
Name:         phonenumbers
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
jay:
----
+82-010-5010-1020
tim:
----
+1-647-1023-1023
Events:  <none>

Inject data into the pod (application) from ConfigMap.

apiVersion: v1
kind: Pod
metadata:
  name: k8s-configmaps-node-pod
  labels:
    app: k8s-configmaps-node
spec:
  containers:
    - name: k8s-configmaps-node-container
      image: jayjodev/k8s-node-app:latest
      envFrom:
        - configMapRef:
            # will save in process.env
            name: phonenumbers
      ports:
        - containerPort: 4000
          protocol: TCP
$ kubectl create -f k8s-configmaps-node-pod.yaml 
pod/k8s-configmaps-node-pod created

$ kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
k8s-configmaps-node-pod   1/1     Running   0          12s

jayjodev/k8s-node-app image is node application to check k8s Configmaps and Secrets data

Create NodePort Service for checking ConfigMap data in a pod

apiVersion: v1
kind: Service
metadata:
  name: k8s-configmaps-node-nodeport-service
spec:
  type: NodePort
  ports:
    - targetPort: 4000
      port: 80
      nodePort: 30001
  selector:
    app: k8s-configmaps-node
$ kubectl create -f k8s-configmaps-node-nodeport-service.yaml 
service/k8s-configmaps-node-nodeport-service created

$ kubectl get services
NAME                                   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
k8s-configmaps-node-nodeport-service   NodePort    10.108.29.96   <none>        80:30001/TCP   40s

If you are using Minikube for testing, type, you can access the app and test in Minikube

$ minikube ssh
docker@minikube:~$ curl localhost:30001/api/configmaps/jay 
+82-010-5010-1020
docker@minikube:~$ curl localhost:30001/api/configmaps/tim
+1-647-1023-1023

or

Access the app and test outside of Minikube

$ minikube service --url k8s-configmaps-node-nodeport-service
šŸƒ  Starting tunnel for service k8s-configmaps-node-nodeport-service.
|-----------|--------------------------------------|-------------|------------------------|
| NAMESPACE |                 NAME                 | TARGET PORT |          URL           |
|-----------|--------------------------------------|-------------|------------------------|
| default   | k8s-configmaps-node-nodeport-service |             | http://127.0.0.1:55532 |
|-----------|--------------------------------------|-------------|------------------------|
http://127.0.0.1:55532
ā—  Because you are using a Docker driver on darwin, the terminal needs to be open to run it.

at another terminal

curl http://127.0.0.1:55532/api/configmaps/jay
+82-010-5010-1020

curl http://127.0.0.1:55532/api/configmap/tim
+1-647-1023-1023
# Environment
envFrom:
  - configMapRef:
      name: <configmap_name>
# Single Environment
env:
  - name: redis
    valueFrom:
      configMapKeyRef:
        name: <configmap_name>
        key: redis

# Volume
volumes:
  - name: <volume_name>
    configMap:
      name: <configmap_name>

Leave a Reply

Your email address will not be published.

ANOTE.DEV