编排之神-Kubernetes存储专题--ConfigMap演练
8.Kubernetes中的存储
8.1 ConfigMap
8.1.1 ConfigMap的功能
- configMap用于保存配置数据,以键值对形式存储。
- configMap 资源提供了向 Pod 注入配置数据的方法。
- 镜像和配置文件解耦,以便实现镜像的可移植性和可复用性。
- etcd限制了文件大小不能超过1M
8.1.2 ConfigMap的使用场景
- 填充环境变量的值
- 设置容器内的命令行参数
- 填充卷的配置文件
8.1.3 ConfigMap创建方式
8.1.3.1 字面值创建
[root@k8s-master mnt]# kubectl create cm ceshi --from-literal name1=dhj
[root@k8s-master mnt]# kubectl describe cm ceshi
8.1.3.2 通过文件创建
[root@k8s-master ~]# vim /etc/resolv.conf # 此文件是本裸金属主机DNS地址指向
[root@k8s-master ~]# kubectl create cm lee2-config --from-file /etc/resolv.conf
[root@k8s-master ~]# kubectl describe cm lee2-config

8.1.3.3 通过目录创建
[root@k8s-master mnt]# mkdir ceshi/
[root@k8s-master mnt]# cp /etc/resolv.conf ceshi/
[root@k8s-master mnt]# kubectl create cm test --from-file ceshi/
[root@k8s-master mnt]# kubectl describe cm test

8.1.3.4 通过yaml文件创建
[root@k8s-master mnt]# kubectl create cm testyaml --from-literal db_host=172.25.254.100 --from-literal db_port=3306 --dry-run=client -o yaml > testyaml.yml
[root@k8s-master mnt]# vim testyaml.yml
apiVersion: v1
data:db_host: 172.25.254.100db_port: "3306"
kind: ConfigMap
metadata:creationTimestamp: nullname: testyaml[root@k8s-master mnt]# kubectl apply -f testyaml.yml
[root@k8s-master mnt]# kubectl describe cm testyaml

8.1.4 ConfigMap的使用方式
- 通过环境变量的方式直接传递给pod
- 通过pod的命令行运行方式
- 作为volume的方式挂载到pod内
8.1.4.1 使用ConfigMap填充环境变量
[root@k8s-master mnt]# kubectl run testpod --image busyboxplus --dry-run=client -o yaml > test.yml[root@k8s-master ~]# vim cmcfg1.yml
[root@k8s-master mnt]# kubectl apply -f cmcfg1.yml[root@k8s-master ~]# kubectl create cm cm-ceshi --from-file cmcfg1.yml --dry-run=client[root@k8s-master ~]# kubectl describe cm cm-ceshi
# 将cm中的内容映射为指定变量
[root@k8s-master ~]# vim testyaml1.yml
apiVersion: v1
kind: Pod
metadata:labels:run: testyamlname: testyaml
spec:containers:- image: busyboxplus:latestname: testyamlcommand:- /bin/sh- -c- envenv:- name: key1valueFrom:configMapKeyRef:name: testyamlkey: db_host- name: key2valueFrom:configMapKeyRef:name: testyamlkey: db_portrestartPolicy: Never[root@k8s-master ~]# kubectl apply -f testyaml1.yml[root@k8s-master mnt]# kubectl create cm testpod --from-file testyaml1.yml[root@k8s-master mnt]# kubectl logs pod/testyaml | grep key
key1=172.25.254.100
key2=3306

# 把cm中的值直接映射为变量
[root@k8s-master ~]# vim testpod2.yml
apiVersion: v1
kind: Pod
metadata:labels:run: testpodname: testpod
spec:containers:- image: busyboxplus:latestname: testpodcommand:- /bin/sh- -c- envenvFrom:- configMapRef:name: testpodrestartPolicy: Never# 查看日志
[root@k8s-master mnt]# kubectl logs pod/testpod | grep db
db_port=3306
db_host=172.25.254.100

# 在pod命令行中使用变量
[root@k8s-master ~]# vim testpod3.yml
apiVersion: v1
kind: Pod
metadata:labels:run: testpodname: testpod
spec:containers:- image: busyboxplus:latestname: testpodcommand:- /bin/sh- -c- echo ${db_host} ${db_port} # 变量调用envFrom:- configMapRef:name: testpodrestartPolicy: Never# 查看日志
[root@k8s-master ~]# kubectl logs pods/testpod
172.25.254.100 3306

8.1.4.2 通过数据卷使用ConfigMap
[root@k8s-master ~]# vim testpod4.yml
apiVersion: v1
kind: Pod
metadata:labels:run: testpodname: testpod
spec:containers:- image: busyboxplus:latestname: testpodcommand:- /bin/sh- -c- cat /config/db_hostvolumeMounts: # 调用卷策略- name: config-volume # 卷名称mountPath: /configvolumes: # 声明卷的配置- name: config-volume # 卷名称configMap:name: testpodrestartPolicy: Never# 查看日志
[root@k8s-master ~]# kubectl logs testpod
172.25.254.100
8.1.4.3 利用ConfigMap填充pod的配置文件
# 建立配置文件模板
[root@k8s-master ~]# vim nginx.conf
server {listen 8000;server_name _;root /usr/share/nginx/html;index index.html;
}# 利用模板生成cm
root@k8s-master ~]# kubectl create cm nginx-conf --from-file nginx.conf
configmap/nginx-conf created
[root@k8s-master ~]# kubectl describe cm nginx-conf
Name: nginx-conf
Namespace: default
Labels: <none>
Annotations: <none>Data
====
nginx.conf:
----
server {listen 8000;server_name _;root /usr/share/nginx/html;index index.html;
}BinaryData
====Events: <none># 建立nginx控制器文件
[root@k8s-master ~]# kubectl create deployment nginx --image nginx:latest --replicas 1 --dry-run=client -o yaml > nginx.yml# 设定nginx.yml中的卷
[root@k8s-master ~]# vim nginx.yml
[root@k8s-master ~]# cat nginx.
cat: nginx.: 没有那个文件或目录
[root@k8s-master ~]# cat nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: nginxname: nginx
spec:replicas: 1selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- image: nginx:latestname: nginxvolumeMounts:- name: config-volumemountPath: /etc/nginx/conf.dvolumes:- name: config-volumeconfigMap:name: nginx-conf# 测试
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-8487c65cfc-cz5hd 1/1 Running 0 3m7s 10.244.2.38 k8s-node2 <none> <none>
[root@k8s-master ~]# curl 10.244.2.38:8000
8.1.4.4 通过热更新cm修改配置
[root@k8s-master ~]# kubectl edit cm nginx-conf
apiVersion: v1
data:nginx.conf: |server {listen 8080; # 端口改为8080server_name _;root /usr/share/nginx/html;index index.html;}
kind: ConfigMap
metadata:creationTimestamp: "2024-09-07T02:49:20Z"name: nginx-confnamespace: defaultresourceVersion: "153055"uid: 20bee584-2dab-4bd5-9bcb-78318404fa7a# 查看配置文件
[root@k8s-master ~]# kubectl exec pods/nginx-8487c65cfc-cz5hd -- cat /etc/nginx/conf.d/nginx.conf
server {listen 8080;server_name _;root /usr/share/nginx/html;index index.html;
}
配置文件修改后不会生效,需要删除pod后控制器会重建pod,这时就生效了
[root@k8s-master ~]# kubectl delete pods nginx-8487c65cfc-cz5hd pod "nginx-8487c65cfc-cz5hd" deleted[root@k8s-master ~]# curl 10.244.2.41:8080