Published on

Thiết lập Github Action self-hosted runner trên Kubernetes cluster

Mở đầu

Bài viết này sẽ giới thiệu cách triển khai ứng dụng CI/CD cho GitHub Action chạy self-hosted runner trên K8s. Một số thứ cần có:

  • Kubernetes cluster
  • GitHub Personal Access Token

Actions Runner Controller (ARC)

Đây là 1 open source trên GitHub cho phép sử dụng Self-hosted runner trên K8s. Chi tiết có thể xem thêm Tại đây, mình sẽ sử dụng công cụ này để thiết lập CI/CD trên hệ thống K8s của mình.

Thiết lập trên Kubernetes

  • Đầu tiên K8s cluster cần được cài đặt cert-manager (X.509 certificate management for Kubernetes and OpenShift):
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.10.1/cert-manager.yaml
  • Tiếp theo chúng ta cần tạo 1 Github Personal Access Token tại đường dẫn https://github.com/settings/tokens/new , trong phần Expiration là thời gian hết hạn của token, tùy theo yêu cầu mà chọn khoảng thời gian phù hợp. Trong phần scope thì tick vào repo.
Create github
token
  • Sau đó copy và lưu lại Token, sau đó chúng ta lưu lại token đó trong secret value trên k8s bằng việc lệnh:
kubectl create secret generic controller-manager -n actions-runner-system --from-literal=github_token=GITHUB_PAT

Trong đó GITHUB_PAT là giá trị của token vừa tạo ở bước trên.

  • Cuối cùng chúng ta deploy Actions Runner Controller lên k8s
kubectl create -f https://github.com/actions/actions-runner-controller/releases/download/v0.26.0/actions-runner-controller.yaml

Thử nghiệm trên GitHub Action

Bây giờ mình sẽ thử thiết lập 1 repo sử dụng Action self-hosted runner. Khi sử dụng ARC, mỗi repo trên github cần được deploy 1 RunnerDeployment trên K8s. Ví dụ mình thiết lập cho repo nvtienanh/django-test-github-runner-self-host-k8s, mình sử dụng 1 namepsace github-runner để quản lý các RunnerDeployment. Ví dụ mình tạo 1 file django-arc.yaml

apiVersion: actions.summerwind.dev/v1alpha1
kind: RunnerDeployment
metadata:
  name: django-test
  namespace: github-runner
spec:
  replicas: 1
  template:
    spec:
      image: summerwind/actions-runner-dind
      dockerdWithinRunnerContainer: true
      repository: nvtienanh/django-test-github-runner-self-host-k8s
      env: []

Sau đó deploy trên Kubernetes cluster:

kubectl create namespace github-runner
kubectl apply -f django-arc.yaml

Cuối cùng trên GitHub chúng ta sẽ tạo 1 workflow dùng để test, chi tiết có thể tham khảo repo ở trên

name: lint_python
on: [pull_request, push]
jobs:
  lint_python:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      - run: pip install --upgrade pip wheel
      - run: pip install bandit black codespell flake8 flake8-2020 flake8-bugbear
          flake8-comprehensions isort mypy pytest pyupgrade safety
      - run: bandit --recursive --skip B101,B105 .
      - run: black --check . || true
      # - run: codespell  # --ignore-words-list="" --skip="*.css,*.js,*.lock"
      - run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
      - run: flake8 . --count --exit-zero --max-complexity=10 --max-line-length=88
          --show-source --statistics
      - run: isort --check-only --profile black . || true
      - run: pip install -r requirements.txt || pip install --editable . || true
      - run: mkdir --parents --verbose .mypy_cache
      - run: mypy --ignore-missing-imports --install-types --non-interactive . || true
      - run: pytest --doctest-modules .
      - run: shopt -s globstar && pyupgrade --py36-plus **/*.py
      # - run: safety check

Kết quả nếu chạy thành công

Self-hosted
runner

Kết bài

Trên đây chỉ là 1 ví dụ đơn giản mà mình đã thực hiện, các bạn có thể tham khảo thêm trên repo actions-runner-controller

  • Automatically scaling runners
  • Using custom volumes
  • Using ARC runners in a workflow
  • Managing access with runner groups
  • Configuring Windows runners
  • Using ARC across organizations
  • Using Entrypoint features
  • Deploying alternative runners
  • Monitoring and troubleshooting