Published on

Dùng ArgoCD cài đặt GitHub ARC trên Kubernetes

Giới thiệu

ArgoCD là một công cụ triển khai (CD) cho Kubernetes trong việc quản lý và triển khai ứng dụng. ArgoCD được dùng để đồng bộ các tệp tin cấu hình (manifest) của Kubernetes lên hạ tầng hiện tại (Infrastructure). Các file manifest thường được lưu trên Git server vì vậy quá trình này người ta hay gọi là GitOps. Trong các bài viết trước mình đã giới thiệu cách triển khai (deloy) ArgoCD như thế nào, bạn vui lòng tìm đọc.

Trong bài viết này, mình sẽ trình bày cách deploy GitHub Action Runner Controller trên một Kubernetes bằng cách sử dụng ArgoCD

GitHub ARC

Đầu tiền, chúng ta phải nói qua GitHub Action là một công cụ CI/CD của GitHub. Trước kia, GitHub thường chỉ thường để lưu source code và chúng ta thường phải sử dụng các công cụ CI/CD của bên thứ 3 như Jenkins, Travis CI, CircleCI,... Với sự ra đời của GitHub Actions cuối năm 2018 đã giúp GitHub lấp đầy khoảng trống CI/CD.

GitHub Action Runner Controller (ARC) là một Kubernetes operator được phát triển bởi GitHub để quản lý và tự động scale các self-hosted runners cho GitHub Actions. Với Action Runner Controller, bạn có thể tạo runner scale sets tự động scale dựa trên số lượng workflows đang chạy trong repository, tổ chức hoặc doanh nghiệp của bạn.

Cũng phải nói thêm rằng, hiện tại GitHub Action Runner Controller có 2 phiên bản:

  • Community supported: được phát triển bởi @mumoshu, nó cho phép chạy self-hosted runner trên Kubernetes
  • GitHub supported: được GitHub chỉnh sửa dựa trên công việc của @mumoshu cho đồng bộ với cơ chế của GitHub Actions

Dưới đây, mình sử dụng bản GitHub supported.

Tiến hành

Chuẩn bị

Để có thể sử dụng được GitHub ARC:

  • Bạn phải có tài khoản GitHub của tổ chức (có thể tạo và có lựa chon Free). Các bạn tự tìm hiểu thêm, bài viết này mình sẽ không đề cập đến cách tạo tài khoản.
  • Tạo PAT (Personal Access Token) theo hướng dẫn: Deploying Using PAT Authentication
    • repo (Full control)
    • admin:org (Full control)
    • admin:public_key (read:public_key)
    • admin:repo_hook (read:repo_hook)
    • admin:org_hook (Full control)
    • notifications (Full control)
    • workflow (Full control)

Các file manifest

oci-secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  labels:
    argocd.argoproj.io/secret-type: repository
  name: actions-runner-controller-charts
  namespace: argocd
stringData:
  enableOCI: "true"
  name: actions-runner-controller-charts
  password: <your-pat-token>
  type: helm
  url: ghcr.io/actions/actions-runner-controller-charts
  username: <your-github-username>
gha-runner-scale-set-controller.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: arc
  namespace: argocd
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  syncPolicy:
    automated:
      selfHeal: true
      prune: true
      allowEmpty: true
    syncOptions:
      - Validate=false
      - CreateNamespace=true
      - PrunePropagationPolicy=foreground
      - PruneLast=true
      - RespectIgnoreDifferences=true
      - Replace=true
  project: default
  source:
    chart: gha-runner-scale-set-controller
    repoURL: ghcr.io/actions/actions-runner-controller-charts
    targetRevision: 0.8.1
    helm:
      releaseName: arc
      passCredentials: false
      valuesObject:
        metrics:
          controllerManagerAddr: ":8080"
          listenerAddr: ":8080"
          listenerEndpoint: "/metrics"
  destination:
    server: "https://kubernetes.default.svc"
    namespace: arc-systems
gha-runner-scale-set.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: your-runner-name
  namespace: argocd
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  syncPolicy:
    automated:
      selfHeal: true
      prune: true
      allowEmpty: true
    syncOptions:
      - CreateNamespace=true
  project: default
  source:
    chart: gha-runner-scale-set
    repoURL: ghcr.io/actions/actions-runner-controller-charts
    targetRevision: 0.8.1
    helm:
      releaseName: your-runner-name
      passCredentials: false
      parameters:
      - name: "githubConfigUrl"
        value: https://github.com/<your-org>
      - name: "githubConfigSecret"
        value: "github-token"
      - name: "controllerServiceAccount.namespace"
        value: "arc-systems"
      - name: "controllerServiceAccount.name"
        value: "arc-gha-rs-controller"
      valuesObject:
        spec:
          containers:
            - name: runner
              image: ghcr.io/actions/actions-runner:latest
              command: ["/home/runner/run.sh"]
              resources:
                limits:
                  memory: "2G"
                  cpu: 1
  destination:
    server: "https://kubernetes.default.svc"
    namespace: arc-runners

Những dòng được highlight nhớ cập nhật giá trị cho đúng với thông tin của bạn. Sau đó lần lượt chạy dòng lệnh

kubectl apply -f oci-secrets.yaml
kubectl create ns arc-runners
kubectl create secret generic github-token --namespace=arc-runners --from-literal=github_token='<your-pat-token>' # <your-pat-token>: là token bạn đã tạo theo hướng dẫn ở trên
kubectl apply -f gha-runner-scale-set-controller.yaml
kubectl apply -f gha-runner-scale-set.yaml

Khi deploy thành công thì trên ArgoCD chúng ta sẽ thấy 2 Application

Thử chạy workflow

Chúng ta thử chạy 1 workflow_dispatch đơn giản để xem runner có hoạt động không

name: Actions Runner Controller Demo
on:
  workflow_dispatch:

jobs:
  Explore-GitHub-Actions:
    # You need to use the INSTALLATION_NAME from the previous step
    runs-on: your-runner-name
    steps:
    - run: echo "🎉 This job uses runner scale set runners!"

Sau trigger workflow, nếu mọi thứ hoạt động thì chúng ta sẽ thấy workflow chạy thành công như hình dưới đây mà mình đã làm

Gỡ bỏ GitHub ARC

Nếu bạn không có nhu cầu sử dụng nữa thì có thể gỡ bỏ hoàn toàn GitHub ARC

# Gỡ bỏ runner
kubectl -n argocd patch app your-runner-name  -p '{"metadata": {"finalizers": ["resources-finalizer.argocd.argoproj.io"]}}' --type merge
kubectl -n argocd delete app your-runner-name

# Gỡ bỏ ARC
kubectl -n argocd patch app arc -p '{"metadata": {"finalizers": ["resources-finalizer.argocd.argoproj.io"]}}' --type merge
kubectl -n argocd delete app arc

# Xóa CRD
kubectl delete crd autoscalingrunnersets.actions.github.com
kubectl delete crd ephemeralrunners.actions.github.com
kubectl delete crd autoscalinglisteners.actions.github.com
kubectl delete crd ephemeralrunnersets.actions.github.com

Chúc thành công,

ANH NGUYỄN