Kubernetes Cron Job for postgres db backup

5/5/2021

Iam trying to create a cron job like this:

<!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-js -->
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgres-backup
spec:
  # Backup the database every day at 2AM
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: postgres-backup
            image: postgres:10.4
            command: ["/bin/bash"]
            args: ["-c", 'PGPASSWORD="$PGPASS" pg_dump -U postgresadmin -h postgres example > /var/backups/backup-$(date +"%m-%d-%Y-%H-%M").sql']
            env:
            - name: PGPASS
              value: admin123
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-pv-volume
          restartPolicy: Never
          volumes:
          - name: postgres-pv-volume
            hostPath:
            # Ensure the file directory is created.
              path: /var/volumes/postgres-backups
              type: DirectoryOrCreate
<!-- end snippet -->

When iam running the crone job the pods getting created with status created. But i was not able to see the backup files in my postgres pod. I think the backup file getting created in the same cronjob pod but how to access the completed pod.

-- Rajesh Davuluri
database-backups
kubernetes
kubernetes-cronjob
postgresql

1 Answer

5/5/2021

Like mdaniel mentioned in the comment, you are using the volume type hostPath, which is a path on the node, so I guess in order for you to find the file being created, you need to login to that node and look under that path on the node.

And personally I don't think you should try to store it in the pod at all, since Job is run to complete, which means you are not able to retrieve anything in it when the job is completed.

-- Nob Wong
Source: StackOverflow