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

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

<pre><code>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
</code></pre>

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

<p>Appended.</p>

<p>I recently found <a href="https://github.com/argoproj/argo" rel="noreferrer">https://github.com/argoproj/argo</a> very good for my usecase.</p>

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

<pre><code>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`" &amp;&amp; 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`" &amp;&amp; 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
</code></pre>

<p><strong>Update</strong></p>

<p>The same configuration as above also works inside a Job spec:</p>

<pre><code>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`" &amp;&amp; sleep 5s; done;']
      - name: job-2
        image: busybox
        command: ['sh', '-c', 'for i in 1 2 3; do echo "job-2 `date`" &amp;&amp; sleep 5s; done;']
      containers:
      - name: job-done
        image: busybox
        command: ['sh', '-c', 'echo "job-1 and job-2 completed"']
      restartPolicy: Never
</code></pre>
