Danh sách bài viết trong series Kubernetes cơ bản
- Bài 1: Kubernetes Cơ Bản – Cài Đặt Minikube Và Chạy Pod Đầu Tiên
- Bài 2: Kubernetes Pod Deployment – Triển Khai Ứng Dụng Đầu Tiên
- Bài 3: Kubernetes Service Ingress – Quản Lý Truy Cập Ứng Dụng
- Bài 4: Kubernetes ConfigMap Secret – Quản Lý Cấu Hình Ứng Dụng
- Bài 5: Kubernetes EKS AWS – Triển Khai Cluster Trên AWS
- Bài 6: Kubernetes Helm – Tự Động Triển Khai Ứng Dụng Dễ Dàng
- Bài 7: Kubernetes CI/CD – Tự Động Triển Khai Với GitHub Actions
- Bài 8: Kubernetes Monitoring – Giám Sát Với Prometheus Và Grafana
- Bài 9: Kubernetes Autoscaling – Tối Ưu Hóa Cluster Hiệu Quả
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:
- Khởi động Minikube:
minikube start --driver=docker
- Tạo repository trên GitHub:
- Truy cập GitHub, tạo repository mới (VD:
k8s-cicd-example
).
- Truy cập GitHub, tạo repository mới (VD:
- Tạo thư mục dự án local:
mkdir k8s-cicd-example cd k8s-cicd-example git init
- Khởi động Minikube:
- 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/
- Sau khi chạy
Bước 2: Tạo Ứng Dụng Và File Kubernetes Manifest
- Hành động:
- 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
- Tạo file
- 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
).
- Cấu trúc thư mục:
Bước 3: Cấu Hình GitHub Actions Workflow
-
Hành động:
- Tạo thư mục và file workflow:
mkdir -p .github/workflows touch .github/workflows/deploy.yml
-
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
- Tạo thư mục và file workflow:
- Kết quả thực tế:
- Cấu trúc thư mục:
k8s-cicd-example/ ├── .github/ │ └── workflows/ │ └── deploy.yml └── nginx-deployment.yaml
- Cấu trúc thư mục:
Bước 4: Đẩy Mã Và Kiểm Tra Pipeline
- Hành động:
- 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). - Kiểm tra pipeline trên GitHub:
- Truy cập repository
k8s-cicd-example
> Actions.
- Truy cập repository
- Commit và đẩy mã lên GitHub:
- 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).
- Sau khi đẩy mã, terminal hiển thị:
Bước 5: Xóa Tài Nguyên Để Dọn Dẹp
- Hành động:
- Xóa tài nguyên trên Minikube local:
kubectl delete -f nginx-deployment.yaml minikube stop minikube delete
- Xóa repository trên GitHub (tùy chọn):
- Truy cập repository > Settings > Delete this repository.
- Xóa tài nguyên trên Minikube local:
- 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
- Sau khi chạy
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).