┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl apply -f myjob.yaml job.batch/my-job created ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods NAME READY STATUS RESTARTS AGE my-job--1-jdzqd 0/1 ContainerCreating 0 7s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get jobs NAME COMPLETIONS DURATION AGE my-job 0/1 17s 17s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods NAME READY STATUS RESTARTS AGE my-job--1-jdzqd 0/1 Completed 0 24s
STATUS 状态变成 Completed意味着执行成功,查看日志
1 2 3 4 5 6 7 8 9
┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get jobs NAME COMPLETIONS DURATION AGE my-job 1/1 19s 46s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl logs my-job--1-jdzqd hello jobs ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$
job的配置参数解析
job的restart策略
1
restartPolicy:Never
Nerver : 只要任务没有完成,则是新创建pod运行,直到job完成 会产生多个pod
OnFailure : 只要pod没有完成,则会重启pod,直到job完成
activeDeadlineSeconds:最大可以运行时间
1 2 3 4 5 6 7 8
┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl explain jobs.spec | grep act activeDeadlineSeconds <integer> may be continuously active before the system tries to terminate it; value given time. The actual number of pods running in steady state will be less false to true), the Job controller will delete all active Pods associated ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$vim myjobact.yaml
┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl delete -f myjob.yaml job.batch "my-job" deleted ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl apply -f myjobact.yaml job.batch/my-job created ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods NAME READY STATUS RESTARTS AGE my-job--1-ddhbj 0/1 ContainerCreating 0 7s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get jobs NAME COMPLETIONS DURATION AGE my-job 0/1 16s 16s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods NAME READY STATUS RESTARTS AGE my-job--1-ddhbj 0/1 Completed 0 23s my-job--1-mzw2p 0/1 ContainerCreating 0 3s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods NAME READY STATUS RESTARTS AGE my-job--1-ddhbj 0/1 Completed 0 48s my-job--1-mzw2p 0/1 Completed 0 28s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get jobs NAME COMPLETIONS DURATION AGE my-job 0/1 55s 55s
其他的一些参数
parallelism: N 一次性运行N个pod completions: M job结束需要成功运行的Pod个数,即状态为Completed的pod数 backoffLimit: N 如果job失败,则重试几次 parallelism:一次性运行几个pod,这个值不会超过completions的值。
┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl apply -f myjob-parma.yaml job.batch/my-job created ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods jobs Error from server (NotFound): pods "jobs" not found ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods job Error from server (NotFound): pods "job" not found ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get jobs NAME COMPLETIONS DURATION AGE my-job 0/6 19s 19s
┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods No resources found in liruiling-job-create namespace. ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl apply -f jobcron.yaml cronjob.batch/test-job configured ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get job NAME COMPLETIONS DURATION AGE test-job-27330246 0/1 0s 0s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods NAME READY STATUS RESTARTS AGE test-job-27330246--1-xn5r6 1/1 Running 0 4s ┌──[root@vms81.liruilongs.github.io]-[~/ansible/k8s-jobs-create] └─$kubectl get pods NAME READY STATUS RESTARTS AGE test-job-27330246--1-xn5r6 0/1 Completed 0 100s test-job-27330247--1-9blnp 0/1 Completed 0 40s