<p>I've created a cronjob in kubernetes to do scheduled backups of my postgresql db (assuming that I have PGPASS Secret already created):</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgres-backup-dev
spec:
  # Backup the database every day at 2AM
  schedule: &quot;0 2 * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: postgres-backup-dev
            image: postgres:10.4
            command: [&quot;/bin/sh&quot;]
            args: [&quot;-c&quot;, 'echo &quot;$PGPASS&quot; &gt; /root/.pgpass &amp;&amp; chmod 600 /root/.pgpass &amp;&amp; pg_dump -Fc -h &lt;host&gt; -U &lt;user&gt; &lt;db&gt; &gt; /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
</code></pre>
<p>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:</p>
<pre><code>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: &quot;1&quot;
            memory: &quot;1Gi&quot;
          limits:
            cpu: &quot;2&quot;
            memory: &quot;2Gi&quot;
        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
</code></pre>
<p>Is there any way to achieve this?</p>
<p>Thanks</p>

<p>You need a shared volume.</p>
<p>If the pods are on the same host, you can use a <code>hostPath</code> volume.</p>
<p>If the pods are on a cluster, you need a different storage backend, like NFS, EFS (on AWS), CephFS or GlusterFS.</p>
