K8s记录-ConfigMap

创建方式

  • 通过命令行指定参数创建,--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
    13
    apiVersion: 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
2
3
4
5
6
7
8
9
10
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
namespace: default
data:
# 配置信息
redis.host: 123
redis.password: 123456

使用ConfigMap

三种方式:

  • 使用环境变量的方式,直接传递给Pod

    • 使用ConfigMap中指定的Key
    • 使用ConfigMap中所有的Key
  • 作为volume的方式挂载到pod内

通过环境变量

引用单个字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: k8s-test
image: test:1.0.0
env:
- name: REDIS_HOST
valueFrom:
configMapKeyRef:
name: test
key: redis.host
- name: REDIS_PASSWORD
valueFrom:
configMapKeyRef:
name: test
key: redis.password
restartPolicy: Never

整个configMap的Key和value都作为环境变量

1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: k8s-test
image: test:1.0.0
envFrom:
- configMapRef:
name: test
restartPolicy: Never

挂载到容器中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: k8s-test
image: test:1.0.0
volumeMounts:
- name: test-volomn
mountPath: /app/config
volumes:
- name: test-volomn
configMap:
name: test
restartPolicy: Never

进入到容器中/app/config路径,会看到存在文件redis.properties

如果想要指定挂载的路径,而不是使用ConfigMap中的key值,可以使用path替换key

1
2
3
4
5
6
7
volumes:
- name: test-volomn
configMap:
name: test
items:
- key: redis.propertie
path: redis.conf

备注:

  1. 删除configmap后原pod不受影响;然后再删除pod后,重启的pod的events会报找不到cofigmap的volume;
  2. pod起来后再通过kubectl edit configmap …修改configmap,过一会pod内部的配置也会刷新。
  3. 在容器内部修改挂进去的配置文件后,过一会内容会再次被刷新为原始configmap内容

还可以添加给mount添加subPath字段

1
2
3
4
volumeMounts:
- name: test-volomn
mountPath: /app/config
subPath: redis.properties

关于subPath,这里不是指的挂载路径(/app/config)的子路径,而是指的configMap中的key,关于subPath的描述,可以自行百度,个人理解指的是挂载的容器卷的子路径,而不是即将要挂载的地方的子路径

备注:

  1. 删除configmap后原pod不受影响;然后再删除pod后,重启的pod的events会报找不到cofigmap的volume。
  2. pod起来后再通过kubectl edit configmap …修改configmap,pod内部的配置也会自动刷新。
  3. 在容器内部修改挂进去的配置文件后,内容可以持久保存,除非杀掉再重启pod才会刷回原始configmap的内容。
  4. subPath必须要与configmap中的key同名。
  5. mountPath如/tmp/prefix:
    <1>当/tmp/prefix不存在时(备注:此时/tmp/prefix和/tmp/prefix/无异),会自动创建prefix文件并把value写进去;
    <2>当/tmp/prefix存在且是个文件时,里面内容会被configmap覆盖;
    <3>当/tmp/prefix存在且是文件夹时,无论写/tmp/prefix还是/tmp/prefix/都会报错

参考和引用:

https://blog.csdn.net/liukuan73/article/details/79492374

文章作者: C.c
文章链接: https://liquidcat.cc/k8s-configMap.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Me