本文介绍了在创建下一个时间表之前,Kubernetes Cron Job终止Pod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Kubernetes Cron作业,用于每5分钟运行一次计划任务.我想确保在下一个计划时间创建新的容器时,较早的容器应已终止.较早的Pod应该在创建新Pod之前终止. Kubernetes可以在创建新的Pod之前终止较早的Pod吗?

I have a Kubernetes Cron Job for running a scheduled task every 5 minutes. I want to make sure that when a new pod is created at next schedule time, the earlier pod should have been terminated. The earlier pod should get terminated before creation of new. Can Kubernetes terminate the earlier pod before creation of new?

我的yaml是:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-scheduled
spec:
  schedule: "*/5 * * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cmm-callout
            env:
              - name: SCHEDULED
                value: "true"
            livenessProbe:
              httpGet:
                path: /myapp/status
                port: 7070
                scheme: HTTPS
              initialDelaySeconds: 120
              timeoutSeconds: 30
              periodSeconds: 120                
            image: gcr.io/projectid/folder/my-app:9.0.8000.34
          restartPolicy: Never

如何确保较早的Pod在创建新Pod之前已终止?

How can I make sure the earlier pod is terminated before new is created?

推荐答案

如果我正确理解了您的情况(较早的pod应该在创建新的pod之前已终止).

If i understood your case correctly (the earlier pod should have been terminated before creation of new one).

1 .请改用 spec.jobTemplate.spec.activeDeadlineSeconds .

在作业达到 activeDeadlineSeconds 后,通过设置此参数-所有正在运行的Pod都将被终止,作业状态将变为:失败,原因为DeadlineExceededed.

By setting this parameter once a Job reaches activeDeadlineSeconds - all of running Pods will be terminated and the Job status will become type: Failed with reason DeadlineExceeded.

示例:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      activeDeadlineSeconds: 60
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster && sleep 420
          restartPolicy: Never

2 .第二种解决方案是设置 concurrencyPolicy .并将当前正在运行的作业替换为新作业.

2. The second solution is to set-up concurrencyPolicy. and replace the currently running job with a new job.

示例:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/2 * * * *"
  concurrencyPolicy: Replace
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster && sleep 420
          restartPolicy: Never

资源:

并发策略

这篇关于在创建下一个时间表之前,Kubernetes Cron Job终止Pod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:54