ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中;
configmap保存的是不需要加密的、应用所需的配置信息;
创建方法与secret几乎完全相同;可以使用kubectl create configmap ;也可以直接编写ConfigMap对象的yaml文件;
一、创建ConfigMap
1.通过命令行参数--from-literal创建
[root@k8s-master ~]# kubectl create configmap test-configmap --from-literal=user=admin --from-literal=pass=123456789
configmap/test-configmap created
[root@k8s-master ~]# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 2d
test-configmap 2 14s
[root@k8s-master ~]# kubectl get configmap test-configmap -o yaml
apiVersion: v1
data:
pass: "123456789"
user: admin
kind: ConfigMap
metadata:
creationTimestamp: "2024-07-24T10:09:01Z"
name: test-configmap
namespace: default
resourceVersion: "59582"
uid: 4cc49582-1a1c-482a-bc45-a922fab212de
2.通过指定文件创建
1)编辑配置文件app.properties
[root@k8s-master configmap]# vim app.properties
[root@k8s-master configmap]# cat app.properties
property.1 = value-1
property.2 = value-2
property.3 = value-3
property.4 = value-4
[mysqld]
!include /home/wing/mysql/etc/mysqld.cnf
port = 3306
socket = /home/wing/mysql/tmp/mysql.sock
pid-file = /wing/mysql/mysql/var/mysql.pid
basedir = /home/mysql/mysql
datadir = /wing/mysql/mysql/var
2)创建并查看configmap
[root@k8s-master configmap]# kubectl create configmap test-configmap2 --from-file=app.properties
configmap/test-configmap2 created
[root@k8s-master configmap]# kubectl get configmap test-configmap2 -o yaml
apiVersion: v1
data:
app.properties: |
property.1 = value-1
property.2 = value-2
property.3 = value-3
property.4 = value-4
[mysqld]
!include /home/wing/mysql/etc/mysqld.cnf
port = 3306
socket = /home/wing/mysql/tmp/mysql.sock
pid-file = /wing/mysql/mysql/var/mysql.pid
basedir = /home/mysql/mysql
datadir = /wing/mysql/mysql/var
kind: ConfigMap
metadata:
creationTimestamp: "2024-07-24T10:16:51Z"
name: test-configmap2
namespace: default
resourceVersion: "60250"
uid: fcfa9130-ee12-4e89-b1d2-97610280c8e9
3.指定目录创建
1)创建一个目录并编写文件
[root@k8s-master config]# vim config1 #文件内容随便写
[root@k8s-master config]# vim config2
[root@k8s-master config]# ls
config1 config2
[root@k8s-master config]# pwd
/root/configmap/config
2)创建configmap并查看
[root@k8s-master configmap]# kubectl create configmap testconfigmap3 --from-file=./config
configmap/testconfigmap3 created
[root@k8s-master configmap]# kubectl get configmap testconfigmap3 -o yaml
apiVersion: v1
data:
config1: |
123123
456456
789
config2: |
abcabc
defdef
hij
kind: ConfigMap
metadata:
creationTimestamp: "2024-07-24T10:25:31Z"
name: testconfigmap3
namespace: default
resourceVersion: "60983"
uid: 0d6a3802-ae9f-417d-b11a-e98430997e39
4.编写yaml文件创建
1)编写yaml文件
[root@k8s-master configmap]# vim configmap.yaml
[root@k8s-master configmap]# cat configmap.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config4
namespace: default
data:
cache_host: memcached-gcxt
cache_port: "11211"
cache_prefix: gcxt
my.cnf: |
[mysqld]
log-bin = mysql-bin
haha = hehe
2)创建并查看
[root@k8s-master configmap]# kubectl apply -f configmap.yaml
configmap/test-config4 created
[root@k8s-master configmap]# kubectl get configmap test-config4 -o yaml
apiVersion: v1
data:
cache_host: memcached-gcxt
cache_port: "11211"
cache_prefix: gcxt
my.cnf: |
[mysqld]
log-bin = mysql-bin
haha = hehe
kind: ConfigMap
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","data":{"cache_host":"memcached-gcxt","cache_port":"11211","cache_prefix":"gcxt","my.cnf":"[mysqld]\nlog-bin = mysql-bin\nhaha = hehe\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test-config4","namespace":"default"}}
creationTimestamp: "2024-07-24T10:30:25Z"
name: test-config4
namespace: default
resourceVersion: "61402"
uid: a633b469-6e89-49ab-b16d-b0a4aa4e43ed
3)查看configmap详细信息
[root@k8s-master configmap]# kubectl describe configmap test-config4
Name: test-config4
Namespace: default
Labels: <none>
Annotations: <none>Data
====
cache_host:
----
memcached-gcxt
cache_port:
----
11211
cache_prefix:
----
gcxt
my.cnf:
----
[mysqld]
log-bin = mysql-bin
haha = hehe
BinaryData
====Events: <none>
二、使用ConfigMap——三种方式
1.通过环境变量的方式
1.1 使⽤valueFrom、configMapKeyRef、name、key指定要⽤的key
1)编写configmap-pod文件
[root@k8s-master configmap]# vim configmap-pod.yaml
[root@k8s-master configmap]# cat configmap-pod.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: pod-configmap
spec:
containers:
- name: test-container
image: daocloud.io/library/nginx
env: #专⻔在容器⾥⾯设置变量的关键字
- name: SPECIAL_LEVEL_KEY #这⾥的-name,是容器⾥设置的新变量的名字
valueFrom:
configMapKeyRef:
name: test-configmap #这⾥是来源于哪个configMap
key: user #configMap⾥的key
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: test-configmap
key: pass
restartPolicy: Never
2)应用yaml文件创建pod并进入容器查看变量
[root@k8s-master configmap]# kubectl apply -f configmap-pod.yaml
pod/pod-configmap created
[root@k8s-master configmap]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 2 (178m ago) 30h
mysql 1/1 Running 2 (178m ago) 28h
pod-configmap 1/1 Running 0 60s
tomcat 1/1 Running 2 (178m ago) 31h
[root@k8s-master configmap]# kubectl exec -it pod-configmap /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# echo $SPECIAL_LEVEL_KEY
admin
# echo $SPECIAL_TYPE_KEY
123456789
# exit
2.通过在pod的命令行下运行
2.1 通过envFrom、configMapRef、name使得configmap中的所有key/value对都自动变成环境变量
1)编写pod-test-configmap.yaml文件和testconfigmap.yaml文件
[root@k8s-master configmap]# vim testconfigmap.yaml
[root@k8s-master configmap]# cat testconfigmap.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: config-map
namespace: default
data:
special.how: very
special.type: charm
[root@k8s-master configmap]# kubectl apply -f testconfigmap.yaml
configmap/config-map created
[root@k8s-master configmap]# vim pod-test-configmap.yaml
[root@k8s-master configmap]# cat pod-test-configmap.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: configmap-test-pod
spec:
containers:
- name: test-container
image: daocloud.io/library/nginx
envFrom:
- configMapRef: #直接使用configmap里面的key名
name: config-map
2)应用并查看
[root@k8s-master configmap]# kubectl apply -f pod-test-configmap.yaml
pod/configmap-test-pod created
[root@k8s-master configmap]# kubectl get pod
NAME READY STATUS RESTARTS AGE
configmap-test-pod 1/1 Running 0 83s
[root@k8s-master configmap]# kubectl exec -it configmap-test-pod /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# env
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=configmap-test-pod
HOME=/root
PKG_RELEASE=1~buster
pass=123456789
user=admin
TERM=xterm
NGINX_VERSION=1.19.6
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NJS_VERSION=0.5.0
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
# exit
3.使用卷挂载的方式挂载到pod内
3.1 将test-configmap4挂载起来
1)编写volume-pod-configmap.yaml文件
[root@k8s-master configmap]# vim volume-pod-configmap.yaml
[root@k8s-master configmap]# cat volume-pod-configmap.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-configmap
spec:
containers:
- name: nginx-configmap
image: daocloud.io/library/nginx
volumeMounts:
- name: configmap-volume4
mountPath: "/tmp/config4" #键值对存放路径
volumes:
- name: configmap-volume4
configMap:
name: test-config4
2)创建并进⼊容器中/tmp/config4查看
[root@k8s-master configmap]# kubectl apply -f volume-pod-configmap.yaml
pod/nginx-configmap created
[root@k8s-master configmap]# kubectl exec -it nginx-configmap /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# ls /tmp/config4/
cache_host cache_port cache_prefix my.cnf
# ls /tmp/config4/cache_host
/tmp/config4/cache_host
# exit
三、ConfigMap实际应用Nginx/Mysql应用
1.Subpath的使用
1.1在Linux中,将⽬录A挂载到⽬录B,则⽬录B原有的⽂件都会被⽬录A下的⽂件覆盖;在k8s中,subpath可以将configMap和secret作为⽂件挂载到容器中⽽不覆盖挂载⽬录下的⽂件;
1.2 subpath示例
1)编写MySQL和nginx文件并创建configmap
[root@k8s-master conf]# pwd
/root/configmap/config/conf
[root@k8s-master conf]# vim my.cnf
[root@k8s-master conf]# cat my.cnf
[mysql]
port=3306
socket=/var/lib/mysql/mysql.sock
default-character-set = utf8mb4
[mysqld]
server-id = 1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin
slow_query_log_file=/data/mysql/slow.log
binlog_format=mixed
max_connections=1000
max_user_connections=1000
character_set_server=utf8
[root@k8s-master conf]# vim nginx.conf
[root@k8s-master conf]# cat nginx.conf
user nginx;
worker_processes 1;error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;
#sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}[root@k8s-master conf]# cd ..
[root@k8s-master config]# kubectl create configmap nginx-mysql --from-file=./conf
configmap/nginx-mysql created
2)编写test-nginx-mysql.yaml文件
[root@k8s-master config]# vim test-nginx-mysql.yaml
[root@k8s-master config]# cat test-nginx-mysql.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: daocloud.io/library/nginx
volumeMounts:
- name: nginx
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: nginx
configMap:
name: nginx-mysql
items:
- key: nginx.conf
path: nginx.conf
---
apiVersion: v1
kind: Pod
metadata:
name: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-user-pass
key: password
volumeMounts:
- name: mysql
mountPath: /etc/my.cnf
subPath: my.cnf
volumes:
- name: mysql
configMap:
name: nginx-mysql
items:
- key: my.cnf # 可以理解为原⽂件名称
path: my.cnf # 修改之后的⽂件名
3)应用test-nginx-mysql文件
[root@k8s-master config]# kubectl apply -f test-nginx-mysql.yaml
pod/nginx createdpod/mysql created
[root@k8s-master config]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql 1/1 Running 0 7m42s
nginx 1/1 Running 0 7m42s
[root@k8s-master config]# kubectl exec -it nginx /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx:/# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;
#sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
root@nginx:/# exit
exit
[root@k8s-master config]# kubectl exec -it mysql /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@mysql:/# cat /etc/my.cnf
[mysql]
port=3306
socket=/var/lib/mysql/mysql.sock
default-character-set = utf8mb4
[mysqld]
server-id = 1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin
slow_query_log_file=/data/mysql/slow.log
binlog_format=mixed
max_connections=1000
max_user_connections=1000
character_set_server=utf8
root@mysql:/# exit
2.configmap热更新
2.1 使⽤该 ConfigMap 挂载的 Volume 中的数据需要⼀段时间(实测⼤概10秒)才能同步更新⽽且必须是yaml⽂件的⽅式创建出来的ConfigMap才可以;
[root@k8s-master configmap]# vim cm.yaml
[root@k8s-master configmap]# cat cm.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: configmap-yaml
labels:
app: configmap
data:
server1.conf: |-
upstream tomcatserver1 {
server 192.168.15.55:8081;
}
server {
listen 80;
server_name 8081.max.com;
location / {
proxy_pass http://tomcatserver1;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
---
kind: Pod
apiVersion: v1
metadata:
name: configmap-pod
labels:
app: configmap-pod
spec:
containers:
- name: nginx
image: daocloud.io/library/nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/conf.d
name: conf-name
volumes:
- name: conf-name
configMap:
name: configmap-yaml
items:
- key: server1.conf
path: server1.conf
[root@k8s-master configmap]# kubectl apply -f cm.yaml
configmap/configmap-yaml created
pod/configmap-pod created
2)修改文件进入pod中查看是否更新
data:
server1.conf: |-
upstream tomcatserver1 {
server 192.168.15.55:8082; #将端口8081修改为8082
}[root@k8s-master configmap]# kubectl apply -f cm.yaml
configmap/configmap-yaml configured
pod/configmap-pod configured
[root@k8s-master configmap]# kubectl exec -it configmap-pod /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@configmap-pod:/# cat /etc/nginx/conf.d/server1.conf | grep 192
server 192.168.15.55:8082;
root@configmap-pod:/# exit