본문 바로가기

Kubernetes

[Kubernetes] ConfigMap

반응형

ConfigMap 생성

 - ConfigMap : 컨테이너 구성 정보를 한곳에 모아서 관리

 

https://www.youtube.com/watch?v=xyGTvkKzrB4&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=34

 

kubectl create configmap NAME [--from-file=source] [--from-liternal=key1=value1]

 

 ttabae-config 이름으로 ConfigMap 생성 및 확인

# kubectl create configmap ttabae-config --from-literal=INTERVAL=2 --from-literal=OPTION=boy --from-file=config.dir/
configmap/ttabae-config created

# kubectl get configmaps ttabae-config 
NAME            DATA   AGE
ttabae-config   3      2m22s

# kubectl describe configmaps ttabae-config 
Name:         ttabae-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
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;
    }
}


BinaryData
====

Events:  <none>

2. ConfigMap의 일부분을 적용하기

 - 생성한 ConfigMap의 Key를 pod의 컨테이너에 적용

# cat Dockerfile 
FROM ubuntu:18.04
RUN apt-get update ; apt-get -y install rig boxes
ENV INTERVAL 5
ENV OPTION stone
ADD genid.sh /bin/genid.sh
RUN chmod +x /bin/genid.sh
ENTRYPOINT ["/bin/genid.sh"]

# cat genid.sh 
#!/bin/bash
mkdir -p /webdata
while true
do
  /usr/bin/rig | /usr/bin/boxes -d $OPTION  > /webdata/index.html
  sleep $INTERVAL
done

 

rig 명령어는 실행할 때마다 랜덤한 fakeID를 생성해줌 (apt install rig 설치 필요)

# rig
Elinor Alford
112 Southern Dr
Berkeley, CA  94704
(510) xxx-xxxx

# rig
Randolph Dunn
387 Southern Dr
Mentor, OH  44060
(216) xxx-xxxx

 

boxes는 박스 형태로 출력해줌  (apt install boxes 설치 필요)

# rig | boxes 
/************************/
/* Cindy Owen           */
/* 813 New First Rd     */
/* Sunnyvale, CA  94086 */
/* (408) xxx-xxxx       */
/************************/
# rig | boxes -d boy
        .-"""-.
       / .===. \
       \/ 6 6 \/
       ( \___/ )
  _ooo__\_____/______
 /                   \
| Willis Owen         |
| 475 Tulip St        |
| Phoenix, AZ  85026  |
| (602) xxx-xxxx      |
 \_______________ooo_/
        |  |  |
        |_ | _|
        |  |  |
        |__|__|
        /-'Y'-\
       (__/ \__)
# rig | boxes -d stone
+-------------------+
| Geneva Lopez      |
| 815 Hamlet St     |
| Beloit, WI  53511 |
| (608) xxx-xxxx    |
+-------------------+

 

genid.yaml에서 ttabae-config 로 생성한 ConfigMap이 적용될 수 있도록 설정

    env:
    - name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: ttabae-config
          key: INTERVAL 

 → key: INTERVAL 만 적용

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: {}

 

ConfigMap의  key: INTERVAL 값인 2가 적용되어 2초마다 랜덤한 값 확인 가

# kubectl apply -f genid.yaml
pod/genid-stone created
root@master:~/10# kubectl get pods
NAME          READY   STATUS              RESTARTS   AGE
genid-stone   0/2     ContainerCreating   0          5s
root@master:~/10# kubectl get pods
NAME          READY   STATUS              RESTARTS   AGE
genid-stone   0/2     ContainerCreating   0          7s
# kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
genid-stone   2/2     Running   0          42s

# kubectl get pods -o wide
NAME          READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
genid-stone   2/2     Running   0          3m15s   10.44.0.1   node2.example.com   <none>           <none>

# curl 10.44.0.1
+--------------------+
| Rolland Dean       |
| 209 Henly Dr       |
| Orlando, FL  32802 |
| (407) xxx-xxxx     |
+--------------------+
# curl 10.44.0.1
+--------------------+
| Twila Rosales      |
| 940 Potter Rd      |
| Kinston, NC  28501 |
| (919) xxx-xxxx     |
+--------------------+
# curl 10.44.0.1
+---------------------+
| Mae Cole            |
| 905 Lemoyer Blvd    |
| Appleton, WI  54911 |
| (414) xxx-xxxx      |
+---------------------+

# kubectl delete pod --all
pod "genid-stone" deleted

 

edit 명령어를 통해서 INTERVAL 값을 2 → 10 으로 변경

# kubectl edit configmaps ttabae-config

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  INTERVAL: "10"
  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;
        }
    }
kind: ConfigMap
metadata:
  creationTimestamp: "2022-12-20T13:21:04Z"
  name: ttabae-config
  namespace: default
  resourceVersion: "142714"
  uid: 4d54a944-9b6b-41cb-af6b-02229a77bc9e

 

다시 genid.yaml 을 통해 pod 실행 후 10초가 지나면 내용이 변경된 것을 확인 가능

# kubectl apply -f genid.yaml
pod/genid-stone created

# kubectl get pods -o wide
NAME          READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
genid-stone   2/2     Running   0          7s    10.44.0.1   node2.example.com   <none>           <none>

# curl 10.44.0.1
+--------------------+
| Agustin Haynes     |
| 497 Tulip St       |
| Hampton, VA  23670 |
| (804) xxx-xxxx     |
+--------------------+
# curl 10.44.0.1
+--------------------+
| Agustin Haynes     |
| 497 Tulip St       |
| Hampton, VA  23670 |
| (804) xxx-xxxx     |
+--------------------+
# curl 10.44.0.1
+-----------------------+
| Kaitlin Booker        |
| 3 Beley Rd            |
| Clearwater, FL  33575 |
| (813) xxx-xxxx        |
+-----------------------+

 

"confingmap의 특정 key 값만 pod 에 적용할 수 있다는 것을 확인 할 수 있다."

3. ConfigMap의 전체를 적용하기

 - 생성한 ConfigMap 전체 key를 pod의 컨테이너에 적용

 

genid-whole.yaml

apiVersion: v1
kind: Pod
metadata:
  name: genid-boy
spec:
  containers:
  - image: smlinux/genid:env
    envFrom:
    - 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: {}

 

genid-whole.yaml 실행 후 pod 확인

# kubectl create -f genid-whole.yaml 
pod/genid-boy created

# kubectl get pods -o wide
NAME          READY   STATUS              RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
genid-boy     0/2     ContainerCreating   0          13s     <none>      node1.example.com   <none>           <none>
genid-stone   2/2     Running             0          6m46s   10.44.0.1   node2.example.com   <none>           <none>

# kubectl get pods -o wide
NAME          READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
genid-boy     2/2     Running   0          17s     10.36.0.2   node1.example.com   <none>           <none>
genid-stone   2/2     Running   0          6m50s   10.44.0.1   node2.example.com   <none>           <none>

# 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=10
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;
    }
}

KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
HOME=/root

# curl 10.36.0.2
         .-"""-.
        / .===. \
        \/ 6 6 \/
        ( \___/ )
  __ooo__\_____/_______
 /                     \
