AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
前面我们提到过可以使用参数`--from-file`从文件中创建 ConfigMap,这个时候文件名就成为了 ConfigMap 中的 key,文件中的内容就是 key 对应的 value。 #### 使用 ConfigMap 中的配置数据挂载到卷 Pod YAML 文件定义中,在`volumes`指定 ConfigMap 的名字,在`volumeMounts.mountPath`指定具体挂载到容器中的目录。 在`/home/shiyanlou/pods`目录下新建`pod-configmap-volume.yaml`文件,并写入如下内容: ~~~yaml apiVersion: v1 kind: Pod metadata: name: dapi-test-pod-5 spec: containers: - name: test-container-5 image: busybox command: ['/bin/sh', '-c', 'ls /etc/config'] #查看是否成功挂载 volumeMounts: - name: config-volume # 引用 volume 的名称 mountPath: /etc/config # 挂载到容器内的目录路径 volumes: - name: config-volume # 定义 volume 的名称 configMap: name: config-4 # 使用 ConfigMap "config-4" restartPolicy: Never ~~~ 执行创建: ~~~yaml $ kubectl create -f pods/pod-configmap-volume.yaml pod/dapi-test-pod-5 created ~~~ 查看日志,获取容器中 /etc/config 目录下的文件列表: ~~~bash $ kubectl logs dapi-test-pod-5 SPECIAL_LEVEL SPECIAL_TYPE ~~~ 这样我们可以很明确的发现,ConfigMap 配置信息的 key 成为了挂载到容器后的文件名。如果容器在 /etc/config 目录下存在文件,挂载后原有文件将会被覆盖。 #### 卷挂载时明确存储路径 使用`path`字段可以明确挂载到容器后的文件名。 在`/home/shiyanlou/pods`目录下新建`pod-configmap-volume-specific-key.yaml`文件,并写入如下内容: ~~~yaml apiVersion: v1 kind: Pod metadata: name: dapi-test-pod-6 spec: containers: - name: test-container-6 image: busybox command: ['/bin/sh', '-c', 'cat /etc/config/keys'] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: config-4 items: - key: SPECIAL_LEVEL path: keys # SPECIAL_LEVEL 的 value 以 keys 文件名进行挂载 restartPolicy: Never ~~~ 执行创建: ~~~bash $ kubectl create -f pods/pod-configmap-volume-specific-key.yaml pod/dapi-test-pod-6 created ~~~ 查看日志,检查 /etc/config/keys 文件中存储的值是否为 SPECIAL\_LEVEL 对应的值: ~~~bash $ kubectl logs dapi-test-pod-6 very ~~~