컨피그맵의 값을 pod 내부 파일로 마운트해서 사용

컨피그맵의 모든 키-쌍 데이터를 파드에 마운트

vagrant@ubuntu:~$ vi volume-mount-configmap.yml
`YAML
apiVersion: v1
kind: Pod
metadata:
  name: configmap-volume-pod
spec:
  containers:
    - name: my-container
      image: busybox
      args: ["tail", "-f", "/dev/null"]
      volumeMounts:                  ⇐ #1에서 정의한 볼륨을 컨테이너 내부의 어떤 디렉터리에 마운트할 것인지 명시
        - name: configmap-volume     ⇐ 컨피그맵 볼룸의 이름 (#1에서 정의한 이름)
          mountPath: /etc/config     ⇐ 컨피그맵 파일이 위치할 경로

  volumes:                           ⇐ #1 사용할 볼륨 목록 
    - name: configmap-volume           
      configMap:
        name: start-k8s              ⇐ 컨피그맵 이름
`

pod 생성

vagrant@ubuntu:~$ kubectl apply -f volume-mount-configmap.yml
pod/configmap-volume-pod created

pod 생성 확인

vagrant@ubuntu:~$ kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
configmap-volume-pod                   1/1     Running   0          27s
container-env-example                  1/1     Running   0          53m
hostname-deployment-7dfd748479-7zdv7   1/1     Running   0          20h
hostname-deployment-7dfd748479-pp8x4   1/1     Running   0          20h
hostname-deployment-7dfd748479-rtgzv   1/1     Running   0          20h

pod의 /etc/config 확인

vagrant@ubuntu:~$ kubectl exec configmap-volume-pod -- ls -l /etc/config
total 0
lrwxrwxrwx    1 root     root            16 Sep 22 02:33 container -> ..data/container
lrwxrwxrwx    1 root     root            10 Sep 22 02:33 k8s -> ..data/k8s

file 내용 확인

vagrant@ubuntu:~$ kubectl exec configmap-volume-pod -- cat /etc/config/container
docker                        

vagrant@ubuntu:~$ kubectl exec configmap-volume-pod -- cat /etc/config/k8s
kubernetes

컨피그맵에 키는 파일명으로, 값은 파일의 내용으로 변경되어서 전달

원하는 키-값 쌍의 데이터만 선택해서 pod로 마운트

vagrant@ubuntu:~$ cp volume-mount-configmap.yml selective-volume-configmap.yml
vagrant@ubuntu:~$ vi selective-volume-configmap.yml

`YAML
apiVersion: v1
kind: Pod
metadata:
  name: configmap-volume-pod
spec:
  containers:
    - name: my-container
      image: busybox
      args: ["tail", "-f", "/dev/null"]
      volumeMounts:
        - name: configmap-volume
          mountPath: /etc/config
  volumes:
    - name: configmap-volume
      configMap:
        name: start-k8s
        items:
        - key: k8s                ⇐ 가져올 키를 명시
          path: k8s_fullname      ⇐ 키 값을 저장할 파일명
`

앞에서 생성한 pod를 삭제

vagrant@ubuntu:~$ kubectl delete -f volume-mount-configmap.yml
pod "configmap-volume-pod" deleted

파드 생성 및 확인

vagrant@ubuntu:~$ kubectl apply -f selective-volume-configmap.yml
pod/configmap-volume-pod created
vagrant@ubuntu:~$ kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
configmap-volume-pod                   1/1     Running   0          8s
container-env-example                  1/1     Running   0          63m
hostname-deployment-7dfd748479-7zdv7   1/1     Running   0          20h
hostname-deployment-7dfd748479-pp8x4   1/1     Running   0          20h
hostname-deployment-7dfd748479-rtgzv   1/1     Running   0          20h

파드(컨테이너) 내부의 파일 생성 여부 확인

vagrant@ubuntu:~$ kubectl exec configmap-volume-pod -- ls /etc/config
k8s_fullname

vagrant@ubuntu:~$ kubectl exec configmap-volume-pod -- cat /etc/config/k8s_fullname
kubernetes

 

반응형

+ Recent posts