Kubernetes Jobs and Cronjobs

 





CronJob’ , . CronJob’ Job’ (, - , ) , , Job , , , . Job’ Kubernetes (workloads).





Kubernetes : ReplicaSets, DaemonSets, Deployments StatefulSets. . , . (node), , .





, , ? , , . (backup, ) - , . . , , (exit status), , .





Job’ Kubernetes

Job’ Kubernetes:





  1. : // . .





  2. / : , /, .





Job’ Kubernetes , . , Job . Job , .





Kubernetes Job

Job’ Kubernetes, Kubernetes, (definition file). , job.yaml. :





apiVersion: batch/v1
kind: Job
metadata:
name: hello-world
spec:
template:
  metadata:
    name: hello-world
  spec:
    containers:
    - name: hello-world
      image: busybox
      command: ["echo", "Running a hello-world job"]
    restartPolicy: OnFailure
      
      



Kubernetes, Kubernetes kubectl. :





$ kubectl apply -f job.yaml
job.batch/hello-world created
      
      



, :





$ kubectl get pods
NAME              	READY    STATUS          RESTARTS      AGE
hello-world-mstmc        0/1 	ContainerCreating   0          1s
      
      



job’ kubectl:





kubectl get jobs hello-world-mstmc
NAME            COMPLETIONS     DURATION     AGE
hello-world-mstmc   1/1           21s        27s
      
      



:





$ kubectl get pods
NAME              	READY   STATUS  	RESTARTS       AGE
hello-world-mstmc       0/1 	Completed          0           25s
      
      



, . Completed, job . Job, , :) - echo “Running a hello-world job” .





, , job , :





kubectl logs -f hello-world-mstmc
Running a hello-world job
      
      



  , , .. “Running a hello-world job”. Job .





CronJob  

, cronjob . , job cronjob’.





, , cronjob , .





kubectl create job --from=cronjob/kubernetes-cron-job manual-cron-job
      
      



--from=cronjob/kubernetes-cron-job



cronjob’ job manual-cron-job





Job’ Kubernetes (Cleanup)

Job Kubernetes , Job, . . , - , Job .





Job kubectl :





kubectl delete jobs job_name







Job  . Kubernetes, Job, , cascade=false



. :





kubectl delete jobs job_name cascade=false







, job’/cronjob’ kubernetes . .





1. failedJobHistoryLimit successfulJobsHistoryLimit:   job’ . , , job’. ,





failedJobHistoryLimit: 5
successfulJobsHistoryLimit: 10
      
      



2. backoffLimit: .





RestartPolicy Job’ Kubernetes

restartPolic



y ( ) “always” (). Job . , restartPolicy



“Never” () “OnFailure” ( ).





Job’ Kubernetes

Job’ , , , Job’ Kubernetes spec.activeDeadlineSeconds



. Job’ .





, .spec.backoffLimit



, .. Job  , . .





Job backoff limit



, deadline



:





apiVersion: batch/v1
kind: Job
metadata:
 name: data-consumer
spec:
 backoffLimit: 5
 activeDeadlineSeconds: 20
 template:
spec:
    containers:
    - name: consumer
        image: busybox
        command: ["/bin/sh", "-c"]
        args: ["echo 'Consuming data'; sleep 1; exit 1"]
    restartPolicy: OnFailure
      
      



Job’

, , Job, “run-once”. .





Job’

, , . job’, , . Job’ Kubernetes, .spec.completions - ( , ). Job (completions). Job , . . , :





apiVersion: batch/v1
kind: Job
metadata:
name: data-consumer
spec:
completions: 5
template:
 metadata:
   name: data-consumer
 spec:
   containers:
   - name: data-consumer
     image: busybox
     command: ["/bin/sh","-c"]
     args: ["echo 'consuming a message from queue'; sleep 5"]
   restartPolicy: OnFailure
      
      



  • completions 5.





Job’ (Work Queue)

job’, , , . , data science .





, :





apiVersion: batch/v1
kind: Job
metadata:
name: data-consumer
spec:
parallelism: 5
template:
 metadata:
   name: data-consumer
 spec:
   containers:
   - name: data-consumer
     image: busybox
     command: ["/bin/sh","-c"]
     args: ["echo 'consuming a message from queue'; sleep $(shuf -i 5-10 -n 1)"]
   restartPolicy: OnFailure
      
      



.spec.completions



. parallelism



. completions



parallelism



(5). Job : 





5 , Job. , , Job . , Job .





Kubernetes Job 5 . , , . , (, AWS SQS). , Job , . :





  • .





  • .





, , ( 5 10), . , , , API.





CronJob’ 

Cronjob job (schedule). «», job’ . , . , job’ .





startingDeadlineSeconds



( ) concurrencyPolicy



Allow, job .





CronJob’ CronJob , . 100, job .





Cannot determine if job needs to be started. Too many missed start time (> 100). Set or decrease .spec.startingDeadlineSeconds or check clock skew.
      
      



, startingDeadlineSeconds



(.. nil



), job’ startingDeadlineSeconds



, . ,   startDeadlineSeconds



200



, , job’ 200 .





CronJob , . , concurrencyPolicy



Forbid



CronJob , .





, , CronJob Job 08:30:00



, startingDeadlineSeconds



. CronJob 08:29:00



10:21:00



, job , job’ 100.





, , CronJob Job 08:30:00



startingDeadlineSeconds



200 . CronJob , ( 08:29:00



10:21:00



), Job 10:22:00. , , 200 (. . 3 ), , .





CronJob Job’, , Job, , .





TL;DR (Too Long; Didn’t Read)

  • Job’ Kubernetes , , , .





  • Job’ Kubernetes ; Job .





  • restartPolicy



    Job «Never» «OnFailure»





  • Job’ completions parallelism , . Job’ , , .





  • Job’ , .spec.backoffLimit



    . .





  • job’, .spec.activeDeadlineSeconds



    . backoffLimit



    . , Job , .





  • Job’ . ttlSecondsAfterFinished



    , alpha.






" Kubernetes". - : " NoSQL k8s ( Apache Cassandra)"








All Articles