2

Sometimes, scheduling an application process, some repetitive operations (such as sending emails, alarms, verification, etc.) is extremely necessary. On the server, we usually use a cron, which is extremely easy to set up and maintain. If you don’t know much about this, you can visit the following link, all you need to know about cron is here:
https://en.wikipedia.org/wiki/Cron

When using Docker, you can run crontab to complete the above operations, but when you use Kubernetes, what components should you use to perform the above operations?

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

This article will introduce how CronJob works and its limitations, and finally give 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

CronJob has been created, it runs a curl image every minute.

At the same time, you need to set resource limits (such as CPU and memory). If you use AWS, Azure, or GCP instances as args, the best way to visualize is to perform simple curl on Google.

This instance will never restart, and there is a limit on the history of successful and failed jobs. In this example, the number of times is set to 3.

  • spec.successfulJobsHistoryLimit: the number of successfully completed cronjobs to be retained
  • spec.failedJobsHistoryLimit: the number of failed cronjobs to keep

If you want to know more about CronJob API, I strongly recommend you to read the content in the following link:
https://docs.koki.io/short/resources/cron-job

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

$ kubectl apply -f cronjob.yml

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

$ kubectl get cronjob

I use Lens to visualize all available cronjobs. It is very useful for tracking and monitoring in Kubernetes.

图片

View log:

图片

You can delete this entry by running the following command:

$ kubectl delete cronjob my-cron-job

In this example, a simple Cron and an instance are run.

I found that one limitation of CronJob is that you need to schedule multiple CronJobs for the same process by adding a row to each process. However, CronJob is not provided in Kubernetes 1.8 beta, you must use parallelism to copy the same CronJob. For another schedule, you need to create another cron entry. I look forward to the opportunity to schedule multiple modes for the same process in the future.

in 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:
https://docs.koki.io/short/resources/cron-job/

Original link: https://dzone.com/articles/kubernetes-cronjob-an-introduction

Rancher
1.2k 声望2.5k 粉丝

Rancher是一个开源的企业级Kubernetes管理平台,实现了Kubernetes集群在混合云+本地数据中心的集中部署与管理。Rancher一向因操作体验的直观、极简备受用户青睐,被Forrester评为“2020年多云容器开发平台领导厂商...