Operation and maintenance practice -- Configmap configuration management of kubernetes storage

1. What is Configmap?

Storage types in k8s are generally divided into Configmap, Secret and Volumes.
configMap is a kind of storage. It is mainly used to save configuration data in the form of key value pairs. The configMap resource provides a method to inject configuration data into the Pod to decouple the image from the configuration file, so as to realize the portability and reusability of the image.

The main application scenarios are:
1. Populates the value of the environment variable
2. Set command line parameters within the container
3. Populate the profile of the volume

2. How to create Configmap

(1) Create using literals

Kubectl create configmap my config -- from literal = key1 = config1 -- from literal = key2 = config2 create a configmap named my config, where the value is directly defined by the command, the value of key1 is config1, and the value of key2 is config2. Check, a new cm is generated, and the key value has been written in.

(2) Create with file

kubectl create configmap my-config-2 --from-file=/etc/resolv.conf creates my-config-2, the name of the file is the name of the key, and the content of the file is the value

(3) Create using directory

Create a test directory and copy two files into it. Kubectl create configmap my-config-3 -- from file = Test creates my-config-3. The file name in the directory is key and the file content is value

(4) Write the yaml file creation of configmap

Write CM1 Yaml file

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm1-config
data:
  db_host: "172.25.0.250"
  db_port: "3306"

Create CM1 config and view

3. How to use configmap?

There are three main types:
(1) It is directly passed to pod through environment variables
(2) By running on the command line of pod
(3) Mount to pod as volume

(1) It is directly passed to pod through environment variables

Write pod Yaml file

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
    - name: pod1
      image: busyboxplus
      command: ["/bin/sh", "-c", "env"]
      env:
        - name: key1
          valueFrom:
            configMapKeyRef:
              name: cm1-config		%hold cm1-config Medium db_host this key Renamed key1
              key: db_host
        - name: key2
          valueFrom:
            configMapKeyRef:
              name: cm1-config		%hold cm1-config Medium db_port this key Renamed key2
              key: db_port
  restartPolicy: Never


Check the log after creating the pod. key1 and key2 have been written

You can also see the details by using descrbie

Another is to edit POD2 Yaml file

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
    - name: pod1
      image: busyboxplus
      command: ["/bin/sh", "-c", "env"]		%env View variables
      envFrom:
        - configMapRef:
            name: cm1-config
  restartPolicy: Never


Create pod1, check the log, and the key and value are all here, and they are the original names

(2) By running on the command line of pod

Edit pod3 Yaml file

apiVersion: v1
kind: Pod
metadata:
  name: pod2
spec:
  containers:
    - name: pod2
      image: nginx
      command: ["/bin/sh", "-c", "cat /config/db_host"]	%see/config/db_host
      volumeMounts:
      - name: config-volume
        mountPath: /config		%hold cm1-config Mount your data to pod2 of/config
  volumes:
    - name: config-volume		%Variable from cm1-config
      configMap:
        name: cm1-config
  #restartPolicy: Never


Create pod2 and view the log

Annotate the command and restart strategy,

Re apply and enter pod2 to check that there are indeed two key values

(3) Mount to pod as volume

Write nginx Conf file

server {
    listen       80;
    server_name  _;

    location / {
        root /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

Create nginx conf


Edit nginx Yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          volumeMounts:
          - name: config-volume
            mountPath: /etc/nginx/conf.d
      volumes:
        - name: config-volume
          configMap:
            name: nginxconf


kubectl apply -f nginx.yaml create my nginx

curl 10.244.141.216 can be accessed

[root@server1 configmap]# curl 10.244.141.216
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
[root@server1 configmap]# kubectl edit cm nginxconf 		% Enter edit and change 80 to 8080
%configmap After a hot update, it does not trigger a correlation Pod The rolling update of needs to be triggered manually
[root@server1 configmap]# kubectl patch deployments. Apps my nginx -- patch '{"spec": {"template": {"metadata": {"annotations": {"version / config": "20200219"}'% update
[root@server1 configmap]# curl 10.244.141.216 		% Test port switching succeeded
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>

Keywords: Linux Operation & Maintenance Kubernetes

Added by nscipione on Sun, 02 Jan 2022 12:37:58 +0200