创建方式
通过命令行指定参数创建,
--from-literal
1
kubectl create configmap test --from-literal redis.host=1234 --from-literal redis.password=123456
通过指定文件/目录创建,
--from-file=<文件>/<文件夹>
文件
1
kubectl create configmap test --from-file test.properties
文件夹
1
kubectl create configmap test --from-file config
查看
1
kubectl get configmap test -o yaml
1
2
3
4
5
6
7
8
9
10
11
12
13apiVersion: v1
data:
redis.properties: |
redis.host=123
redis.password=123456
kind: ConfigMap
metadata:
creationTimestamp: "2021-09-13T03:55:54Z"
name: test
namespace: default
resourceVersion: "5194050"
selfLink: /api/v1/namespaces/default/configmaps/test
uid: cf2d7a6f-c024-4285-8c46-f925e699a895通过标准的yaml创建
1 |
|
使用ConfigMap
三种方式:
使用环境变量的方式,直接传递给Pod
- 使用ConfigMap中指定的Key
- 使用ConfigMap中所有的Key
作为volume的方式挂载到pod内
通过环境变量
引用单个字段
1 | apiVersion: v1 |
整个configMap的Key和value都作为环境变量
1 | apiVersion: v1 |
挂载到容器中
1 | apiVersion: v1 |
进入到容器中/app/config
路径,会看到存在文件redis.properties
如果想要指定挂载的路径,而不是使用ConfigMap中的key值,可以使用path
替换key
1 | volumes: |
备注:
- 删除configmap后原pod不受影响;然后再删除pod后,重启的pod的events会报找不到cofigmap的volume;
- pod起来后再通过kubectl edit configmap …修改configmap,过一会pod内部的配置也会刷新。
- 在容器内部修改挂进去的配置文件后,过一会内容会再次被刷新为原始configmap内容
还可以添加给mount添加subPath字段
1 | volumeMounts: |
关于subPath,这里不是指的挂载路径(/app/config
)的子路径,而是指的configMap中的key,关于subPath的描述,可以自行百度,个人理解指的是挂载的容器卷的子路径,而不是即将要挂载的地方的子路径
备注:
- 删除configmap后原pod不受影响;然后再删除pod后,重启的pod的events会报找不到cofigmap的volume。
- pod起来后再通过kubectl edit configmap …修改configmap,pod内部的配置也会自动刷新。
- 在容器内部修改挂进去的配置文件后,内容可以持久保存,除非杀掉再重启pod才会刷回原始configmap的内容。
- subPath必须要与configmap中的key同名。
- mountPath如/tmp/prefix:
<1>当/tmp/prefix不存在时(备注:此时/tmp/prefix和/tmp/prefix/无异),会自动创建prefix文件并把value写进去;
<2>当/tmp/prefix存在且是个文件时,里面内容会被configmap覆盖;
<3>当/tmp/prefix存在且是文件夹时,无论写/tmp/prefix还是/tmp/prefix/都会报错
参考和引用: