10.3.1 Dynamic queue processing with Jobs

View in the book. Buy the book.

$ cd Chapter10
$ kubectl delete -f 10.1.2_TaskQueue2
deployment.apps "pi-worker" deleted

$ kubectl delete -f 10.2.1_Job
job.batch "addwork" deleted

$ kubectl delete -f 10.2.2_CronJob
cronjob.batch "addwork" deleted

Since our Redis-based queue may have some existing jobs, you can reset it as well using the LTRIM Redis command:

$ kubectl exec -it pod/redis-0 -- redis-cli ltrim queue:task 0 0
OK

You can also run the redis-cli interactively if you prefer to reset the queue:

$ kubectl exec -it pod/redis-0 -- redis-cli
127.0.0.1:6379> LTRIM queue:task 0 0
OK
$ cd Chapter10
$ kubectl create -f 10.2.1_Job
job.batch/addwork created
$ kubectl get job,pod
NAME                COMPLETIONS   DURATION   AGE
job.batch/addwork   1/1           3s         10s

NAME                             READY   STATUS        RESTARTS   AGE
pod/addwork-6svdp                0/1     Completed     0          10s
pod/redis-0                      1/1     Running       0          26m
pod/redis-1                      1/1     Running       0          23m
pod/redis-2                      1/1     Running       0          22m
$ kubectl create -f 10.3.1_JobWorker
job.batch/jobworker created

While the workers are running

$ kubectl get job,pod
NAME                  COMPLETIONS   DURATION   AGE
job.batch/addwork     1/1           5s         29s
job.batch/jobworker   0/1 of 2      16s        16s

NAME                  READY   STATUS      RESTARTS   AGE
pod/addwork-79q4k     0/1     Completed   0          29s
pod/jobworker-drwfm   1/1     Running     0          16s
pod/jobworker-z952g   1/1     Running     0          16s
pod/redis-0           1/1     Running     0          28m
pod/redis-1           1/1     Running     0          25m
pod/redis-2           1/1     Running     0          24m

You can query how much work is in the queue

$ kubectl exec -it pod/redis-0 -- redis-cli llen queue:task
(integer) 5

When the queue is zero, the workers should hit the Completed state after processing their last job.

$ kubectl get job,pod
NAME                  COMPLETIONS   DURATION   AGE
job.batch/addwork     1/1           5s         3m51s
job.batch/jobworker   2/1 of 2      2m27s      3m38s

NAME                  READY   STATUS      RESTARTS   AGE
pod/addwork-79q4k     0/1     Completed   0          3m51s
pod/jobworker-drwfm   0/1     Completed   0          3m38s
pod/jobworker-z952g   0/1     Completed   0          3m38s
pod/redis-0           1/1     Running     0          31m
pod/redis-1           1/1     Running     0          29m
pod/redis-2           1/1     Running     0          27m

Run again

The old Job objects remain, even when completed. To run it again, delete the add work and worker Jobs.

$ kubectl delete -f 10.2.1_Job
job.batch "addwork" deleted
$ kubectl delete -f 10.3.1_JobWorker
job.batch "jobworker" deleted

Run the add work Job again

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

Wait for the addwork Job to complete, then

$ kubectl create -f 10.3.1_JobWorker
job.batch/jobworker created