Kubernetes Configmap

jaeyeon ha·2026년 3월 7일

[교육] Kubernetes

목록 보기
32/34

컨피그맵

컨테이너의 환경 변수와 같은 구성 정보들을 모아서 관리하는 기능 제공

  • 키:값 형식
  • 암호화가 필요 없는 일반 데이터를 저장하는 오브젝트
kubectl create configmap {컨피그맵_이름} --from-literal={Key1}={Value1} --from-literal={Key2}={Value2}
kubectl create configmap {컨피그맵_이름} --from-file={Key}={ValueFile}
kubectl create configmap {컨피그맵_이름} --from-file={KeyFile} # Key_file 내용이 Value 값

configmap 생성

명령어를 통해 configmap 생성

[root@master ~/kube/11/configmap]# echo 'localhost' > url.txt
[root@master ~/kube/11/configmap]# echo 'debug' > DEBUG_INFO
[root@master ~/kube/11/configmap]# kubectl create configmap test-config \
> --from-literal=DB_USER=webadmin \
> --from-literal=DB_UPASS=abcd1234 \
> --from-file=DB_URL=url.txt \
> --from-file=DEBUG_INFO
[root@master ~/kube/11/configmap]# kubectl describe configmaps test-config
Name:         test-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
DB_UPASS:
----
abcd1234
DB_URL:
----
localhost

DB_USER:
----
webadmin
DEBUG_INFO:
----
debug

BinaryData
====

Events:  <none>

yaml 파일을 통해 configmap 생성

[root@master ~/kube/11/configmap]# vi test-config.yaml
[root@master ~/kube/11/configmap]# cat test-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
data:
  DB_USER: webadmin
  DB_PASS: abcd1234
  DB_URL: localhost
  DEBUG_INFO: debug

[root@master ~/kube/11/configmap]# kubectl create -f test-config.yaml
configmap/test-config created
[root@master ~/kube/11/configmap]# kubectl get configmaps
NAME               DATA   AGE
kube-root-ca.crt   1      7d3h
test-config        4      5s
[root@master ~/kube/11/configmap]# kubectl describe configmaps test-config
Name:         test-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
DB_PASS:
----
abcd1234
DB_URL:
----
localhost
DB_USER:
----
webadmin
DEBUG_INFO:
----
debug

BinaryData
====

Events:  <none>

특정 configmap 값 사용하는 예제

smlinux/genid:env ▶ 임의의 명함 만들어주는 이미지

genid 이미지로 testpod 띄우기

[root@master ~/kube/11/configmap]# kubectl run testpod --image=smlinux/genid:env
pod/testpod created
[root@master ~/kube/11/configmap]# kubectl exec -it testpod -- bash
root@testpod:/# rig
Lynda May
16 Sharon Rd
Arlington, TX  76010
(817) xxx-xxxx
root@testpod:/# rig
Dean Conrad
309 Orange West
Ames, IA  50010
(515) xxx-xxxx
root@testpod:/# rig | boxes -d cat
            /\             /\
           |`\\_,--="=--,_//`|
           \ ."  :'. .':  ". /
          ==)  _ :  '  : _  (==
            |>/O\   _   /O\<|
            | \-"~` _ `~"-/ |
           >|`===. \_/ .===`|<
     .-"-.   \==='  |  '===/   .-"-.
.---{'. '`}---\,  .-'-.  ,/---{.'. '}---.
 )  `"---"`     `~-===-~`     `"---"`  (
(  Berta Frazier                        )
 ) 429 Orrand Dr                       (
(  Rome, GA  30161                      )
 ) (404) xxx-xxxx                      (
'---------------------------------------'
root@testpod:/# rig | boxes -d dog
          __   _,--="=--,_   __
         /  \."    .-.    "./  \
        /  ,/  _   : :   _  \/` \
        \  `| /o\  :_:  /o\ |\__/
         `-'| :="~` _ `~"=: |
            \`     (_)     `/
     .-"-.   \      |      /   .-"-.
.---{     }--|  /,.-'-.,\  |--{     }---.
 )  (_)_)_)  \_/`~-===-~`\_/  (_(_(_)  (
(  Matt Diaz                            )
 ) 925 East Parson St                  (
(  Orlando, FL  32802                   )
 ) (407) xxx-xxxx                      (
'---------------------------------------'
root@testpod:/# env
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
HOSTNAME=testpod
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.233.0.1
KUBERNETES_PORT=tcp://10.233.0.1:443
PWD=/
HOME=/root
INTERVAL=5
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP=tcp://10.233.0.1:443
TERM=xterm
OPTION=stone
SHLVL=1
KUBERNETES_SERVICE_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_SERVICE_HOST=10.233.0.1
_=/usr/bin/env
root@testpod:/# ls -l /bin/genid.sh
-rwxr-xr-x. 1 root root 133 Jun 10  2021 /bin/genid.sh
root@testpod:/# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 05:48 ?        00:00:00 /bin/bash /bin/genid.sh
root          41       0  0 05:49 pts/0    00:00:00 bash
root         150       1  0 05:51 ?        00:00:00 sleep 5
root         151      41  0 05:51 pts/0    00:00:00 ps -ef
root@testpod:/# cat /bin/genid.sh
#!/bin/bash
mkdir -p /webdata
while true
do
  /usr/bin/rig | /usr/bin/boxes -d $OPTION  > /webdata/index.html
  sleep $INTERVAL
done

configFile로 들어갈 데이터(config.dir/nginx.conf) 작성

[root@master ~/kube/11/configmap]# vi config.dir/nginx.conf
[root@master ~/kube/11/configmap]# cat config.dir/nginx.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.hml;
    }
}

[root@master ~/kube/11/configmap]# kubectl create configmap test-config \
> --from-literal=INTERVAL=2 \
> --from-literal=OPTION=dog \
> --from-file=config.dir/nginx.conf
configmap/test-config created
[root@master ~/kube/11/configmap]# kubectl get configmaps
NAME               DATA   AGE
kube-root-ca.crt   1      7d4h
test-config        3      7s
[root@master ~/kube/11/configmap]# kubectl describe configmaps test-config
Name:         test-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
INTERVAL:
----
2
OPTION:
----
dog
nginx.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.hml;
    }
}

