0

Lab 17 — ApplicationSet với ArgoCD

🎯 Mục tiêu

Sau Lab này, bạn sẽ:

  • Hiểu ApplicationSet giải quyết vấn đề gì trong ArgoCD.
  • Hiểu cách ApplicationSet tự động tạo nhiều Application.
  • Biết sử dụng List GeneratorGit Generator.
  • Quản lý nhiều environment/cluster mà không phải tạo từng Application thủ công.
  • Hiểu một pattern rất thường gặp trong hệ thống GitOps thực tế.

🤔 1. Vấn đề thực tế

Ở các Lab trước, bạn đã biết cách dùng ArgoCD để quản lý Application.

Ví dụ:

ArgoCD
   │
   └── Todo App
          │
          └── Kubernetes

Ban đầu mọi thứ khá đơn giản.

Nhưng hãy tưởng tượng công ty có:

Development
Staging
Production

Và mỗi environment cần một ArgoCD Application:

ArgoCD
 ├── todo-dev
 ├── todo-staging
 └── todo-production

Nếu có 3 Application thì tạo thủ công vẫn ổn.

Nhưng production thực tế có thể có:

10 applications
×
5 environments
×
3 clusters

Bạn sẽ phải quản lý rất nhiều Application.

😵 Cách làm thủ công

Application 1 YAML
Application 2 YAML
Application 3 YAML
Application 4 YAML
Application 5 YAML
...

Vấn đề là các YAML này thường gần như giống nhau.

Chúng chỉ khác:

name
cluster
namespace
environment
path

Đây chính là lúc ApplicationSet phát huy tác dụng.


🧠 2. Hiểu nhanh

2.1 ApplicationSet là gì?

ApplicationSet là một resource của ArgoCD dùng để tự động tạo và quản lý nhiều Application dựa trên một template.

Thay vì nói:

"Hãy tạo Application A, B, C."

Bạn nói:

"Đây là template Application. Hãy tạo Application cho tất cả environment tôi cung cấp."

Ví dụ:

ApplicationSet
      │
      │ Template
      ▼
 ┌─────────────┐
 │ Todo App    │
 └─────────────┘
      │
      ├──── dev
      ├──── staging
      └──── production

ArgoCD sẽ tự sinh:

todo-dev
todo-staging
todo-production

2.2 ApplicationSet khác Application như thế nào?

Đây là điểm rất quan trọng.

Application

Đại diện cho một application deployment:

Application
    │
    └── todo-production
            │
            └── Kubernetes

ApplicationSet

Đại diện cho logic tạo nhiều Application:

ApplicationSet
      │
      ├── todo-dev
      ├── todo-staging
      └── todo-production

Có thể hiểu đơn giản:

Application = một chiếc xe ApplicationSet = dây chuyền sản xuất xe


🏗️ 3. Architecture

Trong Lab này, chúng ta sẽ xây dựng:

                    Git Repository
                         │
                         │
                         ▼
                  ┌──────────────┐
                  │ ApplicationSet│
                  └───────┬──────┘
                          │
              Generate Applications
                          │
              ┌───────────┼───────────┐
              ▼           ▼           ▼
          todo-dev    todo-staging  todo-prod
              │           │           │
              ▼           ▼           ▼
          Kubernetes  Kubernetes  Kubernetes

Trong Lab đầu tiên, chúng ta dùng List Generator để hiểu concept trước.

Sau đó sẽ dùng Git Generator, cách này gần với production hơn.


🛠️ 4. Chuẩn bị môi trường

Bạn cần có:

  • Kubernetes cluster.
  • ArgoCD đã được cài đặt.
  • kubectl.
  • Git repository chứa Kubernetes manifests.

Kiểm tra ArgoCD:

kubectl get pods -n argocd

Bạn cần thấy các Pod ArgoCD đang Running.

Kiểm tra:

kubectl get applications -n argocd

Nếu ArgoCD đang hoạt động bình thường, chúng ta bắt đầu.


🧪 5. Lab 1 — Tạo nhiều Application bằng List Generator

5.1 Tại sao bắt đầu với List Generator?

Chúng ta chưa cần Git phức tạp.

Trước tiên hãy hiểu:

1 ApplicationSet
        ↓
3 Applications

Sau khi hiểu cơ chế này, chuyển sang Git Generator sẽ dễ hơn rất nhiều.


5.2 Tạo ApplicationSet

Tạo file:

mkdir -p applicationset
cd applicationset

Tạo:

vim todo-applicationset.yaml

Nội dung:

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: todo-apps
  namespace: argocd
spec:
  generators:
    - list:
        elements:
          - environment: dev
            namespace: todo-dev
          - environment: staging
            namespace: todo-staging
          - environment: production
            namespace: todo-production

  template:
    metadata:
      name: "todo-{{environment}}"

    spec:
      project: default

      source:
        repoURL: https://github.com/YOUR_USERNAME/YOUR_REPO.git
        targetRevision: main
        path: kubernetes/base

      destination:
        server: https://kubernetes.default.svc
        namespace: "{{namespace}}"

      syncPolicy:
        automated:
          prune: true
          selfHeal: true

