Job
-
For non-durable tasks, such as compressing files, after the task is completed, the pod needs to end running, and the pod does not need to remain in the system. Job is used at this time.
-
Job is responsible for batch processing short lived one-off tasks (short lived one-off tasks), that is, tasks that are executed only once, and it guarantees that one or more Pods of the batch processing tasks end successfully
yaml file
Execute tasks multiple times
A job task that executes 10 times with a concurrency of 1 outputs hello in the container
apiVersion: batch/v1 #版本
kind: Job #类型
metadata:
name: busybox-job #名称
spec:
completions: 10 # 执行job的次数
parallelism: 1 # 执行job的并发数
template:
metadata:
name: busybox-job-pod #pod名
spec:
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: ["echo", "hello"]
restartPolicy: Never #重启策略
one-time task
If completions are not specified, the default is 1, which is a one-time task
The task will execute the sh command to write the mysql host, database name and password into the container volume
apiVersion: batch/v1 #版本
kind: Job #类型
metadata:
name: mysql-dump
spec:
template:
metadata:
name: mysql-dump
spec:
nodeName: k8s-master2 #部署到指定节点
containers:
- name: mysql-dump
image: mysql:5.7
command: ["/bin/sh","-c","mysqldump --host=mysql-test -uroot -pabc123 --databases mysql > /root/mysql2022.sql"]
volumeMounts:
- mountPath: "/root"
name: mysql-data
restartPolicy: Never
volumes:
- name: mysql-data
hostPath:
path: /opt/mysqldump
CronJob
Run scheduled tasks periodically in k8s, the same as crontab in linux
Note: The execution time of CronJob is the time of controller-manager, so make sure that the time of controller-manager is accurate
cron expression
Format
┌───────────── 分钟 (0 - 59)
│ ┌───────────── 小时 (0 - 23)
│ │ ┌───────────── 月的某天 (1 - 31)
│ │ │ ┌───────────── 月份 (1 - 12)
│ │ │ │ ┌───────────── 周的某天 (0 - 6)(周日到周一;在某些系统上,7 也是星期日)
│ │ │ │ │ 或者是 sun,mon,tue,web,thu,fri,sat
│ │ │ │ │
│ │ │ │ │
* * * * *
*
represent all
?
Denotes that the location is not used
/
followed by how often
-
Represents before and after periods
#
represent the number
common cron
0 0 0 * * ?:每天的零点整执行任务。
0 0 */2 * * ?:每隔2小时执行一次任务。
0 0 12 * * ?:每天中午12点执行任务。
0 15 10 * * ?:每天上午10点15分执行任务。
0 0 6,18 * * ?:每天的早上6点和晚上6点执行任务。
0 0/30 8-18 * * ?:每天的上午8点到下午6点之间,每隔30分钟执行一次任务。
0 0 0 1 1 ?:每年的1月1日零点整执行任务。
0 0 0 * * 2:每周的星期二零点整执行任务。
0 0 0 ? * 6#3:每月的第三个星期六零点整执行任务。
0 0 0 L * ?:每个月的最后一天零点整执行任务。
ymal file
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
concurrencyPolicy: Allow # 并发调度策略:Allow 允许并发调度,Forbid:不允许并发执行,Replace:如果之前的任务还没执行完,就直接执行新的,放弃上一个任务
failedJobsHistoryLimit: 1 # 保留多少个失败的任务
successfulJobHistoryLimit: 3 # 保留多少个成功的任务
suspend: false # 是否挂起任务,若为 true 则该任务不会执行
# startingDeadlineSeconds: 30 # 间隔多长时间检测失败的任务并重新执行,时间不能小于 10
schedule: "* * * * *" # 调度策略
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure