본문 바로가기
INFRA/DevOps

[K8s] ConfigMap

by BTC_프로틴 2023. 1. 16.

ConfigMap이란?

  • 구성 설정이나 환경 설정 정보들을 한 곳에 모아서 관리해주는 API
  • 단, value는 1MiB를 초과할 수 없음

<이미지 출처 : [따배쿠] 10. Kubernetes ConfigMap >

 

ConfigMap 생성

  • Configmap 생성
kubectl create configmap ttabae-config --from-literal=INTERVAL=2 --from-literal=OPTION=BOY --from-file=config.dir/
  • Configmap 확인
kubectl get configmaps ttabae-config
NAME            DATA   AGE
ttabae-config   3      77s
kubectl describe configmaps ttabae-config
Name:         ttabae-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
OPTION:
----
BOY
nginx-config.conf:
----
server {
    listen   80;
    server_name  www.example.com;

    gzip on;
    gzip_types text/plain application/xml;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

INTERVAL:
----
2

BinaryData
====

Events:  <none>
  • Configmap 수정
kubectl edit configmaps ttabae-config

 

ConfigMap의 일부분을 적용하기

  • rig, boxex command를 사용하는 script 생성
vi genid.sh
#!/bin/bash
mkdir -p /webdata
while true
do
  /usr/bin/rig | /usr/bin/boxes -d $OPTION  > /webdata/index.html
  sleep $INTERVAL     #ConfigMap에 설정되어 있는 key:value
done
  • dockerfile 생성
vi Dockerfile
FROM ubuntu:18.04
RUN apt-get update ; apt-get -y install rig boxes
ENV INTERVAL 5          # configmap에 존재하는 value 값으로 대체됨
ENV OPTION stone        # configmap에 존재하는 value 값으로 대체됨
ADD genid.sh /bin/genid.sh
RUN chmod +x /bin/genid.sh
ENTRYPOINT ["/bin/genid.sh"]
  • ConfigMap의 일부분을 적용한 pod 생성 및 확인
vi genid.yaml
apiVersion: v1
kind: Pod
metadata:
  name: genid-stone
spec:
  containers:
  - image: smlinux/genid:env
    env:
    - name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: ttabae-config
          key: INTERVAL
    name: fakeid
    volumeMounts:
    - name: html
      mountPath: /webdata
  - image: nginx:1.14
    name: web-server
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
  volumes:
  - name: html
    emptyDir: {}
kubectl apply -f genid.yaml
kubectl get pods -o wide
NAME          READY   STATUS    RESTARTS   AGE   IP              NODE                NOMINATED NODE   READINESS GATES
genid-stone   2/2     Running   0          22s   192.168.11.84   node1.example.com   <none>           <none>
curl 192.168.11.84
+----------------------+
| Raymundo Hart        |
| 848 Ashland St       |
| Arlington, TX  76010 |
| (817) xxx-xxxx       |
+----------------------+

⚠️ ConfigMap에서 INTERVAL의 값을 변경하면 변경한 시간(초) 만큼의 term 후에 출력 메세지 변경

 

ConfigMap의 전체를 적용하기

  • ConfigMap의 전체를 적용하는 pod 생성 및 확인
vi genid-whole.yaml
apiVersion: v1
kind: Pod
metadata:
  name: genid-boy
spec:
  containers:
  - image: smlinux/genid:env
    envFrom:            # configmap 전체를 적용하기 때문에 환경변수 기재할 필요 없음
    - configMapRef:
        name: ttabae-config
    name: fakeid
    volumeMounts:
    - name: html
      mountPath: /webdata
  - image: nginx:1.14
    name: web-server
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
  volumes:
  - name: html
    emptyDir: {}
kubectl get pods -o wide

NAME          READY   STATUS              RESTARTS   AGE     IP              NODE                NOMINATED NODE   READINESS GATES
genid-boy     0/2     ContainerCreating   0          2s      <none>          node2.example.com   <none>           <none>
genid-stone   2/2     Running             0          7m31s   192.168.11.84   node1.example.com   <none>           <none>
curl 192.168.221.18
         .-"""-.
        / .===. \\
        \\/ 6 6 \\/
        ( \\___/ )
  __ooo__\\_____/______
 /                    \\
| Raul Hudson          |
| 345 West Street Terr |
| Gary, IN  46401      |
| (219) xxx-xxxx       |
 \\_______________ooo__/
        |  |  |
        |_ | _|
        |  |  |
        |__|__|
        /-'Y'-\\
       (__/ \\__)
kubectl exec genid-boy -- env
Defaulted container "fakeid" out of: fakeid, web-server
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=genid-boy
INTERVAL=2
OPTION=BOY
nginx-config.conf=server {
    listen   80;
    server_name  www.example.com;

    gzip on;
    gzip_types text/plain application/xml;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
...

 

ConfigMap의 볼륨으로 적용하기

  • ConfigMap의 key, value를 pod의 container에 볼륨마운트 및 확인
vi genid-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: genid-volume
spec:
  containers:
  - image: smlinux/genid:env
    env:
    - name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: ttabae-config
          key: INTERVAL
    name: fakeid-generator
    volumeMounts:
    - name: html
      mountPath: /webdata
  - image: nginx:1.14
    name: web-server
    ports:
    - containerPort: 80
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    - name: config
      mountPath: /etc/nginx/conf.d
      readOnly: true
  volumes:
  - name: html
    emptyDir: {}
  - name: config
    configMap:
      name: ttabae-config
      items:
      - key: nginx-config.conf
        path: nginx-config.conf
kubectl create -f genid-volume.yaml
kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE     IP               NODE                NOMINATED NODE   READINESS GATES
genid-boy      2/2     Running   0          9m51s   192.168.221.18   node2.example.com   <none>           <none>
genid-stone    2/2     Running   0          17m     192.168.11.84    node1.example.com   <none>           <none>
genid-volume   2/2     Running   0          11s     192.168.11.85    node1.example.com   <none>           <none>
kubectl exec -it genid-volume -c web-server -- /bin/bash
cat /etc/nginx/conf.d/nginx-config.conf
server {
    listen   80;
    server_name  www.example.com;

    gzip on;
    gzip_types text/plain application/xml;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
ls -l /etc/nginx/conf.d/
total 0
lrwxrwxrwx 1 root root 24 Jun 10 08:44 nginx-config.conf -> ..data/nginx-config.conf

 

 

참고 :

[따배쿠] 10. Kubernetes ConfigMap

'INFRA > DevOps' 카테고리의 다른 글

[K8s] Multi-master - HA Kuberenetes cluster 운영  (0) 2023.01.30
[k8s] Secret  (1) 2023.01.25
[k8s] Canary Deployment  (0) 2023.01.09
[K8s] Annotation  (0) 2023.01.03
[k8s] Label  (0) 2022.12.29

댓글