Home > Net >  pg_dump save backup file in another pod on kubernetes
pg_dump save backup file in another pod on kubernetes

Time:11-19

I've created a cronjob in kubernetes to do scheduled backups of my postgresql db (assuming that I have PGPASS Secret already created):

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgres-backup-dev
spec:
  # Backup the database every day at 2AM
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: postgres-backup-dev
            image: postgres:10.4
            command: ["/bin/sh"]
            args: ["-c", 'echo "$PGPASS" > /root/.pgpass && chmod 600 /root/.pgpass && pg_dump -Fc -h <host> -U <user> <db> > /var/backups/backup.dump']
            env:
            - name: PGPASS
              valueFrom:
                secretKeyRef:
                  name: dev-pgpass
                  key: pgpass
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-backup-storage
          restartPolicy: Never
          volumes:
          - name: postgres-backup-storage
            hostPath:
              path: /var/volumes/postgres-backups
              type: DirectoryOrCreate

This works but it's dumping the backup file in the container of the Job that is created by the Cronjob. My goal is to rather dump this file in the container of my original database. My original db deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:10.4
        imagePullPolicy: Always
        resources:
          requests:
            cpu: "1"
            memory: "1Gi"
          limits:
            cpu: "2"
            memory: "2Gi"
        ports:
        - containerPort: 5432
          protocol: TCP
        envFrom:
        - configMapRef:
            name: postgres-config
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgredb
      volumes:
      - name: postgredb
        persistentVolumeClaim:
          claimName: postgres-pv-claim

Is there any way to achieve this?

Thanks

CodePudding user response:

You need a shared volume.

If the pods are on the same host, you can use a hostPath volume.

If the pods are on a cluster, you need a different storage backend, like NFS, EFS (on AWS), CephFS or GlusterFS.

  • Related