Deployments keep Pods running. A Job does the opposite: it runs a Pod until the container exits 0, then stops.
Create one
text
learner@shallows:~$ kubectl create job seed-db --image=busybox -- sh -c 'echo seeding; sleep 2'
job.batch/seed-db created
learner@shallows:~$ kubectl get jobs
NAME STATUS COMPLETIONS DURATION AGE
seed-db Complete 1/1 5s 8s
learner@shallows:~$ kubectl logs job/seed-db
seedingEverything after -- is the command. The Pod shows Completed afterwards and stays, so you can read it. kubectl delete job seed-db removes the Job and its Pods.
The spec
yaml
apiVersion: batch/v1
kind: Job
metadata:
name: seed-db
spec:
completions: 1 # successes needed
parallelism: 1 # Pods at the same time
backoffLimit: 6 # retries before the Job is Failed
template:
spec:
restartPolicy: Never # or OnFailure, never Always
containers:
- name: seed
image: busybox
command: ["sh", "-c", "echo seeding"]hashes-fzxws · hashes-nq784two at a time
both exit 0
hashes-gbtjg · hashes-wz9cptwo more
CompleteCOMPLETIONS 4/4
Failure
A Pod that exits non-zero counts as a failure. The Job starts another after 10 s, then 20 s, 40 s. After backoffLimit retries it stops, with a Failed condition and a Warning event BackoffLimitExceeded:
text
learner@shallows:~$ kubectl describe job flaky
Pods Statuses: 0 Active (0 Ready) / 0 Succeeded / 3 Failed
Events:
Warning BackoffLimitExceeded job-controller Job has reached the specified backoff limit