简体   繁体   English

如何只为/由 Cronjob 创建一个 Pod

[英]how to have only one Pod created for/by Cronjob

I am running this Cronjob at 2 AM in the morning:我在凌晨 2 点运行这个 Cronjob:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgres-backup
spec:
  # Backup the database every day at 2AM
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: postgres-backup
            image: postgres:10.4
            command:
            - "/bin/sh"
            - -c
            - |
              pg_dump -Fc -d postgresql://$DBUSER:$DBPASS@$DBHOST:$DBPORT/$DBNAME > /var/backups/backup_$(date +"%d-%m-%Y_%H-%M").bak;
            env:
            - name: DBHOST
              valueFrom:
                configMapKeyRef:
                  name: dev-db-config
                  key: db_host
            - name: DBPORT
              valueFrom:
                configMapKeyRef:
                  name: dev-db-config
                  key: db_port
            - name: DBNAME
              valueFrom:
                configMapKeyRef:
                  name: dev-db-config
                  key: db_name
            - name: DBUSER
              valueFrom:
                secretKeyRef:
                  name: dev-db-secret
                  key: db_username
            - name: DBPASS
              valueFrom:
                secretKeyRef:
                  name: dev-db-secret
                  key: db_password
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-backup-storage
          - name: postgres-restore
            image: postgres:10.4
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-backup-storage
          restartPolicy: OnFailure
          volumes:
          - name: postgres-backup-storage
            hostPath:
            # Ensure the file directory is created.
              path: /var/volumes/postgres-backups
              type: DirectoryOrCreate

The jobs are getting executed successfully, but what I don't like is that for every Job execution a new Pod is created:作业已成功执行,但我不喜欢的是,每次作业执行都会创建一个新 Pod:

Cornjob Job 执行的多个 Pod

Is there a way to clean previous (old) created Pods?有没有办法清理以前(旧)创建的 Pod? Or maybe is there a way to rerun one an the same Pod/Job every time?或者,也许有办法每次都重新运行同一个 Pod/Job?

If only last job and pod need to be preserved, you can use .spec.successfulJobsHistoryLimit field set to 1 .如果只需要保留最后一个作业和 pod,则可以使用.spec.successfulJobsHistoryLimit字段设置为1

This way only last job and corresponding pod will be preserved.这样只会保留最后一个作业和相应的 pod。 By default it's set to 3 .默认情况下,它设置为3 Also it's possible to set this value to 0 and nothing will be saved after cronjob's execution.也可以将此值设置为0 ,并且在 cronjob 执行后不会保存任何内容。

Same logic has .spec.failedJobsHistoryLimit field, it has 1 by default.相同的逻辑有.spec.failedJobsHistoryLimit字段,默认为1

See jobs history limits .请参阅作业历史限制


This is how it looks when I get events from cronjob:这是我从 cronjob 获取事件时的样子:

$ kubectl describe cronjob test-cronjob
...
Events:
  Type    Reason            Age    From                Message
  ----    ------            ----   ----                -------
  Normal  SuccessfulCreate  2m31s  cronjob-controller  Created job test-cronjob-27304493
  Normal  SawCompletedJob   2m30s  cronjob-controller  Saw completed job: test-cronjob-27304493, status: Complete
  Normal  SuccessfulCreate  91s    cronjob-controller  Created job test-cronjob-27304494
  Normal  SawCompletedJob   90s    cronjob-controller  Saw completed job: test-cronjob-27304494, status: Complete
  Normal  SuccessfulDelete  90s    cronjob-controller  Deleted job test-cronjob-27304493
  Normal  SuccessfulCreate  31s    cronjob-controller  Created job test-cronjob-27304495
  Normal  SawCompletedJob   30s    cronjob-controller  Saw completed job: test-cronjob-27304495, status: Complete
  Normal  SuccessfulDelete  30s    cronjob-controller  Deleted job test-cronjob-27304494

Only one last job is presented:仅提供最后一项工作:

$ kubectl get jobs
NAME                    COMPLETIONS   DURATION   AGE
test-cronjob-27304496   1/1           1s         3s

And one pod:还有一个吊舱:

$ kubectl get pods
NAME                               READY   STATUS      RESTARTS   AGE
test-cronjob-27304496-r4qd8        0/1     Completed   0          38s

You can try to set ttlSecondsAfterFinished , to find out more try:您可以尝试设置ttlSecondsAfterFinished ,以了解更多尝试:

kubectl explain cronjob.spec.jobTemplate.spec.ttlSecondsAfterFinished --api-version=batch/v1beta1

ttlSecondsAfterFinished limits the lifetime of a Job that has finished execution (either Complete or Failed). ttlSecondsAfterFinished 限制已完成执行(完成或失败)的作业的生命周期。 If this field is set, ttlSecondsAfterFinished after the Job finishes, it is eligible to be automatically deleted.如果设置了这个字段,在 Job 完成后 ttlSecondsAfterFinished ,它就有资格被自动删除。 When the Job is being deleted, its lifecycle guarantees (eg finalizers) will be honored.当作业被删除时,它的生命周期保证(例如终结器)将被兑现。 If this field is unset, the Job won't be automatically deleted.如果未设置此字段,则作业不会被自动删除。 If this field is set to zero, the Job becomes eligible to be deleted immediately after it finishes.如果此字段设置为零,则作业在完成后即可立即删除。 This field is alpha-level and is only honored by servers that enable the TTLAfterFinished feature.此字段是 alpha 级别的,仅由启用 TTLAfterFinished 功能的服务器使用。

Shortly, the Job created by CronJob will be deleted after specified seconds.很快,由CronJob创建的Job将在指定秒后被删除。


In your case:在你的情况下:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgres-backup
spec:
  # Backup the database every day at 2AM
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          ttlSecondsAfterFinished: 60 # after 1 minute the and the pods will be deleted by kubernetes
...

There are two ways to fix this有两种方法可以解决此问题

First method, with the job spec第一种方法,使用工作规范

add a .spec.activeDeadlineSeconds field of the Job to a number of seconds.将 Job 的.spec.activeDeadlineSeconds字段添加到秒数。 The activeDeadlineSeconds applies to the duration of the job activeDeadlineSeconds 适用于作业的持续时间

Once a Job reaches activeDeadlineSeconds, all of its running Pods are terminated and the Job status will become type: Failed with reason: DeadlineExceeded.一旦 Job 达到 activeDeadlineSeconds,其所有正在运行的 Pod 都将终止,并且 Job 状态将变为 type: Failed with reason: DeadlineExceeded。

Set the activeDeadlineSeconds to be much longer than the expected running time将 activeDeadlineSeconds 设置为比预期运行时间长得多

Second method, with the pod第二种方法,使用 pod

make the pod so that it terminates itself when the job is complete.制作 pod,使其在作业完成时自行终止。 Something like altering the command to类似于将命令更改为

pg_dump -Fc -d postgresql://$DBUSER:$DBPASS@$DBHOST:$DBPORT/$DBNAME > /var/backups/backup_$(date +"%d-%m-%Y_%H-%M").bak; exit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM