将容器作为作业部署到 (Google) Kubernetes Engine - 如何在完成任务后终止 Pod

Deploying container as a Job to (Google) Kubernetes Engine - How to terminate Pod after completing task

目标是在作业完成后终止 pod。 这是我的 yaml 文件。目前,我的 pod 状态是 completed 在 运行 作业之后。

apiVersion: batch/v1
kind: Job
metadata:
  # Unique key of the Job instance
  name: example-job
spec:
  template:
    metadata:
      name: example-job
    spec:
      containers:
      - name: container-name
        image: my-img
        command: ["python", "main.py"]
      # Do not restart containers after they exit
      restartPolicy: Never
  # of retries before marking as failed.
  backoffLimit: 4 

一个 pod 的作业基本上在该 pod 的主容器成功完成后自行终止。如果它 returns 失败错误代码,它将重试您在 backoffLimit 中指定的次数。

所以看起来你的容器在完成它应该做的任何工作后都没有终止。在对您的工作形象一无所知的情况下,我无法准确地告诉您您需要做什么。 但是,似乎您需要调整 main.py 以在完成它应该做的事情后正确退出。

如果你想在完成任务后删除pod,那么用kubectl删除job即可:

$ kubectl 删除作业

您也可以使用yaml文件脚本通过以下命令自动删除作业:

$kubectl delete -f ./job. yaml

当您使用 kubectl 删除作业时,所有创建的 pods 也会被删除。

您可以使用以下命令检查这些作业和 pods 是否已删除:

$ kubectl get jobs$ kubectl get pods

有关详细信息,请参阅 Jobs.

我已经在我自己的环境中尝试了上述步骤并且对我有效。

您可以在完成后配置和删除作业

在 YAML 中,您可以配置保持 PODs

的限制
successfulJobsHistoryLimit: 0
failedJobsHistoryLimit: 0

您可以在 YAML 中使用上述配置设置历史限制

The .spec.successfulJobsHistoryLimit and .spec.failedJobsHistoryLimit fields are optional. These fields specify how many completed and failed jobs should be kept. By default, they are set to 3 and 1 respectively. Setting a limit to 0 corresponds to keeping none of the corresponding kind of jobs after they finish.

backoffLimit: 4 将退出多次并在将其标记为失败之前尝试 运行 作业。

阅读更多信息:https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#jobs-history-limits