Sometimes it is necessary to schedule an application process and repeat operations such as sending mail, alarms, validation, and so on. On the Server, we typically use a CRon that is easy to set up and maintain. If you don’t know much about it, you can visit the following link, you need to know all the information about the cron here: en.wikipedia.org/wiki/Cron

When using Docker, you can run crontab to do this, but when using Kubernetes, what components should you use to do this?

In practice, Kubernetes operates differently because there may be one or more instances of the same service under load balancing, and crontab only runs once no matter how many instances are started. On the other hand, we need crontab to run once for each process of one or more pods. There is a feature in Kubernetes called CronJob that solves this problem.

This article explains how CronJob works and its limitations, and concludes with a few tips to help you avoid common mistakes.

The following examples are based on Kind.

How to create CronJob:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-cron-job
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: my-cron-job
            image: curlimages/curl
            resources:
              limits:
                cpu: "1"
                memory: "300Mi"
              requests:
                cpu: "1"
                memory: "300Mi"
            args:
            - /bin/sh
            - -c
            - date; echo "Starting an example of CronJob"; resp=$(curl -I --http2 https://www.google.com) ; echo $resp; exit 0
          restartPolicy: Never
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 3
Copy the code

CronJob has been created to run a curl mirror every minute.

At the same time, you need to set resource limits (such as CPU and memory). If you are using AWS, Azure or GCP instances as ARGs, the best way to visualize this is to do a simple curl on Google.

This instance is never restarted, and there is a limit to the number of successful and failed historical jobs, which in this case is set to 3.

  • The spec. SuccessfulJobsHistoryLimit: keep the number of successful completion of the cronjob
  • The spec. FailedJobsHistoryLimit: keep the number of failed cronjob

If you want to learn more about the CronJob API, I strongly recommend reading the following links: docs.koki. IO /short/resou…

Now, run the following command to apply your CronJob in Kubernetes.

$ kubectl apply -f cronjob.yml
Copy the code

If no errors occur, you can use the following command to see your most recently configured cronjob:

$ kubectl get cronjob
Copy the code

I used Lens to visualize all the cronjobs available, which is useful for tracing and monitoring in Kubernetes.

View logs:

To delete this entry, run the following command:

$ kubectl delete cronjob my-cron-job
Copy the code

In this example, you run a simple Cron and an example.

I found that one limitation of cronjobs is that you need to schedule multiple cronjobs for the same process by adding a row to each process. However, cronjobs are not available in Kubernetes 1.8 Beta, and you must use parallelism to copy the same cronjobs. For another schedule, you need to create another CRon entry. I look forward to the opportunity to schedule multiple patterns for the same process in the future.

conclusion

Kubernetes CronJob is very useful and easy to learn, you can visit the following links to read and learn more about API parameters, and run some tests to better understand how it works: docs.koki. IO /short/resou…

Original link: dzone.com/articles/ku…