โœ”๏ธ

ConfigMap & Secret

ConfigMap

์ปจํ…Œ์ด๋„ˆ ๊ตฌ์„ฑ ์ •๋ณด๋ฅผ ํ•œ๊ณณ์— ๋ชจ์•„์„œ ๊ด€๋ฆฌ
ํŠน์ • Pod๋“ค์—๊ฒŒ ํ™˜๊ฒฝ๋ณ€์ˆ˜(์ผ๋ถ€, ์ „์ฒด)๋ฅผ ์ „๋‹ฌ ํ•  ์ˆ˜ ์žˆ์Œ
key value ํ˜•ํƒœ์ด๋‚˜, key์— file์„ ํ˜น์€ value์— file ํ˜น์€ directory๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
(ํŒŒ์ผ์šฉ๋Ÿ‰์€ 1M ์ดํ•˜)
ConfigMap ์ƒ์„ฑ
์ถœ์ฒ˜ : ์œ ํŠœ๋ธŒ ๋”ฐ๋ฐฐ๋Ÿฐ ๋”ฐ๋ฐฐ์ฟ  https://www.youtube.com/@ttabae-learn
#์ƒ์„ฑ์˜ˆ์ œ
#config.dir ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ด ์•ˆ์—๋Š” nginx-confg.conf ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •
kubectl create configmap ttabae-config \ 
        --from-literal=INTERVAL=2 --from-literal=OPTION=boy --from-file=config.dir/

#configmap ์กฐํšŒ
kubectl get configmaps
NAME               DATA   AGE
kube-root-ca.crt   1      11d
ttabae-config      3      26s   #<--- ๊ฐ’์ด 3๊ฐœ ์žˆ๋‹ค๊ณ  ๋ณด์ž„

#์‹คํ–‰ ํ›„ ttabae-config์—๋Š” ๋‹ค์Œ์˜ ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค
#INNTERVAL:2
#OPTION:boy
#nginx-config.conf:ํŒŒ์ผ๋‚ด์šฉ ์–ด์ฉŒ๊ณ ์ €์ฉŒ๊ณ 
#configmap ์ƒ์„ธ์กฐํšŒ
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>

# configMap ์ˆ˜์ •
kubectl edit configmaps ttabae-config
#์œ„์—์„œ ๋งŒ๋“  configMap์˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ pod yaml ํŒŒ์ผ์„ ์ƒ์„ฑ
cat genid.yaml
apiVersion: v1
kind: Pod
metadata:
  name: genid-stone
spec:
  containers:
  - image: bluedove97/genid:env
    env:
    - name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: ttabae-config  #<-- ttabae-config์— ์žˆ๋Š” INTERVAL ํ‚ค๊ฐ’๋ฅผ, ํ™˜๊ฒฝ๋ณ€์ˆ˜ INTERVAL ์— ๋„ฃ์–ด๋ผ.
          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์„ ํ†ต์งธ๋กœ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ์„ธํŒ…ํ•  ๋•Œ
cat genid-whole.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: genid-boy
spec:
  containers:
  - image: bluedove97/genid:env
    envFrom:    #<--- 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: {}
#ConfigMap์˜ key๋ฅผ pod์˜ ์ปจํ…Œ์ด๋„ˆ์— ๋ณผ๋ฅจ๋งˆ์šดํŠธ ํ•˜๊ธฐ. (ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ์ฝ์–ด์˜จ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋„˜๊ธฐ๊ธฐ)
cat genid-volume.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: genid-volume
spec:
  containers:
  - image: bluedove97/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

Secret

์‹œํฌ๋ฆฟ์€ ์ปจํ”ผ๊ทธ๋งต๊ณผ ๋น„์Šทํ•œ ๋งฅ๋ฝ์ด์ง€๋งŒ, base64๋กœ ์ธ์ฝ”๋”ฉํ•ด์„œ ๋ชจ์•„๋‘”๋‹ค.
์‹œํฌ๋ฆฟ ์šฉ๋Ÿ‰์ œํ•œ 1M
Secret ๋งŒ๋“ค๊ธฐ
์ถœ์ฒ˜ : ์œ ํŠœ๋ธŒ ๋”ฐ๋ฐฐ๋Ÿฐ ๋”ฐ๋ฐฐ์ฟ  https://www.youtube.com/@ttabae-learn
#secret ์ƒ์„ฑ
kubectl create secret generic ttabae-secret \
        --from-literal=INTERVAL=2 --from-file=./genid-web-config/

#์กฐํšŒ
kubectl get secrets
  NAME            TYPE     DATA   AGE
  ttabae-secret   Opaque   2      46s   #<--- TYPE Opaque ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ํƒ€์ž…์ด๋‹ค

#yaml๋กœ ์กฐํšŒ --> base64 ์ธ์ฝ”๋”ฉ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณด์ธ๋‹ค.
kubectl get secrets ttabae-secret -o yaml

#secret์„ ํฌํ•จํ•œ pod ์ƒ์„ฑ
cat  genid-env-secret.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: genid-env-secret
spec:
  containers:
  - image: bluedove97/genid:env
    env:
    - name: INTERVAL
      valueFrom:
        secretKeyRef:
          name: ttabae-secret
          key: INTERVAL
    name: fakeid-generator
    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: {}