ConfigMap配置管理
一.ConfigMap简介
Configmap用于保存配置数据,以键值对形式存储。
configMap 资源提供了向 Pod 注入配置数据的方法。
旨在让镜像和配置文件解耦,以便实现镜像的可移植性和可复用性。
典型的使用场景:
-
填充环境变量的值
-
设置容器内的命令行参数
-
填充卷的配置文件
二.ConfigMap创建
创建ConfigMap的方式有4种:
- 使用字面值创建
- 使用文件创建
- 使用目录创建
- 编写configmap的yaml文件创建
1.使用字面值创建
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
[root@server1 ingress]# kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
configmap/my-config created
[root@server1 ingress]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 5d18h
my-config 2 5s
[root@server1 ingress]# kubectl describe cm my-config
Name: my-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
key1:
----
config1
key2:
----
config2
Events: <none>
2.文件创建
kubectl create configmap my-config-2 --from-file=/etc/resolv.conf
[root@server1 ingress]# kubectl create configmap my-config-2 --from-file=/etc/resolv.conf
configmap/my-config-2 created
[root@server1 ingress]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 5d18h
my-config 2 98s
my-config-2 1 13s
[root@server1 ingress]# kubectl describe cm my-config-2
Name: my-config-2
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
resolv.conf:
----
nameserver 114.114.114.114
Events: <none>
3.使用目录创建
kubectl create configmap my-config-3 --from-file=test
[root@server1 ~]# cd configmap/
[root@server1 configmap]# ls
[root@server1 configmap]# mkdir test
[root@server1 configmap]# cp /etc/passwd test/
[root@server1 configmap]# cp /etc/fstab test/
[root@server1 configmap]# ls test/
fstab passwd
[root@server1 configmap]# kubectl create configmap my-config-3 --from-file=test
configmap/my-config-3 created
[root@server1 configmap]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 5d18h
my-config 2 3m33s
my-config-2 1 2m8s
my-config-3 2 10s
[root@server1 configmap]# kubectl describe cm my-config-3
Name: my-config-3
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
fstab:
----
#
# /etc/fstab
# Created by anaconda on Mon Jun 21 22:30:15 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=13a4a38f-65aa-49d3-adb5-a62bb3bc065a /boot xfs defaults 0 0
#/dev/mapper/rhel-swap swap swap defaults 0 0
passwd:
----
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
Events: <none>
4.编写configmap的yaml文件
[root@server1 configmap]# cat cm1.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm1-config
data:
db_host: "172.25.3.250"
db_port: "3306"
[root@server1 configmap]# kubectl apply -f cm1.yml
configmap/cm1-config created
[root@server1 configmap]# kubectl get cm
NAME DATA AGE
cm1-config 2 7s
kube-root-ca.crt 1 5d18h
my-config 2 5m54s
my-config-2 1 4m29s
my-config-3 2 2m31s
[root@server1 configmap]# kubectl describe cm cm1-config
Name: cm1-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
db_port:
----
3306
db_host:
----
172.25.3.250
Events: <none>
三.ConfigMap使用
如何使用configmap:
-
通过环境变量的方式直接传递给pod
-
通过在pod的命令行下运行的方式
-
作为volume的方式挂载到pod内
1.通过环境变量的方式直接传递给pod
指定key名称
[root@server1 configmap]# cat pod.yml
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
key: db_host
- name: key2
valueFrom:
configMapKeyRef:
name: cm1-config
key: db_port
restartPolicy: Never
[root@server1 configmap]# kubectl apply -f pod.yml
pod/pod1 configured
[root@server1 configmap]# kubectl logs pod1
key1=172.25.3.250
key2=3306
使用默认名称
[root@server1 configmap]# cat pod2.yml
apiVersion: v1
kind: Pod
metadata:
name: pod2
spec:
containers:
- name: pod2
image: busyboxplus
command: ["/bin/sh", "-c", "env"]
envFrom:
- configMapRef:
name: cm1-config
restartPolicy: Never
[root@server1 configmap]# vim pod2.yml
[root@server1 configmap]# kubectl apply -f pod2.yml
pod/pod2 created
[root@server1 configmap]# kubectl logs pod2
db_port=3306
db_host=172.25.3.250
2.通过在pod的命令行下运行的方式
[root@server1 configmap]# cat pod3.yml
apiVersion: v1
kind: Pod
metadata:
name: pod3
spec:
containers:
- name: pod3
image: nginx
#command: ["/bin/sh", "-c", "cat /config/db_host"]
volumeMounts:
- name: config-volume
mountPath: /config
volumes:
- name: config-volume
configMap:
name: cm1-config
#restartPolicy: Never
[root@server1 configmap]# vim pod3.yml
[root@server1 configmap]# kubectl apply -f pod3.yml
pod/pod3 created
[root@server1 configmap]# kubectl logs pod3
172.25.3.250
3.作为volume的方式挂载到pod内
添加配置文件到cm
[root@server1 configmap]# vim nginx.conf
[root@server1 configmap]# kubectl create configmap nginxconf --from-file=nginx.conf
configmap/nginxconf created
[root@server1 configmap]# kubectl get cm
NAME DATA AGE
cm1-config 2 28m
kube-root-ca.crt 1 5d18h
nginxconf 1 8s
cm文件内容
[root@server1 configmap]# cat nginx.yml
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
创建pod,获取端口访问
[root@server1 configmap]# kubectl apply -f nginx.yml
deployment.apps/my-nginx created
[root@server1 configmap]# kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-b9b58dbdf-9ww5n 1/1 Running 0 7s
[root@server1 configmap]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-b9b58dbdf-9ww5n 1/1 Running 0 30s 10.244.179.81 server2 <none> <none>
[root@server1 configmap]# curl 10.244.179.81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
编辑cm 访问端口8080,测试端口未切换
[root@server1 configmap]# kubectl edit cm nginxconf
configmap/nginxconf edited
[root@server1 configmap]# curl 10.244.179.81:8080
curl: (7) Failed connect to 10.244.179.81:8080; Connection refused
[root@server1 configmap]# curl 10.244.179.81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
执行热切命令,查看新ip访问,访问端口切换成功
[root@server1 configmap]# kubectl patch deployments.apps my-nginx --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20200219"}}}}}'
deployment.apps/my-nginx patched
[root@server1 configmap]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-6b67dc79c9-zrbcz 1/1 Running 0 10s 10.244.22.17 server4 <none> <none>
my-nginx-b9b58dbdf-9ww5n 0/1 Terminating 0 2m20s 10.244.179.81 server2 <none> <none>
[root@server1 configmap]# curl 10.244.22.17
curl: (7) Failed connect to 10.244.22.17:80; Connection refused
[root@server1 configmap]# curl 10.244.22.17:8080
[root@server1 configmap]# curl 10.244.22.17:8080
<!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>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>