如何将容器作为 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.

首先,我编写了以下脚本以按照书面顺序执行 job1job2 但它没有按我预期的那样工作.

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

上述作业似乎并行运行 job1job2.job1job2 有没有什么好的方法可以按照书面顺序运行?

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-1job-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

更新

与上述相同的配置也适用于作业规范:

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