本文主要是介绍Tekton 基于 cronjob 触发流水线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Tekton 基于 cronjob 触发流水线
Tekton EventListener 在8080端口监听事件,kubernetes 原生 cronjob 定时通过curl 命令向 EventListener 发送事件请求,触发tekton流水线执行,实现定时运行tekton pipeline
任务。
前置要求:
- kubernetes集群中已部署
tekton pipeline
、tekton triggers
以及tekton dashboard
三个组件;
创建serviceaccount
官方示例:https://github.com/tektoncd/triggers/blob/main/examples/rbac.yaml
因为 EventListener 最终需要创建 taskrun、pipelinerun 同时会查询一些其他信息,因此需要为其配置一个 serviceAccount,同时还需要为这个 serviceAccount 赋予相应的权限。
$ cat serviceAccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:name: tekton-triggers-example-sa
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:name: triggers-example-eventlistener-binding
subjects:
- kind: ServiceAccountname: tekton-triggers-example-sa
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: tekton-triggers-eventlistener-roles
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: triggers-example-eventlistener-clusterbinding
subjects:
- kind: ServiceAccountname: tekton-triggers-example-sanamespace: default
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: tekton-triggers-eventlistener-clusterroles
应用yaml
kubectl apply -f serviceAccount.yaml
创建EventListener
EventListener 是一个 Kubernetes 对象,用于侦听 Kubernetes 集群上指定端口上的事件。它公开了一个可寻址接收器,用于接收传入事件并指定一个或多个 Triggers 。
$ cat eventListener.yaml
apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:name: hello-listener
spec:serviceAccountName: tekton-triggers-example-satriggers:- name: hello-triggerbindings:- ref: hello-bindingtemplate:ref: hello-template
应用yaml
kubectl apply -f eventListener.yaml
创建TriggerBinding
TriggerBinding 允许您从事件负载中提取字段并将它们绑定到命名参数,然后可以在 TriggerTemplate 中使用这些命名参数。
$ cat triggerBinding.yaml
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:name: hello-binding
spec:params:- name: messagevalue: Hello from the Triggers EventListener!
应用yaml
kubectl apply -f triggerBinding.yaml
创建TriggerTemplate
TriggerTemplate 是指定资源蓝图的资源,例如 TaskRun 或 PipelineRun ,当您的EventListener 检测到事件。它公开了您可以在资源模板中的任何位置使用的参数。
以下示例通过pipelineSpec
和taskSpec
直接引入task并运行:
$ cat triggerTemplate.yaml
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:name: hello-template
spec:resourcetemplates:- apiVersion: tekton.dev/v1beta1kind: PipelineRunmetadata:generateName: hello-cron-spec:pipelineSpec:tasks:- name: task01taskSpec:steps:- name: step01image: alpinescript: |echo step01- name: step02image: alpinescript: |echo step02- name: task02taskSpec:steps:- name: step01image: alpinescript: |echo step01- name: step02image: alpinescript: |echo step02runAfter:- task01
应用yaml
kubectl apply -f triggerTemplate.yaml
创建cronjob
查看EventListener service地址,并记录该地址
root@kube001:~# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
el-hello-listener ClusterIP 10.96.1.167 <none> 8080/TCP,9000/TCP 14m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 91d
或使用tkn CLI命令查看
root@kube001:~# tkn eventlisteners list
NAME AGE URL AVAILABLE
hello-listener 14 minutes ago http://el-hello-listener.default.svc.cluster.local:8080 True
每3分钟触发一次pipeline任务,注意修改curl地址:
# cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:name: hello
spec:schedule: "*/3 * * * *"successfulJobsHistoryLimit: 0failedJobsHistoryLimit: 0jobTemplate:spec:template:spec:restartPolicy: Nevercontainers:- name: helloimage: curlimages/curlenv:- name: MESSAGEvalue: helloimagePullPolicy: IfNotPresentargs: ["curl", "-X", "POST", "--data",'{"message": "$(MESSAGE)"}',"el-hello-listener.default.svc.cluster.local:8080"]
应用yaml
kubectl apply -f cronjob.yaml
完整示例
$ cat cronjob_trigger.yaml
---
apiVersion: v1
kind: ServiceAccount
metadata:name: tekton-triggers-example-sa
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:name: triggers-example-eventlistener-binding
subjects:
- kind: ServiceAccountname: tekton-triggers-example-sa
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: tekton-triggers-eventlistener-roles
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: triggers-example-eventlistener-clusterbinding
subjects:
- kind: ServiceAccountname: tekton-triggers-example-sanamespace: default
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: tekton-triggers-eventlistener-clusterroles---
apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:name: hello-listener
spec:serviceAccountName: tekton-triggers-example-satriggers:- name: hello-triggerbindings:- ref: hello-bindingtemplate:ref: hello-template---
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:name: hello-binding
spec:params:- name: messagevalue: Hello from the Triggers EventListener!---
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:name: hello-template
spec:resourcetemplates:- apiVersion: tekton.dev/v1beta1kind: PipelineRunmetadata:generateName: hello-cron-spec:pipelineSpec:tasks:- name: task01taskSpec:steps:- name: step01image: alpinescript: |echo step01- name: step02image: alpinescript: |echo step02- name: task02taskSpec:steps:- name: step01image: alpinescript: |echo step01- name: step02image: alpinescript: |echo step02runAfter:- task01---
apiVersion: batch/v1
kind: CronJob
metadata:name: hello
spec:schedule: "*/3 * * * *"successfulJobsHistoryLimit: 0failedJobsHistoryLimit: 0jobTemplate:spec:template:spec:restartPolicy: Nevercontainers:- name: helloimage: curlimages/curlenv:- name: MESSAGEvalue: helloimagePullPolicy: IfNotPresentargs: ["curl", "-X", "POST", "--data",'{"message": "$(MESSAGE)"}',"el-hello-listener.default.svc.cluster.local:8080"]
应用以上配置
kubectl apply -f cronjob_trigger.yaml
查看定时任务
查看触发的pipeline任务
root@kube001:~# kubectl get pipelineruns | grep hello-cron
hello-cron-d6k2n True Succeeded 10m 9m44s
hello-cron-gcfpm True Succeeded 4m4s 3m44s
hello-cron-j4nk6 True Succeeded 63s 38s
查看运行完成的pods
root@kube001:~# kubectl get pods | grep hello-cron
hello-cron-9jwwp-task01-pod 0/2 Completed 0 7m42s
hello-cron-9jwwp-task02-pod 0/2 Completed 0 7m33s
hello-cron-d6k2n-task01-pod 0/2 Completed 0 10m
hello-cron-d6k2n-task02-pod 0/2 Completed 0 10m
hello-cron-gcfpm-task01-pod 0/2 Completed 0 4m42s
hello-cron-gcfpm-task02-pod 0/2 Completed 0 4m32s
hello-cron-j4nk6-task01-pod 0/2 Completed 0 101s
hello-cron-j4nk6-task02-pod 0/2 Completed 0 91s
访问dashboard查看自动执行的pipelinerun
任务详情
这篇关于Tekton 基于 cronjob 触发流水线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!