简介:本文聚焦云原生 CI/CD 框架 Tekton 的部署实践,从环境准备、资源规划到完整部署流程,详细说明如何快速搭建 Tekton 并实现自动化流水线。适合开发、运维及架构师团队参考,尤其适合已使用容器平台的团队快速上手。
Tekton 是基于 Kubernetes 的云原生 CI/CD 框架,其核心设计理念是将流水线步骤拆解为独立的 Pod 单元,通过 Kubernetes 原生能力实现资源弹性、任务编排和自动化触发。相比传统 CI/CD 工具,Tekton 具有以下优势:
本文目标为帮助读者在 Kubernetes 集群中完成 Tekton 核心组件(Pipelines、Dashboard、Triggers)的部署,并验证基础流水线执行能力。
Tekton 适用于以下技术场景:
Tekton 部署包含以下核心模块:
以单节点集群为例,使用轻量级 K3s 快速搭建:
# 安装 K3s(服务器节点)curl -sfL https://get.k3s.io | sh -# 验证节点状态export KUBECONFIG=/etc/rancher/k3s/k3s.yamlkubectl get nodes
关键点:生产环境需配置高可用 Master 节点,并通过 kubeadm 或托管平台创建集群。
# 安装核心组件kubectl apply -f https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml# 验证组件状态kubectl -n tekton-pipelines get pods -w
输出示例:
NAME READY STATUS RESTARTS AGEtekton-pipelines-controller 1/1 Running 0 2mtekton-pipelines-webhook 1/1 Running 0 2m
# 安装 Web UIkubectl apply -f https://storage.googleapis.com/tekton-releases/dashboard/latest/release.yaml# 创建 Ingress 暴露服务(需提前部署 Ingress Controller)cat <<EOF | kubectl apply -f -apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: tekton-dashboardnamespace: tekton-pipelinesspec:rules:- host: tekton.example.comhttp:paths:- path: /pathType: Prefixbackend:service:name: tekton-dashboardport:number: 9097EOF
替代方案:通过 kubectl port-forward 临时访问:
kubectl -n tekton-pipelines port-forward svc/tekton-dashboard 9097:9097
# 安装事件触发模块kubectl apply -f https://storage.googleapis.com/tekton-releases/triggers/latest/release.yamlkubectl apply -f https://storage.googleapis.com/tekton-releases/triggers/latest/interceptors.yaml# 验证 Trigger 组件kubectl -n tekton-pipelines get pods | grep eventlistener
apiVersion: tekton.dev/v1beta1kind: Taskmetadata:name: build-imagespec:params:- name: imageUrltype: stringsteps:- name: buildimage: docker:20.10command: ["docker", "build", "-t", "$(params.imageUrl)", "."]
apiVersion: tekton.dev/v1beta1kind: Pipelinemetadata:name: demo-pipelinespec:params:- name: repoUrltype: stringtasks:- name: fetch-sourcetaskRef:name: git-cloneparams:- name: urlvalue: "$(params.repoUrl)"- name: build-imagetaskRef:name: build-imagerunAfter: [fetch-source]params:- name: imageUrlvalue: "my-registry/demo:$(tasks.fetch-source.results.commit)"
通过 EventListener 绑定 Webhook 与 Pipeline:
apiVersion: triggers.tekton.dev/v1beta1kind: EventListenermetadata:name: git-push-listenerspec:serviceAccountName: tekton-triggers-example-satriggers:- bindings:- ref: git-push-bindingtemplate:ref: git-push-template
组件健康检查:
kubectl -n tekton-pipelines get pods | grep -v Completed
确保所有 Pod 状态为 Running。
流水线执行测试:
# 创建 TaskRun 手动触发任务kubectl apply -f task-run.yaml# 查看执行日志kubectl logs -f <taskrun-pod-name>
Dashboard 访问:
通过浏览器访问 https://tekton.example.com,查看流水线执行状态与日志。
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
Pod 一直处于 Pending 状态 |
资源不足或调度失败 | 检查节点资源与 kubectl describe pod 输出 |
TaskRun 报 PermissionDenied |
ServiceAccount 权限不足 | 为 Task 绑定正确 RBAC 角色 |
| Webhook 未触发 Pipeline | 证书或 URL 配置错误 | 验证 EventListener 服务暴露地址与 Git 仓库配置 |
资源监控:
tekton_taskrun_duration_seconds 等关键指标;tekton-pipelines-controller 副本数。安全加固:
NetworkPolicy 隔离流量;SealedSecret 管理镜像仓库凭证等敏感信息。性能优化:
PodTemplate 预设资源请求;Workspaces 共享卷减少数据拷贝。本文系统阐述了 Tekton 的部署全流程,从环境准备、组件安装到流水线定义与验证。通过声明式配置与 Kubernetes 原生集成,Tekton 可显著提升 CI/CD 流水线的灵活性与可维护性。后续可进一步探索: