10.2.1 Running one-off tasks with Jobs

View in the book. Buy the book.
10.3 Object composition of a Job
Figure 10.3 Object composition of a Job

Create a Job to add the work to the queue instead of using exec.

$ kubectl create -f Chapter10/10.2.1_Job/job_addwork.yaml
job.batch/addwork created

After a time it will show as “Completed”

$ kubectl get job,pods
NAME                COMPLETIONS   DURATION   AGE
job.batch/addwork   1/1           2m57s      3m2s

NAME                            READY   STATUS      RESTARTS   AGE
pod/addwork-thwbq               0/1     Completed   0          3m2s
pod/pi-worker-f7565d87d-8tzb2   1/1     Running     0          7m20s
pod/pi-worker-f7565d87d-dtrkd   1/1     Running     0          7m20s
pod/redis-0                     1/1     Running     0          13m
pod/redis-1                     1/1     Running     0          11m
pod/redis-2                     1/1     Running     0          10m

Observe, as before

$ kubectl get pods,hpa
NAME                            READY   STATUS    RESTARTS   AGE
pod/pi-worker-f7565d87d-2hlsj   0/1     Pending   0          29s
pod/pi-worker-f7565d87d-526pd   0/1     Pending   0          59s
pod/pi-worker-f7565d87d-5ssrw   0/1     Pending   0          29s
pod/pi-worker-f7565d87d-65sbv   0/1     Pending   0          14s
pod/pi-worker-f7565d87d-7kpzt   0/1     Pending   0          59s
pod/pi-worker-f7565d87d-86pvh   0/1     Pending   0          14s
pod/pi-worker-f7565d87d-8tzb2   1/1     Running   0          11m
pod/pi-worker-f7565d87d-bdrft   0/1     Pending   0          29s
pod/pi-worker-f7565d87d-dtrkd   1/1     Running   0          11m
pod/pi-worker-f7565d87d-vchzt   0/1     Pending   0          29s
pod/redis-0                     1/1     Running   0          18m
pod/redis-1                     1/1     Running   0          15m
pod/redis-2                     1/1     Running   0          14m

NAME                                                       REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/pi-worker-autoscaler   Deployment/pi-worker    99%/20%         2         10        8          90s
horizontalpodautoscaler.autoscaling/timeserver             Deployment/timeserver   <unknown>/20%   1         10        1          5h7m

You can’t run the job twice, as the object remains even in the completed state. Clean up the “Completed” Pod like so

kubectl delete -f Chapter10/10.2.1_Job/job_addwork.yaml