ApplicationSet Generators: List, Cluster, Git และ Matrix อธิบายครบทุกแบบ
Argo CD ApplicationSet เป็นเครื่องมือที่ช่วยให้เราสามารถ Deploy Application ไปยังหลาย Cluster หรือหลาย Environment ได้พร้อมๆ กัน โดยการใช้ Generator ต่างๆ เพื่อสร้าง Application Resource อย่างอัตโนมัติ บทความนี้จะอธิบายรายละเอียดของทุก Generator Type พร้อมด้วยตัวอย่างการใช้งาน
ApplicationSet Generators ทั้งหมด
- List Generator – สร้าง Application จากรายการ Parameter
- Cluster Generator – สร้าง Application สำหรับแต่ละ Cluster
- Git Generator – สร้าง Application จากไฟล์หรือ Directory ใน Git
- Matrix Generator – รวม Generator หลายตัวเข้าด้วยกัน
- Merge Generator – รวม Parameter จาก Generator หลายตัว
- SCM Provider Generator – สร้าง Application จากหลาย Repository
- Pull Request Generator – สร้าง Application จาก Pull Request
1. List Generator – การสร้าง Application จากรายการ
List Generator เป็น Generator ที่ง่ายที่สุด ใช้สำหรับสร้าง Application จากรายการค่าที่กำหนดไว้
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: multi-env-apps
namespace: argocd
spec:
generators:
- list:
elements:
- name: development
url: https://github.com/myorg/dev-repo
- name: staging
url: https://github.com/myorg/staging-repo
- name: production
url: https://github.com/myorg/prod-repo
template:
metadata:
name: '{{name}}-app'
spec:
project: default
source:
repoURL: '{{url}}'
targetRevision: HEAD
path: 'apps/{{name}}'
destination:
server: https://kubernetes.default.svc
namespace: '{{name}}'
syncPolicy:
automated:
prune: true
selfHeal: true
List Generator จะสร้าง Application 3 ตัว (development, staging, production) โดยแต่ละตัวจะมีชื่อและ URL ที่แตกต่างกัน
2. Cluster Generator – การสร้าง Application สำหรับแต่ละ Cluster
Cluster Generator ใช้สำหรับสร้าง Application สำหรับแต่ละ Cluster ที่ลงทะเบียนไว้ใน Argo CD
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: multi-cluster-apps
namespace: argocd
spec:
generators:
- clusters:
selector:
matchLabels:
deploy: 'true'
template:
metadata:
name: 'app-{{name}}'
spec:
project: default
source:
repoURL: https://github.com/dotenterprise/gitops-repo
targetRevision: HEAD
path: apps/common
destination:
server: '{{server}}'
namespace: production
syncPolicy:
automated:
prune: true
selfHeal: true
Cluster Generator จะค้นหาทุก Cluster ที่มี Label deploy: true และสร้าง Application สำหรับแต่ละ Cluster
3. Git Generator – การสร้าง Application จากไฟล์ใน Git
3.1 Git Generator – Directory Pattern
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: git-directory-app
namespace: argocd
spec:
generators:
- git:
repoURL: https://github.com/dotenterprise/apps-config
revision: main
directories:
- path: 'apps/*'
template:
metadata:
name: '{{path.basename}}-app'
spec:
project: default
source:
repoURL: https://github.com/dotenterprise/apps-config
targetRevision: main
path: '{{path.path}}'
destination:
server: https://kubernetes.default.svc
namespace: '{{path.basename}}'
syncPolicy:
automated:
prune: true
selfHeal: true
3.2 Git Generator – File Pattern
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: git-files-app
namespace: argocd
spec:
generators:
- git:
repoURL: https://github.com/dotenterprise/apps-config
revision: main
files:
- path: 'deployments/*/values.yaml'
template:
metadata:
name: '{{path[0]}}-app'
spec:
project: default
source:
repoURL: https://github.com/dotenterprise/apps-config
targetRevision: main
path: 'deployments/{{path[0]}}'
destination:
server: https://kubernetes.default.svc
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: true
4. Matrix Generator – การรวม Generator หลายตัว
Matrix Generator ใช้สำหรับรวม Generator หลายตัวเพื่อสร้าง Application ในรูปแบบ Cartesian Product
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: matrix-app
namespace: argocd
spec:
generators:
- matrix:
generators:
- list:
elements:
- app: frontend
- app: backend
- app: database
- clusters:
selector:
matchLabels:
env: production
template:
metadata:
name: '{{app}}-{{name}}'
spec:
project: default
source:
repoURL: https://github.com/dotenterprise/monorepo
targetRevision: HEAD
path: 'apps/{{app}}'
destination:
server: '{{server}}'
namespace: '{{app}}'
syncPolicy:
automated:
prune: true
selfHeal: true
ตัวอย่างนี้จะสร้าง Application จำนวน Apps x Clusters ตัว
5. Merge Generator – การรวม Parameter จาก Generator หลายตัว
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: merge-app
namespace: argocd
spec:
generators:
- merge:
mergeKeys:
- name
generators:
- list:
elements:
- name: app1
version: '1.0'
- name: app2
version: '2.0'
- list:
elements:
- name: app1
region: us-east
- name: app2
region: eu-west
template:
metadata:
name: '{{name}}-app'
spec:
project: default
source:
repoURL: https://github.com/dotenterprise/apps
targetRevision: HEAD
path: 'apps/{{name}}'
helm:
parameters:
- name: version
value: '{{version}}'
- name: region
value: '{{region}}'
destination:
server: https://kubernetes.default.svc
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: true
Merge Generator จะรวม Parameter ทั้ง version และ region สำหรับแต่ละ Application โดยใช้ name เป็น Key
6. SCM Provider Generator
SCM Provider Generator ใช้สำหรับค้นหา Repository ทั้งหมดจาก GitHub, GitLab แล้วสร้าง Application อัตโนมัติ
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: github-scm-apps
namespace: argocd
spec:
generators:
- scmProvider:
github:
organization: dotenterprise
tokenRef:
secretRef:
name: github-token
key: token
filters:
- repositoryMatch: '.*-app$'
- pathsExist:
- 'helm/values.yaml'
template:
metadata:
name: '{{repository}}-app'
spec:
project: default
source:
repoURL: '{{url}}'
targetRevision: '{{branch}}'
path: helm
destination:
server: https://kubernetes.default.svc
namespace: '{{repository}}'
syncPolicy:
automated:
prune: true
selfHeal: true
7. Pull Request Generator
Pull Request Generator ใช้สำหรับสร้าง Preview Application สำหรับแต่ละ Pull Request
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: pr-preview-apps
namespace: argocd
spec:
generators:
- pullRequest:
github:
owner: dotenterprise
repo: web-app
tokenRef:
secretRef:
name: github-token
key: token
template:
metadata:
name: 'preview-{{number}}-app'
spec:
project: default
source:
repoURL: https://github.com/dotenterprise/web-app
targetRevision: '{{head_sha}}'
path: k8s
destination:
server: https://kubernetes.default.svc
namespace: 'preview-{{number}}'
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
การใช้ ApplicationSet บน ผู้ให้บริการโฮสติ้ง Cloud VPS
เมื่อคุณใช้ Argo CD ApplicationSet กับบริการ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง คุณจะได้รับประโยชน์:
- Multi-Cluster Management – จัดการหลาย Cluster จากที่เดียว
- Automated Deployment – ระบบสร้างและจัดการ Application อัตโนมัติ
- Consistency Across Environments – นำ Manifest เดียวกันไปใช้ได้ในหลาย Environment
- Easy Scaling – เพิ่ม Environment ใหม่ได้ง่าย
- Cost Optimization – ลดค่าใช้จ่ายในการจัดการ Infrastructure
Best Practice สำหรับการใช้ ApplicationSet
- ใช้ Label ให้เหมาะสม – กำหนด Label ให้ชัดเจนสำหรับ Cluster
- ทดสอบ Generator ให้ครอบคลุม – ทดสอบว่า Generator สร้าง Application จำนวนที่ถูกต้อง
- ใช้ Sync Policy ที่เหมาะสม – Enable Auto Sync สำหรับ Non-critical Apps เท่านั้น
- จัดการ Secret อย่างปลอดภัย – เก็บ Token ใน Secret Object อย่างปลอดภัย
- Monitor ApplicationSet Status – ติดตามสถานะของ ApplicationSet อย่างสม่ำเสมอ
สรุป
ApplicationSet Generators เป็นเครื่องมือที่มีประสิทธิภาพสำหรับการจัดการ Multi-Cluster และ Multi-Environment Deployment ใน Argo CD การเลือกใช้ Generator ที่เหมาะสม จะช่วยให้คุณลดความซับซ้อนของ GitOps Pipeline
ผู้ให้บริการโฮสติ้ง ได้จัดเตรียม Cloud VPS Infrastructure ที่รองรับการใช้งาน Argo CD และ ApplicationSet อย่างเต็มที่ สำหรับรายละเอียดเพิ่มเติม ท่านสามารถติดต่อทีมงานของ ผู้ให้บริการโฮสติ้ง ได้ที่ https://de.co.th

