티스토리 뷰

 

2024.01.25 - [분류 전체보기] - 터널링 테스트

 

 

# DB를 kubernetes에서 pod와 EBS를 활용하여 구성
0. storageclass 생성 (이건 생성 안해도 됨)
스토리지 클래스를 가장 먼서 생성해야 나머지 리소스를 생성하는데 지장이 없다.

# 이건 생성 안해도 됨
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: test-db-storage
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2
  fsType: ext4
reclaimPolicy: Delete
mountOptions:
  - debug
volumeBindingMode: Immediate
*************************************

# PersistentVolume 생성

#postgres-pv-volume.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  namespace: postgres
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage:  10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mtd/data"


1. PersistentVolumeClaim 생성
- PVC라고 하고 해당 파일을 생성 후 실행하면 실제 EBS에서 볼륨을 생성한다.
- pod가 어떤 pvc를 할당받는지 확인하는 명령어는 kubectl describe {pod_name} -n {namespace_name} 으로 확인 가능하다.
- AWS의 EBS 메뉴에서 확인을 하면 아래와 같이 확인이 가능하다.
- yaml 파일의 내용은 아래와 같다.


#test-db-pv-claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-db-pv-claim
  namespace: postgre
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 10Gi


- 여기서 주의할 점은 pvc.yaml을 delete 할때 리소스가 삭제되지 않고,
삭제 중에서 멈춰있을 수 있다. 이럴경우를 대비하여 삭제 전에 미리 EBS 볼륨과 연결된 인스턴스를 분리시키는 작업을 선행하는 것이 좋다.

2. deployment 생성
#postgredep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-db
  namespace: postgre
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
      tier: postgresql
  template:
    metadata:
      labels:
        app: test
        tier: postgresql
    spec:
      containers:
      - name: postgres
        image: postgres:13.3
        imagePullPolicy: "IfNotPresent"
        ports:
        - containerPort: 5432
        envFrom:
          - secretRef:
              name: test-db-secret
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: test-db-pv
      volumes:
      - name: test-db-pv
        persistentVolumeClaim:
          claimName: test-db-pv-claim


3. secret 생성
#postgresecret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: test-db-secret
  namespace: postgre
stringData:
  POSTGRES_DB: testdb
  POSTGRES_USER: testadmin
  POSTGRES_PASSWORD: test00
  PGDATA: /var/lib/postgresql/data/pgdata

psql -U testadmin  -d testdb
172.16.221.169

4. 생성된 pod 서비스를 확인한다.
- 로그를 먼저 확인

#kubectl logs --tail 1000 -f bcheck-db-5cbf66dd4f-n6xps -n bcheck

PostgreSQL Database directory appears to contain a database; Skipping initialization

2022-11-29 07:45:54.391 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2022-11-29 07:45:54.392 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2022-11-29 07:45:54.392 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2022-11-29 07:45:54.395 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-11-29 07:45:54.400 UTC [26] LOG:  database system was shut down at 2022-11-29 07:45:33 UTC
2022-11-29 07:45:54.406 UTC [1] LOG:  database system is ready to accept connections
- pod 안으로 접근하기

kubectl exec -it {pod_name} -- bash
컨테이너에서 실행할 명령어(bash) 앞에 -- 을 붙여준다.

- secret에 DB정보와 비밀번호를 세팅 했으므로, 별도의 세팅은 하지 않아도 된다.
- 주의할점은 동일한 네임 스페이스에 존재해야 별도의 작업없이 통신이 가능하다.
- 고려사항으로는 백업을 어떤방식으로 할지, EBS 볼륨의 복구 방법 등 생각해 봐야한다.

5.postgreSQL service 생성
#postgresvc.yaml
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
  namespace: postgre
spec:
  type: NodePort
  ports:
   - port: 5432
  selector:
   app: postgres
 

postgreSQL연동 test
psql -h localhost -U root --password -p <NodePort> postgresdb
댓글