| Jackie Deleon         |
| 611 Old Pinbrick Dr   |
| Burlington, NC  27215 |
| (919) xxx-xxxx        |
 \________________ooo__/
         |  |  |
         |_ | _|
         |  |  |
         |__|__|
         /-'Y'-\
        (__/ \__)

4. ConfigMap을 볼륨으로 적용하기

 - 생성한 ConfigMap의 key를 pod의 컨테이너에 볼륨 마운트 하기

 

genid-volume.yaml

- configmap이 mountPath: /etc/nginx/conf.d 로 볼륨 마운트 되게 설

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

 

genid-volume.yaml 실행 후 genid-volume 컨테이너에 접근하여 /etc/nginx/conf.d 디렉토리 내에 configmap 파일이 있는지 확인 가능

# kubectl create -f genid-volume.yaml
pod/genid-volume created

# kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
genid-boy      2/2     Running   0          10m   10.36.0.2   node1.example.com   <none>           <none>
genid-stone    2/2     Running   0          16m   10.44.0.1   node2.example.com   <none>           <none>
genid-volume   2/2     Running   0          6s    10.44.0.2   node2.example.com   <none>           <none>

# kubectl exec -it genid-volume -c web-server -- /bin/bash

root@genid-volume:/# cd /etc/nginx/conf.d/
root@genid-volume:/etc/nginx/conf.d# ls
nginx-config.conf
root@genid-volume:/etc/nginx/conf.d# cat 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;
    }
}
root@genid-volume:/etc/nginx/conf.d# ls -l
total 0
lrwxrwxrwx 1 root root 24 Dec 20 14:07 nginx-config.conf -> ..data/nginx-config.conf

 

 

[참고]

- https://www.youtube.com/watch?v=xyGTvkKzrB4&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=34

반응형

'Kubernetes' 카테고리의 다른 글

[Kubernetes] Istio 정리  (0) 2023.01.25
[Kubernetes] Secret  (0) 2022.12.21
[Kubernetes] kubernetes Canary Deployment  (0) 2022.12.17
[Kubernetes] Annotation  (0) 2022.12.17
[Kubernetes] kubernetes node label  (0) 2022.12.17