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 'Non-sensitive data inside file-1' > file-1.txt
echo -n 'Non-sensitive data inside file-2' > file-2.txt
kubectl create configmap nginx-configmap-vol --from-file=file-1.txt --from-file=file-2.txt
# rm -f file-1 file-2
kubectl get configmaps
kubectl get configmaps nginx-configmap-vol -o yaml
kubectl describe configmaps nginx-configmap-vol
kubectl create configmap nginx-configmap-vol --from-file=file-1.txt -o yaml --dry-run | kubectl replace -f -
==========================================================
1b. Consume above "nginx-configmap-vol" configmap inside Pod from "volumes"
---------------------------------------------------------------------------
#nginx-pod-configmap-vol.yaml
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: file-1.txt
path: file-a.txt
- key: file-2.txt
path: file-b.txt
==========================================================
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 /bin/sh
cd /etc/non-sensitive-data
ls
cat Non-sensitive data inside file-1.txt
cat password.txt
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/file-a.txt
kubectl exec nginx-pod-configmap-vol cat /etc/non-sensitive-data/file-b.txt
*************************************************************************************************************************************************
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=file.1=file.a --from-literal=file.2=file.b
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
kind: Pod
metadata:
name: redis-pod-configmap-env
spec:
containers:
- name: redis-container
image: redis
env:
- name: FILE_1
valueFrom:
configMapKeyRef:
name: redis-configmap-env
key: file.1
- name: FILE_2
valueFrom:
configMapKeyRef:
name: redis-configmap-env
key: file.2
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 redis-pod-configmap-env -it /bin/sh
env | grep FILE
exit
(OR)
# Validate from "outside" the pod
kubectl exec redis-pod-configmap-env env | grep FILE
*******************************************************************************************************************************************
#cm.yaml
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
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"
kubectl create -f podcm.yaml
*************************************************************************************************************************************************
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
Comments
Post a Comment