Get the App
SLTechnology News&Howtos  ›  Servers  › 

58 of kubernetes practice: CronJob

Shulou Source: shulou.com Published: 2022-06-02 04:58:05 10月14日 Update

One: brief introduction

CronJob is a scheduled task, which is similar to the crontab of Linux system, which runs a specified task in a specified period of time.

1. Run only once at a given point in time

two。 Run periodically at a given point in time

II: CronJob definition

1.spec.schedule specifies the running period of the task in the same format as Cron

2.spec.jobTemplate specifies the tasks to be run in the same format as Job

3.spec.startingDeadlineSeconds specifies a deadline for the task to start

4.spec.concurrencyPolicy specifies the concurrency strategy for tasks, and supports three options: Allow, Forbid and Replace

Three: examples

Click (here) to collapse or open

ApiVersion: batch/v1beta1

Kind: CronJob

Metadata:

Name: hello

Spec:

Schedule: "* / 1 *"

JobTemplate:

Spec:

Template:

Spec:

Containers:

-name: hello

Image: busybox

Args:

-/ bin/sh

-- c

-date; echo Hello from the Kubernetes cluster

RestartPolicy: OnFailure1. Kubectl create-f croJob.yaml

2. Kubectl get cronjob hello

3.kubectl get jobs-watch monitors the implementation of job

Four: other

1.Cron Job probably creates a Job object during each scheduled run time. We say this probably because two Job may be created in a particular environment, or none of the Job may be created. We try to reduce this situation, but we can't avoid it completely. Therefore, creating a Job operation should be idempotent.

Based on the parallelism of the Pod it creates, Job is responsible for retrying the creation of the Pod and determining the success or failure of this set of Pod. Cron Job will not check the Pod at all.

two。 Once you no longer need Cron Job, you can simply delete it using the kubectl command: kubectl delete cronjob hello. This will terminate the Job being created. However, running Job will not be terminated and Job or their Pod will not be deleted. To clean up those Job and Pod, you need to list all the Job created by that Cron Job, and then delete them.

3. Once the Job is deleted, the Pod created by Job is also deleted. Note that all Job created by the Cron Job with the name "hello" is named with the prefix string "hello-". If you want to delete all Job in the current Namespace, you can delete them immediately with the command kubectl delete jobs-- all.

Tags: Run task time cycle creation command situation format success three two prefix can be passed name periodicity character string object term Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology Docker Microsoft vpn