Full lesson
Explore the full explanation, examples, and visuals at your own pace.
report.yaml
Kubernetes refuses this manifest. A developer wants their report container to run once and exit, so they add restartPolicy: Never. But it sits inside a Deployment, which is a controller that keeps Pods, the small units that run your containers, alive. Those two goals collide.
apiVersion: apps/v1
kind: Deployment
metadata:
name: report
spec:
replicas: 1
selector:
matchLabels: {app: report}
template:
metadata:
labels: {app: report}
spec:
restartPolicy: Never
containers:
- name: report
image: report:1.0Applying the manifest
We run kubectl apply, and the API server refuses it. The error points to a field path: the restartPolicy inside the Pod template. It says the value Never is unsupported, and the only supported value is Always. A Deployment accepts nothing else there, so the manifest is rejected before anything runs.
kubectl apply -f report.yamlThe Deployment "report" is invalid: spec.template.spec.restartPolicy: Unsupported value: "Never": supported values: "Always"
A Deployment's job: keep it running
The report:1.0 image becomes the Deployment's template, and the Deployment keeps one report Pod running. That count is its desired state, the number of Pods it tries to maintain. So when the container finishes and exits, even successfully, the Pod is restarted. To a Deployment, an exit is a failure to fix.
Rejected before anything runs
Restarting is the Deployment's whole job, so restartPolicy Never contradicts it. When kubectl apply sends report.yaml to the API server, the cluster's front door, it validates the manifest and refuses it. The Deployment is never created, and nothing runs. The error appears before the container ever starts.
Run-once work belongs in a Job.
Work that should run once and finish belongs in a Job. A Job is a Kubernetes object built to run a task until it succeeds, then stop.
Same container, different object
Take the same report container. Under a Deployment, it starts, finishes, and exits, and because the policy is Always, Kubernetes restarts it, again and again. Under a Job, it starts, finishes, exits, and the Job is marked Complete. Then it stops. The exit is identical. The object decides the reaction.
Two ways to fix it
You have two fixes. If the report should run forever, delete the restartPolicy line. The Deployment then defaults to Always, and the manifest applies. But if it truly runs once, the Job is the right home for it.
- Should run forever: delete restartPolicy
- Should run once: move it to a Job
report-job.yaml
Here's the same container moved into a Job. The kind is Job, and the apiVersion is batch/v1. There's no replicas field. This time restartPolicy Never is accepted, because a Job's purpose is to finish. Jobs allow only Never or OnFailure, and Always is rejected.
apiVersion: batch/v1
kind: Job
metadata:
name: report
spec:
template:
spec:
restartPolicy: Never
containers:
- name: report
image: report:1.0The Job runs to completion
Here, the report:1.0 image becomes the Job's template, and the Job runs one report Pod. The Pod does its work and exits successfully, so it goes from Running to Succeeded. The Job then moves to Complete, and nothing restarts. If the container fails, a Job retries by default, up to a limit.
The object you choose decides whether your app should ever stop.
The object you choose decides whether your app should ever stop. So before you write restartPolicy, ask yourself whether this workload should ever stop. If it should keep running, use a Deployment. If it should finish, use a Job, and let the policy follow that choice.