<p>I have the following Kubernetes job defined in <code>job.yaml</code></p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: pi-$count
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl:5.34.0
        command: [&quot;perl&quot;,  &quot;-Mbignum=bpi&quot;, &quot;-wle&quot;, &quot;print bpi(2000)&quot;]
      restartPolicy: Never
  backoffLimit: 4
</code></pre>
<p>I apply this with the following bash script:</p>
<pre><code>for i in {1..3}
do
    kubectl apply -f job.yaml
done
</code></pre>
<p>Is there a way to create 3 jobs here and use an environment variable <code>pi-$count</code> so <code>pi-1</code>, <code>pi-2</code>, <code>pi-3</code> are created?</p>

jipot
<p>You can use <code>sed</code> to replace the <code>$count</code> and create a manifest file for each job and apply it.</p>
<p>For example create a file called <code>pi-job.yaml</code> (I'm taking your code as example)</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: pi-$count
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl:5.34.0
        command: [&quot;perl&quot;,  &quot;-Mbignum=bpi&quot;, &quot;-wle&quot;, &quot;print bpi(2000)&quot;]
      restartPolicy: Never
  backoffLimit: 4
</code></pre>
<p>Create a folder called <strong>job</strong> to store the manifest files (this directory is completely optional)You can use something like this</p>
<pre><code>    mkdir job
    for i in {1..3}
    do
       cat pi-job.yaml | sed &quot;s/\$count/$i/&quot; &gt; ./jobs/job-$i.yaml;
       kubectl apply -f ./jobs/job-$i.yaml
    done
</code></pre>
<p>By executing this code 3 jobs will be created in the name of <strong>pi-1</strong>,<strong>pi-2</strong> and <strong>pi-3</strong>. But the manifest files will reside in the job folder. You need to clean it up. Otherwise you can add one more line to the script to remove the files like this</p>
<pre><code> mkdir job
 for i in {1..3}
 do
   cat pi-job.yaml | sed &quot;s/\$count/$i/&quot; &gt; ./jobs/job-$i.yaml;
   kubectl apply -f ./jobs/job-$i.yaml
   rm -rf ./jobs/job-$i.yaml
 done
</code></pre>
<p>For more detailed information refer to the <a href="https://kubernetes.io/docs/tasks/job/parallel-processing-expansion/#create-jobs-based-on-a-template" rel="nofollow noreferrer">official k8's job document</a></p>
