sunnybhambhani
sunnybhambhani

Reputation: 61

Handling cronjobs in a Pod with multiple containers

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).

Is it possible?

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.

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.

Any help is really appreciated.

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

Thank you, Sunny

Upvotes: 6

Views: 8649

Answers (3)

Hamid Zorgani
Hamid Zorgani

Reputation: 31

Whats about sidecar container for logging as second container which keep running without exit code. Even the job might run the state of the job still failed.

Upvotes: 2

Dawid Kruk
Dawid Kruk

Reputation: 9877

I need to agree with the answer provided by @Arghya Sadhu. It shows how you can run multi container Pod with a CronJob. Before the answer I would like to give more attention to the comment provided by @Chris Stryczynski:

It's not clear whether the containers are run in parallel or sequentially


It is not entirely clear if the workload that you are trying to run:

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.

could be used in parallel (both running at the same time) or require sequential approach (after X completed successfully, run Y).

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 initContainers instead of multi container Pods.

The example of a CronJob that implements the initContainer could be following:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: ubuntu
              image: ubuntu
              command: [/bin/bash]
              args: ["-c","cat /data/hello_there.txt"]
              volumeMounts:
              - name: data-dir
                mountPath: /data
          initContainers:
          - name: echo
            image: busybox
            command: ["bin/sh"]
            args: ["-c", "echo 'General Kenobi!' > /data/hello_there.txt"]
            volumeMounts:
            - name: data-dir
              mountPath: "/data"
          volumes:
          - name: data-dir
            emptyDir: {}

This CronJob will write a specific text to a file with an initContainer and then a "main" container will display its result. It's worth to mention that the main container will not start if the initContainer won't succeed with its operations.

  • $ kubectl logs hello-1234567890-abcde
General Kenobi!

Additional resources:

Upvotes: 5

Arghya Sadhu
Arghya Sadhu

Reputation: 44559

Yes you can create a cronjob with multiple containers. CronJob is an abstraction on top of pod. So in the pod spec you can have multiple containers just like you can have in a normal pod. As an example

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
  namespace: default
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          - name: app
            image: alpine
            command:
            - echo
            - Hello World!
          restartPolicy: OnFailure

Upvotes: 4

Related Questions