Exercise
-
Create the specification of a Job running 3 pods in sequence. Each Pod contains a single container based on the alpine:3.15 image, prints the current date in its standard output and then waits for 3 seconds
-
Run the Job
-
Get the logs of the 3 pods
-
Create a CronJob that prints the current date in the standard output every minute
-
Delete the Job and CronJob
Documentation
- https://kubernetes.io/docs/concepts/workloads/controllers/job/
- https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
Solution
- Create a Job running 3 pods in sequence. Each Pod contains a single container based on the alpine:3.15 image, prints the current date in its standard output and then waits for 3 seconds
k create job my-job --image=alpine:3.15 --dry-run=client -o yaml -- /bin/sh -c "date && sleep 3" > job.yaml
Then modify the specification to specify 3 pods must run in sequence
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
parallelism: 1
completions: 3
template:
spec:
containers:
- command:
- /bin/sh
- -c
- date && sleep 3
image: alpine:3.15
name: my-job
restartPolicy: Never
- Run the Job
k apply -f job.yaml
- Get the logs of the 3 pods
The name of the job is auto set as the value of the pods’ job-name label:
k logs -l job-name=my-job
- Create a CronJob that prints the current date in the standard output every minute
k create cronjob my-cronjob --image=alpine:3.15 --schedule="* * * * *" -- date
- Delete the Job and CronJob
k delete job my-job
k delete cj my-cronjob