Thay repoURL bằng Git repository của bạn.


🔍 6. Đọc YAML theo cách dễ hiểu

Bạn chưa cần nhớ toàn bộ syntax.

Chỉ cần hiểu 3 phần:

ApplicationSet
│
├── generators
│
└── template

generators

Quyết định:

"Tôi muốn tạo Application cho những đối tượng nào?"

Ở đây:

elements:
  - environment: dev
  - environment: staging
  - environment: production

Có 3 environment.


template

Quyết định:

"Mỗi Application được tạo ra sẽ có cấu hình như thế nào?"

Ví dụ:

name: "todo-{{environment}}"

ArgoCD sẽ thay:

{{environment}}

bằng từng giá trị.

Kết quả:

todo-dev
todo-staging
todo-production

▶️ 7. Apply ApplicationSet

Chạy:

kubectl apply -f todo-applicationset.yaml

Kiểm tra:

kubectl get applicationset -n argocd

Kết quả:

NAME        AGE
todo-apps   10s

Bây giờ kiểm tra Application:

kubectl get applications -n argocd

Bạn sẽ thấy:

NAME               SYNC STATUS   HEALTH STATUS
todo-dev           Synced        Healthy
todo-staging       Synced        Healthy
todo-production    Synced        Healthy

🎉 Bạn vừa tạo 3 ArgoCD Application chỉ bằng 1 ApplicationSet.


👀 8. See Result

Kiểm tra chi tiết:

kubectl describe applicationset todo-apps -n argocd

Hoặc mở ArgoCD UI.

Bạn sẽ thấy:

Applications
│
├── todo-dev
├── todo-staging
└── todo-production

Điểm quan trọng:

Bạn không tạo 3 Application YAML riêng biệt.

Bạn chỉ tạo:

1 ApplicationSet

và ArgoCD tự sinh:

3 Applications

🧠 9. Hiểu bản chất

Luồng hoạt động là:

ApplicationSet
      │
      │ Read generator
      ▼
┌───────────────────────────┐
│ dev                       │
│ staging                   │
│ production                │
└────────────┬──────────────┘
             │
             │ Apply template
             ▼
      ┌───────────────┐
      │ Applications  │
      └───────┬───────┘
              │
      ┌───────┼────────┐
      ▼       ▼        ▼
    dev    staging   production

Có một điểm rất quan trọng:

ApplicationSet không trực tiếp deploy application.

Nó tạo ra các Application.

Sau đó:

Application
      ↓
ArgoCD Controller
      ↓
Kubernetes

mới thực hiện deployment.


🧪 10. Lab 2 — Git Generator

List Generator khá dễ hiểu nhưng production thường không muốn hard-code:

- environment: dev
- environment: staging
- environment: production

trong ApplicationSet.

Thay vào đó, chúng ta có thể để Git quyết định environment.

Ví dụ repository:

gitops-repo/
│
└── apps/
    └── todo/
        ├── dev/
        ├── staging/
        └── production/

Git trở thành:

Source of Truth


🏗️ 11. Thiết kế Git Repository

Tạo cấu trúc:

gitops-repo/
│
└── apps/
    └── todo/
        │
        ├── dev/
        │   └── deployment.yaml
        │
        ├── staging/
        │   └── deployment.yaml
        │
        └── production/
            └── deployment.yaml

Mỗi directory đại diện cho một environment.


📝 12. Tạo Git Generator

Tạo:

vim todo-git-applicationset.yaml

Nội dung:

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: todo-git
  namespace: argocd

