configmap
1. configmap概述
1.1 什么是configmap
Configmap 是 k8s 中的资源对象,用于保存非机密性的配置的,数据可以用 key/value
键值对的形式保存,也可通过文件的形式保存。
1.2 Configmap 能解决哪些问题
我们在部署服务的时候,每个服务都有自己的配置文件,如果一台服务器上部署多个
服务:nginx、tomcat、apache 等,那么这些配置都存在这个节点上,假如一台服务
器不能满足线上高并发的要求,需要对服务器扩容,扩容之后的服务器还是需要部署
多个服务:nginx、tomcat、apache,新增加的服务器上还是要管理这些服务的配置,
如果有一个服务出现问题,需要修改配置文件,每台物理节点上的配置都需要修改,
这种方式肯定满足不了线上大批量的配置变更要求。 所以,k8s 中引入了 Configmap
资源对象,可以当成 volume 挂载到 pod 中,实现统一的配置管理。
Configmap 是 k8s 中的资源, 相当于配置文件,可以有一个或者多个 Configmap;
Configmap 可以做成 Volume,k8s pod 启动之后,通过 volume 形式映射到容器内
部指定目录上;
容器中应用程序按照原有方式读取容器特定目录上的配置文件。
在容器看来,配置文件就像是打包在容器内部特定目录,整个过程对应用没有任何
侵入。
1.3 Configmap 应用场景
使用 k8s 部署应用,当你将应用配置写进代码中,更新配置时也需要打包镜像,
configmap 可以将配置信息和 docker 镜像解耦,以便实现镜像的可移植性和可复用
性,因为一个 configMap 其实就是一系列配置信息的集合,可直接注入到 Pod 中给容
器使用。configmap 注入方式有两种,一种将 configMap 做为存储卷,一种是将
configMap 通过 env 中 configMapKeyRef 注入到容器中
使用微服务架构的话,存在多个服务共用配置的情况,如果每个服务中单独一份配
置的话,那么更新配置就很麻烦,使用 configmap 可以友好的进行配置共享。
1.4 局限性
ConfigMap 在设计上不是用来保存大量数据的。在 ConfigMap 中保存的数据不可超过 1
MiB。如果你需要保存超出此尺寸限制的数据,可以考虑挂载存储卷或者使用独立的数
据库或者文件服务。
2. Configmap 创建方法
2.1 命令行直接创建
直接在命令行中指定 configmap 参数创建,通过–from-literal 指定参数
[root@master1 daemonset]# kubectl create configmap tomcat-config --from-literal=tomcat_port=8080 --from-literal=server_name=myapp.tomcat.com
configmap/tomcat-config created
[root@master1 daemonset]# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 10d
tomcat-config 2 24s
[root@master1 daemonset]# kubectl describe configmap tomcat-config
Name: tomcat-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
tomcat_port:
----
8080
server_name:
----
myapp.tomcat.com
Events: <none>
[root@master1 daemonset]#
2.2 通过文件创建
通过指定文件创建一个 configmap,–from-file=<文件>
cat > nginx.conf <<END
server {
server_name www.nginx.com;
listen 80;
root /home/nginx/www/ }
END
#定义一个 key 是 www,值是 nginx.conf 中的内容
[root@master1 daemonset]# kubectl create configmap www-nginx --from-file=www=./nginx.conf
configmap/www-nginx created
[root@master1 daemonset]# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 10d
tomcat-config 2 12m
www-nginx 1 25s
[root@master1 daemonset]# 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>
2.3 指定目录创建 configmap
[root@master1 ~]# mkdir testconfig
[root@master1 ~]# cd testconfig/
cat > my-server.conf <<END
server-id=1
END
cat > my-slave.conf <<END
server-id=2
END
[root@master1 testconfig]# kubectl create configmap mysql-config --from-file=/root/testconfig
configmap/mysql-config created
[root@master1 testconfig]# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 10d
mysql-config 2 71s
tomcat-config 2 83m
www-nginx 1 70m
[root@master1 testconfig]# kubectl describe configmap mysql-config
Name: mysql-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
my-server.conf:
----
server-id=1
my-slave.conf:
----
server-id=2
Events: <none>
[root@master1 testconfig]#
2.4 编写 configmap 资源清单 YAML 文件
cat > mysql-configmap.yaml <<END
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
labels:
app: mysql
data:
master.conf: |
[mysqld]
log-bin
log_bin_trust_function_creators=1
lower_case_table_names=1
slave.conf: |
[mysqld]
super-read-only
log_bin_trust_function_creators=1
END
kubectl apply -f mysql-configmap.yaml
[root@master1 testconfig]# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 10d
mysql 2 1s
mysql-config 2 16m
tomcat-config 2 98m
www-nginx 1 86m
[root@master1 testconfig]# kubectl describe configmap mysql
Name: mysql
Namespace: default
Labels: app=mysql
Annotations: <none>
Data
====
master.conf:
----
[mysqld]
log-bin
log_bin_trust_function_creators=1
lower_case_table_names=1
slave.conf:
----
[mysqld]
super-read-only
log_bin_trust_function_creators=1
Events: <none>
3. 使用 Configmap
3.1 通过环境变量引入:使用 configMapKeyRef
#创建一个存储 mysql 配置的 configmap
cat > mysql1.yaml <<END
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql1
labels:
app: mysql1
data:
log: "1"
lower: "1"
END
kubectl apply -f mysql1.yaml
#创建pod中的内容,引用configmap中的内容
cat > mysql-pod.yaml <<END
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
spec:
containers:
- name: mysql
image: busybox
command: [ "/bin/sh","-c","sleep 3600" ]
env:
- name: log_bin #定义环境变量log_bin
valueFrom:
configMapKeyRef:
name: mysql1 #指定configmap的名字
key: log #指定configmap中的key
- name: lower #定义环境变量lower
valueFrom:
configMapKeyRef:
name: mysql1
key: lower
END
kubectl apply -f mysql-pod.yaml
[root@master1 testconfig]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client-7d8bfb6fcf-txwxf 1/1 Running 7 3d2h
mysql-pod 1/1 Running 0 14s
nfs-provisioner-65475d58bc-nn67v 1/1 Running 0 3d3h
pod-empty 0/1 Terminating 0 5d4h
test-hostpath 2/2 Running 0 5d3h
test-nfs-volume 1/1 Running 0 5d2h
web-0 0/1 Pending 0 3d2h
[root@master1 testconfig]# kubectl exec -it mysql-pod -- /bin/sh
/ # printenv
MY_NGINX_SERVICE_PORT=80
MY_NGINX_PORT=tcp://10.108.124.96:80
log_bin=1
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=mysql-pod
MY_NGINX_NODEPORT_SERVICE_SERVICE_HOST=10.104.102.223
SHLVL=1
MY_NGINX_PORT_80_TCP_ADDR=10.108.124.96
HOME=/root
MY_NGINX_PORT_80_TCP_PORT=80
MY_NGINX_PORT_80_TCP_PROTO=tcp
MY_NGINX_NODEPORT_SERVICE_PORT=tcp://10.104.102.223:80
MY_NGINX_NODEPORT_SERVICE_SERVICE_PORT=80
TERM=xterm
MY_NGINX_NODEPORT_SERVICE_PORT_80_TCP_ADDR=10.104.102.223
MY_NGINX_PORT_80_TCP=tcp://10.108.124.96:80
lower=1
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
MYSQL_PORT_3306_TCP_ADDR=10.102.58.92
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MY_NGINX_NODEPORT_SERVICE_PORT_80_TCP_PORT=80
MYSQL_PORT_3306_TCP_PORT=3306
MY_NGINX_NODEPORT_SERVICE_PORT_80_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
MYSQL_SERVICE_HOST=10.102.58.92
KUBERNETES_PORT_443_TCP_PROTO=tcp
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_PORT=tcp://10.102.58.92:3306
MYSQL_SERVICE_PORT=3306
MY_NGINX_NODEPORT_SERVICE_PORT_80_TCP=tcp://10.104.102.223:80
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
MYSQL_PORT_3306_TCP=tcp://10.102.58.92:3306
KUBERNETES_SERVICE_PORT_HTTPS=443
MY_NGINX_SERVICE_HOST=10.108.124.96
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
/ #
3.2 通过环境变量引入:使用 envfrom
cat > mysql-pod-envfrom.yaml <<END
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod-envfrom
spec:
containers:
- name: mysql
image: busybox
command: [ "/bin/sh","-c","sleep 3600" ]
envFrom:
- configMapRef:
name: mysql1 #指定configmap的名字
END
kubectl apply -f mysql-pod-envfrom.yaml
[root@master1 testconfig]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client-7d8bfb6fcf-txwxf 1/1 Running 7 3d3h
mysql-pod 1/1 Running 0 13m
mysql-pod-envfrom 1/1 Running 0 5s
nfs-provisioner-65475d58bc-nn67v 1/1 Running 0 3d3h
pod-empty 0/1 Terminating 0 5d4h
test-hostpath 2/2 Running 0 5d3h
test-nfs-volume 1/1 Running 0 5d2h
web-0 0/1 Pending 0 3d2h
[root@master1 testconfig]#
[root@master1 testconfig]# kubectl exec -it mysql-pod-envfrom -- /bin/sh
/ # printenv
MY_NGINX_SERVICE_PORT=80
MY_NGINX_PORT=tcp://10.108.124.96:80
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=mysql-pod-envfrom
MY_NGINX_NODEPORT_SERVICE_SERVICE_HOST=10.104.102.223
SHLVL=1
MY_NGINX_PORT_80_TCP_ADDR=10.108.124.96
HOME=/root
MY_NGINX_PORT_80_TCP_PORT=80
MY_NGINX_PORT_80_TCP_PROTO=tcp
MY_NGINX_NODEPORT_SERVICE_SERVICE_PORT=80
MY_NGINX_NODEPORT_SERVICE_PORT=tcp://10.104.102.223:80
TERM=xterm
MY_NGINX_PORT_80_TCP=tcp://10.108.124.96:80
MY_NGINX_NODEPORT_SERVICE_PORT_80_TCP_ADDR=10.104.102.223
lower=1
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
MYSQL_PORT_3306_TCP_ADDR=10.102.58.92
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MY_NGINX_NODEPORT_SERVICE_PORT_80_TCP_PORT=80
log=1
MYSQL_PORT_3306_TCP_PORT=3306
MY_NGINX_NODEPORT_SERVICE_PORT_80_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
MYSQL_SERVICE_HOST=10.102.58.92
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_PORT=tcp://10.102.58.92:3306
MYSQL_SERVICE_PORT=3306
MY_NGINX_NODEPORT_SERVICE_PORT_80_TCP=tcp://10.104.102.223:80
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
MYSQL_PORT_3306_TCP=tcp://10.102.58.92:3306
KUBERNETES_SERVICE_PORT_HTTPS=443
MY_NGINX_SERVICE_HOST=10.108.124.96
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
/ #
3.3 把 configmap 做成 volume,挂载到 pod
cat > mysql2-configmap.yaml <<END
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql2
labels:
app: mysql2
data:
log: "1"
lower: "1"
my.conf: |
[mysqld]
Welcome=mack
END
kubectl apply -f mysql2-configmap.yaml
[root@master1 testconfig]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 10d
mysql 2 61m
mysql-config 2 77m
mysql1 2 51m
mysql2 3 5s
tomcat-config 2 160m
www-nginx 1 147m
cat > mysql-pod-volume.yaml <<END
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod-volume
spec:
containers:
- name: mysql
image: busybox
command: [ "/bin/sh","-c","sleep 3600" ]
volumeMounts:
- name: mysql-config
mountPath: /tmp/config
volumes:
- name: mysql-config
configMap:
name: mysql2
END
kubectl apply -f mysql-pod-volume.yaml
[root@master1 testconfig]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client-7d8bfb6fcf-txwxf 1/1 Running 7 3d3h
mysql-pod 1/1 Running 0 29m
mysql-pod-envfrom 1/1 Running 0 16m
mysql-pod-volume 1/1 Running 0 24s
nfs-provisioner-65475d58bc-nn67v 1/1 Running 0 3d4h
pod-empty 0/1 Terminating 0 5d5h
test-hostpath 2/2 Running 0 5d3h
test-nfs-volume 1/1 Running 0 5d3h
web-0 0/1 Pending 0 3d2h
[root@master1 testconfig]# kubectl exec -it mysql-pod-volume -- /bin/sh
/ # cd /tmp/config
/tmp/config # ls
log lower my.conf
/tmp/config # ll
/bin/sh: ll: not found
/tmp/config # ls -l
total 0
lrwxrwxrwx 1 root root 10 Jun 5 11:57 log -> ..data/log
lrwxrwxrwx 1 root root 12 Jun 5 11:57 lower -> ..data/lower
lrwxrwxrwx 1 root root 14 Jun 5 11:57 my.conf -> ..data/my.conf
/tmp/config # cat log
/tmp/config # cat lower
1/tmp/config # cat my.conf
[mysqld]
Welcome=mack
/tmp/config # ^C
/tmp/config # cat ..data/log
1/tmp/config # cat ..data/lower
1/tmp/config # cat ..data/my.conf
[mysqld]
Welcome=mack
/tmp/config #
4. ConfigMap热更新
查看configmap 的内容
[root@master1 testconfig]# kubectl describe cm mysql2
Name: mysql2
Namespace: default
Labels: app=mysql2
Annotations: <none>
Data
====
log:
----
1
lower:
----
1
my.conf:
----
[mysqld]
Welcome=mack
Events: <none>
[root@master1 testconfig]#
kubectl edit configmap mysql2
#修改如下内容
查看修改后的内容
[root@master1 testconfig]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client-7d8bfb6fcf-txwxf 1/1 Running 7 3d3h
mysql-pod 1/1 Running 0 38m
mysql-pod-envfrom 1/1 Running 0 25m
mysql-pod-volume 1/1 Running 0 9m21s
nfs-provisioner-65475d58bc-nn67v 1/1 Running 0 3d4h
pod-empty 0/1 Terminating 0 5d5h
test-hostpath 2/2 Running 0 5d3h
test-nfs-volume 1/1 Running 0 5d3h
web-0 0/1 Pending 0 3d2h
[root@master1 testconfig]# kubectl exec -it mysql-pod-volume -- /bin/sh
/ # cd /tmp/config
/tmp/config # ls -l
total 0
lrwxrwxrwx 1 root root 10 Jun 5 11:57 log -> ..data/log
lrwxrwxrwx 1 root root 12 Jun 5 11:57 lower -> ..data/lower
lrwxrwxrwx 1 root root 14 Jun 5 11:57 my.conf -> ..data/my.conf
/tmp/config # cat log
2/tmp/config # cat lower
3/tmp/config # cat my.conf
[mysqld]
Welcome=mack
Hello
/tmp/config #
使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(实测大概 10 秒)才能同
步更新
3d2h
[root@master1 testconfig]# kubectl exec -it mysql-pod-volume – /bin/sh
/ # cd /tmp/config
/tmp/config # ls -l
total 0
lrwxrwxrwx 1 root root 10 Jun 5 11:57 log -> …data/log
lrwxrwxrwx 1 root root 12 Jun 5 11:57 lower -> …data/lower
lrwxrwxrwx 1 root root 14 Jun 5 11:57 my.conf -> …data/my.conf
/tmp/config # cat log
2/tmp/config # cat lower
3/tmp/config # cat my.conf
[mysqld]
Welcome=mack
Hello
/tmp/config #
**使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(实测大概 10 秒)才能同**
**步更新**
注:只有选择volume方式可以热更新