Home > Net >  Creating Kubernetes Pod per Kubernetes Job and Cleanup
Creating Kubernetes Pod per Kubernetes Job and Cleanup

Time:03-26

I'm trying to create Kubernetes 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 !

CodePudding user response:

  • 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
  • Related