<p>I have a CRON JOB in Azure k8s which trigger once a day, based on the condition written inside of this CRON JOB (image), I need to start another application/s(Pods) which will do some process and dies</p>

Ashley Alex
<p>I'll describe a solution that requires running <code>kubectl</code> commands from within the <code>CronJob</code> Pod. If the image you're using to create the <code>CronJob</code> doesn't have the <code>kubectl</code> command, you'll need to install it.<br />
In short, you can use a Bash script that creates Deployment (or Pod) and then you can mount that script in a volume to the <code>CronJob</code> Pod.</p>
<p>Below, I will provide a detailed step-by-step explanation.</p>
<hr />
<p>This is a really simple bash script that we'll mount to the <code>CronJob</code> Pod:<br />
<strong>NOTE:</strong> You may need to modify this script as needed. If the image you're using to create the <code>CronJob</code> doesn't have the <code>kubectl</code> command installed, you can install it in this script.</p>
<pre><code>$ cat deploy-script.sh
#!/bin/bash

echo &quot;Creaing 'web-app'&quot;
kubectl create deployment web-app --image=nginx
sleep 10
echo &quot;Deleting 'web-app'&quot;
kubectl delete deployment web-app
</code></pre>
<p>We want to run this script from inside the Pod, so I converted it to <code>ConfigMap</code> which will allow us to mount this script in a volume (see: <a href="https://kubernetes.io/docs/concepts/configuration/configmap/#using-configmaps-as-files-from-a-pod" rel="nofollow noreferrer">Using ConfigMaps as files from a Pod</a>):</p>
<pre><code>$ cat deploy-script-configmap.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: deploy-script
data:
  deployScript.sh: |
    #!/bin/bash

    echo &quot;Creaing 'web-app'&quot;
    kubectl create deployment web-app --image=nginx
    sleep 10
    echo &quot;Deleting 'web-app'&quot;
    kubectl delete deployment web-app

$ kubectl apply -f deploy-script-configmap.yml
configmap/deploy-script created
</code></pre>
<p>Then I created a separate <code>cron-user</code> ServiceAccount with the <code>edit</code> Role assigned and our <code>CronJob</code> will run under this ServiceAccount:</p>
<pre><code>$ cat cron-job.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: cron-user
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cron-user-binding
subjects:
  - kind: ServiceAccount
    name: cron-user
    namespace: default
roleRef:
  kind: ClusterRole
  name: edit
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: deploy-cron-job
spec:
  schedule: &quot;*/2 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: cron-user
          volumes:
            - name: deploy-script
              configMap:
                name: deploy-script
          containers:
          - name: cron-job-1
            image: bitnami/kubectl
            command: [&quot;bash&quot;, &quot;/mnt/deployScript.sh&quot;]
            volumeMounts:
            - name: deploy-script
              mountPath: /mnt/
          
</code></pre>
<p>After applying the above manifest, the <code>deploy-cron-job</code> CronJob was created:</p>
<pre><code>$ kubectl apply -f cron-job.yaml
serviceaccount/cron-user created
clusterrolebinding.rbac.authorization.k8s.io/cron-user-binding created
cronjob.batch/deploy-cron-job created

$ kubectl get cronjob
NAME              SCHEDULE      SUSPEND   ACTIVE   
deploy-cron-job   */2 * * * *   False     1     
</code></pre>
<p>After a while, the <code>CronJob</code> Pod was created and as we can see from the logs, it created a <code>web-app</code> Deployment as expected:</p>
<pre><code>$ kubectl get pods
NAME                                       READY   STATUS        RESTARTS   AGE
deploy-cron-job-1626426840-6w6s9           0/1     Completed     0          21s

$ kubectl logs -f deploy-cron-job-1626426840-6w6s9
Creaing 'web-app'
deployment.apps/web-app created
Deleting 'web-app'
deployment.apps &quot;web-app&quot; deleted
</code></pre>
