一、jobs与cronjobs介绍
1.job介绍
jobs:job负责处理任务,即仅执行一次的任务,它保证批处理任务的一个或多个Pod成功结束。
2.cronjobs介绍
CronJob在Job上加上了时间调度,CronJob是基于调度的Job执行将会自动产生多个job,调度格式参考Linux的cron系统。
二、检查本地kubernetes集群
1.检查工作节点状态
[root@k8s-master jobs]# kubectl get nodes -owide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master Ready control-plane,master 18d v1.23.1 192.168.3.201 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 containerd://1.6.6
k8s-node01 Ready <none> 18d v1.23.1 192.168.3.202 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 containerd://1.6.6
k8s-node02 Ready <none> 18d v1.23.1 192.168.3.203 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 containerd://1.6.6
2.检查系统pod状态
[root@k8s-master jobs]# kubectl get pods -A -owide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system calico-kube-controllers-7bc6547ffb-2nf66 1/1 Running 2 (91m ago) 18d 10.244.235.201 k8s-master <none> <none>
kube-system calico-node-8c4pn 1/1 Running 2 (91m ago) 18d 192.168.3.202 k8s-node01 <none> <none>
kube-system calico-node-f28qq 1/1 Running 2 (91m ago) 18d 192.168.3.203 k8s-node02 <none> <none>
kube-system calico-node-wmc2j 1/1 Running 2 (91m ago) 18d 192.168.3.201 k8s-master <none> <none>
kube-system coredns-6d8c4cb4d-6gm4x 1/1 Running 2 (91m ago) 18d 10.244.235.200 k8s-master <none> <none>
kube-system coredns-6d8c4cb4d-7vxlz 1/1 Running 2 (91m ago) 18d 10.244.235.199 k8s-master <none> <none>
kube-system etcd-k8s-master 1/1 Running 2 (91m ago) 18d 192.168.3.201 k8s-master <none> <none>
kube-system kube-apiserver-k8s-master 1/1 Running 2 (91m ago) 18d 192.168.3.201 k8s-master <none> <none>
kube-system kube-controller-manager-k8s-master 1/1 Running 2 (91m ago) 18d 192.168.3.201 k8s-master <none> <none>
kube-system kube-proxy-8dfw8 1/1 Running 2 (91m ago) 18d 192.168.3.201 k8s-master <none> <none>
kube-system kube-proxy-ghzrv 1/1 Running 2 (91m ago) 18d 192.168.3.203 k8s-node02 <none> <none>
kube-system kube-proxy-j867z 1/1 Running 2 (91m ago) 18d 192.168.3.202 k8s-node01 <none> <none>
kube-system kube-scheduler-k8s-master 1/1 Running 2 (91m ago) 18d 192.168.3.201 k8s-master <none> <none>
三、查询k8s中jobs的相关模块
1.查询查询apiVersion
[root@k8s-master jobs]# kubectl api-resources |grep job
cronjobs cj batch/v1 true CronJob
jobs batch/v1 true Job
2.查询jobs
[root@k8s-master jobs]# kubectl explain jobs --recursive |more
KIND: Job
VERSION: batch/v1
DESCRIPTION:
Job represents the configuration of a single job.
FIELDS:
apiVersion <string>
kind <string>
metadata <Object>
annotations <map[string]string>
clusterName <string>
creationTimestamp <string>
deletionGracePeriodSeconds <integer>
deletionTimestamp <string>
finalizers <[]string>
generateName <string>
generation <integer>
labels <map[string]string>
managedFields <[]Object>
apiVersion <string>
fieldsType <string>
fieldsV1 <map[string]>
manager <string>
operation <string>
subresource <string>
time <string>
name <string>
namespace <string>
ownerReferences <[]Object>
apiVersion <string>
blockOwnerDeletion <boolean>
controller <boolean>
kind <string>
name <string>
uid <string>
resourceVersion <string>
selfLink <string>
uid <string>
spec <Object>
--More--
四、k8s中使用jobs创建一次性任务
1.编辑jobs.yaml文件
[root@k8s-master jobs]# cat jobs.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: jobs-tes
namespace: default
spec:
backoffLimit: 3
activeDeadlineSeconds: 120 #限制容器运行时间
# ttlSecondsAfterFinished: 10 #任务执行完成后,10s内会删除pod
# selector:
# matchLabels:
# app: helloworld
template:
metadata:
name: hellworld
labels:
app: helloworld
spec:
restartPolicy: Never
containers:
- name: helloworld
image: busybox:1.28
command:
- /bin/sh
- -c
- "echo 'hello the world'"
2.运行jobs
kubectl apply -f jobs.yaml
3.查看jobs
[root@k8s-master jobs]# kubectl get jobs.batch jobs-tes
NAME COMPLETIONS DURATION AGE
jobs-tes 1/1 19s 40s
4.查看运行日志
[root@k8s-master jobs]# kubectl logs jobs-tes-ns64n
hello the world
5.删除jobs
[root@k8s-master jobs]# kubectl delete jobs.batch jobs-tes
job.batch "jobs-tes" deleted
五、创建cronjobs类型任务
1.编写cronjobs.yaml文件
[root@k8s-master jobs]# cat cronjobs.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob-test
namespace: default
spec:
schedule: "*/1 * * * *"
successfulJobsHistoryLimit: 3 #保留成功任务的历史记录次数
# startingDeadlineSeconds:150 #上一个任务在150s后没有完成,进行下一个任务;在150s内,检测任务完成,立即执行下一个任务
jobTemplate:
metadata:
name: helloworld
spec:
backoffLimit: 3
activeDeadlineSeconds: 120 #限制容器运行时间
# ttlSecondsAfterFinished: 10 #任务执行完成后,10s内会删除pod
# selector:
# matchLabels:
# app: helloworld
template:
metadata:
name: hellworld
labels:
app: helloworld
spec:
restartPolicy: Never
containers:
- name: helloworld
image: busybox:1.28
command:
- /bin/sh
- -c
- "echo 'hello life'"
2.创建cronjobs
[root@k8s-master jobs]# kubectl apply -f cronjobs.yaml
cronjob.batch/cronjob-test created
3.查看cronjobs状态
[root@k8s-master jobs]# kubectl get cronjobs.batch cronjob-test
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
cronjob-test */1 * * * * False 0 38s 2m32s
4.查看任务完成状态
[root@k8s-master jobs]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cronjob-test-27639747-9rbzx 0/1 Completed 0 2m21s
cronjob-test-27639748-hldht 0/1 Completed 0 81s
cronjob-test-27639749-jqbwk 0/1 Completed 0 21s
5.查看cronjobs日志
[root@k8s-master jobs]# kubectl logs cronjob-test-27639748-hldht
hello life
[root@k8s-master jobs]# kubectl logs cronjob-test-27639749-jqbwk
hello life
[root@k8s-master jobs]# kubectl logs cronjob-test-27639750-8brd4
hello life
转载:https://blog.csdn.net/jks212454/article/details/125909719
查看评论