A restart policy is a PodSpec setting that tells the node’s kubelet what to do when a container exits. For Jobs, use OnFailure to restart a failed container or Never to leave the failed Pod unchanged. Always is the normal Pod default, but it does not fit a Job because Jobs are expected to terminate successfully. section 10.2.1 section 10.3.1
Choose the policy according to the workload’s completion behavior:
- OnFailure restarts a Job container when it crashes without returning success, allowing the worker to continue toward completion. section 10.3.1
- Never prevents container restarts, preserving failed Pods for inspection; this can make development debugging and log queries easier. section 10.2.1
- Always is unsuitable for Jobs because it would restart a successfully completed Pod; Deployments are the more appropriate workload for that behavior. section 10.2.1
A queue worker Job
A Job-based queue worker sets restartPolicy: OnFailure. Two worker Pods can process queued tasks in parallel; each Pod exits when the queue is empty, but restarts if it crashes before successful completion. section 10.3.1
Listing 10.9 Chapter10/10.2.1_Job/job_addwork.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: addwork
spec:
backoffLimit: 2
template:
spec:
containers:
- name: pi-container
image: docker.io/wdenniss/pi_worker:2 #1
command: ["python3", "add_tasks.py"] #2
env:
- name: REDIS_HOST
value: redis-0.redis-service
- name: PYTHONUNBUFFERED
value: "1"
restartPolicy: Never
Shows a complete one-off Job manifest using restartPolicy: Never alongside backoffLimit: 2. section 10.2.1
Listing 10.12 Chapter10/10.3.1_JobWorker/job_worker.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: jobworker
spec:
backoffLimit: 2
parallelism: 2 #1
template:
metadata:
labels:
pod: pi
spec:
containers:
- name: pi-container
image: docker.io/wdenniss/pi_worker:3 #2
env:
- name: REDIS_HOST
value: redis-0.redis-service
- name: PYTHONUNBUFFERED
value: "1"
- name: COMPLETE_WHEN_EMPTY #3
value: "1"
restartPolicy: OnFailure
Shows a parallel queue-worker Job using restartPolicy: OnFailure. section 10.3.1


