如何确保 kubernetes cronjob 不会在失败时重新启动 [英] How to ensure kubernetes cronjob does not restart on failure
问题描述
我有一个向客户发送电子邮件的定时任务.它偶尔会因各种原因失败.我不希望它重新启动,但它仍然可以.
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.
我在 GKE 上运行 Kubernetes.为了让它停止,我必须删除 CronJob,然后手动杀死它创建的所有 pod.
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
推荐答案
事实证明,你必须在 restartPolicy: Never
中设置一个 backoffLimit: 0
组合结合 concurrencyPolicy: Forbid
.
It turns out that you have to set a backoffLimit: 0
in combination with restartPolicy: Never
in combination with concurrencyPolicy: Forbid
.
backoffLimit 表示在被视为失败之前将重试的次数.默认为 6.
backoffLimit means the number of times it will retry before it is considered failed. The default is 6.
concurrencyPolicy 设置为 Forbid
意味着它将运行 0 或 1 次,但不会更多.
concurrencyPolicy set to Forbid
means it will run 0 or 1 times, but not more.
restartPolicy 设置为 Never
意味着它不会在失败时重新启动.
restartPolicy set to Never
means it won't restart on failure.
您需要完成所有这 3 件事,否则您的 cronjob 可能会运行多次.
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 ...
这篇关于如何确保 kubernetes cronjob 不会在失败时重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!