Kubernetes-ConfigMaps

 

Kubernetes-ConfigMaps

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.

A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.

LAB


1. Creating Configmap from "multiple files" & Consuming it inside Pod from "volumes" 



1a.  Create Configmap "nginx-configmap-vol" from "multiple files":


------------------------------------------------------------------

echo -n 'Properties for Staging env' > web_stag.xml

echo -n 'Properties for Prod env' > web_prod.xml


kubectl create configmap nginx-configmap-vol --from-file=web_stag.xml --from-file=web_prod.xml

# rm *


  • kubectl get configmaps
  • kubectl get configmaps nginx-configmap-vol -o yaml
  • kubectl describe configmaps nginx-configmap-vol

1b.  Consume above "nginx-configmap-vol" configmap inside Pod from "volumes" 



#nginx-pod-configmap-vol.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod-configmap-vol
spec:
  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: test-vol
      mountPath: "/etc/non-sensitive-data"
      readOnly: true
  volumes:
    - name: test-vol
      configMap:
        name: nginx-configmap-vol
        items:
        - key: web_stag.xml
          path: web.xml


==========================================================


1c. Create | Display | Validate:

--------------------------------


Create

kubectl create -f nginx-pod-configmap-vol.yaml


Display

kubectl get po

kubectl get configmaps

kubectl describe pod nginx-pod-configmap-vol


 Validate from "inside" the pod

kubectl exec nginx-pod-configmap-vol -it -- bash

cd /etc/non-sensitive-data

ls 

cat web.xml

exit


(OR)


# Validate from "outside" the pod

kubectl exec nginx-pod-configmap-vol ls /etc/non-sensitive-data

kubectl exec nginx-pod-configmap-vol cat /etc/non-sensitive-data/web.xml



*************************************************************************************************************************************************


2. Creating Configmap from "literal values" & Consuming it inside Pod from "environment variables"  



2a.  Create configmap “redis-configmap-env” from "literal values"

-----------------------------------------------------------------


kubectl create configmap redis-configmap-env --from-literal=stagenv=stagenvvalue --from-literal=prodenv=prodenvvalue


kubectl get configmap

kubectl describe configmap redis-configmap-env


===============================================================================


2b. Consume “redis-configmap-env” configmap inside pod from “Environment Variables” inside pod


# redis-pod-configmap-env.yaml

apiVersion: v1
kind: Pod
metadata:
  name: redis-pod-configmap-env
spec:
  containers:
  - name: redis-container
    image: redis
    env:
      - name: PROD
        valueFrom:
          configMapKeyRef:
            name: redis-configmap-env
            key: prodenv
      - name: STAG
        valueFrom:
          configMapKeyRef:
            name: redis-configmap-env
            key: stagenv
  restartPolicy: Never


2c. Create | Display | Validate:


# Create

kubectl create -f  redis-pod-configmap-env.yaml


# Display

kubectl get pods

kubectl get configmaps

kubectl describe pod redis-pod-configmap-env



# Validate from "inside" the pod

kubectl exec -it redis-pod-configmap-env -- env



(OR)


# Validate from "outside" the pod

kubectl exec redis-pod-configmap-env -- env


Try Your Self


kubectl create -f podcm.yaml


#cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: game-demo
data:
  # property-like keys; each key maps to a simple value
  player_initial_lives: "3"
  ui_properties_file_name: "user-interface.properties"
  #
  # file-like keys
  game.properties: |
    enemy.types=aliens,monsters
    player.maximum-lives=5
  user-interface.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true




kubectl create -f cm.yaml

kubectl get cm



# To consume the Config map create below pod

#podcm.yaml

apiVersion: v1
kind: Pod
metadata:
  name: configmap-demo-pod
spec:
  containers:
    - name: demo
      image: nginx
      env:
        # Define the environment variable
        - name: PLAYER_INITIAL_LIVES # Notice that the case is different here
                                     # from the key name in the ConfigMap.
          valueFrom:
            configMapKeyRef:
              name: game-demo           # The ConfigMap this value comes from.
              key: player_initial_lives # The key to fetch.
        - name: UI_PROPERTIES_FILE_NAME
          valueFrom:
            configMapKeyRef:
              name: game-demo
              key: ui_properties_file_name
      volumeMounts:
      - name: config
        mountPath: "/config"
        readOnly: true
  volumes:
    # You set volumes at the Pod level, then mount them into containers inside that Pod
    - name: config
      configMap:
        # Provide the name of the ConfigMap you want to mount.
        name: game-demo
        # An array of keys from the ConfigMap to create as files
        items:
        - key: "game.properties"
          path: "game.properties"
        - key: "user-interface.properties"
          path: "user-interface.properties"


3. Cleanup


# Delete configmaps

kubectl delete configmaps nginx-configmap-vol redis-configmap-env


# Delete pods

kubectl delete pods nginx-pod-configmap-vol redis-pod-configmap-env


# Validate

kubectl get pods

kubectl get configmaps


References

https://kubernetes.io/docs/concepts/configuration/configmap/#:~:text=A%20ConfigMap%20is%20an%20API%20object%20that%20lets%20you%20store,a%20valid%20DNS%20subdomain%20name.

Comments