本文为作者原创文章,为尊重作者劳动成果禁止非授权转载,若需转载请在【全栈工程师修炼指南】公众号留言,或者发送邮件到 [master@weiyigeek.top] 中我将及时回复。
0x02 简单使用
1.tekton 最小单位 Task 任务创建运行
描述: Task在API中表示为一种Task对象,它定义了一系列按顺序运行的步骤,以执行Task所需的逻辑。每个Task都作为一个pod在Kubernetes集群上运行,每个步骤都在自己的容器中运行。
Step 1.创建一个最简单的Task资源清单, 执行输出一段字符串。
代码语言:javascript复制# create a Task
tee Hello-World-Task.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello
spec:
steps:
- name: echo
image: alpine:3.18
script: |
#!/bin/sh
echo "Hello World, Tekton!"
EOF
# create a TaskRun
tee Hello-World-TaskRun.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
name: hello-task-run
spec:
taskRef:
name: hello
EOF
Step 2.创建运行Task 并查看运行的 TaskRun 任务。
代码语言:javascript复制# 部署 Task & TaskRun
kubectl apply --filename Hello-World-Task.yaml
# task.tekton.dev/hello createdkubectl apply --filename Hello-World-TaskRun.yaml
# taskrun.tekton.dev/hello-task-run created
# 查看部署的 TaskRun 任务与Pod
kubectl get taskrun hello-task-run
# NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
# hello-task-run True Succeeded 118s 12s
kubectl get pod hello-task-run-pod
# NAME READY STATUS RESTARTS AGE
# hello-task-run-pod 0/1 Completed 0 2m28s
Step 3.查看Task任务运行的结果
代码语言:javascript复制kubectl logs --selector=tekton.dev/taskRun=hello-task-run
# Hello World, Tekton!
2.使用pipeline流水线调用带有参数的Task任务
描述: 此小节作者创建并运行第一个 Tekton 管道,可以预定义参数。
Step 1.创建一个名为user
的 Task 任务,根据参数输出一段字符串,需要注意的是在没有设置默认值时必须传入指定的参数值。
tee User-Task.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: user
spec:
params:
- name: username
type: string
- name: url
type: string
steps:
- name: hi
image: ubuntu
script: |
#!/bin/bash
echo "Hi, $(params.username)!"
echo "Blog: $(params.url)"
EOF
Step 2.创建一个名为hello-user
的 Pipeline 以及运行 PipelineRun 流水线资源清单。
# Tekton-Pipeline
tee Tekton-Pipeline.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: hello-user
spec:
params:
- name: username
type: string
- name: url
type: string
tasks:
- name: hello
taskRef:
name: hello
- name: user
runAfter:
- hello
taskRef:
name: user
params:
- name: username
value: $(params.username)
- name: url
value: $(params.url)
EOF
# Tekton-PipelineRun : 传入预定义的参数值并调用执行 pipeline。
tee Tekton-PipelineRun.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: hello-user-run
spec:
pipelineRef:
name: hello-user
params:
- name: username
value: "Tekton"
- name: url
value: "https://blog.weiyigeek.top"
EOF
Step 3.依次部署Task
、Pipeline
以及PipelineRun
。
kubectl apply -f User-Task.yaml
# task.tekton.dev/user created
kubectl apply -f Tekton-Pipeline.yaml
# pipeline.tekton.dev/hello-user created
kubectl apply -f Tekton-PipelineRun.yaml
# pipelinerun.tekton.dev/hello-user-run created
Step 4.查看部署运行结果。
代码语言:javascript复制kubectl get pipelines hello-user
# NAME AGE
# hello-user 81s
kubectl get pipelineruns hello-user-run
# NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
# hello-user-run True Succeeded 37s 11s
kubectl get pod -l tekton.dev/pipelineRun=hello-user-run
# NAME READY STATUS RESTARTS AGE
# hello-user-run-hello-pod 0/1 Completed 0 59s
# hello-user-run-user-pod 0/1 Completed 0 55s
kubectl logs -l tekton.dev/pipelineRun=hello-user-run
# Hello World, Tekton!
# Hi, Tekton!
# Blog: https://blog.weiyigeek.top
Step 5.我们也可以使用前面安装的tkn命令tkn pipelinerun logs hello-user-run -f -n default
,来查看创建的pipelinerun执行结果。
Step 6.除此之外,我们也可以使用前面安装的Tekton Dashboard的UI界面查看运行情况。
3.使用Triggers触发器来调用流水线从而带有参数的Task任务
描述: 此处小节将快速演示triggers触发器创建及其使用,在前面编写的 Task 与 Pipelines 下进行。
Step 1.首先创建一个 TriggerTemplate 以及 TriggerBinding 对象.
代码语言:javascript复制# Create a TriggerTemplate
tee Tekton-TriggerTemplate.yaml <<'EOF'
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
name: hello-user-template
spec:
params:
- name: username
default: "WeiyiGeek"
- name: url
default: "https://www.weiyigeek.top"
resourcetemplates:
- apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: hello-user-run-
spec:
pipelineRef:
name: hello-user
params:
- name: username
value: $(tt.params.username)
- name: url
value: $(tt.params.url)
EOF
# Create a TriggerBinding
tee Tekton-TriggerBinding.yaml <<'EOF'
apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerBinding
metadata:
name: hello-user-binding
spec:
params:
- name: username
value: $(body.username)
- name: url
value: $(body.url)
EOF
Step 2.然后再创建 EventListener RABC 以及 EventListener (事件监听器)
代码语言:javascript复制# Create an EventListener RABC
tee Tekton-Trigger-Rabc.yaml <<'EOF'
apiVersion: v1
kind: ServiceAccount
metadata:
name: tekton-robot
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: triggers-example-eventlistener-binding
subjects:
- kind: ServiceAccount
name: tekton-robot
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: tekton-triggers-eventlistener-roles
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: triggers-example-eventlistener-clusterbinding
subjects:
- kind: ServiceAccount
name: tekton-robot
namespace: default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: tekton-triggers-eventlistener-clusterroles
EOF
# Create an EventListener
tee Tekton-Trigger-EventListener.yaml <<'EOF'
apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
name: hello-user-listener
spec:
serviceAccountName: tekton-robot
triggers:
- name: hello-trigger
bindings:
- ref: hello-user-binding
template:
ref: hello-user-template
EOF
Step 3.将上述资源清单配置到Kubernetes集群中
代码语言:javascript复制kubectl apply -f Tekton-TriggerTemplate.yaml
# triggertemplate.triggers.tekton.dev/hello-user-template created
kubectl apply -f Tekton-TriggerBinding.yaml
# triggerbinding.triggers.tekton.dev/hello-user-binding created
kubectl apply -f Tekton-Trigger-Rabc.yaml
# serviceaccount/tekton-robot created
# rolebinding.rbac.authorization.k8s.io/triggers-example-eventlistener-binding created
# clusterrolebinding.rbac.authorization.k8s.io/triggers-example-eventlistener-clusterbinding created
kubectl apply -f Tekton-Trigger-EventListener.yaml
# eventlistener.triggers.tekton.dev/hello-user-listener created
Step 4.验证触发器部署情况
代码语言:javascript复制# 查看创建的 triggertemplates,triggerbindings,eventliste 等资源
kubectl get triggertemplates,triggerbindings,eventliste
# 事件触发地址
eventlistener.triggers.tekton.dev/hello-user-listener
http://el-hello-user-listener.default.svc.cluster.test:8080
Step 5.管理与验证触发器是否能触发Pipeline流水线的执行, 此处我直接使用SVC服务名进行演示请求,在实际场景中你可能需要使用ingress进行暴露。
代码语言:javascript复制# 查看 K8S 集群的DNS
$ kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 405d
# 设置集群DNS的地址到主机resolv.conf文件中
vim /etc/resolv.conf
# 验证解析
ping el-hello-user-listener.default.svc.cluster.test
# 使用 curl 模拟请求触发,注意传递的参数哟。
curl -v
-H 'content-Type: application/json'
-d '{"username": "Tekton","url": "https://www.weiyigeek.top"}'
http://el-hello-user-listener.default.svc.cluster.test:8080
执行结果: {"eventListener":"hello-user-listener","namespace":"default","eventListenerUID":"6a6f31ab-47c7-4ef0-9d78-46450fbbc334","eventID":"7accd3ec-21b6-4d38-a164-145a84a3347e"}
偷偷的告诉你哟?【极客全栈修炼】微信小程序已开放
可直接在微信里面直接浏览博主文章哟,后续将上线更多有趣的小工具。
Step 6.最后验证是否触发绑定的流水线的执行,此时检查PipelineRun日志该名称是自动生成的,每次运行都添加一个后缀,在本例中为hello-user-run-s29pd
。
kubectl get pipelineruns -l triggers.tekton.dev/eventlistener=hello-user-listener
# NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME
# hello-user-run True Succeeded 142m 142m
# hello-user-run-s29pd True Succeeded 4m44s 118s
# 再使用tkn命令中指定PiepelineRun名称查看对应日志
tkn pipelinerun logs hello-user-run-s29pd -f
[hello : echo] Hello World, Tekton!
[user : hi] Hi, Tekton!
[user : hi] Blog: https://www.weiyigeek.top
4.使用dashboard来创建运行 TaskRuns 与 PipelineRuns 触发任务执行
描述: 此处简单使用 tekton-dashboard 界面来创建运行 TaskRuns 与 PipelineRuns 示例,值得注意的是tekton-dashboard 不支持Task任务以及Pipeline对象创建,所以你需要提前在K8S中执行创建。
Step 1.首先,我们需要在集群中使用kubectl命令来创建Tekton的最小单元Task资源清单, 此处模拟拉取作者的主页HTML源码。
代码语言:javascript复制tee Git-Task.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: git
spec:
params:
- name: project-name
type: string
- name: project-url
type: string
steps:
- name: git
image: bitnami/git:2.41.0
script: |
#!/usr/bin/env bash
echo -e "Hello, Tekton Dashboard Test!nAuthor:WeiyiGeek"
echo "Project Name:$(params.project-name)"
echo "Project Url:$(params.project-url)"
pwd && git version && cd /tmp
git clone $(params.project-url)
cd ./$(params.project-name) && pwd && ls
EOF
# 部署该Task
kubectl apply -f Git-Task.yaml
# task.tekton.dev/git created
Step 2.访问tekton-dashboard 界面创建 TaskRuns,点击 Tekton 资源 -> TaskRuns -> 创建名为 git-task -> 选择参数配置
或者YAML方式配置 (此处我将Yaml配置贴上)
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
name: git-task
namespace: default
labels:
action: git
spec:
taskRef:
name: git
kind: Task
params:
- name: project-name
value: weiyigeek
- name: project-url
value: https://gitee.com/WeiyiGeek/weiyigeek.git
serviceAccountName: default
Step 3.点击创建的git-task查看此Task运行结果
Step 4.前面完成TaskRun对象的创建执行,此处将演示PipelineRuns对象创建和触发执行,同样的你需要在K8S集群先执行部署。
代码语言:javascript复制tee Git-Pipeline.yaml <<'EOF'
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: git
spec:
params:
- name: project-name
type: string
- name: project-url
type: string
tasks:
- name: git
taskRef:
name: git
params:
- name: project-name
value: $(params.project-name)
- name: project-url
value: $(params.project-url)
EOF
kubectl apply -f Git-Pipeline.yaml
# pipeline.tekton.dev/git created
Step 5.点击,如上所创建 pipelineRuns -> 选择参数
或者YAML方式配置, 此处我传递了存放我的主页的代码仓库信息。
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: git-pipeline-run
namespace: default
labels:
action: git
spec:
pipelineRef:
name: git
params:
- name: project-name
value: weiyigeek
- name: project-url
value: https://gitee.com/WeiyiGeek/weiyigeek.git
timeouts:
pipeline: git
tasks: git
Step 6.然后点击创建的 git-pipeline-run 流水线任务查看其运行的结果。
温馨提示:更多的发现、搜索和共享可重复使用的任务和管道,可以参考官方提供的类似于Hub的地址 https://hub.tekton.dev/
, 作为新手应该多多学习。
至此,在Tekton-Dashboar中创建执行 TaskRuns 以及 PipelineRuns 对象演示完毕。
本文至此完毕,更多技术文章,尽情等待下篇好文!
原文地址: https://blog.weiyigeek.top/2023/7-22-768.html