Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use job resource object

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you how to use job resource objects. The editor thought it was very practical, so I shared it with you to learn. The following information is about the job resource object.

Job resource object

Pod containers for service classes: RC, RS, DS, Deployment.

The Pod container of the work class: Job--- > execute once, or execute the handler in batch, and launch the container when you are finished.

[root@master ~] # cat job.yaml

Kind: Job

ApiVersion: batch/v1

Metadata:

Name: test-job

Spec:

Template:

Metadata:

Name: test-job

Spec:

Containers:

Name: hello

Image: busybox

Command: ["echo", "hello K8s job!"]

RestartPolicy: Never

[root@master ~] # kubectl get pod

NAME READY STATUS RESTARTS AGE

Test-job-qgc6p 0/1 Completed 0 55s

[root@master ~] # kubectl logs test-job-qgc6p

Hello k8s job!

PS: note that if the task is executed incorrectly in the container, the container will be operated according to the container restart policy. However, the container restart policy can only be: Never and OnFailure.

Improve the execution efficiency of Job

We can add the parallelism option under the Job.spec field. Indicates how many Pod runs at the same time to execute tasks

We can add the completions option under the Job.spec field. Indicates the total number of Pod that needs to be completed.

[root@master ~] # cat job.yaml

Kind: Job

ApiVersion: batch/v1

Metadata:

Name: test-job

Spec:

Completions: 8 / / how many Pod needs to be completed

Parallelism: 2 / / running several pod simultaneously

Template:

Metadata:

Name: test-job

Spec:

Containers:name: hello

Image: busybox

Command: ["echo", "hello K8s job!"]

RestartPolicy: OnFailure

How to execute Job regularly

Kind: CronJob

ApiVersion: batch/v1beta1

Metadata:

Name: hello

Spec:

Schedule: "/ 1 *"

JobTemplate:

Spec:

Template:

Spec:

Containers:name: hello

Image: busybox

Command: ["echo", "hello cronjob!"]

RestartPolicy: OnFailure

[root@master] # kubectl apply-f cronjob.yaml

[root@master ~] # kubectl get cronjobs.batch

NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE

Hello / 1 * False 0 47s 51s

[root@master ~] # kubectl logs hello-1579053480-vpm6t

Hello cronjob!

Looking at the status of the Pod at this point, you will find that every minute a new Pod is run to perform the tasks specified by the command.

Exercise:

The crontab task above is scheduled to run at 10:05 on January 15, 2020.

[root@master ~] # cat cronjob.yaml

Kind: CronJob

ApiVersion: batch/v1beta1

Metadata:

Name: hello

Spec:

Schedule: "5 10 15 1 3"

JobTemplate:

Spec:

Template:

Spec:

Containers:name: hello

Image: busybox

Command: ["echo", "hello cronjob!"]

RestartPolicy: OnFailure

At this point, you will find that if you specify a specific time, the task may not be carried out.

[root@master ~] # kubectl api-versions / / View api version

Add an apiVersion library.

[root@master ~] # vim / etc/kubernetes/manifests/kube-apiserver.yaml

/ / add to the yaml file

-- runtime-config=batch/v2alpha1=true

Then restart the kubelet service and re-identify the contents of the api yaml file.

/ / View the api version library

[root@master ~] # kubectl api-versions

PS: note that the job for the specified time still cannot be run properly at this time, because the official K8s has not perfected this feature in the support of cronjob as a resource object. It has yet to be developed.

Like job resources, the concurrent Job parameter: parallelism is also supported under cronjob.spec.jobTemplate.spec, and the total number of completed Pod parameters: completions.

On the use of job resource objects to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report