BinaryData
====

Events:  <none>

configmap에서의 특정 환경 변수를 사용하는 pod 생성

[root@master ~/kube/11/configmap]# vi genid-stone.yaml
[root@master ~/kube/11/configmap]# cat genid-stone.yaml
apiVersion: v1
kind: Pod
metadata:
  name: genid-stone
spec:
  containers:
  - name: genid
    image: smlinux/genid:env
    **env:**
    **- name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: test-config
          key: INTERVAL**
    volumeMounts:
    - name: html
      mountPath: /webdata
  - name: webserver
    image: nginx:1.14
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
  volumes:
  - name: html
    emptyDir: {}
[root@master ~/kube/11/configmap]# kubectl apply -f genid-stone.yaml
[root@master ~/kube/11/configmap]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
genid-stone   2/2     Running   0          22s
testpod       1/1     Running   0          31m
[root@master ~/kube/11/configmap]# kubectl exec -it genid-stone -c genid -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=genid-stone
TERM=xterm
INTERVAL=2
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.233.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.233.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.233.0.1
KUBERNETES_SERVICE_HOST=10.233.0.1
OPTION=stone
HOME=/root

configmap INTERVAL 2 ▶ 10 수정

[root@master ~/kube/11/configmap]# kubectl edit configmaps test-config
configmap/test-config edited
[root@master ~/kube/11/configmap]# kubectl describe configmaps test-config
Name:         test-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
INTERVAL:
----
10
OPTION:
----
dog
nginx.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.hml;
    }
}

BinaryData
====

Events:  <none>
[root@master ~/kube/11/configmap]# kubectl exec -it genid-stone -c genid -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=genid-stone
TERM=xterm
INTERVAL=2
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.233.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.233.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.233.0.1
KUBERNETES_SERVICE_HOST=10.233.0.1
OPTION=stone
HOME=/root

▶ 변경되지 이전에 구동된 pod의 env는 변경되지 않음

[root@master ~/kube/11/configmap]# kubectl delete -f genid-stone.yaml
pod "genid-stone" deleted
[root@master ~/kube/11/configmap]# kubectl create -f genid-stone.yaml
pod/genid-stone created
[root@master ~/kube/11/configmap]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
genid-dog     2/2     Running   0          8m10s
genid-stone   2/2     Running   0          2s
testpod       1/1     Running   0          40m
[root@master ~/kube/11/configmap]# kubectl get pod -o wide
NAME          READY   STATUS    RESTARTS   AGE     IP               NODE    NOMINATED NODE   READINESS GATES
genid-dog     2/2     Running   0          8m15s   10.233.71.47     node3   <none>           <none>
genid-stone   2/2     Running   0          7s      10.233.102.184   node1   <none>           <none>
testpod       1/1     Running   0          40m     10.233.75.8      node2   <none>           <none>
[root@master ~/kube/11/configmap]# kubectl exec genid-stone -c genid -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=genid-stone
INTERVAL=10
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.233.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.233.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.233.0.1
KUBERNETES_SERVICE_HOST=10.233.0.1
KUBERNETES_SERVICE_PORT=443
OPTION=stone
HOME=/root

▶ 새롭게 생성한 pod 에 대해서는 변경 사항 적용됨

configmap의 전체 환경 변수 사용하는 pod 생성

[root@master ~/kube/11/configmap]# vi genid-dog.yaml
[root@master ~/kube/11/configmap]# cat genid-dog.yaml
apiVersion: v1
kind: Pod
metadata:
  name: genid-dog
spec:
  containers:
  - name: genid
    image: smlinux/genid:env
    **envFrom:
    - configMapRef:
        name: test-config**
    volumeMounts:
    - name: html
      mountPath: /webdata
  - name: webserver
    image: nginx:1.14
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
  volumes:
  - name: html
    emptyDir: {}
[root@master ~/kube/11/configmap]# kubectl apply -f genid-dog.yaml
[root@master ~/kube/11/configmap]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
genid-dog     2/2     Running   0          12s
testpod       1/1     Running   0          32m
[root@master ~/kube/11/configmap]# kubectl exec genid-dog -c genid -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=genid-dog
INTERVAL=10
OPTION=dog
nginx.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.hml;
    }
}

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

0개의 댓글