且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何设置Kubernetes作业的时间限制?

更新时间:2023-01-09 08:06:32

通过使用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.