liveBook · Manning

Restart policy

As explained in Kubernetes for Developers

A restart policy is a Pod setting that determines whether workloads are restarted after termination or failure.

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

How Kubernetes for Developers covers it

How deep it goes
Explains OnFailure, Never, and Always through Job behavior, but defers broader Pod lifecycle details.
Examples
Uses one-off task Jobs and parallel Redis queue workers configured with Never or OnFailure.
What it assumes
Assumes basic Pods and containers, while teaching the distinction between Job completion and container failure.
What you can do after
Operate/configure Jobs with a restart policy matched to failure recovery, debugging, or successful termination.
How it connects
Connects PodSpec, Jobs, Deployments, kubelet behavior, and backoff limits.

Also explained in

Compare how 3 books explain restart policy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.