๊ธฐ๋ฐ€์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‚ค-๊ฐ’์˜ ์Œ์œผ๋กœ ์ €์žฅํ•˜๋Š”๋ฐ ์“ฐ๋Š” ์˜ค๋ธŒ์ ํŠธ. ConfigMap์— ์ €์žฅ๋œ ๊ฐ’์€ Pod์ด ์‚ฌ์šฉํ•œ๋‹ค.

3 minute read

๊ธฐ๋ฐ€์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‚ค-๊ฐ’์˜ ์Œ์œผ๋กœ ์ €์žฅํ•˜๋Š”๋ฐ ์“ฐ๋Š” ์˜ค๋ธŒ์ ํŠธ. ConfigMap์— ์ €์žฅ๋œ ๊ฐ’์€ Pod์ด ์‚ฌ์šฉํ•œ๋‹ค.

Pod์€ (1) ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ฑฐ๋‚˜ (2) ๋ณผ๋ฅจ ๋งˆ์šดํŠธ๋ฅผ ํ†ตํ•ด ConfigMap ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

ConfigMap ์ƒ์„ฑ

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  db_host: "xxxxx.amazon.com"
  db_username: "xxxx"
  db_password: "xxxx"

data ๊ฐ’์„ CLI ์ธ์ž๋กœ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•๋“ค๋„ ์žˆ์ง€๋งŒ, ๋ณธ์ธ์€ ์ง์ ‘ yaml ํŒŒ์ผ์— ๋ช…์‹œํ•˜๋Š” ๊ฑธ ์„ ํ˜ธํ•œ๋‹ค.

Pod์˜ ConfigMap ์‚ฌ์šฉ

ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ธฐ

Pod์€ spec.containers[*].env[*] ํ•ญ๋ชฉ์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋Š”๋ฐ, ์ด๋•Œ, valueFrom.configMapKeyRef์— ConfigMap์˜ ์ด๋ฆ„์„ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

...
spec:
  containers:
    - name: ...
      image: ...
      env:
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: my-config # ConfigMap name
              key: db_host

์ „์ฒด ๊ฐ’์„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ธฐ

spec.containers[*].envFrom[*] ํ•ญ๋ชฉ์— configMapRef๋ฅผ ์„ค์ •ํ•ด ConfigMap์˜ ๊ฐ’ ์ „์ฒด๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ํ•˜๋‚˜์”ฉ ๋‚˜์—ดํ•˜๊ธฐ ๊ท€์ฐฎ๊ฑฐ๋‚˜, ํ•˜๋‚˜์”ฉ ๋‚˜์—ดํ•˜๋‹ค๊ฐ€ ์‹ค์ˆ˜ ํ•  ๊ฒƒ ๊ฐ™์œผ๋ฉด(ํœด๋จผ ์—๋Ÿฌ) envFrom[*].configMapRef๋ฅผ ์“ฐ์ž.

...
spec:
  containers:
    - name: ...
      image: ...
      envFrom:
        - configMapRef: # load all config values
            name: my-config

๋ณผ๋ฅจ ๋งˆ์šดํŠธ๋ฅผ ํ†ตํ•ด ๋ฐ›๊ธฐ

ConfigMap์— ๋งˆ์šดํŠธํ•  ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ ๋Š”๋‹ค.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  db_host: "xxxxx.amazon.com"
  db_username: "xxxx"
  db_password: "xxxx"
  nginx.conf: | # indentation ์ฃผ์˜!
    user  nginx;
    worker_processes  2;

    err_log    /var/log/nginx/err.log warn;
    pid         /var/run/nginx.pid;

    http {
        include         /etc/nginx/mime.types;
        default_type  application/octet-stream;
        include        /etc/nginx/conf.d/*.conf;
    }

๋‹จ, ํŒŒ์ผ ๊ฐ’์„ data๋กœ ๋„˜๊ฒจ์ค„ ๋•Œ, indentation์ด ์•ˆ ๋งž์œผ๋ฉด ํŒŒ์‹ฑํ•  ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋‹ˆ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค!!

...
spec:
  containers:
    - name: ...
      image: python-alpine
      volumeMounts: # declare volume for config file
        - name: config-volume
          mountPath: /etc/config
          readOnly: true # (optional)
  volumes:
    - name: config-volume
      configMap:
        name: my-config # ConfigMap name
        items:
          - key: "nginx.conf"
            path: "nginx.conf"

๋ณผ๋ฅจ ๋งˆ์šดํŠธ๋ฅผ ํ†ตํ•ด ์ „์ฒด ๋ฐ›๊ธฐ

...
spec:
  containers:
    - name: ...
      image: python-alpine
      volumeMounts: # declare volume for config file
        - name: config-volume
          mountPath: /etc/config
          readOnly: true # (optional)
  volumes:
    - name: config-volume
      configMap:
        name: my-config # ConfigMap name

ConfigMap์— ์ •์˜๋œ ํ‚ค๊ฐ€ ํŒŒ์ผ ์ด๋ฆ„์ด ๋˜์–ด ๋ชจ๋“  ํ‚ค-๊ฐ’์ด ํŒŒ์ผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.


๋งˆ์šดํŠธ๋œ ConfigMap์˜ ์—…๋ฐ์ดํŠธ

Pod์—์„œ ConfigMap์„ ๋ณผ๋ฅจ ๋งˆ์šดํŠธ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ConfigMap์˜ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ ๋˜๋ฉด ํ”„๋กœ์ ์…˜๋œ ํ‚ค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—…๋ฐ์ดํŠธ ๋œ๋‹ค. ๋‹จ, ๊ฐ’์ด ์ „ํŒŒ๋˜๋Š” ๋ฐ ์ง€์—ฐ ์‹œ๊ฐ„์ด ์žˆ๊ณ , ์บ์‹ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ์—…๋ฐ์ดํŠธ ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

ConfigMap์˜ ๊ฐ’์ด ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ๋˜๋Š” ๊ฑด Pod์—์„œ ๋ณผ๋ฅจ ๋งˆ์šดํŠธ๋กœ ์—ฐ๊ฒฐํ–ˆ์„ ๊ฒฝ์šฐ ๋ฟ์ด๋‹ค. ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ ConfigMap์˜ ๊ฐ’์€ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, Pod์„ ์žฌ์‹œ์ž‘ ํ•ด์•ผ ํ•œ๋‹ค.


References

Categories:

Updated: