作业完成后如何自动删除Kubernetes作业

时间:2019-07-19 02:07:42

标签: kubernetes google-kubernetes-engine

我正在GKE上运行kubernetes作业,并希望在作业完成后自动删除该作业。

这是我的工作配置文件。 我设置了ttlSecondsAfterFinished: 0,但是该作业没有被自动删除。 我想念什么吗?

  • 集群/节点版本:1.12.8-gke.10
apiVersion: batch/v1
kind: Job
metadata:
  name: myjob
spec:
  # automatically clean up finished job
  ttlSecondsAfterFinished: 0
  template:
    metadata:
      name: myjob
    spec:
      containers:
        - name: myjob
          image: gcr.io/GCP_PROJECT/myimage:COMMIT_SHA
          command: ["bash"]
          args: ["deploy.sh"]
      # Do not restart containers after they exit
      restartPolicy: Never

3 个答案:

答案 0 :(得分:0)

看起来该功能现在在GKE上仍然不可用。

https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/ https://cloud.google.com/kubernetes-engine/docs/concepts/alpha-clusters#about_feature_stages

backtrace:
  #00  pc 0000000000682d6c  /data/app/name-6aGG2YsCG8wNCGYk_6BJdQ==/split_config.arm64_v8a.apk (CMActionController::advanceByTime(float)+444)
  #01  pc 000000000068d8cc  /data/app/name-6aGG2YsCG8wNCGYk_6BJdQ==/split_config.arm64_v8a.apk (CMNodeController::advanceByTime(float)+52)
  #02  pc 000000000050ff38  /data/app/name-6aGG2YsCG8wNCGYk_6BJdQ==/split_config.arm64_v8a.apk (CMScreen::advanceByTime(float)+108)
  #03  pc 00000000006f46b8  /data/app/name-6aGG2YsCG8wNCGYk_6BJdQ==/split_config.arm64_v8a.apk (CMScreenManager::render()+256)

答案 1 :(得分:0)

这取决于您如何创建工作。

如果您使用的是CronJob,则可以使用spec.successfulJobsHistoryLimitspec.failedJobsHistoryLimit并将值设置为0。这将表明K8不会对以前完成的任何作业进行排序。

如果要使用YAML创建吊舱,则必须手动将其删除。但是,您也可以将CronJob设置为每5分钟执行一次命令。

kubectl delete job $(kubectl get job -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}')

它将删除状态为成功的所有作业。

答案 2 :(得分:-2)

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: jp-runner
rules:
- apiGroups: ["batch"]
  resources: ["jobs"]
  verbs: ["get", "list", "delete"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: jp-runner
subjects:
- kind: ServiceAccount
  name: sa-jp-runner
roleRef:
  kind: Role
  name: jp-runner
  apiGroup: ""
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: sa-jp-runner
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: clean-jobs
spec:
  concurrencyPolicy: Forbid
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-jp-runner
          containers:
          - name: clean-jobs
            image: bitnami/kubectl:latest
            command:
            - /bin/sh
            - -c
            - kubectl delete jobs $(kubectl get jobs -o=jsonpath='{.items[?(@.status.succeeded==1)].metadata.name}')
          restartPolicy: Never
      backoffLimit: 0