Job:一次性任务
负责批量处理短暂的一次性任务,即仅执行一次的任务,它保证批处理任务的一个或多个pod成功结束。
[root@k8s-master ~]# crictl pull perl:5.34.0
Image is up to date for sha256:3677b00a789857f6ff24774645688ad2327e54286d8ca56c4be98f0c2743f58b
[root@k8s-master ~]# crictl images
IMAGE TAG IMAGE ID SIZE
docker.io/library/perl 5.34.0 3677b00a78985 336MB
[root@k8s-master ~]# cat job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
[root@k8s-master ~]# kubectl apply -f job.yaml
job.batch/pi created
[root@k8s-master ~]# kubectl describe jobs pi
Name: pi
Namespace: default
Selector: controller-uid=32e7eae7-6bc4-4403-9f17-722a865e0e76
Labels: controller-uid=32e7eae7-6bc4-4403-9f17-722a865e0e76
job-name=pi
Annotations: batch.kubernetes.io/job-tracking:
Parallelism: 1
Completions: 1
Completion Mode: NonIndexed
Start Time: Thu, 20 Oct 2022 14:47:01 +0800
Completed At: Thu, 20 Oct 2022 14:47:09 +0800
Duration: 8s
Pods Statuses: 0 Active (0 Ready) / 1 Succeeded / 0 Failed
Pod Template:
Labels: controller-uid=32e7eae7-6bc4-4403-9f17-722a865e0e76
job-name=pi
Containers:
pi:
Image: perl:5.34.0
Port: <none>
Host Port: <none>
Command:
perl
-Mbignum=bpi
-wle
print bpi(2000)
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 8s job-controller Created pod: pi-sn2qz
Normal Completed 0s job-controller Job completed
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pi-sn2qz 0/1 Completed 0 36s
[root@k8s-master ~]# kubectl get jobs
NAME COMPLETIONS DURATION AGE
pi 1/1 8s 41s
#查看pod标准输出
[root@k8s-master ~]# pods=$(kubectl get pods --selector=job-name=pi --output=jsonpath='{.items[*].metadata.name}')
[root@k8s-master ~]# echo $pods
pi-sn2qz
[root@k8s-master ~]# kubectl logs $pods
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
cronJob:定时任务
类似于linux系统的crontab,在指定的时间周期运行指定的任务
[root@k8s-master ~]# cat cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
[root@k8s-master ~]# crictl pull busybox:1.28
Image is up to date for sha256:8c811b4aec35f259572d0f79207bc0678df4c736eeec50bc9fec37ed936a472a
[root@k8s-master ~]# crictl images
IMAGE TAG IMAGE ID SIZE
docker.io/calico/cni master 220710ee32240 87.4MB
docker.io/calico/node master aea2c0916b4d6 87MB
docker.io/library/busybox 1.28 8c811b4aec35f 728kB
[root@k8s-master ~]# kubectl apply -f cronjob.yaml
cronjob.batch/hello created
[root@k8s-master ~]# kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello * * * * * False 0 24s 22m
[root@k8s-master ~]# kubectl describe cronjobs
Name: hello
Namespace: default
Labels: <none>
Annotations: <none>
Schedule: * * * * *
Concurrency Policy: Allow
Suspend: False
Successful Job History Limit: 3
Failed Job History Limit: 1
Starting Deadline Seconds: <unset>
Selector: <unset>
Parallelism: <unset>
Completions: <unset>
Pod Template:
Labels: <none>
Containers:
hello:
Image: busybox:1.28
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
date; echo Hello from the Kubernetes cluster
Environment: <none>
Mounts: <none>
Volumes: <none>
Last Schedule Time: Thu, 20 Oct 2022 14:55:00 +0800
Active Jobs: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 27s cronjob-controller Created job hello-27770815
Normal SawCompletedJob 24s cronjob-controller Saw completed job: hello-27770815, status: Complete
[root@k8s-master ~]# kubectl get jobs
NAME COMPLETIONS DURATION AGE
hello-27770815 1/1 3s 43s
pi 1/1 8s 8m42s
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-27770815-sgqwj 0/1 Completed 0 49s
pi-sn2qz 0/1 Completed 0 8m48s
[root@k8s-master ~]# kubectl logs hello-27770815-sgqwj
Thu Oct 20 06:55:00 UTC 2022
Hello from the Kubernetes cluster