一、ConfigMap实例演示
ConfigMap是一种比较特殊的存储卷,它的主要作用是用来存储配置信息的
1.1 创建configmap
编写configmap.yaml文件,内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap
namespace: dev
data:
info: |
username: admin
password: admin123
然后使用如下命令创建configmap
[root@master volume]# kubectl apply -f configmap.yaml
namespace/dev created
configmap/configmap created
[root@master volume]#
查看创建的configmap
[root@master volume]# kubectl get configmap -n dev
NAME DATA AGE
configmap 1 43s
[root@master volume]#
通过如下命令查看configmap的详细信息
[root@master volume]# kubectl describe configmap configmap -n dev
Name: configmap
Namespace: dev
Labels: <none>
Annotations: <none>
Data
====
info:
----
username: admin
password: admin123
Events: <none>
[root@master volume]#
1.2 configmap挂载到pod上
编辑pod_configmap.yaml文件,内容如下:
apiVersion: v1
kind: Pod
metadata:
name: pod-configmap
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
volumeMounts:
- name: config
mountPath: /configmap/config
volumes:
- name: config
configMap:
name: configmap
使用如下命令创建pod
[root@master volume]# kubectl apply -f pod_configmap.yaml
pod/pod-configmap created
[root@master volume]#
查看创建pod资源
[root@master volume]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-configmap 1/1 Running 0 4m29s
[root@master volume]#
如下进入pod中,查看挂载到的配置文件
[root@master volume]# kubectl exec -it pod-configmap -n dev /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# cd /configmap/config
# ls
info
# cat info
username: admin
password: admin123
#
1.3 configmap实现动态更新
编辑configmap.yaml文件,如下,将密码修改为123456
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap
namespace: dev
data:
info: |
username: admin
password: 123456
然后使用如下命令更新configmap
[root@master volume]# kubectl apply -f configmap.yaml
namespace/dev unchanged
configmap/configmap configured
[root@master volume]#
稍微等一会,然后继续查看pod中的info文件中的内容,发现此时已经更新了
# cat info
username: admin
password: 123456
#
至此,configmap即完成了配置文件的动态更新