Followers

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.


Next Secrets


COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
static_page
DevOpsWorld: Kubernetes-ConfigMaps
Kubernetes-ConfigMaps
DevOpsWorld
https://www.devopsworld.co.in/p/configmaps-configmap-is-api-object-used.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/p/configmaps-configmap-is-api-object-used.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content