spec:
  generators:
    - git:
        repoURL: https://github.com/YOUR_USERNAME/YOUR_GITOPS_REPO.git
        revision: main
        directories:
          - path: apps/todo/*

  template:
    metadata:
      name: "todo-{{path.basename}}"

    spec:
      project: default

      source:
        repoURL: https://github.com/YOUR_USERNAME/YOUR_GITOPS_REPO.git
        targetRevision: main
        path: "{{path}}"

      destination:
        server: https://kubernetes.default.svc
        namespace: "todo-{{path.basename}}"

      syncPolicy:
        automated:
          prune: true
          selfHeal: true

🧠 13. Git Generator hoạt động thế nào?

Git Generator sẽ nhìn vào:

apps/todo/*

Git repository có:

apps/todo/dev
apps/todo/staging
apps/todo/production

ArgoCD phát hiện:

dev
staging
production

Sau đó áp dụng template.

Kết quả:

todo-dev
todo-staging
todo-production

▶️ 14. Apply

Chạy:

kubectl apply -f todo-git-applicationset.yaml

Kiểm tra:

kubectl get applications -n argocd

Kết quả mong đợi:

NAME               SYNC STATUS
todo-dev           Synced
todo-staging       Synced
todo-production    Synced

🎯 15. Thử nghiệm sức mạnh của ApplicationSet

Đây mới là phần thú vị.

Giả sử team muốn thêm:

qa

Bạn không cần sửa ApplicationSet.

Chỉ cần tạo:

apps/todo/qa/

Commit:

git add .
git commit -m "add qa environment"
git push

Sau khi ApplicationSet phát hiện thay đổi:

Git
 │
 │ new directory
 ▼
ApplicationSet
 │
 │ generate
 ▼
todo-qa

Kiểm tra:

kubectl get applications -n argocd

Bạn sẽ thấy thêm:

todo-qa

🔥 Đây chính là sức mạnh của GitOps.


🔄 16. Thử xóa Environment

Bây giờ thử xóa:

apps/todo/qa/

Commit và push.

ApplicationSet sẽ nhận ra environment không còn tồn tại.

Nếu cấu hình:

syncPolicy:
  automated:
    prune: true

Application tương ứng có thể được tự động xử lý theo cấu hình ApplicationSet/ArgoCD.

Trong production, cần đặc biệt cẩn thận với prune, nhất là production environment.


🧪 17. Exercise

Hãy tự thực hành mà chưa xem đáp án.

Exercise 1 — Thêm QA

Tạo:

apps/todo/qa/

Sau đó kiểm tra:

kubectl get applications -n argocd

Mục tiêu:

todo-qa

Exercise 2 — Thêm UAT

Tạo:

apps/todo/uat/

Mục tiêu:

todo-uat

Exercise 3 — Xóa environment

Xóa:

apps/todo/uat/

Theo dõi:

kubectl get applications -n argocd

Quan sát Application todo-uat.


🔧 18. Troubleshooting

❌ ApplicationSet không tạo Application

Kiểm tra:

kubectl get applicationset -n argocd

Sau đó:

kubectl describe applicationset todo-git -n argocd

Đặc biệt chú ý:

Events

❌ Git repository không được đọc

Kiểm tra:

repoURL:
revision:

Nếu repository private, ArgoCD phải được cấu hình credential để truy cập Git repository.


❌ Application được tạo nhưng OutOfSync

Kiểm tra:

kubectl get applications -n argocd

Sau đó xem chi tiết Application:

kubectl describe application todo-dev -n argocd

Nguyên nhân thường gặp:

Git path sai
Manifest sai
Namespace chưa tồn tại
Kubernetes resource lỗi

💡 19. Kinh nghiệm Production

19.1 Đừng biến ApplicationSet thành một file quá phức tạp

ApplicationSet nên tập trung vào:

Generate Application

Còn deployment configuration nên nằm trong:

Helm
Kustomize
GitOps repository

Không nên nhồi tất cả logic vào ApplicationSet.


19.2 Tách environment rõ ràng

Ví dụ:

apps/
└── todo/
    ├── dev/
    ├── staging/
    └── production/

Giúp team dễ hiểu:

Path
 ↓
Environment
 ↓
Application
 ↓
Cluster

19.3 Production cần kiểm soát chặt hơn

Không nên mặc định:

automated:
  prune: true

cho mọi production workload.

Một thao tác Git sai có thể dẫn đến:

Git change
    ↓
ApplicationSet
    ↓
ArgoCD
    ↓
Production

GitOps rất mạnh, nhưng:

Automation càng mạnh thì guardrail càng quan trọng.

Có thể sử dụng:

Pull Request
   ↓
Review
   ↓
Merge
   ↓
ArgoCD
   ↓
Production

🧠 20. Tổng kết

Trước ApplicationSet:

Application YAML
Application YAML
Application YAML
Application YAML

Sau ApplicationSet:

              ApplicationSet
                    │
        ┌───────────┼───────────┐
        ▼           ▼           ▼
     dev         staging     production

Bạn chỉ cần quản lý:

Template
+
Generator

ArgoCD sẽ tạo các Application tương ứng.

Điều quan trọng nhất cần nhớ

Application
    =
Một deployment

ApplicationSet
    =
Cách tự động tạo nhiều deployment

Và với Git Generator:

Git Repository
      │
      │ directories
      ▼
ApplicationSet
      │
      │ generate
      ▼
Applications
      │
      ▼
Kubernetes

Đây là một pattern rất quan trọng khi bạn bắt đầu quản lý nhiều application, nhiều environment hoặc nhiều Kubernetes cluster bằng ArgoCD.


🏁 21. Checklist

  • [ ] Hiểu ApplicationSet khác Application.
  • [ ] Tạo ApplicationSet bằng List Generator.
  • [ ] Tạo nhiều Application từ một template.
  • [ ] Sử dụng Git Generator.
  • [ ] Tự động tạo Application từ Git directory.
  • [ ] Thêm environment mới thông qua Git.
  • [ ] Xóa environment và quan sát ApplicationSet.
  • [ ] Biết các vấn đề production liên quan đến prune và automation.

🚀 Bước tiếp theo

Ở Lab này, chúng ta đã giải quyết bài toán:

"Có quá nhiều Application phải tạo và quản lý thủ công."

Nhưng chúng ta vẫn chưa giải quyết một bài toán lớn hơn:

"Làm thế nào để triển khai cùng một application lên nhiều Kubernetes cluster một cách có hệ thống?"

Đó là lúc các pattern như ApplicationSet + multiple clusters + Progressive Delivery trở nên quan trọng.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí