ApplicationSet คืออะไร? สร้าง Application จำนวนมากอัตโนมัติใน Argo CD

ApplicationSet คืออะไร? สร้าง Application จำนวนมากอัตโนมัติใน Argo CD

บทนำ: ปัญหาการจัดการ Application หลายตัว

เมื่อทำการ deploy application บน Kubernetes cluster หลายตัวหรือสภาพแวดล้อมต่างๆ (multi-cluster หรือ multi-tenant) นักพัฒนาและ DevOps Engineer มักจะต้องสร้าง Application resources จำนวนมากใน Argo CD ซ้ำๆ กัน ซึ่งเป็นงานที่ยุ่งยากและเสี่ยงต่อความผิดพลาด

ApplicationSet เป็นทางออกที่ยอดเยี่ยมสำหรับปัญหานี้ โดยอนุญาตให้คุณสร้าง Application resources หลายตัวโดยอัตโนมัติจากเทมเพลตเดียว

ApplicationSet คืออะไร

ApplicationSet เป็น Kubernetes Custom Resource Definition (CRD) ที่ขยายความสามารถของ Argo CD ให้สามารถสร้าง Application resources จำนวนมากจากเทมเพลตและตัวแปรที่กำหนดไว้

ประโยชน์หลักของ ApplicationSet

  • ลดความซ้ำซ้อน – เขียนเทมเพลตเดียว สร้างได้หลายตัว
  • ยืดหยุ่นและปรับตัวได้ดี – รองรับสถานการณ์ที่ซับซ้อน
  • ลดความผิดพลาด – ลดการพิมพ์ซ้ำและความไม่สอดคล้องกัน
  • ง่ายต่อการบำรุงรักษา – เปลี่ยนแปลงเทมเพลตเดียว มีผลกับทั้งหมด

โครงสร้างพื้นฐานของ ApplicationSet

ApplicationSet ประกอบด้วยส่วนหลัก 3 ส่วน:

1. Generators

ส่วนที่กำหนดวิธีสร้าง Application instances:

  • List Generator – สร้าง Application จากรายชื่อที่ระบุไว้
  • Cluster Generator – สร้าง Application สำหรับแต่ละ cluster
  • Git Generator – สร้าง Application จากไฟล์ใน Git repository
  • Matrix Generator – สร้างแบบ Cartesian product จากหลายชุดข้อมูล

2. Template

แม่แบบของ Application resource ที่จะสร้าง โดยสามารถใส่ตัวแปรที่จะถูกแทนที่ด้วยค่าจาก generators

3. Strategy

กลยุทธ์ในการ deploy Application instances

ตัวอย่าง ApplicationSet: List Generator

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: environments-appset
  namespace: argocd
spec:
  generators:
  - list:
      elements:
      - name: dev
        cluster: dev-cluster
        repoURL: https://github.com/yourorg/config-repo
      - name: staging
        cluster: staging-cluster
        repoURL: https://github.com/yourorg/config-repo
      - name: production
        cluster: prod-cluster
        repoURL: https://github.com/yourorg/config-repo
  template:
    metadata:
      name: 'my-app-{{name}}'
    spec:
      project: default
      source:
        repoURL: '{{repoURL}}'
        targetRevision: HEAD
        path: 'apps/{{name}}'
      destination:
        server: 'https://kubernetes.default.svc'
        namespace: 'app-{{name}}'
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
        - CreateNamespace=true

ApplicationSet นี้จะสร้าง Application 3 ตัว: my-app-dev, my-app-staging, และ my-app-production

ตัวอย่าง ApplicationSet: Cluster Generator

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: multi-cluster-appset
  namespace: argocd
spec:
  generators:
  - clusters:
      selector:
        matchLabels:
          production: 'true'
  template:
    metadata:
      name: 'my-app-{{name}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/yourorg/config-repo
        targetRevision: HEAD
        path: 'apps/my-app'
      destination:
        server: '{{server}}'
        namespace: default
      syncPolicy:
        syncOptions:
        - PrunePropagationPolicy=background

Cluster Generator จะสแกน cluster ทั้งหมดที่มี label production: true และสร้าง Application สำหรับแต่ละตัว

ตัวอย่าง ApplicationSet: Git Generator

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: git-generator-appset
  namespace: argocd
spec:
  generators:
  - git:
      repoURL: https://github.com/yourorg/config-repo
      revision: main
      directories:
      - path: 'apps/*'
  template:
    metadata:
      name: '{{path.basename}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/yourorg/config-repo
        targetRevision: main
        path: '{{path}}'
      destination:
        server: https://kubernetes.default.svc
        namespace: 'app-{{path.basename}}'
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
        - CreateNamespace=true

Git Generator จะค้นหาทุกโฟลเดอร์ภายใต้ apps/ directory และสร้าง Application สำหรับแต่ละตัว

ตัวอย่าง ApplicationSet: Matrix Generator

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: matrix-appset
  namespace: argocd
spec:
  generators:
  - matrix:
      generators:
      - list:
          elements:
          - cluster: west
          - cluster: east
      - list:
          elements:
          - env: dev
          - env: staging
          - env: prod
  template:
    metadata:
      name: 'app-{{cluster}}-{{env}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/yourorg/config-repo
        targetRevision: HEAD
        path: 'apps/{{cluster}}/{{env}}'
      destination:
        server: https://kubernetes.default.svc
        namespace: 'app-{{env}}'
      syncPolicy:
        automated:
          prune: true

Matrix Generator สร้าง Application 6 ตัว (2 clusters x 3 environments)

ApplicationSet vs App of Apps Pattern

ก่อนที่จะมี ApplicationSet นักพัฒนามักใช้ App of Apps pattern โดยสร้าง parent application ที่ deploy applications ตัวอื่นๆ

  • ApplicationSet: ง่ายกว่า รองรับ generators หลายชนิด สร้างแบบอัตโนมัติ
  • App of Apps: ยืดหยุ่นกว่า แต่ซับซ้อนกว่า ต้องเขียน parent app เอง

ใช้ ApplicationSet ถ้าต้องการสร้าง Application คล้ายกันจำนวนมาก ใช้ App of Apps ถ้าต้องการความยืดหยุ่นเพิ่มเติม

การนำ ApplicationSet ไปใช้บน ผู้ให้บริการโฮสติ้ง Cloud VPS

สำหรับองค์กรที่ใช้ ผู้ให้บริการโฮสติ้ง Cloud VPS เพื่อ host Kubernetes clusters หลายตัว ApplicationSet มีประโยชน์อย่างยิ่ง

  • ความเชื่อถือได้สูงและ uptime ที่ดี
  • ประสิทธิภาพทำให้ syncing applications หลายตัวมีความเสถียร
  • ความปลอดภัยระดับ enterprise
  • การสนับสนุนที่ดีจากทีม ผู้ให้บริการโฮสติ้ง

Best Practices และเคล็ดลับ

1. ใช้ Descriptive Naming Conventions

ตั้งชื่อ ApplicationSet ที่อธิบายจุดประสงค์ชัดเจน

2. เพิ่ม Labels และ Annotations

ใส่ labels ที่มีความหมาย เพื่อช่วยในการค้นหาและจัดการ

3. ทดสอบเทมเพลต

ก่อนนำไปใช้กับ production ให้ทดสอบ ApplicationSet ใน dev หรือ staging ก่อน

4. Monitor ApplicationSet Status

kubectl get applicationsets -n argocd
kubectl describe applicationset <name> -n argocd

สรุป

ApplicationSet เป็นคุณลักษณะอันทรงพลังของ Argo CD ที่ช่วยให้การสร้างและจัดการ Applications หลายตัวกลายเป็นเรื่องอัตโนมัติและเชื่อถือได้

ถ้าคุณต้องการแพลตฟอร์ม Kubernetes ที่เชื่อถือได้ ผู้ให้บริการโฮสติ้ง Cloud VPS คือตัวเลือกที่ดี ที่มีความเสถียร ความปลอดภัย และการสนับสนุนระดับ enterprise

บริษัท ผู้ให้บริการโฮสติ้ง Co, Ltd.

เว็บไซต์: https://de.co.th

บริการ Cloud VPS: https://de.co.th/cloud-vps

บริการ Cloud Hosting: https://de.co.th/cloud-hosting