# 控制器 - Job Kuboard 正在计划中

参考文档: Jobs - Run to Completion

Kubernetes中的 Job 对象将创建一个或多个 Pod,并确保指定数量的 Pod 可以成功执行到进程正常结束:

  • 当 Job 创建的 Pod 执行成功并正常结束时,Job 将记录成功结束的 Pod 数量
  • 当成功结束的 Pod 达到指定的数量时,Job 将完成执行
  • 删除 Job 对象时,将清理掉由 Job 创建的 Pod

一个简单的例子是:创建一个 Job 对象用来确保一个 Pod 的成功执行并结束。在第一个 Pod 执行失败或者被删除(例如,节点硬件故障或机器重启)的情况下,该 Job 对象将创建一个新的 Pod 以重新执行。

当然,您也可以使用 Job 对象并行执行多个 Pod。

# 运行一个Job的例子

在下面这个 Job 的例子中,Pod 执行了一个跟 π 相关的计算,并打印出最终结果,该计算大约需要 10 秒钟执行结束。

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
1
2
3
4
5
6
7
8
9
10
11
12
13
  • 执行如下命令创建该对象:

    kubectl apply -f https://kuboard.cn/statics/learning/job/job.yaml
    
    1
  • 执行命令查看创建结果

    kubectl describe jobs/pi
    
    1

    输出结果如下

    Name:             pi
    Namespace:        default
    Selector:         controller-uid=b1db589a-2c8d-11e6-b324-0209dc45a495
    Labels:           controller-uid=b1db589a-2c8d-11e6-b324-0209dc45a495
                      job-name=pi
    Annotations:      <none>
    Parallelism:      1
    Completions:      1
    Start Time:       Tue, 07 Jun 2016 10:56:16 +0200
    Pods Statuses:    0 Running / 1 Succeeded / 0 Failed
    Pod Template:
      Labels:       controller-uid=b1db589a-2c8d-11e6-b324-0209dc45a495
                    job-name=pi
      Containers:
      pi:
        Image:      perl
        Port:
        Command:
          perl
          -Mbignum=bpi
          -wle
          print bpi(2000)
        Environment:        <none>
        Mounts:             <none>
      Volumes:              <none>
    Events:
      FirstSeen    LastSeen    Count    From            SubobjectPath    Type        Reason            Message
      ---------    --------    -----    ----            -------------    --------    ------            -------
      1m           1m          1        {job-controller }                Normal      SuccessfulCreate  Created pod: pi-dtn4q
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    • 执行以下命令可查看所有结束的 Pod
      kubectl get pods
      
      1
    • 执行以下命令可获得该 Job 所有 Pod 的名字:
      pods=$(kubectl get pods --selector=job-name=pi --output=jsonpath='{.items[*].metadata.name}')
      echo $pods
      
      1
      2
      输出结果如下
      pi-aiw0a
      
      1
      在这个命令中:
      • selector 与 Job 定义中的 selector 相同
      • --output=jsonpath 选项指定了一个表达式,该表达式从返回结果列表中的每一个 Pod 的信息中定位出 name 字段的取值
    • 执行以下命令可查看 Pod 的日志:
      kubectl logs $pods
      
      1
      输出结果如下:
      3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
      
      1