23、K8S-控制器之Job

Kubernetes学习目录

1、基础知识

1.1、场景

在我们日常的工作中,经常会遇到临时执行一个动作,但是这个动作必须在某个时间点执行才可以,而我们又
不想一直这么傻傻的等待,即使等待到了,由于特殊原因,我们执行的时候,已经不是准确的时间点了。
针对于这种场景,我们一般使用job的方式来帮助我们定制化的完成任务。

1.2、分类

在k8s场景中,关于job的执行,主要有两种类型:串行job、并行job。
串行job:即所有的job任务都在上一个job执行完毕后,再开始执行
并行job:如果存在多个job,我们可以设定并行执行的job数量。

1.3、资源清单解析

apiVersion: batch/v1 # API群组及版本
kind: Job            # 资源类型特有标识
metadata:
  name <string>      # 资源名称,在作用域中要唯一
  namespace <string> # 名称空间;Job资源隶属名称空间级别
spec:
  selector <object>     # 标签选择器,必须匹配template字段中Pod模板中的标签
  template <object>     # Pod模板对象
  completions <integer> # 期望的成功完成的作业次数,成功运行结束的Pod数量
  ttlSecondsAfterFinished <integer> # 终止状态作业的生存时长,超期将被删除
  parallelism <integer>             # 作业的最大并行度,默认为1
  backoffLimit <integer>            # 将作业标记为Failed之前的重试次数,默认为6
  activeDeadlineSeconds <integer>    # 作业启动后可处于活动状态的时长

2、实践1 - 单个任务

2.1、定义资源配置清单 

cat >job-single.yml<<'EOF'
apiVersion: batch/v1
kind: Job
metadata:
  name: job-single
spec:
  template:
    metadata:
      name: job-single
    spec:
      restartPolicy: Never
      containers:
      - name: job-single
        image: 192.168.10.33:80/k8s/busybox:latest
        command: [ "/bin/sh", "-c", "for i in 9 8 7 6 5 4 3 2 1; do echo $i; sleep 2; done" ]
EOF

属性解析:
对于job来说,他的重启策略只有两种:仅支持Never和OnFailure两种,不支持Always,否则的话就成死循环了。

2.2、应用资源清单

master1 ]# kubectl apply -f job-single.yml 
job.batch/job-single created

master1 ]# kubectl get pods
NAME               READY   STATUS    RESTARTS   AGE
job-single-s8rfs   1/1     Running   0          2s

master1 ]# kubectl logs job-single-s8rfs --timestamps
2023-03-21T23:13:58.857401271+08:00 9
2023-03-21T23:14:00.857756611+08:00 8
2023-03-21T23:14:02.858705956+08:00 7
2023-03-21T23:14:04.859418774+08:00 6
2023-03-21T23:14:06.859870776+08:00 5
2023-03-21T23:14:08.860462764+08:00 4
2023-03-21T23:14:10.860692201+08:00 3
2023-03-21T23:14:12.861133843+08:00 2
2023-03-21T23:14:14.861628754+08:00 1

# 执行完成的状态
master1 ]# kubectl get pods
NAME               READY   STATUS      RESTARTS   AGE
job-single-s8rfs   0/1     Completed   0          2m16s

3、实践2 - 多个串行任务

3.1、定义资源配置清单 

cat >job-multi-one-task.yml<<'EOF'
apiVersion: batch/v1
kind: Job
metadata:
  name: job-multi-one-task
spec:
  completions: 5
  parallelism: 1
  template:
    spec:
      containers:
      - name: job-multi
        image: 192.168.10.33:80/k8s/busybox:latest
        command: ["/bin/sh","-c","echo job; sleep 3"]
      restartPolicy: OnFailure
EOF

3.2、应用资源清单

master1 ]# kubectl apply -f job-multi-one-task.yml 

master1 ]# kubectl get pods
NAME                       READY   STATUS      RESTARTS   AGE
job-multi-one-task-cdcfn   0/1     Completed   0          13s
job-multi-one-task-f6pxp   0/1     Completed   0          27s
job-multi-one-task-nglhw   0/1     Completed   0          41s
job-multi-one-task-psj6z   0/1     Completed   0          20s
job-multi-one-task-vzbl4   0/1     Completed   0          34s


# 安装完成的时间排序
job_list=$(kubectl get pods | grep job | sort -k 5 | awk '{print $1}')

# 查询日志的时间
master1 ]# for i in $job_list;do kubectl logs $i --timestamps=true; done
2023-03-21T23:20:26.125247993+08:00 job
2023-03-21T23:20:19.135072499+08:00 job
2023-03-21T23:20:12.107366037+08:00 job
2023-03-21T23:20:04.957326877+08:00 job
2023-03-21T23:20:33.146876609+08:00 job

结果显示: 这些任务,确实是串行的方式来执行,由于涉及到任务本身是启动和删除,所以时间间隔要大于3s

4、实践3 - 并行任务

4.1、定义资源配置清单 

cat >job-multi-bing.yml<<'EOF'
apiVersion: batch/v1
kind: Job
metadata:
  name: job-multi-bing
spec:
  completions: 6
  parallelism: 2
  template:
    spec:
      containers:
      - name: job-multi-bing
        image: 192.168.10.33:80/k8s/busybox:latest
        command: ["/bin/sh","-c","echo job; sleep 3"]
      restartPolicy: OnFailure
EOF

4.2、应用资源清单

master1 ]# kubectl apply  -f job-multi-bing.yml 
job.batch/job-multi-bing created

master1 ]# kubectl get jobs.batch 
NAME             COMPLETIONS   DURATION   AGE
job-multi-bing   0/6           5s         5s

master1 ]# kubectl get pods
NAME                   READY   STATUS      RESTARTS   AGE
job-multi-bing-22vf6   0/1     Completed   0          22s
job-multi-bing-5kqww   0/1     Completed   0          36s
job-multi-bing-fphn5   0/1     Completed   0          29s
job-multi-bing-pn6tg   0/1     Completed   0          36s
job-multi-bing-pv8z7   0/1     Completed   0          29s
job-multi-bing-xvsh4   0/1     Completed   0          22s

master1 ]# job_list=$(kubectl get pods | grep job | sort -k 5 | awk '{print $1}')
master1 ]# for i in $job_list;do kubectl logs $i --timestamps=true; done
2023-03-21T23:28:30.111105399+08:00 job
2023-03-21T23:28:30.156575615+08:00 job
2023-03-21T23:28:23.114241589+08:00 job
2023-03-21T23:28:23.077039456+08:00 job
2023-03-21T23:28:15.973194391+08:00 job
2023-03-21T23:28:16.029781746+08:00 job

结果显示:
 这6条任务确实是两两并行执行的

 

posted @ 2023-03-21 23:31  小粉优化大师  阅读(210)  评论(0编辑  收藏  举报