Batch Jobs

Jobs

 一个job创建一个或者更多的pods并确保指定数量的pods成功终止。当所有的pods成功结束,job会追踪这个过程。当成功结束数量达到指定数量,job本身结束。删除一个Job会清理由它创建的所有pods

一个简单的案例就是为了可靠地运行一个Pod知道结束创建一个Job对象。如果先前的Pod出故障或者被删除,Job对象将会开启一个新的Pod

一个Pod可以被用于并行运行多个pods


从版本1.5开始extensions/v1beta1.Job 被废弃了,计划在1.6删除。请使用batch/v1.Job代替


job.yaml

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    metadata:
      name: pi
    spec:
      containers:
      - name: pi
        p_w_picpath: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
# kubectl create -f job.yaml 
job "pi" created
# kubectl describe jobs/pi
Name:		pi
Namespace:	default
Image(s):	perl
Selector:	controller-uid=ababe7e0-e788-11e6-bf7b-0050568f7dea
Parallelism:	1
Completions:	1
Start Time:	Tue, 31 Jan 2017 15:41:26 +0800
Labels:		controller-uid=ababe7e0-e788-11e6-bf7b-0050568f7dea,job-name=pi
Pods Statuses:	0 Running / 1 Succeeded / 0 Failed
No volumes.
Events:
  FirstSeen	LastSeen	Count	From			SubobjectPath	TypReason			Message
  ---------	--------	-----	----			-------------	--------	------			-------
  2m		2m		1	{job-controller }			Normal		SuccessfulCreate	Created pod: pi-w94ls






# pods=$(kubectl get pods --selector=job-name=pi --output=jsonpath={.items..metadata.name})
# echo $pods
pi-w94ls


# kubectl logs $pods
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275898



Writing a Job Spec

像Kubernetes所有其他配置,Job配置需要apiVersion,kind和metadata字段。一个Job也需要.spec部分

.spec.template 是.spec唯一需要的字段


Parallel Jobs

有三种主要类型的Jobs:

  1. 非并行Jobs

    通常只需要启动一个pod,除非这个pod出故障

    一旦pod成功终止job就运行结束

  2. Parallel Jobs with a fixed completion count:

    specify a none-zero positive value for .spec.completions

    the job is complete when there is one successful pod for each value in the range 1 to .spec.completions

    not implemented yet: each pod passed a different index in the range 1 to .spec.completions

  3. 带有工作队列的并行Jobs



Controlling Parallelism


Handing Pod and Containers Failures


Job Termination and Cleanup




Parallel Processing using Expansions



Coarse Parallel Processing using a Work Queue












参考文档:

https://kubernetes.io/docs/user-guide/jobs/