Kubernetes CronJob Backup for PostgreSQL

Learn how to run and manage scheduled tasks in Kubernetes using CronJobs. This guide covers YAML examples, concurrency policies, and best practices for running reliable, recurring workloads inside your cluster.

Table of contents
Key Takeaways

1. Kubernetes CronJob is a reliable way to automate database backups (e.g., PostgreSQL) on a schedule.

2. The tutorial includes a custom Docker image with PostgreSQL and AWS CLI for backup and S3 upload.

3. Backup process is handled through a shell script invoked by the CronJob.

4. Environment variables manage credentials securely via the Kubernetes spec.

5. Backup files are uploaded to AWS S3, and Slack notifications provide status updates.

6. Ideal for production-grade backup automation in Kubernetes clusters.

Introduction

On a Kubernetes cluster, there can be many scenarios where you want to automate scheduled backups of your PostgreSQL database (or any other database). This step-by-step guide will walk you through how to use a Kubernetes CronJob for PostgreSQL backup, using a Docker container with AWS CLI to upload backups to an S3 bucket.

Overview of the Kubernetes CronJob Backup Strategy

Here’s what we’ll do:

  1. Create a Docker image (Ubuntu + PostgreSQL + AWS CLI).
  2. Write a shell script to:
    • Connect to PostgreSQL
    • Take a database dump
    • Compress it
    • Upload to AWS S3
    • Send a Slack notification
  3. Create a Kubernetes CronJob to run this script on schedule.

Step 1: Create the Backup Script (postgres-backup.sh)

#!/bin/bash

cd /home/root
date1=$(date +%Y%m%d-%H%M)
mkdir pg-backup

PGPASSWORD="$PG_PASS" pg_dumpall -h postgresql-postgresql.devtroncd -p 5432 -U postgres > pg-backup/postgres-db.tar
file_name="pg-backup-"$date1".tar.gz"

# Compressing backup file
tar -zcvf $file_name pg-backup

notification_msg="Postgres-Backup-failed"
filesize=$(stat -c %s $file_name)
mfs=10
if [[ "$filesize" -gt "$mfs" ]]; then
  aws s3 cp $file_name $S3_BUCKET
  notification_msg="Postgres-Backup-was-successful"
fi

# Send Slack Notification
send_slack_notification() {
  payload='payload={"text": "'$1'"}'
  curl --silent --data-urlencode "$(printf "%s" $payload)" ${APP_SLACK_WEBHOOK} || true
}
send_slack_notification $notification_msg

Step 2: Create the Docker Image (Dockerfile)

This image includes PostgreSQL, AWS CLI, and the shell script for backups.

FROM ubuntu:18.04

RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y python curl openssh-server gnupg2

# Install pip & AWS CLI
RUN curl -sO https://bootstrap.pypa.io/get-pip.py && python get-pip.py
RUN pip install awscli

# PostgreSQL
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7FCC7D46ACCC4CF8
RUN apt update && apt -y install postgresql-11

# Copy backup script
COPY . /home/root/

USER root

Step 3: Build and Push the Docker Image

docker build -t ubuntu-aws-postgres:latest -f dockerfile .
docker tag ubuntu-aws-postgres:latest <your-account>.dkr.ecr.us-east-2.amazonaws.com/aws-cli-ubuntu
docker push <your-account>.dkr.ecr.us-east-2.amazonaws.com/aws-cli-ubuntu:latest

Replace <your-account> with your actual AWS ECR account ID.

Step 4: Create Kubernetes CronJob YAML

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgresql-backup-cron-job
spec:
  schedule: "0 8,20 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: postgresql-backup-job-pod
              image: <your-ecr-url>/aws-cli-ubuntu:latest
              env:
                - name: S3_BUCKET
                  value: "s3://postgres-backup-11334/dev-pro-cluster/"
                - name: PG_PASS
                  value: "dummy-password"
                - name: AWS_ACCESS_KEY_ID
                  value: "your-access-key"
                - name: AWS_SECRET_ACCESS_KEY
                  value: "your-secret"
                - name: AWS_DEFAULT_REGION
                  value: "us-east-2"
                - name: APP_SLACK_WEBHOOK
                  value: "https://hooks.slack.com/services/XXXX"
              imagePullPolicy: Always
              args:
                - /bin/bash
                - -c
                - cd /home/root; bash postgres-backup.sh;
          restartPolicy: OnFailure
      backoffLimit: 3

Replace secrets and environment variables with your own secure values.

Step 5: Apply the CronJob to Kubernetes

kubectl apply -f postgresql-backup-cron-job.yaml -n <namespace>

To view or manage cronjobs:

kubectl get cronjob -n <namespace>
kubectl delete cronjob <cron-job-name> -n <namespace>

Why Use Kubernetes CronJob for Backups?

  • Fully automated database backups
  • Works inside Kubernetes clusters without external tools
  • Easily integrates with AWS S3 and notification systems
  • Portable and customizable across environments
  • Highly reliable with Kubernetes scheduling and retry policies

Conclusion

Kubernetes CronJobs provide a powerful and reliable way to run scheduled tasks like database backups, log rotations, or periodic cleanups within your cluster. By using native Kubernetes objects, you can maintain consistency, scalability, and observability for your recurring jobs.

Make sure to define resource requests and limits, monitor job status, and set proper concurrency policies to avoid unexpected behavior in production.

💡
Coming soon: Devtron is soon launching Agentic AI to simplify Kubernetes operations like CronJob setup, backup automation, and CI/CD tasks, all through natural language prompts. Stay tuned!

FAQ

How to Set Up a Kubernetes Cron Job for Postgres Backups?

Create a Docker image with Postgres and AWS CLI, write a script to take a dump and upload it to S3, and schedule it as a Kubernetes cron job.

How Do I Backup a Postgres Database in Kubernetes Using Docker?

Build a custom Docker image with Postgres and AWS CLI, then write a script to dump the Postgres database and upload it to an S3 bucket through Kubernetes.

How Do I Configure the Kubernetes CronJob for Backups?

Write a postgresql-backup-cron-job.yaml file to schedule the backup job, which connects to the Postgres pod, creates a backup, and uploads it to an S3 bucket.

How Do I Verify and Delete a Kubernetes Cron Job?

Verify with kubectl get cronjob -n <> and delete with kubectl delete cronjob <<cron-job-name>> -n <<namespace>>.

Related articles

Related articles