K8skubernetes

508.【kubernetes】ConfigMap: 在 Pod

2022-12-04  本文已影响0人  七镜

一、创建并启动 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 的限制条件

上一篇 下一篇

猜你喜欢

热点阅读