Kubernetes

DB Pod ์ƒ์„ฑํ•˜๊ธฐ

uuuhhh 2022. 12. 15. 15:51

๐Ÿ“Œ ๋ชฉํ‘œ


  • Kubernetes Cluster์— DB Pod๋ฅผ ๋„์›Œ๋ณด์ž !
    • MySQL์„ ์‚ฌ์šฉํ•˜๋Š”..
    • SpringBoot์˜ ๋ฐฑ์—”๋“œ์™€ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด..
  • ๋จผ์ € ์ค€๋น„๋ฌผ !
    • Deployment
    • Secret
    • Persistent Volume (PV)
    • Persistent Volume Claim (PVC)
  • ์œ„ ๊ฒƒ๋“ค์ด ์™œ ํ•„์š”ํ•œ์ง€๋Š” ์ง์ ‘ ๋งค๋‹ˆํŽ˜์ŠคํŠธ ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜๋ฉด์„œ ์•Œ์•„๋ณด์ž

 

 

โ–ช๏ธŽ DB Deployment.yaml


apiVersion: apps/v1
kind: Deployment
metadata:
  name: db-mysql
  labels:
    app: db-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db-mysql
  template:
    metadata:
      labels:
        app: db-mysql
    spec:
      nodeName: cnp-node-w-1vmd
      containers:
      - name: db-mysql
        image: mysql:8.0.31
        env:
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: username
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: root-password
        ports:
        - containerPort: 3306
      volumes:
        - name: volume
          persistentVolumeClaim:
            claimName: mysql-pvc
  • ๋จผ์ € Deployment๋ฅผ ์‚ดํŽด๋ณด๋ฉด ํŠน๋ณ„ํžˆ ๋ด์•ผ ํ•  ๋‘ ๊ฐ€์ง€๋ฅผ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
    1. spec.template.spec.containers.env
    2. spec.template.spec.volumes

 

1. spec.template.spec.containers.env (secret)


  • spec.template.spec.containers.env์—๋Š” mysql์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋„ฃ์–ด์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ์ด๋ฅผ github์˜ public repo์— push๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์น˜๋ช…์ ์ธ ๋ณด์•ˆ ์ด์Šˆ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

์ฐธ์กฐ : https://www.padok.fr/en/blog/kubernetes-secrets

 

  • ์ด๋Ÿฌํ•œ ์ด์Šˆ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด k8s์—๋Š” secret์„ ์ง€์›ํ•œ๋‹ค.
  • MYSQL_ROOT_PASSWORD์— ๋Œ€ํ•œ ๊ฐ’์„ mysql-password์˜ secret์—์„œ password์— ๋Œ€ํ•œ key ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ฉฐ ๋ณด์•ˆ์„ ํ–ฅ์ƒํ•œ๋‹ค.

 

2. spec.template.spec.volumes (PV / PVC)


  • ์ƒ์„ฑํ•œ pod๋‚ด์— ์ €์žฅ์†Œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ์–ธ์ œ๋“ ์ง€ k8s๋‚ด์˜ ํŒŒ๋“œ๋Š” ์ฃฝ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์‹œ์Šคํ…œ ์„ค๊ณ„์ƒ DB ํŒŒ๋“œ๊ฐ€ ์ข…๋ฃŒ๋œ๋‹ค๋ฉด ๊ด€๋ จ ๋ฐ์ดํ„ฐ๋Š” ๋ชจ๋‘ ์—†์–ด์งˆ ๊ฒƒ์ด๋‹ค.

์ฐธ์กฐ : ์ด๋ฏธ์ง€ ํด๋ฆญ

 

  • ๋”ฐ๋ผ์„œ ๋ณ„๊ฐœ๋กœ ๊ด€๋ฆฌํ•˜๋Š” PV ์Šคํ† ๋ฆฌ์ง€๋ฅผ ๋‘๋ฉฐ ํŒŒ๋“œ์— ์ง์ ‘ ์—ฐ๊ฒฐํ•˜์ง€ ์•Š๊ณ  ํŒŒ๋“œ์™€ PV ์‚ฌ์ด์— PVC๋ฅผ ๋‘์–ด ์š”์ฒญ์„ ์ค‘๊ฐ„์—์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

โ–ช๏ธŽ DB Secret.yaml


apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
type: Opaque
data:
  username: *******
  password: *******
  root-password: *******
  host: *******
  • ๊ทธ๋Ÿผ ์œ„์—์„œ ์—ด์‹ฌํžˆ ์–˜๊ธฐํ–ˆ๋˜ 1๋ฒˆ์— ๋Œ€ํ•œ ์‹œํฌ๋ฆฟ์„ ์ƒ์„ฑํ•ด๋ณด์ž
  • ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•˜๊ฒŒ ๋ณผ ๊ฒƒ์€ data.password !
    • data ํ•˜์œ„์— ๋„ฃ๊ณ  ์‹ถ์€ ๊ฐ’์„ key-value ์Œ์œผ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค !
  • ๊ทธ๋ƒฅ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์™€๋‹ค๋‹ค ์ ์œผ๋ฉด ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ง€๋งŒ no no..
  • secret์—๋Š” value ๊ฐ’๋“ค์„ base64๋กœ ์ธ์ฝ”๋”ฉํ•œ ๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.
    • ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ base64๋กœ ์ธ์ฝ”๋”ฉํ•ด์š”?
  • $ echo -n password | base64์˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค๋ฉด password๋ฅผ base64๋กœ ์ธ์ฝ”๋”ฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ’์„ ๊ฐ€์ง„๋‹ค.

 

  • ๊ทผ๋ฐ ์ž ๊น.. -n ์˜ต์…˜์€ ๋ญ์ฃ ?
  • ๋งŒ์•ฝ -n ์˜ต์…˜์„ ์ฃผ์ง€ ์•Š๊ณ  ์ธ์ฝ”๋”ฉ์„ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด..
  • $ echo password | base64

 

  • ์กฐ๊ธˆ ๋‹ค๋ฅธ๋ฐ..?
  • echo ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ž๋™์œผ๋กœ trailing newline์„ ์‚ฝ์ž…ํ•œ๋‹ค.
  • ์œ„์™€ ๊ฐ™์€ ์ด์œ ๋กœ -n ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์„œ trailing newline์„ ์—†์•ค ํ›„์— ์ธ์ฝ”๋”ฉ์„ ํ•ด์•ผ secret์—์„œ ์›ํ•˜๋Š” ์ •์ƒ์ ์ธ ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

  • -n ์˜ต์…˜ ์—†์ด ์ธ์ฝ”๋”ฉํ•œ ๊ฐ’์œผ๋กœ ํ‚ค ๊ฐ’์„ ๋ถ€์—ฌํ•˜๋ฉด mysql: unknown option ‘–”‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

  • ๊ทธ๋Ÿผ secret ์ƒ์„ฑ!
    • $ kubectl apply -f db-secret.yaml -n cucumovie-main

 

โ–ช๏ธŽ PV.yaml / PVC.yaml


  • PV
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pvc
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /volume/pv
  nodeAffinity:
    required:
      nodeSelectorTerms:
        - matchExpressions:
            - key: name
              operator: In
              values:
              - node

 

  • PV์˜ path๋ฅผ ๋…ธ๋“œ์˜ /volume/pv๋กœ ์ง€์ •ํ•œ ํ›„ PV ์ƒ์„ฑ !

 

  • PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  storageClassName: local-storage
  resources:
    requests:
      storage: 10Gi
  accessModes:
    - ReadWriteOnce

 

  • PVC ์ƒ์„ฑ !

 

  • ์ด์ œ ๋ชจ๋“  ์ค€๋น„๋ฅผ ๋‹ค ๋งˆ์ณค์œผ๋‹ˆ..
  • Deployment ์ƒ์„ฑ !

 

  • ์ƒ์„ฑ๋œ DB Pod์— ์ ‘์†

 

  • ์ ‘์†ํ•œ Pod์—์„œ mysql์— ๋กœ๊ทธ์ธํ•˜๊ณ  ํ˜„์žฌ ๊ณ„์ •์ด ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชฉ๋ก ํ™•์ธ
  • ์ •์ƒ์ ์œผ๋กœ DB Pod๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค !

 

 

ref.


์ฟ ๋ฒ„๋„คํ‹ฐ์Šค Web-DB pod ์—ฐ๊ฒฐํ•˜๊ธฐ