Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run containers sequentially as a Kubernetes job?

I'm trying to replace my legacy job scheduler with Kubernetes job and wondering how to write sequential jobs as a Kubernetes job.

First, I wrote the following script to execute job1 and job2 in the written order but it didn't work as I expected.

apiVersion: batch/v1 kind: Job metadata:   name: sequential spec:   activeDeadlineSeconds: 100   template:     metadata:       name: sequential_jobs     spec:       containers:       - name: job1         image: image1       - name: job2         image: image2       restartPolicy: Never 

The job described above seems to run job1 and job2 in parallel. Is there any good way to run job1 and job2 in the written order?

Appended.

I recently found https://github.com/argoproj/argo very good for my usecase.

like image 341
k-kawa Avatar asked Nov 21 '16 05:11

k-kawa


People also ask

Can Kubernetes job have multiple pods?

Kubernetes Job Controller creates a pod based on the single pod template in the Job spec. So No you can't have multiple pods in a Job.

Do init containers run in parallel?

Because init containers run to completion before any app containers start, init containers offer a mechanism to block or delay app container startup until a set of preconditions are met. Once preconditions are met, all of the app containers in a Pod can start in parallel.

Can Kubernetes manage batch workloads?

To execute and manage a batch task on your cluster, you can use a Kubernetes Job. You can specify the maximum number of Pods that should run in parallel as well as the number of Pods that should complete their tasks before the Job is finished. A Job can also be used to run multiple Pods at the same time.


1 Answers

After a few attempts, I did this and that solved the basic problem (similar to what the OP has posted). This configuration ensures that job-1 completes before job-2 begins. If job-1 fails, job-2 container is not started. I still need to work on the retries and failure handling, but the basics works. Hopefully, this will help others:

apiVersion: v1 kind: Pod metadata:   name: sequential-job spec:   initContainers:   - name: job-1     image: busybox     # runs for 15 seconds; echoes job name and timestamp     command: ['sh', '-c', 'for i in 1 2 3; do echo "job-1 `date`" && sleep 5s; done;']   - name: job-2     image: busybox     # runs for 15 seconds; echoes job name and timestamp     command: ['sh', '-c', 'for i in 1 2 3; do echo "job-2 `date`" && sleep 5s; done;']   # I don't really need the 'containers', but syntax requires    # it so, I'm using it as a place where I can report the    # completion status   containers:   - name: job-done     image: busybox     command: ['sh', '-c', 'echo "job-1 and job-2 completed"']   restartPolicy: Never 

Update

The same configuration as above also works inside a Job spec:

apiVersion: batch/v1 kind: Job metadata:   name: sequential-jobs spec:   template:     metadata:       name: sequential-job     spec:       initContainers:       - name: job-1         image: busybox         command: ['sh', '-c', 'for i in 1 2 3; do echo "job-1 `date`" && sleep 5s; done;']       - name: job-2         image: busybox         command: ['sh', '-c', 'for i in 1 2 3; do echo "job-2 `date`" && sleep 5s; done;']       containers:       - name: job-done         image: busybox         command: ['sh', '-c', 'echo "job-1 and job-2 completed"']       restartPolicy: Never 
like image 142
Bloodysock Avatar answered Sep 22 '22 12:09

Bloodysock