如何设置Kubernetes作业的时间限制? [英] How to set a time limit for a Kubernetes job?
问题描述
我想启动Kubernetes工作,并给它一个固定的截止日期。如果在截止日期到来之前吊舱仍在运行,我希望该作业自动终止。
I'd like to launch a Kubernetes job and give it a fixed deadline to finish. If the pod is still running when the deadline comes, I'd like the job to automatically be killed.
是否存在类似的东西? (起初,我以为Job规范的 activeDeadlineSeconds
涵盖了此用例,但现在我看到 activeDeadlineSeconds
仅将限制重新尝试工作的时间;它不会主动杀死慢/失控的工作。)
Does something like this exist? (At first I thought that the Job spec's activeDeadlineSeconds
covered this use case, but now I see that activeDeadlineSeconds
only places a limit on when a job is re-tried; it doesn't actively kill a slow/runaway job.)
推荐答案
通过使用GNU timeout
实用程序在容器的入口点命令上设置超时。
You can self-impose timeouts on the container's entrypoint command by using GNU timeout
utility.
例如,以下计算作业pi的前4000个数字将在10秒后超时:
For example the following Job that computes first 4000 digits of pi will time out after 10 seconds:
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
metadata:
name: pi
spec:
containers:
- name: pi
image: perl
command: ["/usr/bin/timeout", "10", "perl", "-Mbignum=bpi", "-wle", "print bpi(4000)"]
restartPolicy: Never
您可以玩这些数字,看看是否超时。通常,计算4000位数的pi在我的工作站上大约需要23秒,因此,如果将其设置为5秒,它可能总是会失败,而如果将其设置为120秒,它将总是可以工作。
You can play with the numbers and see it timeout or not. Typically computing 4000 digits of pi takes ~23 seconds on my workstation, so if you set it to 5 seconds it'll probably always fail and if you set it to 120 seconds it will always work.
这篇关于如何设置Kubernetes作业的时间限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!