一次性任务
apiVersion: batch/v1
kind: Job //资源类型为Job
metadata:
name: myjob
spec:
parallelism: 2 //设置并行数量
completions: 6 //设置完成Pod总数
template:
metadata:
name: myjob
spec:
containers:
- name: hello
image: busybox
command: ["echo", "hello k8s job!"]
restartPolicy: Never //对于Job只能设置为Never或OnFailure
启动Job:kubectl apply -f myjob.yml
查看Job状态:
kubectl get job
kubectl get pod --show-all
查看Pod输出:kubectl logs myjob-nfkxk
删除Job:kubectl delete -f myjob.yml
Job运行失败时查看kubectl get pod --show-all,restartPolicy: Never会创建多个Pod启动,restartPolicy: OnFailure会自动重启该Pod。
定时执行
apiVersion: batch/v2alpha1 //CronJob的apiVersion
kind: CronJob //资源类型为CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *" //每一分钟启动一次
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
command: ["echo", "hello k8s job!"]
restartPolicy: OnFailure
修改API Server:/etc/kubernetes/manifests/kube-apiserver.yaml
--runtime-config=batch/v2alpha1=true
重启kubelet服务:systemctl restart kubelet.service //kubelet重启kube-apiserver
查看kube-apiserver版本:kubectl api-versions
创建CronJob:kubectl apply -f cronjob.yml
查看状态:
kubectl get cronjob
kubectl get jobs
kubectl get pod --show-all
kubectl logs hello-1505181720-lf350