Tekton:Kubernetes 原生 CI/CD 流水线

Tekton:Kubernetes 原生 CI/CD 流水线

Jenkins 太重、GitHub Actions 依赖外部服务、Drone CI 功能有限------Tekton 是 Google 推出的 Kubernetes 原生 CI/CD 框架,每个流水线步骤都是一个普通的 Pod,资源弹性、易扩展,非常适合已经在使用 K8s 的团队。这篇文章讲如何在 K3s 上安装 Tekton,并实现一条完整的构建→测试→推送流水线。

Tekton 核心概念

复制代码
Step       = 流水线里的一步(一个容器命令)
Task       = 一组 Step 的集合(可重用)
Pipeline   = 多个 Task 的编排
TaskRun    = Task 的一次运行实例
PipelineRun = Pipeline 的一次运行实例
Trigger    = 触发器(响应 Webhook 自动运行)

服务器配置

Tekton 跑在 K8s 集群上,建议先有一个 K3s 集群:

  • 单节点 K3s:4 核 8GB,用于开发/测试
  • 生产集群:推荐 3 个 worker 节点

我把 K3s 集群搭在雨云服务器 rainyun+com 上,多台服务器组集群,注册填优惠码 2026off 领 5 折优惠券,买多台服务器有明显的价格优势。

安装 Tekton

安装 K3s(如果还没有)

bash 复制代码
curl -sfL https://get.k3s.io | sh -
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
kubectl get nodes

安装 Tekton Pipelines

bash 复制代码
# 安装 Tekton Pipelines(核心组件)
kubectl apply -f https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml

# 安装 Tekton Dashboard(Web UI)
kubectl apply -f https://storage.googleapis.com/tekton-releases/dashboard/latest/release.yaml

# 安装 Tekton Triggers(Webhook 触发)
kubectl apply -f https://storage.googleapis.com/tekton-releases/triggers/latest/release.yaml
kubectl apply -f https://storage.googleapis.com/tekton-releases/triggers/latest/interceptors.yaml

# 查看安装状态
kubectl -n tekton-pipelines get pods -w

访问 Tekton Dashboard

bash 复制代码
# 端口转发查看
kubectl -n tekton-pipelines port-forward svc/tekton-dashboard 9097:9097

# 或创建 Ingress(需要 Traefik/Nginx Ingress)
kubectl apply -f - << 'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tekton-dashboard
  namespace: tekton-pipelines
spec:
  rules:
    - host: tekton.你的域名.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: tekton-dashboard
                port:
                  number: 9097
EOF

编写第一个 Task

yaml 复制代码
# task-build-go.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: build-go
  namespace: default
spec:
  params:
    - name: repo-url
      type: string
    - name: image-name
      type: string
    - name: image-tag
      default: "latest"
      type: string
  workspaces:
    - name: source
  steps:
    # 第一步:克隆代码
    - name: clone
      image: alpine/git:latest
      command:
        - git
        - clone
        - "$(params.repo-url)"
        - .
      workingDir: $(workspaces.source.path)

    # 第二步:运行测试
    - name: test
      image: golang:1.22-alpine
      command:
        - go
        - test
        - ./...
      workingDir: $(workspaces.source.path)

    # 第三步:构建镜像并推送
    - name: build-push
      image: gcr.io/kaniko-project/executor:latest
      args:
        - "--context=$(workspaces.source.path)"
        - "--dockerfile=Dockerfile"
        - "--destination=$(params.image-name):$(params.image-tag)"
      env:
        - name: DOCKER_CONFIG
          value: /kaniko/.docker
      volumeMounts:
        - name: docker-config
          mountPath: /kaniko/.docker
  volumes:
    - name: docker-config
      secret:
        secretName: docker-registry-secret
bash 复制代码
kubectl apply -f task-build-go.yaml

创建 Pipeline

yaml 复制代码
# pipeline-ci.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: ci-pipeline
  namespace: default
spec:
  params:
    - name: repo-url
    - name: image-name
    - name: image-tag
  workspaces:
    - name: shared-workspace
  tasks:
    - name: build
      taskRef:
        name: build-go
      params:
        - name: repo-url
          value: "$(params.repo-url)"
        - name: image-name
          value: "$(params.image-name)"
        - name: image-tag
          value: "$(params.image-tag)"
      workspaces:
        - name: source
          workspace: shared-workspace
bash 复制代码
kubectl apply -f pipeline-ci.yaml

手动触发 PipelineRun

yaml 复制代码
# pipelinerun-manual.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  generateName: ci-pipeline-run-
  namespace: default
spec:
  pipelineRef:
    name: ci-pipeline
  params:
    - name: repo-url
      value: "https://github.com/你的用户名/你的仓库.git"
    - name: image-name
      value: "registry.你的域名.com/my-app"
    - name: image-tag
      value: "v1.0.0"
  workspaces:
    - name: shared-workspace
      volumeClaimTemplate:
        spec:
          accessModes: [ReadWriteOnce]
          resources:
            requests:
              storage: 1Gi
bash 复制代码
kubectl apply -f pipelinerun-manual.yaml

# 查看运行状态
kubectl get pipelineruns -w

# 查看日志
tkn pipelinerun logs -f ci-pipeline-run-xxxxx

配置 Git Webhook 自动触发

bash 复制代码
# 安装 tkn CLI
curl -LO https://github.com/tektoncd/cli/releases/latest/download/tkn_linux_amd64.tar.gz
tar -xzf tkn_linux_amd64.tar.gz
sudo mv tkn /usr/local/bin/

创建 EventListener:

yaml 复制代码
# trigger.yaml
apiVersion: triggers.tekton.dev/v1beta1
kind: EventListener
metadata:
  name: git-webhook
  namespace: default
spec:
  triggers:
    - name: push-trigger
      interceptors:
        - ref:
            name: github
          params:
            - name: secretRef
              value:
                secretName: github-webhook-secret
                secretKey: token
            - name: eventTypes
              value: ["push"]
      bindings:
        - ref: git-push-binding
      template:
        ref: pipeline-trigger-template
bash 复制代码
kubectl apply -f trigger.yaml

# 暴露 EventListener 接收 Webhook
kubectl -n default expose svc el-git-webhook --type=NodePort

Tekton 是 Kubernetes 生态里最原生的 CI/CD 方案,每次构建都是标准的 Pod,天然支持并行、资源限制、弹性伸缩。在雨云rainyun的 K3s 集群上部署 Tekton,多台服务器的集群方案性价比很高,打造完整的 GitOps 工作流。

相关推荐
做个文艺程序员1 小时前
第02篇:K8s 存储与配置管理:ConfigMap、Secret、PV/PVC 实战——Java SaaS 多租户配置最佳实践
java·容器·kubernetes
张忠琳3 小时前
【kubevirt】(virt-launcher Part 6)virt-launcher 设备/网络/存储/外设层
云原生·架构·kubernetes·kubevirt
jiayong235 小时前
CI/CD深度解析01-核心概念与原理
运维·git·ci/cd
qq_356408665 小时前
Kubernetes Loki 日志收集系统部署文档 (读写分离模式 + Ceph S3 + Nginx 日志分离)
ceph·nginx·kubernetes
随风丶飘7 小时前
AI 接入 CI/CD 实测:构建失败自动诊断与修复,能省多少排查时间?
人工智能·ci/cd
初心丨哈士奇7 小时前
用 AI 自动生成前端代码影响范围报告:从 CI 到测试用例
ci/cd·aigc·前端工程化
宇明一不急9 小时前
K8S-中nodePort、port、targetPort和containerPort
云原生·容器·kubernetes
puamac9 小时前
gitLab CI/CD 执行流程说明
ci/cd·gitlab
维度跃迁笔记10 小时前
2核4G轻量服务器部署GitLab实战:配置调优与CI/CD拆分方案
服务器·ci/cd·gitlab
Bigger10 小时前
GitLab-Runner + AI 代码审查服务 + 远程大模型 全套部署运维实战
前端·ci/cd·ai编程