<p>I have a requirement in which I need to create a cronjob in kubernetes but the pod is having multiple containers (with single container its working fine).</p>

<p>Is it possible? </p>

<p>The requirement is something like this:
1. First container: Run the shell script to do a job.
2. Second container: run fluentbit conf to parse the log and send it.</p>

<p>Previously I thought to have a deployment in place and that is working fine but since that deployment was used just for 10 mins jobs I thought to make it a cron job.</p>

<p>Any help is really appreciated.</p>

<p>Also about the cronjob I am not sure if a pod can support multiple containers to do that same.</p>

<p>Thank you,
Sunny</p>

<p>I need to agree with the answer provided by @Arghya Sadhu. It shows how you can run multi container <code>Pod</code> with a <code>CronJob</code>. Before the answer I would like to give more attention to the comment provided by @Chris Stryczynski:</p>
<blockquote>
<p>It's not clear whether the containers are run in parallel or sequentially</p>
</blockquote>
<hr />
<p>It is not entirely clear if the workload that you are trying to run:</p>
<blockquote>
<p>The requirement is something like this:</p>
<ol>
<li>First container: Run the shell script to do a job.</li>
<li>Second container: run fluentbit conf to parse the log and send it.</li>
</ol>
</blockquote>
<p>could be used in <code>parallel</code> (both running at the same time) or require <code>sequential</code> approach (after X completed successfully, run Y).</p>
<p>If the workload could be run in parallel the answer provided by @Arghya Sadhu is correct, however if one workload is depending on another, I'd reckon you should be using <code>initContainers</code> instead of multi container <code>Pods</code>.</p>
<p>The example of a <code>CronJob</code> that implements the <code>initContainer</code> could be following:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: &quot;*/1 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: ubuntu
              image: ubuntu
              command: [/bin/bash]
              args: [&quot;-c&quot;,&quot;cat /data/hello_there.txt&quot;]
              volumeMounts:
              - name: data-dir
                mountPath: /data
          initContainers:
          - name: echo
            image: busybox
            command: [&quot;bin/sh&quot;]
            args: [&quot;-c&quot;, &quot;echo 'General Kenobi!' &gt; /data/hello_there.txt&quot;]
            volumeMounts:
            - name: data-dir
              mountPath: &quot;/data&quot;
          volumes:
          - name: data-dir
            emptyDir: {}
</code></pre>
<p>This <code>CronJob</code> will write a specific text to a file with an <code>initContainer</code> and then a &quot;main&quot; container will display its result. It's worth to mention that the main container will not start if the <code>initContainer</code> won't succeed with its operations.</p>
<ul>
<li><code>$ kubectl logs hello-1234567890-abcde</code></li>
</ul>
<pre class="lang-sh prettyprint-override"><code>General Kenobi!
</code></pre>
<hr />
<p>Additional resources:</p>
<ul>
<li><em><a href="https://linchpiner.github.io/k8s-multi-container-pods.html" rel="noreferrer">Linchpiner.github.io: K8S multi container pods</a></em></li>
</ul>
