Cron Jobs are time-based, scheduled tasks that run periodically at fixed times, dates, or intervals. They’re an important feature in any tech stack. From renewing SSL certificates with Let’s Encrypt to refreshing configurations, Cron Jobs have tons of uses limited really only to your imagination. What sort of thing might you need to run periodically in your infrastructure?
Traditionally, Cron Jobs were handled by your server or virtual machines operating system. Kubernetes makes Cron Jobs easy by taking care of the scheduling on its own, running the Cron Job’s container in a special pod that can be assigned to any worker node in the cluster. Like many other Kubernetes features, it’s easy to use, portable, and requires no separate VM or dedicated server. Yipee.io takes Cron Jobs in Kubernetes one step further by allowing you to visualize your infrastructure, including all of your Cron Jobs. We’ve also integrated a special Cron Job option and interface, making it easier than ever to create them with Kubernetes. Follow along with the steps and screenshots below to make your first Kubernetes Cron Job.
Step 1: Create a new Cron Job controller
A Cron Job controller can be added by clicking ‘Cron Job’ in the controllers dropdown located at the upper left corner of the Kubernetes editor screen. A modal dialog will pop up asking for the desired specifications of your new cron job controller. Add a “Cron Schedule” of “*/1 * * * *” as well as a “Starting Deadline Seconds” of 10, then click “CREATE.”
Step 2: Configure your Cron Job controller’s restart policy
Click on your new Cron Job controller on the main canvas and a ‘Cron Job’ side panel will appear. Change the bottom “Restart” dropdown to whichever options best suits your needs. For this example, we’ll select “On Failure” as our “Restart” option. Notice there is an error on the Cron Job sidebar, this is because we have yet to add a container to our new cron job controller. We’ll do that in the next step.
Step 3: Create a new container from the busybox image and add it to your new Cron Job controller
A container can be added by clicking ‘Container’ in the containers dropdown located at the upper left corner of the Kubernetes editor screen (the second dropdown to the left). A modal dialog will pop up asking for the desired specifications of your new container. Create a good name for your new container and set it’s image to “busybox”, then click “CREATE.”
Drag a and drop your new container into your new Cron Job’s graphic (rounded square shape) on the canvas. When this is done successfully it should look similar to the screenshot below.
Step 4: Add arguments to your new container
Click on your new container that now lives inside your Cron Job controller’s graphic on the canvas. Notice the sidebar will change to the “Container” sidebar. Scroll down an expand the “Advanced” section, that is where we’ll add the container’s arguments. In the “Arguments” field enter “/bin/sh -c date; echo Here’s my first cronjob” and save your new model.
Step 5: Download and run
Click on the download button in the upper right menu on the editor page to download your model’s Kubernetes yml file. Create your new cronjob locally by running “kubectl apply -f [your downloaded kubernetes yml filename]”. You can also check up on your cronjob by running “kubectl get cronjob [your cronjob controllers name]”.