How to Set up a Kubernetes CronJob to Create Medusa Backups
Guest Author: Mariano Arellano
Learn how easy it is to use Kubernetes CronJobs to automate
kubectl commands with our example of How to create a Medusa backup in K8ssandra.
If you’ve been following the awesome workshop on the K8ssandra repository or just deployed your first cluster, you are probably wondering “How am I going to automate helm commands to run daily backups?”.
The good news is: you don’t have to.
ServiceAccount / ClusterRole / ClusterRoleBinding
In order to do this, we will use this neat trick to directly access the API server from within a pod, using
kubectl to apply a
We first need to create a custom
ClusterRoleBinding for the pod that will be created by our CronJob, so let’s do that first.
Create a file called
medusa-backup-sa.yaml, and paste the following manifest:
metadata.namespace accordingly to suit your needs, save and apply:
clusterRole is only giving access to the
cassandra.k8ssandra.io/cassandrabackups API resource with enough permissions to create, get and list backups.
We are going to set the schedule for the cron job to every day of the month at 1:20 AM, but you can set the schedule to anything that fits your environment.
The cronJob itself will spawn a pod to execute the job that creates a backup called
medusa-daily-yyyymmddhhmmss, for example,
cassandraBackup manifest will be printed to
printf to convert our
\n to new lines, and then piped to
kubectl to apply the manifest from
The full command that will be executed from the pod is:
Medusa backup cronJob
Before applying the manifest below, verify the
cassandraDatacentervalue for the manifest. In this example we are using
dc1, but yours may differ. Same goes for
When ready, save the manifest below as
medusa-cronjob.yaml and deploy it with
kubectl apply -f medusa-cronjob.yaml:
Let’s analyze what happened when the
k8ssandra-medusa-backup CronJob was triggered (note that the following is just a small piece of the complete
describecommand, not the entire result):
And there you go! I hope this post has helped you understand the capabilities of CronJobs and running restricted
kubectl commands from a pod.
- DataStax Apache Cassandra on Kubernetes Workshop on GitHub
- Kubernetes Documentation: CronJobs
- Apache Cassandra Backup and Restore Tool on GitHub
- Kubernetes Documentation: Directly Accessing the Rest API
- Chronitor ChronTab Guru Editor
- NetIQ Documentation: Understanding Cron Syntax in the Job Scheduler
About the Author: Mariano Arellano started his career in IT and Network and infrastructure support but found his true passion in software development where he gained experience in planning, development, testing, and maintenance of IT. He loves to tackle problems by automating processes and is passionate about open source, containers, self-hosting, privacy, Rust, Bash, microcontrollers, and ARM.