如何作为Kubernetes作业顺序运行容器?
我正在尝试用Kubernetes作业替换旧的作业调度程序,并想知道如何将连续作业作为Kubernetes作业编写.
I'm trying to replace my legacy job scheduler with Kubernetes job and wondering how to write sequential jobs as a Kubernetes job.
首先,我编写了以下脚本以按书面顺序执行job1
和job2
,但是它没有按我预期的那样工作.
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
上述作业似乎可以并行运行job1
和job2
.
有什么好的方法可以按书面顺序运行job1
和job2
?
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?
已添加.
我最近发现 https://github.com/argoproj/argo 非常适合我的用例.
I recently found https://github.com/argoproj/argo very good for my usecase.
经过几次尝试,我做到了,并且解决了基本问题(类似于OP发布的内容).此配置确保job-1
在job-2
开始之前完成.如果job-1
失败,则不会启动job-2
容器.我仍然需要进行重试和失败处理,但是基础工作正常.希望这会帮助其他人:
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
更新
与上述相同的配置也可以在Job规范中使用:
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