[Cloud native | Learning Kubernetes from scratch] 26. Configuration management center configmap

This article has been included in the column " Learn k8s from scratch "
Previous article: In-depth understanding of kubectl Click jump

insert image description here

Configmap overview

What is a Configmap?

Configmap is a resource object in k8s, which is used to save non-confidential configuration. Data can be saved in the form of key/value key-value pairs or in the form of files.

What problems does Configmap solve?

When we deploy services, each service has its own configuration file. If multiple services are deployed on a server: nginx, tomcat, apache, etc., then these configurations exist on this node. In order to meet the requirements of high concurrency, the server needs to be expanded. After the expansion, the server still needs to deploy multiple services: nginx, tomcat, apache. The newly added server still needs to manage the configuration of these services. If there is a problem with one service, it needs to be modified. The configuration file, the configuration on each physical node needs to be modified, this method certainly cannot meet the requirements of online large-scale configuration changes. Therefore, k8s introduces the Configmap resource object, which can be mounted to the pod as a volume to achieve unified configuration management.

1. Configmap is a resource in k8s, which is equivalent to a configuration file and can have one or more Configmaps;

2. Configmap can be made into Volume. After the k8s pod is started, it is mapped to the specified directory inside the container in the form of volume;

3. The application in the container reads the configuration file on the specific directory of the container in the original way.

4. From the perspective of the container, the configuration file seems to be packaged in a specific directory inside the container, and the whole process does not intrude on the application.

Configmap application scenarios

1. Use k8s to deploy applications. When you write the application configuration into the code, you also need to package the image when updating the configuration. configmap can decouple the configuration information from the docker image, so as to realize the portability and reusability of the image, because A configMap is actually a collection of configuration information, which can be directly injected into the Pod for the container to use. There are two ways to inject configmap. One is to use configMap as a storage volume, and the other is to inject configMap into the container through configMapKeyRef in env.

2. If the microservice architecture is used, there are situations where multiple services share the configuration. If there is a single configuration in each service, it is very troublesome to update the configuration. Using configmap can be used to share the configuration in a friendly way.

limitation

ConfigMaps are not designed to hold large amounts of data. The data saved in the ConfigMap cannot exceed 1 MiB. If you need to store data that exceeds this size limit, consider mounting a storage volume or using a separate database or file server.

Configmap creation method

Create directly from the command line

直接在命令行中指定 configmap 参数创建,通过--from-literal 指定参数 
[root@k8smaster ~]# kubectl create configmap tomcat-config --from-literal=tomcat_port=8080 --from-literal=server_name=myapp.tomcat.com configmap/tomcat-config created
[root@k8smaster ~]# kubectl get cm
NAME            DATA   AGE
tomcat-config   2      4s
[root@k8smaster ~]# kubectl describe configmap tomcat-config 
Name:         tomcat-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
server_name:
----
myapp.tomcat.com
tomcat_port:
----
8080
Events:  <none>

Create from file

通过指定文件创建一个 configmap,--from-file=<文件>  下面的不是完整的nginx 只是一个例子
[root@k8smaster ~]# vim nginx.conf 
server {
    
     
 server_name www.nginx.com; 
 listen 80; 
 root /home/nginx/www/ 
} 
#定义一个 key 是 www,值是 nginx.conf 中的内容 --from-file是key 对应的值在下面
[root@k8smaster ~]# kubectl create configmap www-nginx --from-file=www=./nginx.conf 
configmap/www-nginx created
[root@k8smaster ~]# kubectl describe configmap www-nginx 
Name:         www-nginx
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
www:
----
server {
    
     
 server_name www.nginx.com; 
 listen 80; 
 root /home/nginx/www/ 
}

Events:  <none>

Specify a directory to create a configmap to simulate mysql

[root@k8smaster ~]# mkdir configmap
[root@k8smaster ~]# cd configmap/
[root@k8smaster configmap]# vim my-server.cnf 
server-id=1 
[root@k8smaster configmap]# vim my-slave.cnf 
server-id=2 
#指定目录创建 configmap 
[root@k8smaster configmap]# kubectl create configmap mysql-config --from-file=/root/configmap/ 
configmap/mysql-config created
#查看 configmap 详细信息 
[root@k8smaster configmap]# kubectl describe configmap mysql-config 
Name:         mysql-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
my-slave.cnf:
----
server-id=2 

my-server.cnf:
----
server-id=1 

Events:  <none>

Write configmap resource manifest YAML file

[root@k8smaster configmap]# vim mysql-configmap.yaml                   配置文件多行要写| 不然有问题
apiVersion: v1
kind: ConfigMap 
metadata: 
  name: mysql
  labels:
    app: mysql
data: 
  master.cnf: |
    [mysqld]
    log-bin
    log_bin_trust_function_creators=1
    lower_case_table_names=1
  slave.cnf: |
    [mysqld]
    super-read-only
    log_bin_trust_function_creators=1

