在前一篇文章介绍了在Kubernetes集群中使用ConfigMap的环境变量引用的方式,这篇将继续介绍卷引用的方法。
环境准备
本文使用Kubernetes 1.17,可参看下文进行快速环境搭建:
ConfigMap介绍
可参看下文说明了解ConfigMap创建、查询和删除等操作的方法。
使用示例
这里以如下用户名和ID为例,如何在Kubernetes中创建和使用Configmap进行说明
变量说明 | 标识符 | 设定值 |
---|---|---|
用户名 | user.name | liumiao |
用户ID | user.id | 1003 |
创建ConfigMap
准备如下设定内容
[root@host131 config]# kubectl get cm
No resources found in default namespace.
[root@host131 config]#
[root@host131 config]# cat user.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: user-configmap
namespace: default
data:
user.name: liumiao
user.id: '1001'
[root@host131 config]#
使用kubectl create 命令创建ConfigMap,执行日志如下所示:
[root@host131 config]# kubectl create -f user.yml
configmap/user-configmap created
[root@host131 config]#
确认ConfigMap
使用kubectl get configmap即可获取已经创建的ConfigMap信息
[root@host131 config]# kubectl get cm
NAME DATA AGE
user-configmap 2 5s
[root@host131 config]#
[root@host131 config]# kubectl describe cm user-configmap
Name: user-configmap
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
user.id:
----
1001
user.name:
----
liumiao
Events: <none>
[root@host131 config]#
卷引用方式
准备如下的pod的示例yaml文件:
[root@host131 config]# cat busybox-pod-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: configmap-test-pod-volume
spec:
containers:
- name: busybox-container
image: busybox:latest
command: ["sleep", "1000"]
volumeMounts:
- name: user-config-volume
mountPath: /etc/user-config
volumes:
- name: user-config-volume
configMap:
name: user-configmap
restartPolicy: Never
[root@host131 config]#
- 生成pod
[root@host131 config]# kubectl create -f busybox-pod-volume.yaml
pod/configmap-test-pod-volume created
[root@host131 config]#
- 确认生成的pod
[root@host131 config]# kubectl get pods |grep volume
configmap-test-pod-volume 1/1 Running 0 42s
[root@host131 config]#
- 挂载的ConfigMap信息确认
[root@host131 config]# kubectl exec -it configmap-test-pod-volume sh
/ # cd /etc/user-config/
/etc/user-config # ls
user.id user.name
/etc/user-config # cat user.id user.name
1001liumiao/etc/user-config #
/etc/user-config #
可以看到user-configmap已经被挂载至示例pod中,其中所设定的user.id和user.name的内容也能够正常地查看。