官方文檔:https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/vim
一、建立yaml文件api
vim job.yamlspa
apiVersion: batch/v1 kind: Job metadata: name: pi spec: template: spec: containers: - name: pi image: perl # 計算兩千位的值 command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] # 重啓策略 restartPolicy: Never # 限制重啓的次數 backoffLimit: 4
二、建立容器rest
kubectl create -f job.yaml
三、查看容器
kubectl get pods日誌
NAME READY STATUS RESTARTS AGE # 計算完成 pi-q8rvl 0/1 Completed 0 4m5s
四、執行完成後查看job
kubectl get jobcode
NAME COMPLETIONS DURATION AGE
pi 1/1 91s 106s
五、查看日誌
kubectl logs pi-q8rvl視頻
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513...............
六、刪除方法blog
kubectl delete -f job.yaml
官方文檔:https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/crontab
1.建立一個yaml文件文檔
vim cronjob.yaml
apiVersion: batch/v1beta1 kind: CronJob metadata: name: hello spec: # 分時日月周,與crontab語法同樣 schedule: "*/1 * * * *" # job模板 jobTemplate: spec: template: spec: # 容器配置 containers: - name: hello image: busybox # 到達時間段執行命令 args: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster # 若是異常退出就重啓 restartPolicy: OnFailure
二、建立容器
kubectl create -f cronjob.yaml
三、查看容器
kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-1566882780-t88jl 0/1 Completed 0 71s
四、查看cronjob
kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 <none> 21s
五、查看日誌
kubectl logs hello-1566883020-7hw2s
Tue Aug 27 05:17:10 UTC 2019 Hello from the Kubernetes cluster
六、刪除
kubectl delete -f cronjob.yaml