如何为 Kubernetes 作业设置时间限制? [英] How to set a time limit for a Kubernetes job?

查看:59
本文介绍了如何为 Kubernetes 作业设置时间限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启动一个 Kubernetes 作业并给它一个固定的截止日期来完成.如果截止日期到来时 pod 仍在运行,我希望自动终止该作业.

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

(清单采用自 https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/#running-an-example-job)

您可以玩这些数字,看看它是否超时.通常在我的工作站上计算 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