Product docs and API reference are now on Akamai TechDocs.
Search product docs.
Search for “” in product docs.
Search API reference.
Search for “” in API reference.
Search Results
 results matching 
 results
No Results
Filters
Create a Kubernetes CronJob
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Linode Kubernetes Engine (LKE) is an open source container orchestration system that helps deploy and manage containerized applications. If you are not familiar with Kubernetes, read our Beginner’s Guide to Kubernetes. Kubernetes (K8s) includes the capability to schedule jobs to run at a particular time with CronJobs. CronJobs use the same scheduling syntax as cron and crontab, which are standard Linux utilities. If you are not familiar with cron, you can refer to our Schedule Tasks with Cron guide.
In this guide, you learn the syntax for creating Kubernetes CronJobs. You also learn how to generate automatic daily backups for a Drupal website using Kubernetes CronJobs.
Before You Begin
Ensure you have deployed a Kubernetes cluster. To deploy a Linode Kubernetes Engine (LKE) cluster, see our Deploy and Manage a Cluster with Linode Kubernetes Engine guide.
After deploying your Kubernetes cluster, make sure your local environment has kubectl installed, and you can access your cluster using
kubectl
.This guide uses a Drupal website deployed with LKE to demonstrate how to back up a MySQL database. To follow along, ensure you use the How to Install Drupal with Linode Kubernetes Engine guide to deploy your own Drupal site.
An Overview of a Kubernetes CronJob
Like other Kubernetes resources, CronJobs are defined in a manifest file. The example manifest file creates a CronJob that deploys a simple “Hello” script once a minute.
- File: cronjob.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
apiVersion: batch/v1 kind: CronJob metadata: name: hello spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: hello image: busybox imagePullPolicy: IfNotPresent command: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster restartPolicy: OnFailure
A Kubernetes CronJob must contain the apiVersion
, kind
, and metadata
fields. It must also contain the spec.schedule
field which defines the time interval for the CronJob. The time interval syntax for a Kubernetes is the same as Linux system Cron jobs. You can refer to the How to Use Cron and crontab - The Basics section of our Cron guide for time interval syntax details. Finally, the jobTemplate
field is required. This field defines the requirements for the Pod that executes your CronJob. It also defines the commands to execute from the Pod. To learn more about defining jobTemplates
, see the Kubernetes Jobs documentation.
To launch a CronJob, execute the command below. Ensure your replace cronjob.yaml
with the name of your CronJob’s manifest file.
kubectl create -f cronjob.yaml
You can use the kubectl get cronjob hello
command to view information about your CronJob. Use the kubectl logs $pods
command to view your Pod logs, and the kubectl delete cronjob hello
command to delete the CronJob when it’s no longer needed.
How to Backup a Mysql Database Using a Kubernetes Cronjob
This section shows you how to back up a MySQL database. The example assumes a Drupal website has been deployed using LKE. You can adapt the Kubernetes manifest file example for your own MySQL database. The steps you accomplish in this section are the following:
- Before creating the CronJob, you test it by logging into your MySQL Pod and executing the CronJob’s commands
- Create the CronJob to backup your MySQL database
- Schedule your CronJob
Test Your CronJob Command
To back up your MySQL database, you need to find your Kubernetes cluster’s MySQL Pod name. Issue the following command to find your Pod’s name:
kubectl get pods
Your output displays a similar output:
NAME READY STATUS RESTARTS AGE
mysql-6bf46f94bf-tcgs2 1/1 Running 0 13m
drupal-77f665d45b-568tl 1/1 Running 0 5m1s
The mysqldump
command can be executed after logging into your MySQL Pod. Execute the following command replacing mysql-6bf46f94bf-tcgs2
with your own Pod’s name.
kubectl exec -it mysql-56f9846bb7-tlbv6 -- /bin/bash
Viewing your MySQL Pod’s terminal prompt, run the mysqldump
command to generate a backup copy of your database. Enter your database’s password when prompted. You can verify the dump.sql
backup file created using the ls
command.
kustomization.yaml
file.Create your CronJob
Your CronJob file incorporates the mysqldump
command that you tested in the previous section. It is not recommended to store your database’s password in your Kubernetes manifest file. Your MySQL Pod should already have the MySQL database’s password stored in its context, so you can alter your mysqldump
command in the following way:
mysqldump drupal-db -p$MYSQL_ROOT_PASSWORD > dump.sql
Create a new Kubernetes manifest file with the following content:
- File: backup.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
apiVersion: batch/v1beta1 kind: CronJob metadata: name: backup spec: schedule: "@daily" jobTemplate: spec: template: spec: containers: - name: mysql image: mysql imagePullPolicy: IfNotPresent command: - /bin/sh - -c - mysqldump drupal-db -p$MYSQL_ROOT_PASSWORD > dump.sql; - date; echo Drupal database has been backed up restartPolicy: OnFailure
Apply the CronJob to your Kubernetes cluster.
kubectl create -f backup.yaml
You should see a similar response if the CronJob is created successfully.
cronjob.batch/backup created
To get the status of the job, issue the following command:
kubectl get cronjob backup
Your output displays a similar result when the command executes successfully.
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
backup @daily False 0 <none> 117s
The job shows up with <none>
for the last schedule. The following day, try again, and you can see that the job ran at midnight. You can also check the Pod’s logs for the output by using the kubectl logs $pods
command.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on