<p>What is the best practice for backing up a Postgres database running on <a href="https://cloud.google.com/container-engine/" rel="noreferrer">Google Cloud Container Engine</a>?</p>

<p>My thought is working towards storing the backups in <a href="https://cloud.google.com/storage/" rel="noreferrer">Google Cloud Storage</a>, but I am unsure of how to connect the Disk/Pod to a Storage Bucket.</p>

<p>I am running Postgres in a Kubernetes cluster using the following configuration:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - image: postgres:9.6.2-alpine
          imagePullPolicy: IfNotPresent
          env:
            - name: PGDATA
              value: /var/lib/postgresql/data
            - name: POSTGRES_DB
              value: my-database-name
            - name: POSTGRES_PASSWORD
              value: my-password
            - name: POSTGRES_USER
              value: my-database-user
          name: postgres-container
          ports:
            - containerPort: 5432
          volumeMounts:
            - mountPath: /var/lib/postgresql
              name: my-postgres-volume
      volumes:
        - gcePersistentDisk:
            fsType: ext4
            pdName: my-postgres-disk
          name: my-postgres-volume
</code></pre>

<p>I have attempted to create a <a href="https://kubernetes.io/docs/user-guide/jobs/" rel="noreferrer">Job</a> to run a backup:</p>

<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: postgres-dump-job
spec:
  template:
    metadata:
      labels:
        app: postgres-dump
    spec:
      containers:
        - command:
            - pg_dump
            - my-database-name
          # `env` value matches `env` from previous configuration.
          image: postgres:9.6.2-alpine
          imagePullPolicy: IfNotPresent
          name: my-postgres-dump-container
          volumeMounts:
            - mountPath: /var/lib/postgresql
              name: my-postgres-volume
              readOnly: true
      restartPolicy: Never
      volumes:
        - gcePersistentDisk:
            fsType: ext4
            pdName: my-postgres-disk
          name: my-postgres-volume
</code></pre>

<p>(As far as I understand) this should run the <a href="https://www.postgresql.org/docs/current/static/backup-dump.html" rel="noreferrer"><code>pg_dump</code></a> command and output the backup data to stdout (which should appear in the <code>kubectl logs</code>).</p>

<p>As an aside, when I inspect the Pods (with <code>kubectl get pods</code>), it shows the Pod never gets out of the "Pending" state, which I gather is due to there not being enough resources to start the Job.</p>

<p>Is it correct to run this process as a Job?
How do I connect the Job to Google Cloud Storage?
Or should I be doing something completely different?</p>

<p>I'm guessing it would be unwise to run <code>pg_dump</code> in the database Container (with <code>kubectl exec</code>) due to a performance hit, but maybe this is ok in a dev/staging server?</p>

<p>As @Marco Lamina said you can run pg_dump on postgres pod like </p>

<pre><code>DUMP
// pod-name         name of the postgres pod
// postgres-user    database user that is able to access the database
// database-name    name of the database
kubectl exec [pod-name] -- bash -c "pg_dump -U [postgres-user] [database-name]" &gt; database.sql


RESTORE
// pod-name         name of the postgres pod
// postgres-user    database user that is able to access the database
// database-name    name of the database
cat database.sql | kubectl exec -i [pod-name] -- psql -U [postgres-user] -d [database-name]
</code></pre>

<p>You can have a job pod that does run this command and exports this to a file storage system such as AWS s3.</p>
