backend-tech-forge / benchmark

A enterprise level performance testing solution. Taking inspiration from nGrinder, this project aims to develop a Spring Boot application mirroring nGrinder's functionality as closely as feasible.
MIT License
4 stars 0 forks source link

RDB, PV, PVC are connected but the data removed when restart DB pod #72

Closed ghkdqhrbals closed 7 months ago

ghkdqhrbals commented 7 months ago

First our PV, PVC, DB connection setting yaml is bellow.


apiVersion: apps/v1
kind: Deployment
metadata:
  name: db-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: benchmark-db
  template:
    metadata:
      labels:
        app: benchmark-db
    spec:
      # directory creation setup
      initContainers:
        - name: init-data-dir
          image: busybox
          command: ["sh", "-c", "mkdir -p /var/lib/postgresql/production/data"]
          volumeMounts:
            - name: benchmark-vol
              mountPath: /var/lib/postgresql/production/data
      containers:
        - args:
            - -c
            - wal_level=logical
            - -c
            - max_connections=500
            - -p
            - "5433"
          env:
            - name: POSTGRES_DB
              value: test
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: password
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: username
          image: postgres:12-alpine
          name: chat-db
          ports:
            - containerPort: 5433
              hostPort: 5433
              protocol: TCP
          volumeMounts:
            - name: benchmark-vol
              mountPath: /var/lib/postgresql/production/data
      restartPolicy: Always
      volumes:
        - name: benchmark-vol
          persistentVolumeClaim:
            claimName: benchmark-pvc

apiVersion: v1
kind: PersistentVolume
metadata:
  name: benchmark-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  storageClassName: "benchmark-gce-pd-1"
  persistentVolumeReclaimPolicy: Retain
  gcePersistentDisk:
    pdName: pd-1
    fsType: ext4

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: benchmark-pvc
spec:
  accessModes:
    - ReadWriteMany
  volumeName: benchmark-pv
  resources:
    requests:
      storage: 10Gi
  storageClassName: "benchmark-gce-pd-1"

I use gcp persistant disk.

gyuminhwangbo@Gyuminui-MacBookAir kubernetes % gcloud compute disks list
NAME                                                LOCATION    LOCATION_SCOPE  SIZE_GB  TYPE         STATUS
gke-benchmark-cluster-1-default-pool-5c552e5b-5fhg  us-east1-b  zone            50       pd-balanced  READY
gke-benchmark-cluster-1-default-pool-59d3581b-okyl  us-east1-c  zone            50       pd-balanced  READY
gke-benchmark-cluster-1-default-pool-4422c9f2-nrpx  us-east1-d  zone            50       pd-balanced  READY
pd-1

So it looks fine but when I remove db-pod, all data is removed :(

ghkdqhrbals commented 7 months ago

Solved

As @rbq suggest in https://github.com/docker-library/postgres/issues/263#issuecomment-460998040, add PGDATA environment path to solve this issue!

apiVersion: apps/v1
kind: Deployment
metadata:
  name: db-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: benchmark-db
  template:
    metadata:
      labels:
        app: benchmark-db
    spec:
      containers:
        ...
          env:
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata # inform postgresql to connect dataset with this directory
            ...