本篇筆記將介紹另一個 Kubernetes 功能, Job 可以讓 Kubernetes 排程在特定時間完成批次作業.
PS C:\k8s> code .\simplejob.yaml
2. 使用命令 kubectl create -f .\simplejob.yaml 建立 job 部署
PS C:\k8s> kubectl create -f .\simplejob.yaml
job.batch "finalcountdown" created
PS C:\k8s> kubectl get jobs
NAME DESIRED SUCCESSFUL AGE
finalcountdown 1 1 20s
PS C:\k8s> kubectl get pods
NAME READY STATUS RESTARTS AGE
finalcountdown-jstdw 0/1 Completed 0 25s
SuccessfulCreate
1m job-controller Created pod: finalcountdown-jstdw
PS C:\k8s> kubectl describe jobs/finalcountdown
Name: finalcountdown
Namespace: default
Selector: controller-uid=15bd6557-d9fd-11e8-b771-00155d54100f
Labels: controller-uid=15bd6557-d9fd-11e8-b771-00155d54100f
job-name=finalcountdown
Annotations: <none>
Parallelism: 1
Completions: 1
Start Time: Sat, 27 Oct 2018 23:29:25 +0800
Pods Statuses: 0 Running / 1 Succeeded / 0 Failed
Pod Template:
Labels: controller-uid=15bd6557-d9fd-11e8-b771-00155d54100f
job-name=finalcountdown
Containers:
counter:
Image: busybox
Port: <none>
Host Port: <none>
Command:
bin/sh
-c
for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 1m job-controller Created pod: finalcountdown-jstdw
PS C:\k8s> kubectl log finalcountdown-jstdw
log is DEPRECATED and will be removed in a future version. Use logs instead.
9
8
7
6
5
4
3
2
1
接下來介紹另一種屬於排程的 Cronjob
PS C:\k8s> code .\cronjob.yaml
2. 使用命令 kubectl create -f .\cronjob.yaml 部署 cronjob
PS C:\k8s> kubectl create -f .\cronjob.yaml
cronjob.batch "hellocron" created
PS C:\k8s> kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hellocron */1 * * * * False 0 23s 1m
PS C:\k8s> kubectl edit cronjobs/hellocron
5. 將檔案儲存
PS C:\k8s> kubectl edit cronjobs/hellocron
cronjob.batch "hellocron" edited
PS C:\k8s> kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hellocron */1 * * * * True 0 3m 9m
PS C:\k8s> kubectl get pods
NAME READY STATUS RESTARTS AGE
hellocron-1540655220-mq858 0/1 Completed 0 6m
PS C:\k8s> kubectl logs hellocron-1540655220-mq858
Sat Oct 27 15:47:11 UTC 2018
Hello from your Kubernetes cluster
以上介紹 Kubernetes 執行單一工作與排程工作的方式.