
在k8s上跑个一次性任务或者定时任务
不知道怎么写yaml 查看帮助
root@guoguo-M5-Pro:~# kubectl create job -h
Create a job with the specified name.
Examples:
# Create a job
kubectl create job my-job --image=busybox
#最简单的例子 没有写任务 创建一个job
# Create a job with command
kubectl create job my-job --image=busybox -- date
#这是写了个 date显示时间的任务
# Create a job from a CronJob named "a-cronjob"
kubectl create job test-job --from=cronjob/a-cronjob
Options:
--allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
the template. Only applies to golang and jsonpath output formats.
--dry-run='none': Must be "none", "server", or "client". If client strategy, only print the object that would be
sent, without sending it. If server strategy, submit server-side request without persisting the resource.
--field-manager='kubectl-create': Name of the manager used to track field ownership.
--from='': The name of the resource to create a Job from (only cronjob is supported).
--image='': Image name to run.
-o, --output='': Output format. One of:
json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
--save-config=false: If true, the configuration of current object will be saved in its annotation. Otherwise, the
annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
--show-managed-fields=false: If true, keep the managedFields when printing objects in JSON or YAML format.
--template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
template format is golang templates [http:///pkg/text/template/#pkg-overview].
--validate=true: If true, use a schema to validate the input before sending it
Usage:
kubectl create job NAME --image=image [--from=cronjob/name] -- [COMMAND] [args...] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).查看他的yaml格式
kubectl create job -n jsh my-job --image=/apps/nginx:1.20.2 --dry-run -o yaml -- date
W0729 14:54:53.746401 122482 helpers.go:557] --dry-run is deprecated and can be replaced with --dry-run=client.
#--dry-run 是测试不运行
#-o yaml 以yaml格式输出
#-- date 这个才是执行的任务 显示时间apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: my-job
namespace: jsh
spec:
template:
metadata:
creationTimestamp: null
spec:
containers:
- command:
- date #执行的任务
image: /apps/nginx:1.20.2
name: my-job
resources: {}
restartPolicy: Never
status: {}这个就是一次性任务 执行完就退出了!
或者这样的 这个是自己写的
root@guoguo-M5-Pro:~# cat hello_world-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-hello-world-test
namespace: jsh
spec:
template:
metadata:
spec:
containers:
- name: hello
image: /apps/nginx:1.20.2
command: ["echo","hello,world!"]
restartPolicy: Never #pod重启策略 Never 不重启 和 OnFailure 有错误 返回非0 就重启
#比如你执行的任务 需要联网 网络不好 需要大文件操作 第一次没成功 可能需要多次查看一下他执行了没有
root@guoguo-M5-Pro:~# kubectl logs -n jsh job-hello-world-test-t52mr
hello,world!Linux有crontab为什么还要使用这种任务,他的场景是,它可以访问svc的内部资源,内部域名,外面是解析不了的
如果需要并发 多个pod去泡
apiVersion: batch/v1
kind: Job
metadata:
name: job-curl
namespace: jsh
spec:
completions: 6 #此Job 完成pod的总数量
parallelism: 2 #每次并发跑两个
metadata:
template:
spec:
containers:
- name: curl
image: /apps/nginx:1.20.2
command:
- "/bin/bash"
- "-c"
- "curl jsh-backend-svc && echo hello world"
restartPolicy: OnFailure #只有俩选择 要么Never 不重启 或者OnFailre 非0退出就重启root@guoguo-M5-Pro:~# kubectl get pods -n jsh
NAME READY STATUS RESTARTS AGE
job-curl-55f4r 0/1 Completed 0 110s
job-curl-phw9q 0/1 Completed 0 111s
job-curl-vw4wp 0/1 Completed 0 113s
job-curl-wjm47 0/1 Completed 0 113s
job-curl-wldts 0/1 Completed 0 111s
job-curl-zskxl 0/1 Completed 0 110s
jsh-backend-dep-7c74848f7c-kjnj9 1/1 Running 0 4h43m
jsh-backend-dep-7c74848f7c-rm72h 1/1 Running 0 4h43m
jsh-front-dep-9fb8f688c-9bpqg 1/1 Running 0 19h
root@guoguo-M5-Pro:~# kubectl logs -f -n jsh job-curl-55f4r
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
hello worldjob就这些内容,可能做一些流水线可能需要用到job
下面是定时任务,用的就比较多了
类似于crontab
crojob
crojob是分布式的,启动的job可以跑在集群上的任何一个节点上去
root@guoguo-M5-Pro:~# cat echo-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: echo-cronjob
namespace: jsh
spec:
schedule: "* * * * *" #计划任务 分时日月周
jobTemplate:
spec:
template:
spec:
containers:
- name: cronjob
image: /apps/nginx:1.20.2
command: ["/bin/bash","-c","echo hello world && date"]
restartPolicy: OnFailure #OnFailre 非0退出就重启 或者 Never 不重启 就这俩选择
^Croot@guoguo-M5-Pro:~# kubectl get pods -n jsh -w
NAME READY STATUS RESTARTS AGE
echo-cronjob-28704043-kcxrn 0/1 Completed 0 2m49s
echo-cronjob-28704044-cnktl 0/1 Completed 0 109s
echo-cronjob-28704045-c6hc2 0/1 Completed 0 49s
jsh-backend-dep-7c74848f7c-kjnj9 1/1 Running 0 5h27m
jsh-backend-dep-7c74848f7c-rm72h 1/1 Running 0 5h27m
jsh-front-dep-9fb8f688c-9bpqg 1/1 Running 0 20h
^Croot@guoguo-M5-Pro:~# kubectl logs -n jsh echo-cronjob-28704043-kcxrn
hello world
Mon Jul 29 16:43:00 CST 2024
root@guoguo-M5-Pro:~/count_script# kubectl get cronjobs.batch -n jsh
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
echo-cronjob * * * * * False 0 16s 5h14mcronjob的使用场景
比如我部署了一个Elasticsearch服务在k8s上,他是有个service的域名的,然后我需要每天去清理5天前的index索引
就可以写个curl 的清理命令 清理es 调用cronjob 定时任务执行
这是其中一个场景,比如还有备份数据库的任务,集群服务的检查都可以
















