508. 【kubernetes】ConfigMap-在-Pod-中挂载文件(非目录)

本文介绍了如何在Kubernetes环境中创建ConfigMap,包括通过YAML文件和从文件创建,并展示了如何将ConfigMap挂载到Pod中作为应用的配置文件。同时,提到了使用ConfigMap的一些限制,如必须先于Pod创建,受命名空间限制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、创建并启动 ConfigMap

  1. 方法一:
[root@k8s0 test_config_map]#  cat >cm-qijing.yaml <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-qijing
data:
  key-qijing-file.txt: |
    from key-qijing-file.txt
    hello qijing

EOF
  1. 方法二:
[root@k8s0 test_config_map]#  cat >key-qijing-file.txt <<EOF
from key-qijing-file.txt
hello qijing
EOF
[root@k8s0 test_config_map]#  kubectl create cm cm-qijing --from-file=./key-qijing-file.txt 
configmap/cm-qijing created
  • 两种方式的结果都是一样的

二、挂载ConfigMap中的文件,创建并启动 Pod

[root@k8s0 test_config_map]#  cat test-cm-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: cm-test-pod
spec:
  containers:
  - name: cm-test
    image: busybox
    command: ["ls", "-l", "/tmp"]
    volumeMounts:
    - name: example
      mountPath: /tmp/key-qijing-file.txt
      subPath: key-qijing-file.txt
  volumes:
  - name: example
    configMap:
      name: cm-qijing
      items:
      - key: key-qijing-file.txt
        path: key-qijing-file.txt
  restartPolicy: Never
[root@k8s0 test_config_map]#  kubectl create -f test-cm-pod.yaml 
pod/cm-test-pod created
[root@k8s0 test_config_map]#  kubectl get po
NAME          READY   STATUS              RESTARTS   AGE
cm-test-pod   0/1     ContainerCreating   0          3s
[root@k8s0 test_config_map]#  kubectl get po
NAME          READY   STATUS      RESTARTS   AGE
cm-test-pod   0/1     Completed   0          4s
[root@k8s0 test_config_map]#  kubectl get po
NAME          READY   STATUS      RESTARTS   AGE
cloudwavedb   1/1     Running     0          38m
cm-test-pod   0/1     Completed   0          4s
[root@k8s0 test_config_map]#  kubectl logs -f cm-test-pod
total 4
-rw-r--r--    1 root     root            38 Nov 27 03:59 key-qijing-file.txt
  • 可以看到在容器内部已经能看到通过 ConfigMap 挂载的文件了。这个ConfigMap也是一个应用程序的配置文件解决方案。

三、使用 ConfigMap 的限制条件

  • ConfigMap 必须在 Pod 之前创建,Pod 才能引用它。
  • 如果 Pod 使用 envFrom 基于 ConfigMap 定义环境变量,则无效的变量名称将被忽略,并在事件中被记录为 InvalidVariableNames。
  • ConfigMap 受命名空间限制,只有处于相同命名空间中的 Pod 才可以引用它。
  • ConfigMap 无法用于静态 Pod。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值