0

I have a use case that my "./main" binary should run inside the pod and stop after some time (90 seconds) before launching a new pod by the cronJob object.

But I am not confused about how to add both sleep and run my binary in the background together. Please suggest a good approach to this and excuse me for any wrong syntax.

Dockerfile

FROM golang:alpine
WORKDIR /app
COPY main /app
RUN  apk update && apk add bash
CMD  ["./main &"]

---
cronjob.yaml

apiVersion: batch/v1
kind: CronJob
metadata:
  name: cron
  namespace: test-cron
spec:
  schedule: "*/2 * * * *"
  concurrencyPolicy: Replace
  successfulJobsHistoryLimit: 0
  failedJobsHistoryLimit: 0
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          volumes:
          - name: log
            hostPath:
                path: /data/log/test-cron/
          containers:
            - name: test-cron
              image:  test-kafka-0.5
              command: ["sleep", "90"] // By adding this, the sleep command is working but my binary is not running inside my container.
Deepak Mourya
  • 340
  • 2
  • 10
  • how about you append the two, like this: command: ["./main", "&&", "sleep", "90"] – faizan Dec 07 '22 at 14:39
  • Yes tried CMD `./main && sleep 90` but pod does not sleep for 90sec. – Deepak Mourya Dec 07 '22 at 15:23
  • Do you want `main` to be killed after 90 seconds, or what should happen then? A Kubernetes CronJob only schedules things in whole minutes; is the cron scheduling related to the 90-second timeout, or just the way you're launching the container? – David Maze Dec 08 '22 at 11:51
  • @DavidMaze, yes I just want to run my main binary and after that pod should be killed in 90sec. – Deepak Mourya Dec 08 '22 at 18:05

1 Answers1

1

Kubernetes has built-in support for killing a Pod after a deadline; you do not need to try to implement this manually.

In your Dockerfile, set up your image to run the program normally. Do not try to include any sort of "sleep" or "kill" option, and do not try to run the program in the background.

CMD ["./main"]

In the Job spec, you need to set an activeDeadlineSeconds: field.

apiVersion: batch/v1
kind: CronJob
spec:
  jobTemplate:
    spec:
      activeDeadlineSeconds: 90  # <-- add
      template:
        spec:
          containers:
            - name: test-cron
              image:  test-kafka-0.5
              # do not override `command:`

An identical option exists at the Pod level. For your use case this doesn't especially matter, but if the Job launches multiple Pods then there's a difference of whether each individual Pod has the deadline or whether the Job as a whole does.

It looks from the question like you're trying to run this job every two minutes and not have two concurrent copies of it. Assuming the Pod is created and starts promptly, this should accomplish that. If you had a reason to believe the Job would run faster the second time and just want to restart it, the CronJob might not be the setup you're after.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thanks, it worked like what I wanted, I was chasing for the sleep command but activeDeadlineSeconds is the real hero here. :) – Deepak Mourya Dec 09 '22 at 13:05