You will be fine

<GCP 쿠버네티스로 웹서비스하기> Postgres, Redis 설치

by BFine
반응형

GCP의 쿠버네티스 안에 Postgres, Redis를 설치해보았다.

 

가. 구성

 a. 구성도

  -  요렇게 Service로 통신하는 형태로 구성도를 그려보았다

 

 b. Service

  -  타입을 Load Balancer로 지정하면 외부에서 Load Balancer IP로 접근이 가능하다.

  -  타입을 Cluster로 지정할경우 내부에서 Private 하게 Pod끼리만 통신할 수 있다.

 

나. Postgres

 a. 설정 .yaml

  -  Deployment는 레플리카셋(이건 설정하지 않음), Pod을 관리해준다. 

     => 설정을 통해 자동으로 업데이트, 복구 등 처리한다.

apiVersion: v1
kind: Service
metadata:
  name: postgresql-svc
  labels:
    app: drawinglots
spec:
  ports:
    - port: 5432
  selector:
    app: drawinglots
    tier: postgresql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresql-pv-claim
  labels:
    app: drawinglots
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drawinglots-postgresql
  labels:
    app: drawinglots
spec:
  selector:
    matchLabels:
      app: drawinglots
      tier: postgresql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: drawinglots
        tier: postgresql
    spec:
      containers:
      - image: postgres:10
        name: postgresql
        env:
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: POSTGRES_PASSWORD
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: POSTGRES_DB
        - name: PGDATA // Postgres는 요고 하지않으면 오류남
          value: /var/lib/postgresql/data/pgdata
        ports:
        - containerPort: 5432
          name: postgresql
        volumeMounts:
        - name: postgresql-persistent-storage
          mountPath: /var/lib/postgresql/data
      volumes:
      - name: postgresql-persistent-storage
        persistentVolumeClaim:
          claimName: postgresql-pv-claim

 

 b. Secret 설정

  -  DB의 Password를 Env로 보낼때 Secret을 참조하도록 만들 수 있다.

  -  Secret은 명령어를 통해 설정하는게 편하다. 

     => kubectl create secret generic db-secret --from-file=./POSTGES_PASSWORD

 

 c. PVC

  -  PVC는 PersistentVolumeClaim 에 대해서 개발자가 요청해서 작성하면 자동으로 Storage에 반영된다.

  -  Pod에 저장하는게 아니고 GCP의 Storage를 mount해서 데이터를 보관한다.

 

다. Redis

 a. 설정 .yaml

  -  Redis는 Mount 없이 Pod에 저장하도록 설정했다.

apiVersion: v1
kind: Service
metadata:
  name: redis-svc
  labels:
    app: drawinglots
spec:
  ports:
    - port: 6379
  selector:
    app: drawinglots
    tier: redis
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: drawinglots-redis
  labels:
    app: drawinglots
spec:
  selector:
    matchLabels:
      app: drawinglots
      tier: redis
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: drawinglots
        tier: redis
    spec:
      containers:
      - image: redis:6
        name: redis
        ports:
        - containerPort: 6379
          name: redis

 

라. GCP 쿠버네티스에 Postgres, Redis 설치

 a. 명령어

  -  위의 파일을 저장하고  Kubectl create -f 파일명.yaml 실행하기만 하면 된다.

  -  --dry-run을 옵션으로 주면 create가 되는지 미리 확인할 수 있다. 

 

 b. Pod 생성확인

  -  kubectl get pod -o wide 둘이 다른노드에 배정되었다!!

반응형

블로그의 정보

57개월 BackEnd

BFine

활동하기