Kubernetes

Linux - K8S - Job & CronJob

本文主要是介绍Linux - K8S - Job & CronJob,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
[00:10:21 root@master1 controller]#cat 13-controller-job-single.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: job-single
spec:
  template:
    metadata:
      name: job-single
    spec:
      restartPolicy: Never
      containers:
      - name: job-single
        image: 10.0.0.55:80/mykubernetes/pod_test:v0.2 
        command: [ "/bin/sh", "-c", "for i in 9 8 7 6 5 4 3 2 1; do echo $i; sleep 2; done" ]
[00:10:23 root@master1 controller]#kubectl apply -f 13-controller-job-single.yaml 
job.batch/job-single created
[00:10:38 root@master1 controller]#kubectl get jobs.batch 
NAME         COMPLETIONS   DURATION   AGE
job-single   0/1           4s         4s
[00:10:42 root@master1 controller]#kubectl describe job job-single 
Name:             job-single
Namespace:        default
Selector:         controller-uid=051a3c20-2031-4ae8-ac79-71a76188062c
Labels:           controller-uid=051a3c20-2031-4ae8-ac79-71a76188062c
                  job-name=job-single
Annotations:      <none>
Parallelism:      1
Completions:      1
Completion Mode:  NonIndexed
Start Time:       Sat, 11 Dec 2021 00:10:38 +0800
Pods Statuses:    1 Running / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  controller-uid=051a3c20-2031-4ae8-ac79-71a76188062c
           job-name=job-single
  Containers:
   job-single:
    Image:      10.0.0.55:80/mykubernetes/pod_test:v0.2
    Port:       <none>
    Host Port:  <none>
    Command:
      /bin/sh
      -c
      for i in 9 8 7 6 5 4 3 2 1; do echo $i; sleep 2; done
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  12s   job-controller  Created pod: job-single--1-jdrhn

[00:10:54 root@master1 controller]#kubectl describe job job-single 
Name:             job-single
Namespace:        default
Selector:         controller-uid=051a3c20-2031-4ae8-ac79-71a76188062c
Labels:           controller-uid=051a3c20-2031-4ae8-ac79-71a76188062c
                  job-name=job-single
Annotations:      <none>
Parallelism:      1
Completions:      1
Completion Mode:  NonIndexed
Start Time:       Sat, 11 Dec 2021 00:10:38 +0800
Completed At:     Sat, 11 Dec 2021 00:10:58 +0800
Duration:         20s
Pods Statuses:    0 Running / 1 Succeeded / 0 Failed
Pod Template:
  Labels:  controller-uid=051a3c20-2031-4ae8-ac79-71a76188062c
           job-name=job-single
  Containers:
   job-single:
    Image:      10.0.0.55:80/mykubernetes/pod_test:v0.2
    Port:       <none>
    Host Port:  <none>
    Command:
      /bin/sh
      -c
      for i in 9 8 7 6 5 4 3 2 1; do echo $i; sleep 2; done
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  32s   job-controller  Created pod: job-single--1-jdrhn
  Normal  Completed         12s   job-controller  Job completed
[00:11:10 root@master1 controller]#kubectl get job job-single  -o wide
NAME         COMPLETIONS   DURATION   AGE   CONTAINERS   IMAGES                                    SELECTOR
job-single   1/1           20s        44s   job-single   10.0.0.55:80/mykubernetes/pod_test:v0.2   controller-uid=051a3c20-2031-4ae8-ac79-71a76188062c
[00:11:22 root@master1 controller]#kubectl get pod
NAME                  READY   STATUS      RESTARTS   AGE
job-single--1-jdrhn   0/1     Completed   0          65s
===============================================

[00:13:45 root@master1 controller]#cat 14-controller-job-multi-chuan.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: job-multi-chuan
spec:
  completions: 5
  parallelism: 1
  template:
    spec:
      containers:
      - name: job-multi
        image: 10.0.0.55:80/mykubernetes/pod_test:v0.2 
        command: ["/bin/sh","-c","echo job; sleep 3"]
      restartPolicy: OnFailure
[00:13:49 root@master1 controller]#kubectl apply -f 14-controller-job-multi-chuan.yaml 
job.batch/job-multi-chuan created
[00:14:00 root@master1 controller]#kubectl get job job-multi-chuan -o wide
NAME              COMPLETIONS   DURATION   AGE   CONTAINERS   IMAGES                                    SELECTOR
job-multi-chuan   2/5           12s        12s   job-multi    10.0.0.55:80/mykubernetes/pod_test:v0.2   controller-uid=87237cab-d4a0-430d-8d2e-4131ccdd3071

[00:14:21 root@master1 controller]#kubectl describe jobs.batch job-multi-chuan 
Name:             job-multi-chuan
Namespace:        default
Selector:         controller-uid=87237cab-d4a0-430d-8d2e-4131ccdd3071
Labels:           controller-uid=87237cab-d4a0-430d-8d2e-4131ccdd3071
                  job-name=job-multi-chuan
Annotations:      <none>
Parallelism:      1
Completions:      5
Completion Mode:  NonIndexed
Start Time:       Sat, 11 Dec 2021 00:14:00 +0800
Completed At:     Sat, 11 Dec 2021 00:14:24 +0800
Duration:         24s
Pods Statuses:    0 Running / 5 Succeeded / 0 Failed
Pod Template:
  Labels:  controller-uid=87237cab-d4a0-430d-8d2e-4131ccdd3071
           job-name=job-multi-chuan
  Containers:
   job-multi:
    Image:      10.0.0.55:80/mykubernetes/pod_test:v0.2
    Port:       <none>
    Host Port:  <none>
    Command:
      /bin/sh
      -c
      echo job; sleep 3
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  38s   job-controller  Created pod: job-multi-chuan--1-dx492
  Normal  SuccessfulCreate  33s   job-controller  Created pod: job-multi-chuan--1-52pks
  Normal  SuccessfulCreate  28s   job-controller  Created pod: job-multi-chuan--1-q454c
  Normal  SuccessfulCreate  23s   job-controller  Created pod: job-multi-chuan--1-hn5fd
  Normal  SuccessfulCreate  19s   job-controller  Created pod: job-multi-chuan--1-cx8bf
  Normal  Completed         14s   job-controller  Job completed
[00:14:38 root@master1 controller]#kubectl get pod
NAME                       READY   STATUS      RESTARTS   AGE
job-multi-chuan--1-52pks   0/1     Completed   0          76s
job-multi-chuan--1-cx8bf   0/1     Completed   0          62s
job-multi-chuan--1-dx492   0/1     Completed   0          81s
job-multi-chuan--1-hn5fd   0/1     Completed   0          66s
job-multi-chuan--1-q454c   0/1     Completed   0          71s

===

[00:15:34 root@master1 controller]#cat 15-controller-job-multi-bing.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  name: job-multi-bing
spec:
  completions: 6
  parallelism: 2
  template:
    spec:
      containers:
      - name: job-multi-bing
        image: 10.0.0.55:80/mykubernetes/pod_test:v0.2 
        command: ["/bin/sh","-c","echo job; sleep 3"]
      restartPolicy: OnFailure
[00:15:37 root@master1 controller]#kubectl apply -f 15-controller-job-multi-bing.yaml 
job.batch/job-multi-bing created
[00:15:46 root@master1 controller]#kubectl get job job-multi-bing 
NAME             COMPLETIONS   DURATION   AGE
job-multi-bing   2/6           6s         6s
[00:15:52 root@master1 controller]#kubectl get job job-multi-bing -o wide
NAME             COMPLETIONS   DURATION   AGE   CONTAINERS       IMAGES                                    SELECTOR
job-multi-bing   4/6           10s        10s   job-multi-bing   10.0.0.55:80/mykubernetes/pod_test:v0.2   controller-uid=39afa349-4bc0-4b0f-a70a-4f02b0720548
[00:15:56 root@master1 controller]#kubectl describe jobs.batch job-multi-bing 
Name:             job-multi-bing
Namespace:        default
Selector:         controller-uid=39afa349-4bc0-4b0f-a70a-4f02b0720548
Labels:           controller-uid=39afa349-4bc0-4b0f-a70a-4f02b0720548
                  job-name=job-multi-bing
Annotations:      <none>
Parallelism:      2
Completions:      6
Completion Mode:  NonIndexed
Start Time:       Sat, 11 Dec 2021 00:15:46 +0800
Completed At:     Sat, 11 Dec 2021 00:16:01 +0800
Duration:         15s
Pods Statuses:    0 Running / 6 Succeeded / 0 Failed
Pod Template:
  Labels:  controller-uid=39afa349-4bc0-4b0f-a70a-4f02b0720548
           job-name=job-multi-bing
  Containers:
   job-multi-bing:
    Image:      10.0.0.55:80/mykubernetes/pod_test:v0.2
    Port:       <none>
    Host Port:  <none>
    Command:
      /bin/sh
      -c
      echo job; sleep 3
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  19s   job-controller  Created pod: job-multi-bing--1-djfw4
  Normal  SuccessfulCreate  19s   job-controller  Created pod: job-multi-bing--1-lslbr
  Normal  SuccessfulCreate  14s   job-controller  Created pod: job-multi-bing--1-wsgkr
  Normal  SuccessfulCreate  14s   job-controller  Created pod: job-multi-bing--1-n7sqd
  Normal  SuccessfulCreate  9s    job-controller  Created pod: job-multi-bing--1-j5qxw
  Normal  SuccessfulCreate  9s    job-controller  Created pod: job-multi-bing--1-55j2r
  Normal  Completed         4s    job-controller  Job completed
[00:16:05 root@master1 controller]#kubectl get pod
NAME                      READY   STATUS      RESTARTS   AGE
job-multi-bing--1-55j2r   0/1     Completed   0          18s
job-multi-bing--1-djfw4   0/1     Completed   0          28s
job-multi-bing--1-j5qxw   0/1     Completed   0          18s
job-multi-bing--1-lslbr   0/1     Completed   0          28s
job-multi-bing--1-n7sqd   0/1     Completed   0          23s
job-multi-bing--1-wsgkr   0/1     Completed   0          23s


========================================
CrodJob
========================================
[00:16:57 root@master1 controller]#cat 16-controller-cronjob-simple.yaml 
apiVersion: batch/v1
kind: CronJob
metadata:
  name: cronjob
spec:
  schedule: "*/2 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: cronjob
            image: 10.0.0.55:80/mykubernetes/pod_test:v0.2 
            command: ["/bin/sh","-c","echo job"]
[00:17:01 root@master1 controller]#kubectl apply -f 16-controller-cronjob-simple.yaml 
cronjob.batch/cronjob created

[00:19:22 root@master1 controller]#kubectl get pod
NAME                        READY   STATUS      RESTARTS   AGE
cronjob-27319218--1-2c877   0/1     Completed   0          2m29s
cronjob-27319220--1-5jk6q   0/1     Completed   0          29s

===

[00:21:02 root@master1 controller]#cat 17-controller-cronjob-second.yaml 
apiVersion: batch/v1
kind: CronJob
metadata:
  name: cronjob-second
spec:
  schedule: "* * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: cronjob
            image: 10.0.0.55:80/mykubernetes/pod_test:v0.2
            command: ["/bin/sh","-c","i=0; until [ $i -eq 60 ]; do sleep 10; let i=i+10; echo $i job; done"]
[00:21:03 root@master1 controller]#kubectl apply -f 17-controller-cronjob-second.yaml 
cronjob.batch/cronjob-second created

[00:22:59 root@master1 controller]#kubectl get jobs.batch 
NAME                      COMPLETIONS   DURATION   AGE
cronjob-second-27319222   0/1           60s        60s
cronjob-second-27319223   0/1           0s         0s

  

这篇关于Linux - K8S - Job & CronJob的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!