更新时间:2022-09-27 gmt 08:00

configmap-凯发k8国际娱乐官网入口

configmap是一种用于存储应用所需配置信息的资源类型,用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件。

通过configmap可以方便的做到配置解耦,使得不同环境有不同的配置。

创建configmap

下面示例创建了一个名为configmap-test的configmap,configmap的配置数据在data字段下定义。

apiversion: v1
kind: configmap
metadata:
  name: configmap-test
data:                     # 配置数据
  property_1: hello
  property_2: world

在环境变量中引用configmap

configmap最为常见的使用方式就是在环境变量和volume中引用。

例如下面例子中,引用了configmap-test的property_1,将其作为环境变量example_property_1的值,这样容器启动后里面example_property_1的值就是property_1的值,即“hello”

apiversion: v1
kind: pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx:alpine
    name: container-0
    resources:
      limits:
        cpu: 100m
        memory: 200mi
      requests:
        cpu: 100m
        memory: 200mi
    env:
    - name: example_property_1
      valuefrom:
        configmapkeyref:          # 引用configmap
          name: configmap-test
          key: property_1
  imagepullsecrets:
  - name: default-secret

在volume中引用configmap

在volume中引用configmap,就是通过文件的方式直接将configmap的每条数据填入volume,每条数据是一个文件,键就是文件名,键值就是文件内容。

如下示例中,创建一个名为vol-configmap的volume,这个volume引用名为“configmap-test”的configmap,再将volume挂载到容器的“/tmp”路径下。pod创建成功后,在容器的“/tmp”路径下,就有两个文件property_1和property_2,它们的值分别为“hello”“world”

apiversion: v1
kind: pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx:alpine
    name: container-0
    resources:
      limits:
        cpu: 100m
        memory: 200mi
      requests:
        cpu: 100m
        memory: 200mi
    volumemounts:
    - name: vol-configmap           # 挂载名为vol-configmap的volume
      mountpath: "/tmp"
  imagepullsecrets:
  - name: default-secret
  volumes:
  - name: vol-configmap
    configmap:                      # 引用configmap
      name: configmap-test
分享:
网站地图