避免在Kubernetes中为一个cron执行点运行多个cron作业

时间:2019-04-04 07:46:27

标签: kubernetes cron

编辑:问题已解决,这是我的错误,我只是使用了错误的cron设置。我假设“ * 2 * * *”每天仅在2点运行一次,但实际上它在每小时2点之后每分钟运行一次。因此Kubernetes的行为正确。

我一直在一个cron执行点运行多个作业。但是似乎只有这些作业的运行时间很短。知道为什么会发生这种情况以及如何预防吗?我使用concurrencyPolicy: ForbidbackoffLimit: 0restartPolicy: Never

cron作业的示例,该作业本应每天运行一次,但仅在其计划运行时间之后运行多次:

job-1554346620                   1/1           11s        4h42m   
job-1554346680                   1/1           11s        4h41m                     
job-1554346740                   1/1           10s        4h40m 

相关配置:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: job
spec:
  schedule: "* 2 * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: job
              image: job_image:latest
              command: ["rake", "run_job"]
          restartPolicy: Never
          imagePullSecrets:
            - name: regcred
      backoffLimit: 0

2 个答案:

答案 0 :(得分:1)

您好,您不清楚您期望什么-调查问题,但如果我理解正确,您的意思是不要同时运行所有cronjobs:
1.第一种选择-更改时间表时间,
2.第二个选项尝试在规范模板中使用其他选项,例如-并行作业-描述:https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/

对于工作队列Job,必须保留.spec.completions不变,并将.spec.parallelism设置为非负整数


  jobTemplate:
    spec:
      parallelism: 1
      template:

要重新创建此任务,请提供更多详细信息。

除“ 工作历史”外 默认情况下, successfulJobsHistoryLimit failedJobsHistoryLimit 设置为 3 1 < / strong>。
请前往:https://kubernetes.io/docs/tasks/job/ 如果您有兴趣,可以在“规格”部分中设置限制:


successfulJobsHistoryLimit: 1

failedJobsHistoryLimit: 1

希望获得帮助。

答案 1 :(得分:1)

在k8s上运行CronJobs的最常见问题是:

产生许多消耗所有群集资源的Pod

设置适当的CronJob限制非常重要

如果不确定自己需要什么,只需将以下示例作为模板:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-first-conjob
  namespace: devenv-admitriev
spec:
  schedule: "*/10 * * * *" # MM HH DD MM WKD -- Minutes, Hour, Day, Month, Weekday (eg. Sun, Mon)
  successfulJobsHistoryLimit: 3 # how many completed jobs should be kept
  failedJobsHistoryLimit: 1 # how many failed jobs should be kept
  suspend: false # Here you can suspend cronjob without deliting it
  concurrencyPolicy: Forbid # Choose Forbid if you don't want concurrent executions of your Job

  # The amount of time that Kubernetes can miss and still start a job.
  # If Kubernetes missed too many job starts (100)
  # then Kubernetes logs an error and doesn’t start any future jobs.
  startingDeadlineSeconds: 300 # if a job hasn't started in this many seconds, skip
  jobTemplate:
    spec:
      parallelism: 1 # How many pods will be instantiated at once.
      completions: 1 # How many containers of the job are instantiated one after the other (sequentially) inside the pod.
      backoffLimit: 3 # Maximum pod restarts in case of failure
      activeDeadlineSeconds: 1800 # Limit the time for which a Job can continue to run
      template:
        spec:
          restartPolicy: Never # If you want to restart - use OnFailure
          terminationGracePeriodSeconds: 30
          containers:
          - name: my-first-conjob
            image: busybox
            command:
              - /bin/sh
            args:
              - -c
              - date; echo sleeping....; sleep 90s; echo exiting...;
            resources:
              requests:
                memory: '128Mi'
              limits:
                memory: '1Gi'