Bài 7: Kubernetes CI/CD – Tự Động Triển Khai Với GitHub Actions

Tại Sao Cần CI/CD Với Kubernetes?

Kubernetes CI/CD là cách tự động hóa triển khai ứng dụng lên Kubernetes, giúp tiết kiệm thời gian và giảm lỗi thủ công. GitHub Actions là một công cụ CI/CD phổ biến, cho phép bạn chạy pipeline khi đẩy mã lên repository. Trong bài 6, bạn đã dùng Helm để triển khai ứng dụng. Bài này sẽ hướng dẫn bạn tích hợp Kubernetes với GitHub Actions để tự động triển khai ứng dụng Nginx lên Minikube.

Lưu ý: Bạn cần đã cài đặt Minikube, kubectl, và có tài khoản GitHub. Hướng dẫn này dùng Minikube 1.33.1 trên Ubuntu 22.04.

Bước 1: Chuẩn Bị Minikube Và GitHub Repository

  • Hành động:
    1. Khởi động Minikube:
      minikube start --driver=docker
    2. Tạo repository trên GitHub:
      • Truy cập GitHub, tạo repository mới (VD: k8s-cicd-example).
    3. Tạo thư mục dự án local:
      mkdir k8s-cicd-example
      cd k8s-cicd-example
      git init
  • Kết quả thực tế:
    • Sau khi chạy minikube start, terminal hiển thị:
          minikube v1.33.1 on Ubuntu 22.04
          Using the docker driver
          Starting control plane node in cluster minikube
          Done! kubectl is now configured to use "minikube" cluster
    • Sau khi chạy git init, terminal hiển thị:
      Initialized empty Git repository in /path/to/k8s-cicd-example/.git/

Bước 2: Tạo Ứng Dụng Và File Kubernetes Manifest

  • Hành động:
    1. Tạo file nginx-deployment.yaml:
      touch nginx-deployment.yaml

      Mở file và dán nội dung:

      apiVersion: apps/v1
      kind: Deployment
      metadata:
      name: nginx-deployment
      spec:
      replicas: 2
      selector:
       matchLabels:
         app: nginx
      template:
       metadata:
         labels:
           app: nginx
       spec:
         containers:
         - name: nginx
           image: nginx:latest
           ports:
           - containerPort: 80
      ---
      apiVersion: v1
      kind: Service
      metadata:
      name: nginx-service
      spec:
      selector:
       app: nginx
      ports:
      - protocol: TCP
       port: 80
       targetPort: 80
      type: ClusterIP
  • Kết quả thực tế:
    • Cấu trúc thư mục:
      k8s-cicd-example/
      └── nginx-deployment.yaml

      (Kiểm tra bằng lệnh ls).

Bước 3: Cấu Hình GitHub Actions Workflow

  • Hành động:

    1. Tạo thư mục và file workflow:
      mkdir -p .github/workflows
      touch .github/workflows/deploy.yml
    2. Mở file .github/workflows/deploy.yml và dán nội dung:

      name: Deploy to Kubernetes
      
      on:
      push:
       branches:
         - main
      
      jobs:
      deploy:
       runs-on: ubuntu-latest
      
       steps:
       - name: Checkout code
         uses: actions/checkout@v3
      
       - name: Setup Minikube
         uses: medyagh/setup-minikube@master
         with:
           minikube-version: 'v1.33.1'
           driver: docker
      
       - name: Deploy to Kubernetes
         run: kubectl apply -f nginx-deployment.yaml
      
       - name: Verify Deployment
         run: kubectl get pods
  • Kết quả thực tế:
    • Cấu trúc thư mục:
      k8s-cicd-example/
      ├── .github/
      │   └── workflows/
      │       └── deploy.yml
      └── nginx-deployment.yaml

Bước 4: Đẩy Mã Và Kiểm Tra Pipeline

  • Hành động:
    1. Commit và đẩy mã lên GitHub:
      git add .
      git commit -m "Add Kubernetes manifest and CI/CD workflow"
      git branch -M main
      git remote add origin https://github.com/your-username/k8s-cicd-example.git
      git push -u origin main

      (Thay your-username bằng tên người dùng GitHub của bạn).

    2. Kiểm tra pipeline trên GitHub:
      • Truy cập repository k8s-cicd-example > Actions.
  • Kết quả thực tế:
    • Sau khi đẩy mã, terminal hiển thị:
      To https://github.com/your-username/k8s-cicd-example.git
          [new branch]      main -> main
    • Trên GitHub Actions, log pipeline (rút gọn):
      Deploy to Kubernetes
      Setup Minikube
      [command] minikube start --driver=docker
          Done! kubectl is now configured to use "minikube" cluster
      Deploy to Kubernetes
      [command] kubectl apply -f nginx-deployment.yaml
      deployment.apps/nginx-deployment created
      service/nginx-service created
      Verify Deployment
      [command] kubectl get pods
      NAME                               READY   STATUS    RESTARTS   AGE
      nginx-deployment-5d9f8b6f5-abcde   1/1     Running   0          10s
      nginx-deployment-5d9f8b6f5-fghij   1/1     Running   0          10s

      (Pipeline chạy thành công, triển khai 2 Pod Nginx).

Bước 5: Xóa Tài Nguyên Để Dọn Dẹp
  • Hành động:
    1. Xóa tài nguyên trên Minikube local:
      kubectl delete -f nginx-deployment.yaml
      minikube stop
      minikube delete
    2. Xóa repository trên GitHub (tùy chọn):
      • Truy cập repository > Settings > Delete this repository.
  • Kết quả thực tế:
    • Sau khi chạy kubectl delete, terminal hiển thị:
      deployment.apps "nginx-deployment" deleted
      service "nginx-service" deleted
    • Sau khi chạy minikube delete, terminal hiển thị:
          Deleting "minikube" in docker ...
          Deleted minikube cluster

Lưu Ý Quan Trọng

  • Minikube trong CI/CD: Minikube phù hợp để thử nghiệm. Trong production, dùng cluster thực (VD: EKS, GKE).
  • Bảo mật: Nếu dùng cluster production, lưu thông tin truy cập (kubeconfig) trong GitHub Secrets.
  • Tài liệu tham khảo: Xem thêm về GitHub Actions (GitHub Actions Documentation).
Điều hướng chuỗi bài viết<< Bài 6: Kubernetes Helm – Tự Động Triển Khai Ứng Dụng Dễ Dàng
>> Bài 8: Kubernetes Monitoring – Giám Sát Với Prometheus Và Grafana
Article Thumbnail
Article Thumbnail
Datadog Webinar: Modernize AWS Logs at Scale
Chia sẻ bài viết:
Theo dõi
Thông báo của
0 Góp ý
Được bỏ phiếu nhiều nhất
Mới nhất Cũ nhất
Phản hồi nội tuyến
Xem tất cả bình luận