Finished pods created by Kubernetes CronJob getting deleted after sometime

11/15/2019

I created a CronJob by using below yaml file.

kind: CronJob
metadata:
  name: $DEPLOY_NAME
spec:
  # Run the job once a day at 8 PM
  schedule: "0 20 * * *"
  # If the previous job is not yet complete during the scheduled time, do not start the next job
  concurrencyPolicy: Forbid
  jobTemplate:
      spec:
        # The pods will be available for 3 days (259200 seconds) so that logs can be checked in case of any failures
        ttlSecondsAfterFinished: 259200
        template:
          spec:
            containers:
            - name: $DEPLOY_NAME
              image: giantswarm/tiny-tools
              imagePullPolicy: IfNotPresent
              resources:
                requests:
                  cpu: "0.01"
                  memory: 256Mi
                limits:
                  cpu: "0.5"
                  memory: 512Mi
              command: ["/bin/sh"]
              args: ["-c", "cd /home/tapi && sh entrypoint.sh"]

As mentioned in ttlSecondsAfterFinished, k8s keeps my job in the cluster. However, the pod created by the job (after completion) gets deleted after sometime.

As per garbage collection policy, my pod object should be dependent upon my job. And since job object is not garbage collected, my pod object should also remain alive. Am i missing something?

-- Manish Bansal
kubernetes
kubernetes-jobs

1 Answer

11/15/2019

The .spec.successfulJobsHistoryLimit and .spec.failedJobsHistoryLimit fields are optional. These fields specify how many completed and failed jobs should be kept. By default, they are set to 3 and 1 respectively.

You might need to set these fields to appropriate value

-- P Ekambaram
Source: StackOverflow