Exploring Kubernetes CronJobs

Introduction: In this tutorial, we will explore Kubernetes CronJobs, a resource that allows you to run jobs periodically at specified intervals. We will use Meshery Playground, an interactive live cluster environment, to perform hands-on labs for working with CronJobs in Kubernetes.

Prerequisites:

  • Basic understanding of Kubernetes concepts.
  • Meshery Playground access. If you don’t have an account, you can sign up at Meshery Playground.

Lab Scenario: Scheduled Backups using CronJobs

Objective: Learn how to use Kubernetes CronJobs to schedule and automate periodic backups of a sample application.

Steps:

1. Accessing Meshery Playground:

  • Log in to the Meshery Playground using your credentials.
  • Once logged in, navigate to the Meshery Playground dashboard.

2. Deploying an Application:

  • Deploy a simple application that needs periodic backups. You can use a web application or any other application of your choice.

Deploying a web application

To deploy a web application on Meshery, follow these steps:

  1. Navigate to the Configuration menu in Meshery.
  2. Select “Designs” from the menu.
  3. Click on the “Import Design” button. Configuration menu
  4. Fill in the required details for your application or design, including the Design File Name, Design Type, and choose your preferred upload method:
    • For File Upload, click “Browse” to locate and select the design file.
    • For URL Import, paste the link to the design.
  5. Click the “Import” button to initiate the upload process. Click import button
  6. Upon successful import, a pop-up message will confirm the completion, and it will also indicate that your design has been automatically saved in Meshery. Design is auto-saved

3. Creating a CronJob for Backups:

  • Create a CronJob that runs a backup script at a specified interval to back up the application data.
Alternate: Command line instructions \```yaml # backup-cronjob.yaml apiVersion: batch/v1beta1 kind: CronJob metadata: name: backup-cronjob spec: schedule: "0 * * * *" # Run every hour jobTemplate: spec: template: spec: containers: - name: backup-container image: backup-image:latest # Replace with your backup script image # Add volume mounts and commands as needed for backup restartPolicy: OnFailure \```

Apply the CronJob configuration:

`bash mesheryctl pattern apply -f backup-cronjob.yaml \`

4. Verifying CronJob Execution:

  • Monitor the execution of the CronJob and verify that backups are created at the specified intervals.

5. Scaling and Updating CronJobs:

  • Explore how to scale the CronJob or update its schedule by modifying the CronJob configuration.

`bash kubectl edit cronjob backup-cronjob \`

Use Meshery Playground to visualize the changes and observe the impact on the scheduled backups.

6. Clean-Up:

  • Delete the CronJob and application resources after completing the lab.

`bash kubectl delete cronjob backup-cronjob kubectl delete -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/application/deploying-wordpress/mysql-deployment.yaml kubectl delete -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/application/deploying-wordpress/wordpress-deployment.yaml \`

7. Saving and Sharing

  • Save your scenario in Meshery Playground for future reference.
  • Invite friends to collaborate in your design.

Share your scenario with other Meshery users or the community for collaborative learning.

Conclusion

Congratulations! You’ve successfully completed the lab on exploring Kubernetes CronJobs using Meshery Playground. This hands-on experience has provided valuable insights into scheduling and automating tasks in Kubernetes using CronJobs. Explore more scenarios in the Meshery Playground to enhance your skills in cloud-native technologies.

Related Tutorials Content