profile image

L o a d i n g . . .

컨피그맵

배경

개별 Pod-definiton file에서 env 키로 환경변수를 관리할 수 있다.

kind: Pod
spec:
 containers:
 - image: luksa/fortune:env
   env:
   - name: INTERVAL
     value: "30"
   name: html-generator
...

하지만 Pod-definiton 파일이 많아질 경우, 환경변수 데이터 관리가 복잡해질 수 있다.

컨피그맵은 이를 중앙에서 손쉽게 관리할 수 있도록 해준다. Pod를 생성할 때, config map을 통해 환경변수를 주입해줄 수 있다. 

컨피그맵 소개

컨피그맵 항목을 환경변수로 컨테이너로 전달

apiVersion: v1
kind: Pod
metadata:
  name: fortune-env-from-configmap
spec:
  containers:
  - image: luksa/fortune:env
    env:
    - name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: fortune-config
          key: sleep-interval
...

 

컨피그맵 생성

$ kubectl create configmap my-config
➥   --from-file=foo.json
➥   --from-file=bar=foobar.conf
➥   --from-file=config-opts/
➥   --from-literal=some=thing

아래 그림은 위 명령어의 결과를 나타낸 그림이다. 

위 명령어 결과

 

컨피그맵 볼륨을 사용해 컨피그맵 항목을 파일로 노출

configmap-files/ 디렉토리와 그 안의 파일

↓ 컨피그맵 생성 명령어

$ kubectl create configmap fortune-config --from-file=configmap-files

-> configmap "fortune-config" created

↓ 생성한 컨피그맵 yaml 파일

$ kubectl get configmap fortune-config -o yaml
apiVersion: v1
data:
  my-nginx-config.conf: |
    server {
      listen              80;
      server_name         www.kubia-example.com;

      gzip on;
      gzip_types text/plain application/xml;

      location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
      }
    }
  sleep-interval: |
    25
kind: ConfigMap
...

 

Pod 에게 configMap 항목 전달

↓ 컨피그맵 항목을 파일로 마운트한 Pod

apiVersion: v1
kind: Pod
metadata:
  name: fortune-configmap-volume
spec:
  containers:
  - image: nginx:alpine
    name: web-server
    volumeMounts:
    ...
    - name: config
      mountPath: /etc/nginx/conf.d
      readOnly: true
    ...
  volumes:
  ...
  - name: config
    configMap:
      name: fortune-config
  ...

 

복사했습니다!