← 返回上一頁
CI/CD Kubernetes

用 Tekton + Argo CD 打造 Kubernetes 原生 CI/CD 自動化流水線

本頁目錄
Tekton 搭配 ArgoCD 的 CI/CD Pipeline 架構圖

在雲原生的世界裡,CI/CD 已經不是什麼新鮮事了,但要在 Kubernetes 上打造一套真正「原生」的自動化管線,選對工具很重要。我自己在實務上摸索了一陣子,最後選定了 Tekton 負責 CI、Argo CD 負責 CD,這篇就來分享整個從安裝到串接的過程。

Tekton 跟 Argo CD 到底在幹嘛?

Tekton — Kubernetes 原生的 CI 引擎

Tekton 是一個跑在 Kubernetes 上的開源 CI/CD 框架,它用 CRD(Custom Resource Definition)來定義所有東西。簡單說就是:

元件 說明
Step 最小的執行單位,就是一個容器裡跑一段指令
Task 一組 Step 的集合,例如「拉程式碼 → 裝套件 → 跑測試」
Pipeline 多個 Task 串起來,可以定義執行順序跟相依性
Workspace Task 之間共享檔案的機制,底層就是 PVC
Trigger 監聽外部事件(像 GitHub Webhook)來自動啟動 Pipeline

它最大的優勢就是所有東西都是 Kubernetes 資源,不用額外維護一台 Jenkins Server,Pipeline 本身就是 YAML,可以跟程式碼一起版控。

Tekton 架構元件互動圖

Argo CD — 聲明式 GitOps 持續交付

Argo CD 的概念更單純:它盯著你的 Git Repo,只要 Repo 裡的 Kubernetes manifest 有變動,它就會自動同步到叢集。這就是 GitOps 的精髓 — Git 就是唯一的真實來源(Single Source of Truth)。

舉個例子,假設你有一個微服務架構的應用,裡頭有好幾個 Deployment 跟 Service 的 YAML 放在 Git 上。當 Tekton 把新版本的 image build 好、push 完之後更新了 manifest 裡的 image tag,Argo CD 就會偵測到差異,自動幫你 rollout。整個過程完全不用手動 kubectl apply

安裝 Tekton

前置條件

先確認你手上有:

項目 說明
Kubernetes 叢集 Minikube / Kind / GKE / EKS / AKS 都行
kubectl 已設定好 context,能正常操作叢集
Helm v3 以上,用來安裝元件

安裝 Tekton Pipelines

加入 Helm Repo 然後直接裝:

helm repo add tektoncd https://storage.googleapis.com/tekton-releases/pipeline/charts
helm repo update
helm install tektoncd-pipeline tektoncd/pipeline

裝完確認 Pod 有沒有跑起來:

kubectl get pods -n tekton-pipelines

全部顯示 Running 就代表成功了。

安裝 Tekton Dashboard(選配)

如果你想要有個 Web UI 看 Pipeline 的執行狀況:

helm repo add tektoncd https://storage.googleapis.com/tekton-releases/dashboard/charts
helm repo update
helm install tektoncd-dashboard tektoncd/dashboard

裝好後 port-forward 出來:

kubectl port-forward svc/tektoncd-dashboard -n tekton-pipelines 9097:9097

打開瀏覽器連 http://localhost:9097 就能看到了。

安裝 Argo CD

裝 CLI 工具

Linux:

curl -sSL -o /usr/local/bin/argocd \
  https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
chmod +x /usr/local/bin/argocd

macOS 直接用 Homebrew:

brew install argoproj/tap/argocd

裝完跑一下確認版本:

argocd version

裝 Argo CD Server

一樣用 Helm:

helm repo add argo https://argoproj.github.io/argo-helm
helm repo update
helm install argocd argo/argo-cd

拿初始密碼:

kubectl -n argocd get secret argocd-initial-admin-secret \
  -o jsonpath="{.data.password}" | base64 -d; echo

Port-forward 出來:

kubectl port-forward svc/argocd-server -n argocd 8080:443

用瀏覽器打開 https://localhost:8080,帳號 admin、密碼就是剛剛拿到的那串。記得第一次登入後馬上改密碼。

實戰:用 Tekton + Argo CD 建構完整 Pipeline

Tekton 搭配 ArgoCD 的 CI/CD Pipeline 架構圖

建立 Tekton Task

拿一個 Node.js 專案來當範例,我們要做的事情是:clone → install → test → build。

建立 nodejs-build-task.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: nodejs-build-task
spec:
  params:
    - name: source-url
      type: string
      description: 原始碼 Git 倉庫 URL
  steps:
    - name: checkout
      image: alpine/git
      script: |
        git clone $(params.source-url) .
    - name: install-dependencies
      image: node:14
      workingDir: $(workspaces.source.path)
      script: |
        npm install
    - name: test
      image: node:14
      workingDir: $(workspaces.source.path)
      script: |
        npm test
    - name: build
      image: node:14
      workingDir: $(workspaces.source.path)
      script: |
        npm run build

套用到叢集:

kubectl apply -f nodejs-build-task.yaml

建立 Tekton Pipeline

建立 nodejs-build-pipeline.yaml,把 Task 串起來:

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: nodejs-build-pipeline
spec:
  params:
    - name: source-url
      type: string
      description: 原始碼 Git 倉庫 URL
  workspaces:
    - name: source
  tasks:
    - name: build-task
      taskRef:
        name: nodejs-build-task
      params:
        - name: source-url
          value: $(params.source-url)
      workspaces:
        - name: source
          workspace: source

套用:

kubectl apply -f nodejs-build-pipeline.yaml

設定 Argo CD Application

登入 Argo CD 後台,點左上角「New App」,填入:

欄位
Application Name 你的應用名稱,例如 nodejs-app
Project default 或自訂的 project
Sync Policy 選 Automatic,讓它自動幫你同步
Repository URL 你的 Git Repo 位址
Path K8s manifest 檔案所在路徑
Cluster URL 目標叢集的 API Server
Namespace 要部署到哪個 namespace

按下「Create」就搞定了。

串接 Webhook 實現全自動

整個流程的最後一塊拼圖就是 Webhook。以 GitHub 為例,到 Repo 的 Settings → Webhooks 設定,把 Tekton 的 EventListener endpoint 填進去。這樣每次 push 程式碼就會:

  1. GitHub 發 Webhook 到 Tekton EventListener
  2. Tekton 啟動 Pipeline:clone → install → test → build → push image
  3. Pipeline 完成後更新 Git Repo 中的 manifest(例如改 image tag)
  4. Argo CD 偵測到 manifest 變動,自動 sync 到叢集

整個 CI/CD 就跑起來了,完全不需要人工介入。

小結

Tekton 和 Argo CD 這個組合在 Kubernetes 生態系裡算是相當成熟的 CI/CD 方案了。Tekton 負責 CI 的部分:build、test、push image,所有定義都是 K8s CRD,天然就能被版控跟重用;Argo CD 負責 CD:盯著 Git Repo,有變動就自動部署,完美實現 GitOps 理念。

我自己用下來覺得最大的好處是——所有東西都在 Kubernetes 裡面,不用再去維護一台獨立的 CI Server。Pipeline 定義就是 YAML,放在 Git 裡,團隊成員都能看到、都能改,review 起來也很方便。

當然實務上還有很多可以優化的地方,像是加入 image scanning、整合 Slack 通知、設定 Pipeline 的 resource limits 等等。但基本的骨架有了,後續要擴充就不難了。

分享這篇
X LinkedIn Facebook Hacker News Reddit

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料