Declarative Setup จัดการ Argo CD Configuration ทั้งหมดด้วย YAML

Declarative Setup จัดการ Argo CD Configuration ทั้งหมดด้วย YAML

Argo CD เป็นเครื่องมือ GitOps ที่มีพลังสำหรับการจัดการ Kubernetes deployment อย่างไรก็ตาม การกำหนดค่า Argo CD เองนั้นมักจำเป็นต้องใช้ Web UI หรือ CLI command ต่างๆ แต่ Declarative Setup เสนอวิธีการที่ดีกว่าหลายเท่า โดยให้คุณจัดการการกำหนดค่า Argo CD ทั้งหมดผ่าน YAML files ที่เก็บไว้ใน Git repository บทความนี้จะอธิบายแนวคิด ประโยชน์ และวิธีปฏิบัติสำหรับ Declarative Setup ใน Argo CD

Declarative Setup คืออะไร?

Declarative Setup คือแนวทางการจัดการ Argo CD โดยใช้ YAML manifest files ที่สอดคล้องกับ GitOps philosophy เมื่อใช้ Declarative Setup คุณจะกำหนดการกำหนดค่า Argo CD ทั้งหมด เช่น repositories, projects, applications, RBAC policies, notification settings ลงใน YAML files และจัดเก็บไว้ใน Git repository

แทนที่จะคลิกปุ่มในเว็บ UI หรือพิมพ์ kubectl commands คุณเพียงแค่ commit YAML files ไปยัง repository และให้ Argo CD ทำการแปลงตามรูปแบบ Version control system เอง นี่คือจุดแข็งของ Declarative Setup – ทุกสิ่งที่ทำจะมีการติดตามใน Git history

ความแตกต่างระหว่าง Declarative และ Imperative

  • Imperative Approach: ใช้ Web UI หรือ CLI commands เพื่อบอกระบบ “ทำอะไร” ทีละขั้นตอน
  • Declarative Approach: ใช้ YAML files เพื่อระบุ “สถานะที่ต้องการ” และให้ Argo CD ดูแลให้สถานะปัจจุบันตรงกับสถานะที่ต้องการ

ประโยชน์ของ Declarative Setup

1. Version Control และ Auditability

ทุกการเปลี่ยนแปลงการกำหนดค่า Argo CD จะถูกบันทึกใน Git repository ทำให้คุณสามารถดูประวัติการเปลี่ยนแปลง ใครเปลี่ยนแปลง และเมื่อไร

2. Reproducibility

เมื่อคุณต้องสร้าง Argo CD cluster ใหม่หรือ restore จาก disaster คุณสามารถใช้ YAML files เดิมเพื่อสร้างการกำหนดค่าเดียวกันได้อย่างรวดเร็ว

3. Collaboration

Team members สามารถทำการ review configuration changes ผ่าน Pull Request ก่อนให้มีผลใช้งาน

4. Consistency และ Standards

การกำหนดค่าทั้งหมดมาจากแหล่งเดียว ทำให้มั่นใจว่ามีความสอดคล้องกัน

5. Disaster Recovery

หากเกิดปัญหาเกี่ยวกับ Argo CD คุณสามารถ re-apply configuration จากไฟล์ YAML เพื่อกู้คืนสถานะได้อย่างรวดเร็ว

6. GitOps Philosophy

Declarative Setup สอดคล้องกับ GitOps philosophy อย่างสมบูรณ์

การกำหนดค่า Repositories แบบ Declarative

Repository credentials ใน Argo CD สามารถกำหนดได้ผ่าน YAML manifests โดยใช้ Kubernetes Secret resources

สร้าง Repository Secret

apiVersion: v1
kind: Secret
metadata:
  name: private-repo-credentials
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: repository
type: Opaque
stringData:
  type: git
  url: https://github.com/myorg/myrepo.git
  password: your-github-token
  username: not_used
---
apiVersion: v1
kind: Secret
metadata:
  name: ssh-repo-credentials
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: repository
type: Opaque
stringData:
  type: git
  url: [email protected]:myorg/another-repo.git
  sshPrivateKey: |
    -----BEGIN OPENSSH PRIVATE KEY-----
    [Your SSH private key content here]
    -----END OPENSSH PRIVATE KEY-----

การกำหนดค่า Projects แบบ Declarative

AppProject resources ใช้เพื่อจัดกลุ่มและควบคุม Applications รวมถึง RBAC policies

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: production-project
  namespace: argocd
spec:
  description: โครงการสำหรับ production environment
  sourceRepos:
  - https://github.com/myorg/production-apps.git
  - https://github.com/myorg/shared-configs.git
  destinations:
  - namespace: 'production-*'
    server: 'https://kubernetes.default.svc'
  orphanedResources:
    warn: true
---
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: development-project
  namespace: argocd
spec:
  description: โครงการสำหรับ development environment
  sourceRepos:
  - '*'
  destinations:
  - namespace: 'development'
    server: 'https://kubernetes.default.svc'

การกำหนดค่า Applications แบบ Declarative

Application manifests เป็นหัวใจของ Argo CD โดยกำหนด source code repository และ destination cluster

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-web-app
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  project: production-project
  source:
    repoURL: https://github.com/myorg/production-apps.git
    targetRevision: main
    path: web-app/
    helm:
      values: |
        image:
          repository: myregistry.azurecr.io/web-app
          tag: v1.2.3
  destination:
    server: https://kubernetes.default.svc
    namespace: production-apps
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
    - CreateNamespace=true
    retry:
      limit: 5
      backoff:
        duration: 5s
        factor: 2
        maxDuration: 3m

การกำหนดค่า RBAC แบบ Declarative

