<p>How to create kubernetes job configuration spec to run perl script ? once the script completes execution , job created pod should go to completion .</p>

Kishore Kumar
<p>As we can find in the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/" rel="nofollow noreferrer">Kubernetes Jobs documentation</a> - when a specified number of successful completions is reached, the Job is complete:</p>
<blockquote>
<p>A Job creates one or more Pods and will continue to retry execution of the Pods until a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions. When a specified number of successful completions is reached, the task (ie, Job) is complete.</p>
</blockquote>
<p>I guess you would like to use a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/#parallel-jobs" rel="nofollow noreferrer">non-parallel Job</a>:</p>
<blockquote>
<ul>
<li>normally, only one Pod is started, unless the Pod fails.</li>
<li>the Job is complete as soon as its Pod terminates successfully.</li>
</ul>
</blockquote>
<p>A container in a Pod may fail for a number of reasons, such as because the process in it (e.g a perl script) exited with a non-zero exit code. This will happen, for example, when the perl script fails:<br />
<strong>NOTE:</strong> I typed <code>prin</code> instead of <code>print</code>.</p>
<pre><code>$ cat test-perl.pl
#!/usr/bin/perl
use warnings;
use strict;

prin(&quot;Hello World\n&quot;);

$ ./test-perl.pl
Undefined subroutine &amp;main::prin called at ./test-perl.pl line 5.

$ echo $?
255   ### non-zero exit code
</code></pre>
<p>I've created an example to illustrate how you can create a Job that runs a perl script and completes successfully.</p>
<hr />
<p>First, I created a simple perl script:</p>
<pre><code>$ cat perl-script.pl
#!/usr/bin/perl
use warnings;
use strict;

my @i = (1..9);

for(@i){
print(&quot;$_: Hello, World!\n&quot;);

$ ./perl-script.pl
1: Hello, World!
2: Hello, World!
3: Hello, World!
4: Hello, World!
5: Hello, World!
6: Hello, World!
7: Hello, World!
8: Hello, World!
9: Hello, World!

$ echo $?
0
</code></pre>
<p>Then I created a Docker image with the above script and pushed it to my <a href="https://hub.docker.com/" rel="nofollow noreferrer">DockerHub</a> repository:<br />
<strong>NOTE:</strong> I used the <a href="https://hub.docker.com/_/perl" rel="nofollow noreferrer">perl</a> image.</p>
<pre><code>$ ls
Dockerfile  perl-script.pl

$ cat Dockerfile
FROM perl:5.20
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ &quot;perl&quot;, &quot;./perl-script.pl&quot; ]

$ docker build . --tag mattjcontainerregistry/perl-script
Sending build context to Docker daemon  3.072kB
...
Successfully tagged mattjcontainerregistry/perl-script:latest

$ docker push mattjcontainerregistry/perl-script:latest
The push refers to repository [docker.io/mattjcontainerregistry/perl-script]
...
latest: digest: sha256:2f8789af7f61cfb021337810963a9a19f133d78e9ad77159fbc1d425cfb1d7db size: 3237
</code></pre>
<p>Finally, I created a <code>perl-script</code> Job which runs the perl script I created earlier:</p>
<pre><code>$ cat perl-job.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: perl-script
spec:
  template:
    spec:
      containers:
      - name: perl-script
        image: mattjcontainerregistry/perl-script:latest
      restartPolicy: Never

$ kubectl apply -f perl-job.yml
job.batch/perl-script created

$ kubectl get pods
NAME                READY   STATUS      RESTARTS   AGE
perl-script-gspzz   0/1     Completed   0          10s

$ kubectl get job
NAME          COMPLETIONS   DURATION   AGE
perl-script   1/1           1s         18s

$ kubectl logs -f perl-script-gspzz
1: Hello, World!
2: Hello, World!
3: Hello, World!
4: Hello, World!
5: Hello, World!
6: Hello, World!
7: Hello, World!
8: Hello, World!
9: Hello, World!
</code></pre>
<p>As you can see, the <code>perl-script</code> Job is complete, so it works as expected.</p>
