经过几次尝试,我做到了这一点并且解决了基本问题(类似于OP发布的内容)。此配置确保在开始job-1
之前完成job-2
。如果job-1
失败,job-2
容器不会启动。我仍然需要处理重试和故障处理,但基础工作。希望这会帮助其他人:
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 规范:
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