RBAC ใน Argo CD สามารถกำหนดได้ผ่าน argocd-rbac-cm ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-rbac-cm
  namespace: argocd
data:
  policy.default: role:viewer
  policy.csv: |
    p, role:admin, applications, *, */*, allow
    p, role:admin, repositories, *, *, allow
    p, role:admin, projects, *, *, allow
    p, role:readonly, applications, get, */*, allow
    p, role:deployer, applications, get, */*, allow
    p, role:deployer, applications, sync, */*, allow
    g, developers-team, role:deployer
    g, ops-team, role:admin
    g, auditors, role:readonly

การกำหนดค่า Argo CD Server แบบ Declarative

argocd-cm ConfigMap เป็นตัวควบคุมหลักสำหรับการกำหนดค่า Argo CD Server

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-cm
  namespace: argocd
data:
  application.instanceLabelKey: argocd.argoproj.io/instance
  application.resourceTrackingMethod: annotation
  server.insecure: 'false'
  url: https://argocd.example.com
  webhook.github.secret: your-webhook-secret
  oidc.config: |
    name: "Azure AD"
    issuer: "https://login.microsoftonline.com/YOUR-TENANT-ID/v2.0"
    clientID: "YOUR-CLIENT-ID"
    clientSecret: "$oidc.azuread.clientSecret"
    requestedScopes:
    - "openid"
    - "profile"
    - "email"

การกำหนดค่า Notifications แบบ Declarative

Argo CD Notifications ช่วยให้คุณส่งการแจ้งเตือนไปยัง Slack, Teams, Email เมื่อเกิด events

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-notifications-cm
  namespace: argocd
data:
  service.slack: |
    token: $slack-token
  template.app-deployed: |
    message: |
      Application {{.app.metadata.name}} is now running new version.
      Sync Result:
      *Revision:* {{.app.status.sync.revision}}
    slack:
      attachments: |
        [{"color": "#18be52", "fields": [{"title": "Sync Status", "value": "{{.app.status.sync.status}}", "short": true}]}]
  trigger.on-deployed: |
    - when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status == 'Healthy'
      send: [app-deployed]

Bootstrap Argo CD ด้วย Declarative Configuration

สร้าง Application ที่ชี้ไปยัง repository ที่เก็บ configuration เพื่อให้ Argo CD จัดการตัวเอง

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: argocd-bootstrap
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/myorg/argocd-config.git
    targetRevision: main
    path: argocd-config/
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

โครงสร้าง Directory ที่แนะนำ

argocd-config/
├── argocd/
│   ├── base/
│   │   ├── argocd-cm.yaml
│   │   ├── argocd-rbac-cm.yaml
│   │   └── kustomization.yaml
│   └── overlays/
│       ├── production/
│       └── staging/
├── projects/
│   ├── production-project.yaml
│   └── development-project.yaml
├── repositories/
│   └── github-credentials.yaml
├── notifications/
│   └── argocd-notifications-cm.yaml
└── applications/
    ├── production/
    └── staging/

Best Practices สำหรับ Declarative Setup

1. ใช้ Kustomize หรือ Helm สำหรับการจัดการ Variants

เมื่อคุณมี configuration ที่ต้องการ reuse ระหว่าง environments ให้ใช้ Kustomize overlays หรือ Helm values

2. เก็บ Secrets อย่างปลอดภัย

ไม่ควร commit plain text secrets ไปยัง Git repository ให้ใช้ Sealed Secrets หรือ External Secret Operator

3. ทำการ Review Configuration Changes

ให้ทีมทำการ review ทุก Pull Request ที่เกี่ยวข้องกับ configuration changes

4. ใช้ Namespace ที่ถูกต้อง

ให้สร้าง Argo CD resources ทั้งหมดใน argocd namespace

5. ทดสอบการตั้งค่าก่อนใช้งานจริง

ให้ test Declarative Configuration ใน development environment ก่อนนำไปใช้ production

Argo CD บน ผู้ให้บริการโฮสติ้ง Cloud VPS

เมื่อ deploy Argo CD บน ผู้ให้บริการโฮสติ้ง Cloud VPS การใช้ Declarative Setup ช่วยให้จัดการการกำหนดค่า infrastructure ได้อย่างมีประสิทธิภาพ

ผู้ให้บริการโฮสติ้ง Cloud VPS มอบ:

  • Kubernetes Cluster Management: สามารถสร้างและจัดการ Kubernetes cluster ได้อย่างง่ายดาย
  • High Performance Infrastructure: CPU, Memory, และ Storage ที่เพียงพอสำหรับ Argo CD
  • Network Security: Private network, firewall, และ DDoS protection
  • Backup and Disaster Recovery: Automatic backup features

สรุป

Declarative Setup ใน Argo CD คือวิธีที่ดีที่สุดในการจัดการการกำหนดค่า ทำให้ระบบ GitOps สมบูรณ์ ด้วยการจัดเก็บทุก configuration ใน YAML files คุณได้รับ version control, auditability, reproducibility และการร่วมมือที่ดีขึ้น

หากคุณกำลังหา infrastructure ที่มั่นคงสำหรับ host Argo CD ให้พิจารณา ผู้ให้บริการโฮสติ้ง Cloud VPS ซึ่งมอบ managed Kubernetes services พร้อมทุก features ที่คุณต้องการ

เกี่ยวกับ ผู้ให้บริการโฮสติ้ง: ผู้ให้บริการโฮสติ้ง Co, Ltd. (https://de.co.th) เป็น cloud service provider ที่มอบบริการ Cloud VPS, Cloud Hosting, และ Cloud Email สำหรับธุรกิจในประเทศไทย