简体   繁体   中英

Creating Kubernetes Pod per Kubernetes Job and Cleanup

I'm trying to create Kube.netes job with the following requirements:

  1. Only one pod can be created for each job at most
  2. If the pod failed - the job will fail
  3. Max run time of the pod will be 1 hour
  4. If the job finished successfully - delete the job

I tried the following configurations:

apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Release.Name }}
  annotations:
    "helm.sh/hook": post-install
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  template:
    spec:
      containers:
        - name: {{ .Release.Name }}
          image: {{ .Values.image }}
          env:
            - name: ARG1
              value: {{ required "ARG1 is mandatory" .Values.ENV.ARG1 }}
            - name: GITLAB_USER_EMAIL
              value: {{ .Values.ENV.GITLAB_USER_EMAIL }}
          envFrom:
            - secretRef:
                name: {{ .Release.Name }}
      restartPolicy: Never
  backoffLimit: 1
  activeDeadlineSeconds: 3600

But it's not working as expected, any ideas? Thanks !

  • Only one pod can be created for each job at most

The requested parallelism (.spec.parallelism) can be set to any non-negative value. If it is unspecified, it defaults to 1. If it is specified as 0, then the Job is effectively paused until it is increased.

For Cronjobs could be helpful successfulJobsHistoryLimit: 0, failedJobsHistoryLimit: 0 this will remove the PODs if it's get failed or success so no history or POD will stays. So only one pod will get created or run.

  • If the pod failed - the job will fail

That will be the default behavior, also restartPolicy: Never so it won't get restarted.

  • Max run time of the pod will be 1 hour

    activeDeadlineSeconds: 3600 you have already added

  • If the job finished successfully - delete the job

ttlSecondsAfterFinished: 100 will solve your issue.

apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Release.Name }}
  annotations:
    "helm.sh/hook": post-install
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  template:
    spec:
      containers:
        - name: {{ .Release.Name }}
          image: {{ .Values.image }}
          env:
            - name: ARG1
              value: {{ required "ARG1 is mandatory" .Values.ENV.ARG1 }}
            - name: GITLAB_USER_EMAIL
              value: {{ .Values.ENV.GITLAB_USER_EMAIL }}
          envFrom:
            - secretRef:
                name: {{ .Release.Name }}
      restartPolicy: Never
  backoffLimit: 1
  ttlSecondsAfterFinished: 100
  activeDeadlineSeconds: 3600

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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