欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Tekton系列之实践篇-从触发到完成的完整执行过程

Tekton系列之实践篇-从触发到完成的完整执行过程

2025/3/29 15:43:09 来源:https://blog.csdn.net/m0_66011019/article/details/146480311  浏览:    关键词:Tekton系列之实践篇-从触发到完成的完整执行过程

以下介绍的是基于 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: stringdefault: "https://gitee.com/wyxsxx/wyx123.git"- name: branch    # 分支名称type: stringdefault: "main"steps:- name: cloneimage: 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: sourcesteps:- name: run-custom-scriptimage: alpine:3.15  # 按需替换为 Python/Node.js 等镜像workingDir: "$(workspaces.source.path)/app"script: |# 这里执行你的自定义脚本(示例:打印文件列表)echo "=== 文件列表 ==="ls -lecho "==============="

 定义 Pipeline

# pipeline.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:name: simple-demo-pipeline
spec:workspaces:- name: shared-data  # 共享存储目录params:- name: git-urltype: stringdefault: "https://gitee.com/wyxsxx/wyx123.git"tasks:- name: clone-repotaskRef:name: git-cloneworkspaces:- name: sourceworkspace: shared-dataparams:- name: repo-urlvalue: "$(params.git-url)"- name: run-scripttaskRef:name: run-scriptrunAfter: ["clone-repo"]  # 依赖 clone 任务workspaces:- name: sourceworkspace: shared-data

 运行 Pipeline

# pipelinerun.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:generateName: simple-demo-run-
spec:pipelineRef:name: simple-demo-pipelineworkspaces:- name: shared-datavolumeClaimTemplate:  # 自动创建 PVCspec:accessModes:- ReadWriteOnceresources:requests:storage: 1Giparams:- name: git-urlvalue: "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 agosimple-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-pipelineworkspaces:- name: shared-datavolumeClaimTemplate:  # 自动创建 PVCspec:storageClassName: local-pathaccessModes:- ReadWriteOnceresources: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

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词