[Kubernetes] Jobs & CronJobs

A Job creates one or more pods and will continue to retry execution of the Pods until a specified number of them successfully terminate. As pods successfully complete, the Job tracks the successful completions. When the specified number of successful completions is reached, the job is complete.

Jobs

A job runs container workloads until they complete, then shuts down the container(s).

Pod backoff failure policy

  • Set “spec.backoffLimit” to specify the number of retries before considering a Job as failed.
    • The back-off limit is set by default to 6.
  • Set “spec.activeDeadlineSeconds” to automatically terminate the job.
    • Once a Job reaches activeDeadlineSeconds, all of its running Pods are terminated and the Job status will become type: Failed
  • .spec.activeDeadlineSeconds takes precedence over its .spec.backoffLimit.
apiVersion: batch/v1
kind: Job
metadata:
  name: my-job
spec:
  activeDeadlineSeconds: 30 
  backoffLimit: 4
  template:
    spec:
      containers:
      - name: job-container
        image: busybox
        command: ['echo', 'Hello World']
      restartPolicy: Never
kubectl apply –f my-job.yaml

kubectl get jobs

kubectl describe job <job-name>

kubectl get pods –-selector=job-name=<job-name>
kubectl get pods –l=job-name=<job-name>

kubectl logs <pod-name>

kubectl delete job <job-name>

CronJobs

A CronJob re-runs a job periodically according to a schedule.

spec.schedule

The schedule consists of 5 components

  • minute (0 – 59)
  • hour (0 – 23)
  • day of the month (1 – 31)
  • month (1 – 12)
  • day of the week (0, Sunday – 6, Saturday)
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: "*/1 * * * *"  # every minute
jobTemplate:
  spec:
    template:
        spec:
          containers:
          - name: job-container
            image: busybox
            command: ['/bin/sh', '-c', 'date']
          restartPolicy: OnFailure
kubectl apply –f my-cronjob.yaml

kubectl get cronjobs

# check the event and find the job name
kubectl describe cronjob <cronjob-name>

kubectl get pods 

kubectl logs <pod-name>

kubectl delete cronjob <cronjob-name>

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s