Job就是创建一定数目的pod来完成一些work items, 当work items完成之后,Job会run to completion.
如下是job的yaml配置

apiVersion: v1
items:
- apiVersion: batch/v1
  kind: Job
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"batch/v1","kind":"Job","metadata":{"annotations":{},"name":"pi","namespace":"default"},"spec":{"backoffLimit":4,"completions":2,"parallelism":4,"template":{"spec":{"containers":[{"command":["bash","-c","echo 'scale=10000; 4*a(1)' | bc -l "],"image":"resouer/ubuntu-bc","name":"pi"}],"restartPolicy":"Never"}}}}
    creationTimestamp: "2020-06-12T08:15:18Z"
    labels:
      controller-uid: fec1e3e0-a0b4-4917-a949-cd771448f6ee
      job-name: pi
    managedFields:
    - apiVersion: batch/v1
      fieldsType: FieldsV1
      fieldsV1:
        f:status:
          f:active: {}
          f:startTime: {}
      manager: kube-controller-manager
      operation: Update
      time: "2020-06-12T08:15:18Z"
    - apiVersion: batch/v1
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            .: {}
            f:kubectl.kubernetes.io/last-applied-configuration: {}
        f:spec:
          f:backoffLimit: {}
          f:completions: {}
          f:parallelism: {}
          f:template:
            f:spec:
              f:containers:
                k:{"name":"pi"}:
                  .: {}
                  f:command: {}
                  f:image: {}
                  f:imagePullPolicy: {}
                  f:name: {}
                  f:resources: {}
                  f:terminationMessagePath: {}
                  f:terminationMessagePolicy: {}
              f:dnsPolicy: {}
              f:restartPolicy: {}
              f:schedulerName: {}
              f:securityContext: {}
              f:terminationGracePeriodSeconds: {}
      manager: kubectl
      operation: Update
      time: "2020-06-12T08:15:18Z"
    name: pi
    namespace: default
    resourceVersion: "6426484"
    selfLink: /apis/batch/v1/namespaces/default/jobs/pi
    uid: fec1e3e0-a0b4-4917-a949-cd771448f6ee
  spec:
    backoffLimit: 4
    completions: 2
    parallelism: 4
    selector:
      matchLabels:
        controller-uid: fec1e3e0-a0b4-4917-a949-cd771448f6ee
    template:
      metadata:
        creationTimestamp: null
        labels:
          controller-uid: fec1e3e0-a0b4-4917-a949-cd771448f6ee
          job-name: pi
      spec:
        containers:
        - command:
          - bash
          - -c
          - 'echo ''scale=10000; 4*a(1)'' | bc -l '
          image: resouer/ubuntu-bc
          imagePullPolicy: Always
          name: pi
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
        dnsPolicy: ClusterFirst
        restartPolicy: Never
        schedulerName: default-scheduler
        securityContext: {}
        terminationGracePeriodSeconds: 30
  status:
    active: 2
    startTime: "2020-06-12T08:15:18Z"
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

Job的定义如上,其中比较重要的field有

  • .spec.template -- 如开头所述,Job其实是创建多个Pod来执行work items。这里的template就是pod 定义的template。
  • .spec.completions -- 它定义的是 Job 至少要完成的 Pod 数目,即 Job 的最小完成数
  • .spec.parallelism -- 它定义的是一个 Job 在任意时间最多可以启动多少个 Pod 同时运行

parallelism比较好理解,也就是Job的并行Pod数目。那么completion应该怎么理解呢?按如下进行试验

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: resouer/ubuntu-bc
        command: [ "bash", "-c", "echo 'scale=10000; 4*a(1)' | bc -l " ]
      restartPolicy: Never
  backoffLimit: 4
  completions: 4
  parallelism: 2

当通过kubectl describe job pi查看job状态,可以看到有2个Pod处在running state,即parallelism定义的并行数目。

Name:           pi
Namespace:      default
Selector:       controller-uid=e499f959-2119-418b-9632-e50eb35a3018
Labels:         controller-uid=e499f959-2119-418b-9632-e50eb35a3018
                job-name=pi
Annotations:    Parallelism:  2
Completions:    4
Start Time:     Fri, 12 Jun 2020 04:43:35 -0400
Pods Statuses:  2 Running / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  controller-uid=e499f959-2119-418b-9632-e50eb35a3018
           job-name=pi
  Containers:
   pi:
    Image:      resouer/ubuntu-bc
    Port:       <none>
    Host Port:  <none>
    Command:
      bash
      -c
      echo 'scale=10000; 4*a(1)' | bc -l
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  22s   job-controller  Created pod: pi-dqh7d
  Normal  SuccessfulCreate  22s   job-controller  Created pod: pi-tsd7q

通过kubectl get pods, 可以看到有两个pi pod处在running的状态,同上面的Job state相对应。

[dylanz@ ~/codeDir/kubernetes]$kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
dns-test   1/1     Running   0          8d
pi-dqh7d   1/1     Running   0          2m37s
pi-tsd7q   1/1     Running   0          2m37s

等待一段时间之后,通过kubectl get pods, 可以看到前面run的pod已经completed,又有两个Pod run起来。

[dylanz@ ~/codeDir/kubernetes]$kubectl get pods
NAME       READY   STATUS      RESTARTS   AGE
dns-test   1/1     Running     0          8d
pi-d2grj   1/1     Running     0          69s
pi-dqh7d   0/1     Completed   0          3m50s
pi-tsd7q   0/1     Completed   0          3m50s
pi-xg6pw   1/1     Running     0          65s
web-0      1/1     Running     0          8d
web-1      1/1     Running     0          8d

至此,可以得出,Job开始会创建parallelism个pod。当pod任务完成之后,kubernetes会继续创建parallelism个pod直到completions.


龙内cool
0 声望0 粉丝