kubernetes: Kubernetes Job failed status after a graceful termination

What happened?

Let’s take the following Kubernetes Cronjob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: sleep-cronjob
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      backoffLimit: 0 # Avoid two executions
      template:
        spec:
          containers:
          - name: sleep
            image: busybox
            command: ["sh", "-c", "sleep 120"] # 2 minutes
          restartPolicy: Never
          terminationGracePeriodSeconds: 300 # 5 minutes

It runs a job every 5 minutes, and the pod sleeps for 2 minutes.

If during the sleep I execute the kubectl delete pod <POD_NAME> for a graceful termination, it will work, it will wait for the specified terminationGracePeriodSeconds and in this case, it will allow the pod to finish its job successfully.

But, the Kubernetes Job will be marked as 1 Failed with the reason BackoffLimitExceeded.

I have tried the Job configuration activeDeadlineSeconds and I’m still getting the error.

Could this be a bug or am I missing some configuration?

What did you expect to happen?

The job is mark as completed.

How can we reproduce it (as minimally and precisely as possible)?

Using the following, run it, and delete the pod before it finish its operation and then check for associated job status.

apiVersion: batch/v1
kind: CronJob
metadata:
  name: sleep-cronjob
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      backoffLimit: 0 # Avoid two executions
      template:
        spec:
          containers:
          - name: sleep
            image: busybox
            command: ["sh", "-c", "sleep 120"] # 2 minutes
          restartPolicy: Never
          terminationGracePeriodSeconds: 300 # 5 minutes

Anything else we need to know?

No response

Kubernetes version

Client Version: v1.29.5
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.29.5-gke.1091000

Cloud provider

GCP with GKE v1.29.5-gke.1091000

OS version

No response

Install tools

No response

Container runtime (CRI) and version (if applicable)

No response

Related plugins (CNI, CSI, …) and versions (if applicable)

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

both scripts you authored in this ticket are buggy. they both exit with 143 when receiving sigterm, so it correctly shows a failed status.