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

标签 kubernetes google-cloud-platform google-kubernetes-engine

我有一个容器,该容器运行从MySQL数据库获取的一些数据,并仅在console.log()中显示结果,并希望在GKE中将其作为cron作业运行。到目前为止,我已经在本地计算机上运行了该容器,并且已经成功地将该容器部署到了GKE(据我所知,没有抛出任何错误)。

但是,创建的Pod只是保持“正在运行”,而不是在任务完成后停止。是应该在执行所有代码后自动停止Pod,还是需要明确的指令来停止Pod?如果是,那么在创建后终止Pod的命令是什么(由Cron Job执行)?

我读到的是,默认情况下应该有某种〜30s的终止宽限期,但是在运行了约20分钟的微小执行的cronjob之后,所有pod仍在运行。不知道是否存在从代码内部终止pod的方法,否则让cronjob生成许多空闲运行的pod有点愚蠢。下面的我的cronjob.yaml:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: test
spec:
  schedule: "5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: test
            image: gcr.io/project/test:v1
            # env:
            #   - name: "DELAY"
            #     value: 15
          restartPolicy: OnFailure

最佳答案

CronJob本质上是作业的小甜饼。也就是说,它知道如何创建作业并在特定时间执行它们。话虽如此,当我们查看垃圾收集并清理CronJob的行为时,我们可以简单地看一下Kubernetes文档对in the context of jobs这个话题的评价:

When a Job completes, no more Pods are created, but the Pods are not deleted either. Keeping them around allows you to still view the logs of completed pods to check for errors, warnings, or other diagnostic output. The job object also remains after it is completed so that you can view its status. It is up to the user to delete old jobs after noting their status. Delete the job with kubectl (e.g. kubectl delete jobs/pi or kubectl delete -f ./job.yaml).

关于kubernetes - 将容器作为CronJob部署到(Google)Kubernetes Engine-如何在完成任务后停止Pod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52301036/

相关文章:

windows-10 - Windows 持久卷上的 Kubernetes

docker - 在minikube中使用本地docker镜像

python - 使用 Python 2.7 的 Google Cloud ML

kubernetes - GCP/GKE 上的私有(private) Kubernetes 集群上的 Tekton

permissions - 用于cluster-admin的GKE clusterrolebinding失败,出现权限错误

kubernetes - docker-compose 是一个 self 修复的协调器吗?

mysql - 将 mysql workbench 连接到 GCE mysql

ubuntu - 尝试安装 Google Cloud SDK 时出现格式错误的输入错误

google-cloud-platform - 如何在 Google Kubernetes 上启用 Google 托管证书?

Jenkins X : trying to execute 'jx boot' from a non requirements repo