Tuesday 4 August 2020

Kubernetes: ConfigMaps

ConfigMap is a special type of volume used to store the Application Configurations. ConfigMaps must be created before a Pod using them.

 

Using ConfigMap,

a.   We can make the variables available within a Pod.

b.   ConfigMap can be mount to a location, where the application can read. When you mount ConfigMap as a volume, at the mount point, files will be created with the name of key, and file content has the value.

c.    It can be used to provide command-line arguments.

 

ConfigMaps can be created from multiple sources.

a.   Directories

b.   Files

c.    Literal Values

 

Demo 1: Create ConfigMap from a file

Step 1: Create a file ‘appConfigurations’ with the below content.

 

appConfigurations

appName=Chat-Server
version=1.23.4

Step 2: Let's create a ConfigMap using the contents of the file ‘appConfigurations’.

kubectl create cm chat-server-config-map --from-file=appConfigurations

 

Above statement creates a configmap with the name ‘chat-server-config-map’.

$kubectl create cm chat-server-config-map --from-file=appConfigurations
configmap/chat-server-config-map created

You can query config maps to confirm that ‘chat-server-config-map’ is created.

$kubectl get cm
NAME                     DATA   AGE
chat-server-config-map   1      36s

You can see the content of configMap using ‘kubectl describe’ command.

$kubectl describe cm chat-server-config-map
Name:         chat-server-config-map
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
appConfigurations:
----
appName=Chat-Server
version=1.23.4

Events:  <none>

Step 3: Let’s create a Pod that uses this configMap ‘chat-server-config-map’.

$kubectl create -f configMapDemo1.yml 
pod/configmap-demo-pod1 created

Query Pods.

$kubectl get pods
NAME                  READY   STATUS      RESTARTS   AGE
configmap-demo-pod1   0/1     Completed   0          9s

Let’s see what is printed by the Pod ‘configmap-demo-pod1’.

$kubectl logs configmap-demo-pod1
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
appConfigurations=appName=Chat-Server
version=1.23.4

HOSTNAME=configmap-demo-pod1
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
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
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/

You can see that the keys ‘appName’ and ‘version’ are printed to console.

 

Demo 2: Creating ConfigMap using literal values

Below statement create a config map using literal values.

kubectl create cm chat-bot-configs --from-literal=version=1.234 --from-literal=appName=ChatBot

$kubectl create cm chat-bot-configs --from-literal=version=1.234 --from-literal=appName=ChatBot
configmap/chat-bot-configs created

Let’s query config maps to confirm ‘chat-bot-configs’ is created.

$kubectl get cm
NAME                     DATA   AGE
chat-bot-configs         2      4s
chat-server-config-map   1      14m

You can get the content of configmap using ‘kubectl describe’ command.

$kubectl describe cm chat-bot-configs
Name:         chat-bot-configs
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
appName:
----
ChatBot
version:
----
1.234
Events:  <none>

You can even view the content of configmap in YAML format like below.

$kubectl get cm chat-bot-configs -o yaml
apiVersion: v1
data:
  appName: ChatBot
  version: "1.234"
kind: ConfigMap
metadata:
  creationTimestamp: "2020-06-08T10:58:28Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:appName: {}
        f:version: {}
    manager: kubectl
    operation: Update
    time: "2020-06-08T10:58:28Z"
  name: chat-bot-configs
  namespace: default
  resourceVersion: "325135"
  selfLink: /api/v1/namespaces/default/configmaps/chat-bot-configs
  uid: e5365462-178b-411c-bb47-21fab1e1b122

Demo 3: Populate a volume with the data stored in configmap.

 

Let’s create a configuration file.

document-storage-configs.conf

version=1.23
downSync=30
upSync=1
fullSync=120

Create a ConfigMap from the above configuration file.

kubectl create cm document-storage-configmap --from-file=document-storage-configs.conf

$kubectl create cm document-storage-configmap --from-file=document-storage-configs.conf
configmap/document-storage-configmap created

Query ConfigMaps to confirm.

$kubectl get cm
NAME                         DATA   AGE
chat-bot-configs             2      52m
chat-server-config-map       1      66m
document-storage-configmap   1      3s

Let’s create a Pod that mounts this configMap as a file.

 

configMapDemo2.yml

apiVersion: v1
kind: Pod
metadata:
  name: configmap-demo-pod2
  labels:
    app: document-storage
    author: krishna
    serviceType: desktop
spec:
  containers:
    - name: document-storage
      image: jboss/wildfly
      volumeMounts:
      - name: conf
        mountPath: /app/configs 
  volumes:
  - name: conf
    configMap:
      name: document-storage-configmap
      items:
      - key: document-storage-configs.conf
        path: document-storage-configs

Create Pod using the above definition file.

$kubectl create -f configMapDemo2.yml
pod/configmap-demo-pod2 created

Now, login to the container by executing the below command.

kubectl exec -it configmap-demo-pod2 -c document-storage -- /bin/bash

 

You can see that configuration file is located at location /app/configs

cat /app/configs/document-storage-configs

$kubectl exec -it configmap-demo-pod2 -c document-storage -- /bin/bash
[jboss@configmap-demo-pod2 ~]$ 
[jboss@configmap-demo-pod2 ~]$ cat /app/configs/document-storage-configs 
version=1.23
downSync=30
upSync=1
fullSync=120





Previous                                                    Next                                                    Home

No comments:

Post a Comment