Using Configmaps

Introduced through environment variables: use configMapKeyRef

#创建一个存储 mysql 配置的 configmap 
[root@k8smaster ~]# vim mysql-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
data:
    log: "1"
    lower: "1"
[root@k8smaster ~]# kubectl apply -f mysql-configmap.yaml 
configmap/mysql created
#创建 pod,引用 Configmap 中的内容 
[root@k8smaster ~]# vim mysql-pod.yaml 
apiVersion: v1
kind: Pod 
metadata: 
  name: mysql-pod
spec: 
  containers:
  - name: mysql
    image: busybox
    imagePullPolicy: IfNotPresent
    command: [ "/bin/sh", "-c", "sleep 3600" ]
    env:    
    - name: log_bin 	#定义环境变量 log_bin 		mysql容器里会有这个变量 值是1
      valueFrom:
        configMapKeyRef:
          name: mysql 	#指定 configmap 的名字 
          key: log 		#指定 configmap 中的 key 
    - name: lower 		#定义环境变量 lower 			同上
      valueFrom:  
        configMapKeyRef:
          name: mysql
          key: lower       
  restartPolicy: Never

#更新资源清单文件 
[root@k8smaster ~]# kubectl apply -f mysql-pod.yaml 
pod/mysql-pod created

[root@xianchaomaster1 ~]# kubectl exec -it mysql-pod -c mysql -- /bin/sh 
/ # printenv 
log_bin=1
lower=1

Introduced through environment variables: use envfrom

[root@xianchaomaster1 ~]# vim mysql-pod-envfrom.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: mysql-pod-envfrom
spec:
  containers:
  - name: mysql
    image: busybox
    imagePullPolicy: IfNotPresent
    command: [ "/bin/sh", "-c", "sleep 3600" ]
    envFrom:
    - configMapRef:
        name: mysql 		#指定 configmap 的名字 
  restartPolicy: Never
#更新资源清单文件 
[root@k8smaster ~]# kubectl apply -f mysql-pod-envfrom.yaml 
pod/mysql-pod-envfrom created
[root@k8smaster ~]# kubectl exec -it mysql-pod-envfrom -- /bin/sh 
/ # printenv 
lower=1
log=1
#引入到容器里了 这里的log和lower的值和mysqlconfigmap.yaml文件的值一样的

Make the configmap a volume and mount it to the pod

[root@k8smaster ~]# vim mysql-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
data:
    log: "1"
    lower: "1"
    my.cnf: |
     [mysqld]
     nihao=paopao    
[root@k8smaster ~]# kubectl apply -f mysql-configmap.yaml 
configmap/mysql configured
[root@k8smaster ~]# kubectl describe cm mysql
Name:         mysql
Namespace:    default
Labels:       app=mysql
Annotations:  
Data
====
lower:
----
1
my.cnf:
----
[mysqld] 
nihao=paopao 

log:
----
1
Events:  <none>
[root@k8smaster ~]# vim mysql-pod-volume.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: mysql-pod-volume
spec:
  containers:
  - name: mysql
    image: busybox
    imagePullPolicy: IfNotPresent
    command: [ "/bin/sh","-c","sleep 3600" ]
    volumeMounts:
    - name: mysql-config
      mountPath: /tmp/config
  volumes:
  - name: mysql-config
    configMap:					#把configmap做成卷 卷的名字叫mysql-config 挂载到tmp config目录下
      name: mysql
  restartPolicy: Never
[root@k8smaster ~]# kubectl apply -f mysql-pod-volume.yaml 
pod/mysql-pod-volume created
[root@k8smaster ~]# kubectl exec -it mysql-pod-volume -- /bin/sh 
/ # cd /tmp/config/ 
/tmp/config # ls
log     lower   my.cnf
#是不会有配置文件的 因为做成卷了

Configmap hot update

把 logs: “1”变成 log: “2” 保存退出 
[root@k8smaster ~]# kubectl edit configmap mysql 
configmap/mysql edited
[root@k8smaster ~]# kubectl exec -it mysql-pod-volume -- /bin/sh 
/ # cat /tmp/config/log 
2
#发现 log 值变成了 2,更新生效了
注意: 
更新 ConfigMap 后
使用该 ConfigMap 挂载的 Env 不会同步更新 (环境变量不可通过configmap实时更新改变)
 
使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(实测大概 10 秒)才能同步更新

write at the end

It is not easy to create, if you think the content is helpful to you, please give me a three-link follow to support me! If there are any mistakes, please point them out in the comments and I will change them in time!

The series currently being updated: Learning k8s from scratch

Thank you for watching, the article is mixed with personal understanding, if there is any error, please contact me to point out~

Guess you like

Origin blog.csdn.net/qq_45400861/article/details/127182698