ๆœˆไบฎ

[Kubernetes] ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํด๋Ÿฌ์Šคํ„ฐ์— MongoDB ๋ฐฐํฌํ•˜๊ธฐ ๋ณธ๋ฌธ

docker,kubernetes

[Kubernetes] ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํด๋Ÿฌ์Šคํ„ฐ์— MongoDB ๋ฐฐํฌํ•˜๊ธฐ

๋“€๋„ค 2023. 8. 1. 10:37

 

๐Ÿ‘พ ์ฝ๊ธฐ ์ „์—

์ด ๊ธ€์—์„œ๋Š” StatefulSet์ด ์•„๋‹Œ Deployment๋ฅผ ์ด์šฉํ•˜์—ฌ MongoDB๋ฅผ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์— ๋ฐฐํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ํ…Œ์ŠคํŠธํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉํ•˜๊ธฐ์— ์ ํ•ฉํ•˜๋ฉฐ, ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ถ”๊ฐ€์ ์ธ ์„ค์ •์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ์šฉ MongoDB๋ฅผ ๋ฐฐํฌํ•˜๋Š” ๊ณผ์ •์—์„œ ํ•„์š”ํ•œ ๋‹จ๊ณ„๋“ค์„ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

  1. PersistentVolume (PV) ๋ฐ PersistentVolumeClaim (PVC) ์ƒ์„ฑ (https://yueliang-front-end.tistory.com/32)
  2. Secret ์ƒ์„ฑ (MongoDB root ๊ณ„์ • ์ƒ์„ฑ์„ ์œ„ํ•ด ํ•„์š”)
  3. Deployment ๋ฐ Service ์ƒ์„ฑ
  4. MongoDB ์ธ์Šคํ„ด์Šค ์ ‘์†

 

STEP 2: Secret ์ƒ์„ฑ

Secret์€ ๋ฏผ๊ฐํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ €์žฅํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์˜ ์˜ค๋ธŒ์ ํŠธ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด kubectl create secret ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ Secret์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

kubectl create secret generic dev-secret \\
    --from-literal=username=admin \\
    --from-literal=password='S!B\\*d$zDsb='

๋˜๋Š” Secret ์˜ค๋ธŒ์ ํŠธ์˜ YAML ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

apiVersion: v1
kind: Secret
metadata:
  name: dev-secret
  namespace: YOUR_NAMESPACE
type: Opaque
data:
  mongodb-root-username: YOUR_BASE64_ENCODED_USERNAME
  mongodb-root-password: YOUR_BASE64_ENCODED_PASSWORD

๊ณต์‹ : https://kubernetes.io/ko/docs/tasks/configmap-secret/managing-secret-using-kubectl/

 

STEP 3: Deployment ์ƒ์„ฑ

MongoDB๋ฅผ ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•œ Deployment์™€ Service๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

โš ๏ธ ์ฃผ์˜ํ•ด์•ผ ํ•  ์ : Permission denied ๋ฌธ์ œ

๋ณผ๋ฅจ์„ ํ™œ์šฉํ•  ๋•Œ "Permission denied" ๋ฌธ์ œ๋ฅผ ๊ฒฝํ—˜ํ–ˆ๋‹ค๋ฉด spec.securityContext ๋ฐ spec.container.securityContext ์„ค์ •์„ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ค์ •๋“ค์€ ํŒŒ๋“œ๊ฐ€ ๋ณผ๋ฅจ์— ์“ฐ๊ธฐ ๋ฐ ์ฝ๊ธฐ ๊ถŒํ•œ์„ ๊ฐ–๋„๋ก ๋•์Šต๋‹ˆ๋‹ค. ๋ณผ๋ฅจ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ํ•ด๋‹น ๋ณผ๋ฅจ์˜ ์‚ฌ์šฉ์ž ID์™€ ๊ทธ๋ฃน ID๋ฅผ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋Š” ๋ณผ๋ฅจ ๋‚ด๋ถ€์— ์ ‘๊ทผํ•˜์—ฌ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. MongoDB์™€ ๊ฐ™์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์„ค์น˜ํ•  ๋•Œ, ์ด๋Ÿฌํ•œ ๊ถŒํ•œ ์„ค์ •์€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณผ๋ฅจ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •, ๋ฐ์ดํ„ฐ, ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์ค‘์š”ํ•œ ๋ฆฌ์†Œ์Šค๋“ค์„ ์ €์žฅํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ, ์ ์ ˆํ•œ ๊ถŒํ•œ ์—†์ด๋Š” ๋ณผ๋ฅจ์˜ ์ƒ์„ฑ๊ณผ ์‚ฌ์šฉ์ด ์ œํ•œ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋˜ํ•œ, initContainers ์„ค์ •์—๋„ ์ฃผ์˜๋ฅผ ๊ธฐ์šธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณผ๋ฅจ์— ์“ฐ๊ธฐ ๋ฐ ์ฝ๊ธฐ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์€ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ๋œ ํ›„๊ฐ€ ์•„๋‹ˆ๋ผ, ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์„ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์ด๋ฃจ์–ด์ ธ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” initContainers ์„ค์ • ๋‚ด์—์„œ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, **initContainers**๋Š” ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ๋˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๊ถŒํ•œ ์„ค์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ค‘์š”ํ•œ ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.

์ด์–ด์„œ, ์•„๋ž˜๋Š” ์˜ˆ์‹œ YAML ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.

 

+ NodePort: 27017 (mongodb ๊ธฐ๋ณธํฌํŠธ๋กœํ•จ ๋ฐ”๊ฟ”๋„ ์ƒ๊ด€์—†์Œ,์™ธ๋ถ€์ ‘์†์„ ์œ„ํ•ด NodePort๋กœ)

+  mountPath: /data/db  (mongo-db๊ธฐ๋ณธ ๋ฃจํŠธ ๋ณ€๊ฒฝ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์Œ! ๋ฐ˜๋“œ์‹œ ๋˜‘๊ฐ™์ด ์ž‘์„ฑ)

+ image: mongo:4.4.18 (VM์—์„œ ์ƒ์„ฑํ•ด์„œ 4๋ฒ„์ „์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค. VM์—์„œ๋„ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ์ƒ๋žตํ•˜๊ณ  ํƒœ๊ทธ๋•Œ๊ณ  mongo๋กœ ์ƒ์„ฑํ•ด๋„ ์ƒ๊ด€์—†์Œ)

apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
  namespace: test
  labels:
    app: mongodb
spec:
  ports:
  - port: 27017
    NodePort: 27017
  selector:
    app: mongodb
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-deployment
  namespace: test
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      securityContext:
        fsGroup: YOUR_VOLUME_GROUP_ID
      containers:
      - name: mongodb
        image: mongo:4.4.18
        imagePullPolicy: IfNotPresent
        securityContext:
          runAsUser: YOUR_VOLUME_USER_ID
          runAsGroup: YOUR_VOLUME_GROUP_ID
        ports:
        - containerPort: 27017
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: dev-secret
              key: mongodb-root-username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: dev-secret
              key: mongodb-root-password
        volumeMounts:
          - name: mongodb-data
            mountPath: /data/db
        resources:
          requests:
            cpu: 500m
            memory: 100Mi
      initContainers:
      - name: init-chown-data
        image: busybox:1.31.1
        command:
          - chmod
          - '777'
          - /data/db
        volumeMounts:
        - name: mongodb-data
          mountPath: /data/db
      volumes:
      - name: mongodb-data
        persistentVolumeClaim:
          claimName: mongodb-dev-pvc

 

 

STEP 4: MongoDB ์ ‘์†

MongoDB ์ธ์Šคํ„ด์Šค์— ์ ‘์†ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// Mongo Shell๋กœ ์ ‘์†
mongo

// Admin ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์ด๋™
use admin

// ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ ์„ค์ •ํ•œ ์ดˆ๊ธฐ ID, PW๋กœ ์ธ์ฆ
db.auth('YOUR_ID', 'YOUR_PASSWORD')

// ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๊ณ„์ • ๋งŒ๋“ค์–ด์„œ ์—ฐ๊ฒฐ
use YOUR_NEW_DATABASE_NAME

// ์ƒˆ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ณ„์ • ์ƒ์„ฑ
db.createUser({
  user: "YOUR_USERNAME",
  pwd: "YOUR_PASSWORD",
  roles: [
    { role: "dbAdmin", db: "YOUR_DATABASE_NAME" },
    { role: "readWrite", db: "YOUR_DATABASE_NAME" }
  ]
})

// ๊ณ„์ • ํ™•์ธ
show users

 

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ ์ ‘์†

// pod ์ด๋ฆ„ ํ™•์ธ
kubectl get pods --namespace=YOUR_NAMESPACE

// pod๋กœ ์ ‘์†
kubectl exec YOUR_POD_NAME -it --namespace=YOUR_NAMESPACE -- bash

// MongoDB Shell๋กœ ์ ‘์†
kubectl exec YOUR_POD_NAME -it --namespace=YOUR_NAMESPACE -- mongo

 


 

์ฐธ๊ณ 

https://kubernetes.io/ko/docs/tasks/configmap-secret/managing-secret-using-kubectl/

๋ฐ˜์‘ํ˜•

'docker,kubernetes' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Kubernetes] PersistentVolume (PV)๊ณผ PersistentVolumeClaim (PVC)๋ž€?  (0) 2023.07.25
Comments