Seamless Deployment: Optimizing K8s Configuration with ConfigMap and Secret (Part 1)

Linux | Red Hat Certified | IT Technology | Operations Engineer👇 Join our technical exchange QQ group with 1000 members Note: [Public Account] for faster approval

Seamless Deployment: Optimizing K8s Configuration with ConfigMap and Secret (Part 1)

In Kubernetes, a ConfigMap is an object used to store configuration data. It provides a way to separate configuration data from containers, allowing the configuration of containers to be managed and modified independently of the container images. ConfigMaps can inject configuration information into containers during application deployment, enabling containers to dynamically adapt to different environments and requirements.

1. Applicable Scenarios

(1) Configuration management for containerized applications: Store application configuration data in a ConfigMap, allowing containers to retrieve this configuration data at runtime, thus dynamically adapting to different environments and requirements.

(2) Sharing configuration data among multiple containers: Store common configuration data required by multiple containers in a ConfigMap, avoiding the issues of duplicate storage and management of configuration data, while also facilitating modifications and updates to the configuration.

(3) Managing configuration data for Kubernetes resources: Store configuration data for Kubernetes resources in a ConfigMap, allowing for configuration and management of resources by modifying the ConfigMap.

Next, we will introduce how to store and use ConfigMaps.

2. Creating and Validating ConfigMap

(1) Create a ConfigMap using a YAML configuration file and validate whether changes are synchronized.

[root@k8s-master volume]# cat mycm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
  namespace: myns
data:
  # Use data instead of spec
  username: sulibao
  passwd: slb317418
---
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx-configmap
  namespace: mynsspec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - name: nginx-port
      containerPort: 80
    volumeMounts:
    - name: myvolume
      mountPath: /root/mymsg
  volumes:
  - name: myvolume
    configMap:
      # Specify volume type as configmap
      name: my-configmap
[root@k8s-master volume]# kubectl apply -f mycm.yaml
configmap/my-configmap created
pod/my-nginx-configmap created
[root@k8s-master volume]# kubectl get pods,cm -n myns
  # Pod created successfully
NAME                     READY   STATUS    RESTARTS   AGE
pod/my-nginx-configmap   1/1     Running   0          67s
NAME                         DATA   AGE
kube-root-ca.crt             1      40m
configmap/my-configmap       2      67s
[root@k8s-master volume]# kubectl exec -it my-nginx-configmap -n myns -- /bin/sh -c "cat /root/mymsg/username"   # Content matches, creation successful
sulibao
[root@k8s-master volume]# kubectl exec -it my-nginx-configmap -n myns -- /bin/sh -c "cat /root/mymsg/passwd"
slb317418
# Next, edit the content of this ConfigMap to change passwd, then verify if the pod changes, result successful
[root@k8s-master volume]# kubectl edit cm my-configmap -n myns
[root@k8s-master volume]# kubectl get cm my-configmap -n myns -o yaml | grep passwd
  passwd: num123456
      {"apiVersion":"v1","data":{"passwd":"slb317418","username":"sulibao"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"my-configmap","namespace":"myns"}}
[root@k8s-master volume]# kubectl exec -it my-nginx-configmap -n myns -- /bin/sh -c "cat /root/mymsg/passwd"
num123456

(2) Create ConfigMap from a directory or file using –from-file.

# Create ConfigMap from a directory
[root@k8s-master volume]# kubectl create cm my-cm --from-file=/root/volume/a/ -n myns
configmap/my-cm created
[root@k8s-master volume]# kubeckubec get cm -n myns
-bash: kubeckubec: command not found
[root@k8s-master volume]# kubectl get cm -n myns
NAME               DATA   AGE
kube-root-ca.crt   1      33m
my-cm              2      16s
[root@k8s-master volume]# kubectl describe cm my-cm -n myns
Name:         my-cm
Namespace:    myns
Labels:       <none>
Annotations:  <none>
Data====
passwd:----slb317418
username:----sulibao
BinaryData====
Events:  <none>
# Create ConfigMap from two files, same effect as creating from a directory
[root@k8s-master a]# ll
total 8
-rw-r--r-- 1 root root 10 Dec 16 16:54 passwd
-rw-r--r-- 1 root root  8 Dec 16 16:54 username
[root@k8s-master a]# kubectl create cm my-cm --from-file=/root/volume/a/username --from-file=/root/volume/a/passwd -n myns
configmap/my-cm created
[root@k8s-master a]# kubectl get cm -n myns
NAME               DATA   AGE
kube-root-ca.crt   1      36m
my-cm              2      8s
[root@k8s-master a]# kubectl describe cm my-cm -n myns
Name:         my-cm
Namespace:    myns
Labels:       <none>
Annotations:  <none>
Data====
passwd:----slb317418
username:----sulibao
BinaryData====
Events:  <none></none></none></none></none></none></none>

(3) Create ConfigMap by passing information via command line.

[root@k8s-master volume]# kubectl create cm my-cm --from-literal=username=sulibao --from-literal=passwd=slb317418 -n myns
configmap/my-cm created
[root@k8s-master volume]# kubectl get cm -n myns
NAME               DATA   AGE
kube-root-ca.crt   1      40m
my-cm              2      10s
[root@k8s-master volume]# kubectl describe cm my-cm -n myns
Name:         my-cm
Namespace:    myns
Labels:       <none>
Annotations:  <none>
Data====
passwd:----slb317418
username:----sulibao
BinaryData====
Events:  <none></none></none></none>

3. How to Use ConfigMap

(1) Use env or envFrom to replace environment variables.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap1
  namespace: myns
data:
  username: sulibao
  passwd: slb317418
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap2
  namespace: myns
data:
  email: 123.qq.com
---
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx-configmap
  namespace: mynsspec:
  containers:
  - name: busybox
    image: busybox
    command: ["/bin/sh","-c","env;sleep 3000"]
    env:
    - name: name   # Name to be replaced, the final variable name is this
      valueFrom:
        configMapKeyRef:
          name: my-configmap1    # Specify the name of the ConfigMap
          key: username    # Specify a key in the ConfigMap, the value of this key will replace the specified name above
    - name: passwd
      valueFrom:
        configMapKeyRef:
          name: my-configmap1
          key: passwd
    envFrom:
    - configMapRef:
        name: my-configmap2    # Specify the name of the ConfigMap
[root@k8s-master volume]# kubectl get pods,cm -n myns
NAME                     READY   STATUS    RESTARTS   AGE
pod/my-nginx-configmap   1/1     Running   0          4m52s
NAME                         DATA   AGE
configmap/kube-root-ca.crt   1      123m
configmap/my-configmap1      2      4m52s
configmap/my-configmap2      1      4m52s
[root@k8s-master volume]# kubectl logs my-nginx-configmap -n myns | grep -E '(email|passwd|name)'
email=123.qq.com
name=sulibao
passwd=slb317418
[root@k8s-master volume]# kubectl exec -it my-nginx-configmap -n myns -- /bin/sh    # Enter pod for verification
/ # / # env
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=my-nginx-configmap
SHLVL=1
HOME=/root
TERM=xterm
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
email=123.qq.com
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
name=sulibao
passwd=slb317418
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=//
# echo $name
sulibao
# echo $email
123.qq.com

(2) Using ConfigMap with volumes.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
  namespace: myns
data:
  username: sulibao
  passwd: slb317418
---
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx-configmap
  namespace: mynsspec:
  containers:
  - name: busybox
    image: busybox
    command: ["/bin/sh","-c","cd /root/mymsg;sleep 3000"]
    volumeMounts:
    - name: myvolume
      mountPath: /root/mymsg
  volumes:
  - name: myvolume
    configMap:
      name: my-configmap
[root@k8s-master volume]# kubectl get pods,cm -n myns
NAME                     READY   STATUS    RESTARTS   AGE
pod/my-nginx-configmap   1/1     Running   0          10s
NAME                         DATA   AGE
configmap/kube-root-ca.crt   1      145m
configmap/my-configmap       2      10s
[root@k8s-master volume]# kubectl exec -it my-nginx-configmap -n myns -- /bin/sh -c "cat /root/mymsg/passwd"
slb317418
[root@k8s-master volume]# kubectl exec -it my-nginx-configmap -n myns -- /bin/sh -c "cat /root/mymsg/username"
sulibao

4. ConfigMap Configuration Triggers Deployment Rolling Update

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
  namespace: myns
data:
  username: sulibao
  passwd: slb317418
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-deploy
  name: my-deploy
  namespace: mynsspec:
  replicas: 3
  selector:
    matchLabels:
      app: my-deploy
  template:
    metadata:
      labels:
        app: my-deploy
    spec:
      containers:
      - image: busybox
        command: ["/bin/sh","-c","touch /root/a.txt;cat /root/mymsg/username > /root/a.txt;sleep 3000"]
        name: busybox
        volumeMounts:
        - name: myvolume
          mountPath: /root/mymsg
      volumes:
      - name: myvolume
        configMap:
          name: my-configmap
[root@k8s-master volume]# kubectl get pods,cm -n myns
NAME                             READY   STATUS    RESTARTS   AGE
pod/my-deploy-574476c4d9-85m78   1/1     Running   0          8s
pod/my-deploy-574476c4d9-fghm2   1/1     Running   0          8s
pod/my-deploy-574476c4d9-w4rrj   1/1     Running   0          8s
NAME                         DATA   AGE
configmap/kube-root-ca.crt   1      11m
configmap/my-configmap       2      8s
[root@k8s-master volume]# kubectl exec -it my-deploy-574476c4d9-85m78 -n myns -- /bin/sh -c "cat /root/a.txt"
sulibao
[root@k8s-master volume]#  kubectl patch deployment my-deploy -n myns --patch '{"spec": {"template": {"metadata": {"annotations": {"update": "2" }}}}}'  # This command updates the deployment to trigger a rolling update
[root@k8s-master volume]# kubectl edit cm my-configmap -n myns   # Changed configuration content
[root@k8s-master volume]# kubectl get pods -n myns -w  # Compare with the previous pods, all have been updated
NAME                         READY   STATUS    RESTARTS   AGE
my-deploy-664d69c7cf-5zkwz   1/1     Running   0          2m9s
my-deploy-664d69c7cf-dsxkp   1/1     Running   0          2m13s
my-deploy-664d69c7cf-vslz4   1/1     Running   0          2m5s
[root@k8s-master volume]# kubectl exec -it my-deploy-664d69c7cf-5zkwz -n myns -- /bin/sh -c "cat /root/a.txt"

Seamless Deployment: Optimizing K8s Configuration with ConfigMap and Secret (Part 1)Seamless Deployment: Optimizing K8s Configuration with ConfigMap and Secret (Part 1)For course inquiries, add: HCIE666CCIE↑ Or scan the QR code above ↑If you have any technical points or content you want to see,you can leave a message below to let us know!

Leave a Comment