站点图标 IDC铺

Kubernetes之Job控制器

Job控制器介绍

Job控制器用于Pod对象运行一次性任务,容器中的进程在正常运行结束后不会对其进行重启,而是将Pod对象置于”Completed”(完成)状态,若容器中的进程因错误而终止,则需要按照重启策略配置确定是否重启,未运行完成的Pod对象因其所在的节点故障而意外终止后会被调度。
Job控制器的Pod对象的状态转换如下图所示:

创建Job对象

Job控制器的spec字段内嵌的必要字段只有template,不需要定义标签选择器,控制器会自动关联,除了这一点与Deployment控制器不同,其它别无二致。

1.创建Job控制器配置清单
使用busybox镜像,然后沉睡120s,完成后即正常退出容器

cat busybox-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: busybox-job
spec:
  template:
    spec:
      containers:
      - name: busybox
        image: busybox:latest
        command: [ "/bin/sh", "-c", "sleep 120s" ]
      restartPolicy: Never

Pod模版中的spec.restartPolicy默认为”Always”,这对Job控制器来说非常不适用,”Never”和”OnFeailure”才比较合适Job控制器

2.创建Job控制器

kubectl apply -f busybox-job.yaml

3.查看Job控制器及Pod状态

kubectl get job -o wide
NAME          COMPLETIONS   DURATION   AGE   CONTAINERS   IMAGES           SELECTOR
busybox-job   0/1           36s        36s   busybox      busybox:latest   controller-uid=8e85200f-43eb-4f24-ab6d-64c545287d51

kubectl get pods -o wide | grep busybox
busybox-job-wtdvf                                1/1     Running   0          45s    10.244.3.150   k8s-node01   <none>           <none>

120s后,Job控制器创建的Pod对象完成了任务

kubectl get pods -o wide | grep busybox
busybox-job-wtdvf                                0/1     Completed   0          3m38s   10.244.3.150   k8s-node01   <none>           <none>

查看Job控制器的详细信息
如下SelectorLables都是Job控制器自动生成后自动关联,控制器自动生成的controller-uid-随机字符串,控制器携带了后面的字符串是为了防止所管理的Pod发生重合。
下面可以看到Job运行成功后及完成了操作并没有进程重启,这得助于我们设置的restartPolicy

 

串行式Job

将并行度属性job.spec.parallelism的值设置为1,并设置总任务数job.spec.completions属性便能够让Job控制器以串行方式运行多任务,下面是一个需要串行5此任务的Job控制器示例:

cat busybox-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: busybox-job
spec:
  parallelism: 1
  completions: 5
  template:
    spec:
      containers:
      - name: busybox
        image: busybox:latest
        command: [ "/bin/sh", "-c", "sleep 20s" ]
      restartPolicy: OnFailure

创建Job控制器

kubectl apply -f busybox-job.yaml

动态监控Pod对象作业的变化

kubectl get pods -l job-name=busybox-job --watch

NAME                READY   STATUS    RESTARTS   AGE
busybox-job-q8wqr   0/1     Pending   0          0s
busybox-job-q8wqr   0/1     Pending   0          0s
busybox-job-q8wqr   0/1     ContainerCreating   0          0s
busybox-job-q8wqr   1/1     Running             0          20s
busybox-job-q8wqr   0/1     Completed           0          39s
busybox-job-lppcw   0/1     Pending             0          0s
busybox-job-lppcw   0/1     Pending             0          0s
busybox-job-lppcw   0/1     ContainerCreating   0          0s
busybox-job-lppcw   1/1     Running             0          19s
busybox-job-lppcw   0/1     Completed           0          39s
busybox-job-8jw2q   0/1     Pending             0          0s
busybox-job-8jw2q   0/1     Pending             0          0s
busybox-job-8jw2q   0/1     ContainerCreating   0          0s
busybox-job-8jw2q   1/1     Running             0          19s
busybox-job-8jw2q   0/1     Completed           0          40s
busybox-job-bcxpn   0/1     Pending             0          0s
busybox-job-bcxpn   0/1     Pending             0          0s
busybox-job-bcxpn   0/1     ContainerCreating   0          0s
busybox-job-bcxpn   1/1     Running             0          18s
busybox-job-bcxpn   0/1     Completed           0          38s
busybox-job-5t7xm   0/1     Pending             0          0s
busybox-job-5t7xm   0/1     Pending             0          0s
busybox-job-5t7xm   0/1     ContainerCreating   0          0s
busybox-job-5t7xm   1/1     Running             0          20s
busybox-job-5t7xm   0/1     Completed           0          41s

如上,Job控制器需要执行五次任务,每次一个Pod执行一个任务,依次执行,执行成功后的Pod即为完成状态

kubectl get pods -l job-name=busybox-job
NAME                READY   STATUS      RESTARTS   AGE
busybox-job-5t7xm   0/1     Completed   0          4m22s
busybox-job-8jw2q   0/1     Completed   0          5m40s
busybox-job-bcxpn   0/1     Completed   0          5m
busybox-job-lppcw   0/1     Completed   0          6m19s
busybox-job-q8wqr   0/1     Completed   0          6m58s

并行式Job

并行式Job我们只需要修改job.spec.parallelism属性与job.spec.completions属性即可;
job.spec.parallelism属性表示了每次启动多少队列执行作业(即为Pod数量)
job.spec.completions属性表示了作业的总数量

如下示例一个5个作业,同时启动5个队列进行作业。

cat busybox-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: busybox-job
spec:
  parallelism: 5
  completions: 5
  template:
    spec:
      containers:
      - name: busybox
        image: busybox:latest
        command: [ "/bin/sh", "-c", "sleep 20s" ]
      restartPolicy: OnFailure
kubectl apply -f busybox-job.yaml

查看Job控制器运行状态,如下Job控制器中的Pod对象创建时间是一致的。

删除Job

Job控制器中的Pod运行完成后,将不再占用系统资源,用户可以按照需求保留或使用资源删除命令将Pod删除,不过如果某控制器的容器应用总是无法正常结束运行,而其restartPolicy又设置为了重启,则它可能会一直处于不停地重启和错误的循环当中。所幸的是,Job控制器提供了两个属性用于抑制这种情况的发生,具体如下:

例如,下面的配置清单为,表示其失败重试次数为5此,并且如果超出100秒的时间仍然未运行完成,那么则将其终止:

cat busybox-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: busybox-job
spec:
  backoffLimit: 5
  activeDeadlineSeconds: 100
  parallelism: 1
  completions: 5
  template:
    spec:
      containers:
      - name: busybox
        image: busybox:latest
        command: [ "/bin/sh", "-c", "sleep 30s" ]
      restartPolicy: OnFailure
退出移动版