以下介绍的是基于 Gitee 仓库 的 Tekton 工作流程
操作流程
定义task
克隆代码的task
# task-clone.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: git-clone
spec:
workspaces:
- name: source # 工作目录
params:
- name: repo-url # 你的 Gitee 仓库地址
type: string
default: "https://gitee.com/wyxsxx/wyx123.git"
- name: branch # 分支名称
type: string
default: "main"
steps:
- name: clone
image: alpine/git # 使用轻量级 Git 镜像
script: |
git clone -b $(params.branch) $(params.repo-url) $(workspaces.source.path)/app
执行自定义脚本的 Task
# task-script.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: run-script
spec:
workspaces:
- name: source
steps:
- name: run-custom-script
image: alpine:3.15 # 按需替换为 Python/Node.js 等镜像
workingDir: "$(workspaces.source.path)/app"
script: |
# 这里执行你的自定义脚本(示例:打印文件列表)
echo "=== 文件列表 ==="
ls -l
echo "==============="
定义 Pipeline
# pipeline.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: simple-demo-pipeline
spec:
workspaces:
- name: shared-data # 共享存储目录
params:
- name: git-url
type: string
default: "https://gitee.com/wyxsxx/wyx123.git"
tasks:
- name: clone-repo
taskRef:
name: git-clone
workspaces:
- name: source
workspace: shared-data
params:
- name: repo-url
value: "$(params.git-url)"
- name: run-script
taskRef:
name: run-script
runAfter: ["clone-repo"] # 依赖 clone 任务
workspaces:
- name: source
workspace: shared-data
运行 Pipeline
# pipelinerun.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: simple-demo-run-
spec:
pipelineRef:
name: simple-demo-pipeline
workspaces:
- name: shared-data
volumeClaimTemplate: # 自动创建 PVC
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
params:
- name: git-url
value: "https://gitee.com/wyxsxx/wyx123.git"
执行和验证
# 部署 Task 和 Pipeline
kubectl apply -f task-clone.yaml
kubectl apply -f task-script.yaml
kubectl apply -f pipeline.yaml
# 触发流水线
kubectl create -f pipelinerun.yaml
查看运行状态
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
affinity-assistant-a953a26f33-0 0/1 Terminating 0 25s
simple-demo-run-18ppnc-clone-repo-fg8kp-pod-j2vgt 0/1 Completed 0 25s
simple-demo-run-18ppnc-run-script-vprlh-pod-kqhxx 0/1 Completed 0 14s
[root@k8s-master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-a953a26f33 Bound pvc-46ceb060-aa14-488c-a2df-f2da70643786 1Gi RWO local-path 47s
[root@k8s-master ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
simple-demo-run-18ppnc-clone-repo-fg8kp-pod-j2vgt 0/1 Completed 0 58s
simple-demo-run-18ppnc-run-script-vprlh-pod-kqhxx 0/1 Completed 0 47s
^C[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 108m
[root@k8s-master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-a953a26f33 Bound pvc-46ceb060-aa14-488c-a2df-f2da70643786 1Gi RWO local-path 102s
实时查看日志
[root@k8s-master tekton-test]# tkn pipelinerun logs -f
? Select pipelinerun: [Use arrows to move, type to filter]
> simple-demo-run-1697gz started 1 second ago
simple-demo-run-18ppnc started 2 minutes ago
dashbord查看是否执行成功
流程图解
+---------------------+
| PipelineRun 触发 |
+---------------------+
|
V
+---------------------+
| 1. clone-repo |
| - 克隆代码到 PVC |
+---------------------+
|
V
+---------------------+
| 2. run-script |
| - 执行自定义脚本 |
+---------------------+
分布流程详解
1.pipelinerun触发
用户执行kubectl create -f pipelinerun.yaml
tekton行为:
- 创建pipelinerun实例
- 解析关联的pipeline
- 自动创建PVC用于共享工作目录
2.task顺序执行
2.1 克隆代码
集群内:
- 创建taskrun
- 生成pod
pod内操作:
# 容器内执行的实际命令
git clone -b main https://gitee.com/wyxsxx/wyx123.git /workspace/shared-data/app
2.2 执行脚本
集群内操作:
- 前一个task(clone-repo)成功后,创建taskrun(run-script)
- 生成pod(run-script-pod)
pod内操作:
# 进入工作目录并执行脚本
cd /workspace/shared-data/app
echo "=== 文件列表 ==="
ls -l
3.资源清理
1. Workspaces 共享存储
- PVC 自动创建:
spec:
pipelineRef:
name: simple-demo-pipeline
workspaces:
- name: shared-data
volumeClaimTemplate: # 自动创建 PVC
spec:
storageClassName: local-path
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
2. 依赖控制
- Pipeline 任务顺序:
克隆完成 → PVC 数据就绪 → 触发脚本执行
3. 错误处理机制
- 失败重试
retries: 2 # 失败后自动重试次数
- 超时控制
timeout: 30m # 超过30分钟则终止任务
四、实际执行日志示例
查看 PipelineRun 状态
[root@k8s-master tekton-test]# tkn pipelinerun list
NAME STARTED DURATION STATUS
simple-demo-run-1697gz 12 minutes ago 16s Succeeded
simple-demo-run-18ppnc 14 minutes ago 18s Succeeded