Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure kubernetes cronjob does not restart on failure

Tags:

kubernetes

I have a cronjob that sends out emails to customers. It occasionally fails for various reasons. I do not want it to restart, but it still does.

I am running Kubernetes on GKE. To get it to stop, I have to delete the CronJob and then kill all the pods it creates manually.

This is bad, for obvious reasons.

apiVersion: batch/v1beta1 kind: CronJob metadata:   creationTimestamp: 2018-06-21T14:48:46Z   name: dailytasks   namespace: default   resourceVersion: "20390223"   selfLink: [redacted]   uid: [redacted] spec:   concurrencyPolicy: Forbid   failedJobsHistoryLimit: 1   jobTemplate:     metadata:       creationTimestamp: null     spec:       template:         metadata:           creationTimestamp: null         spec:           containers:           - command:             - kubernetes/daily_tasks.sh             env:             - name: DB_HOST               valueFrom:                 fieldRef:                   apiVersion: v1                   fieldPath: status.hostIP             envFrom:             - secretRef:                 name: my-secrets             image: [redacted]             imagePullPolicy: IfNotPresent             name: dailytasks             resources: {}             terminationMessagePath: /dev/termination-log             terminationMessagePolicy: File           dnsPolicy: ClusterFirst           restartPolicy: Never           schedulerName: default-scheduler           securityContext: {}           terminationGracePeriodSeconds: 30   schedule: 0 14 * * *   successfulJobsHistoryLimit: 3   suspend: true status:   active:   - apiVersion: batch     kind: Job     name: dailytasks-1533218400     namespace: default     resourceVersion: "20383182"     uid: [redacted]   lastScheduleTime: 2018-08-02T14:00:00Z 
like image 973
Doug Avatar asked Aug 02 '18 15:08

Doug


People also ask

What is restart Never in Kubernetes?

restartPolicy only refers to restarts of the containers by the kubelet on the same node. After containers in a Pod exit, the kubelet restarts them with an exponential back-off delay (10s, 20s, 40s, …), that is capped at five minutes.

What is backoff limit in Kubernetes?

backoffLimit to specify the number of retries before considering a Job as failed. The back-off limit is set by default to 6. Failed Pods associated with the Job are recreated by the Job controller with an exponential back-off delay (10s, 20s, 40s ...) capped at six minutes.

How do I stop Kubernetes CronJob?

Edit your current cronjob resource to include the . spec. suspend field and set it to true. Any currently running jobs will complete but future jobs will be suspended.


1 Answers

It turns out that you have to set a backoffLimit: 0 in combination with restartPolicy: Never in combination with concurrencyPolicy: Forbid.

backoffLimit means the number of times it will retry before it is considered failed. The default is 6.

concurrencyPolicy set to Forbid means it will run 0 or 1 times, but not more.

restartPolicy set to Never means it won't restart on failure.

You need to do all 3 of these things, or your cronjob may run more than once.

spec:   concurrencyPolicy: Forbid   failedJobsHistoryLimit: 1   jobTemplate:     metadata:       creationTimestamp: null     spec:       [ADD THIS -->]backoffLimit: 0       template:        ... MORE STUFF ... 
like image 71
Doug Avatar answered Sep 24 '22 01:09

Doug