Argo CD Project (AppProject) จัดกลุ่ม Application และควบคุมสิทธิ์

Argo CD Project (AppProject) คืออะไร

Argo CD Project หรือ AppProject เป็นวัตถุในระดับคลัสเตอร์ที่ช่วยให้คุณสามารถจัดกลุ่ม Application และกำหนดข้อจำกัดในการเข้าถึง โดยมี RBAC (Role-Based Access Control) ที่มีประสิทธิภาพ AppProject ช่วยให้คุณสามารถแยกเขตเพื่อให้หลายทีมสามารถใช้สิ่งอำนวยความสะดวก Argo CD เดียวกันได้อย่างปลอดภัย

กล่าวอีกนัยหนึ่ง AppProject ช่วยในการ:

  • จัดกลุ่ม Application ตามทีม โปรเจกต์ หรือสภาพแวดล้อม
  • จำกัดว่า Application ใดสามารถอ้างอิงถึง Git repository ใดได้
  • กำหนดว่า Application ใดสามารถปรับใช้ได้กับคลัสเตอร์ Kubernetes หรือเนมสเปซใด
  • กำหนดสิทธิ์การเข้าถึงแบบละเอียด
  • จัดการทรัพยากรคลัสเตอร์ที่อนุญาต

เหตุใดจึงใช้ Argo CD Project

ในสภาพแวดล้อมการผลิตที่จริงจัง โดยเฉพาะอย่างยิ่งเมื่อใช้โครงสร้างพื้นฐาน Cloud VPS จาก ผู้ให้บริการโฮสติ้ง การบริหารจัดการสิทธิ์และการแยกเขตเป็นสิ่งจำเป็น

ตัวอย่างเหตุการณ์ที่ AppProject มีประโยชน์:

  • การแยกแยะหลายทีม: ทีม Frontend สามารถจัดการเฉพาะ Application frontend ส่วนทีม Backend จัดการเฉพาะ Application backend
  • ความปลอดภัย: ทีมหนึ่งไม่สามารถแก้ไข Application ของทีมอื่นได้
  • การควบคุมต้นทาง: จำกัด Git repository ที่แต่ละทีมสามารถปรับใช้ได้
  • การควบคุมปลายทาง: จำกัดคลัสเตอร์ Kubernetes ที่ Application สามารถปรับใช้ได้

สร้าง AppProject YAML Manifest

ในการสร้าง AppProject คุณต้องกำหนด AppProject CRD (Custom Resource Definition) ใน Kubernetes ลองดูตัวอย่าง AppProject พื้นฐาน:

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: frontend-team
  namespace: argocd
spec:
  description: "Project สำหรับทีม Frontend"
  sourceRepos:
  - 'https://github.com/mycompany/frontend-repo'
  - 'https://github.com/mycompany/common-repo'
  destinations:
  - namespace: 'frontend-*'
    server: 'https://kubernetes.default.svc'
  clusterResourceWhitelist:
  - group: ''
    kind: 'Namespace'
  - group: 'apps'
    kind: 'Deployment'
  - group: ''
    kind: 'Service'
  - group: ''
    kind: 'ConfigMap'
  - group: ''
    kind: 'Secret'

การกำหนดค่า sourceRepos

sourceRepos ใช้ในการระบุ Git repository ที่ AppProject นี้สามารถอ้างอิงได้ นี่คือการควบคุมปลายทางสำหรับแหล่งที่มา

sourceRepos:
- 'https://github.com/mycompany/frontend-repo'
- 'https://github.com/mycompany/backend-repo'
- 'https://github.com/mycompany/*'

คุณยังสามารถใช้ wildcard patterns เพื่อให้มีความยืดหยุ่นมากขึ้น:

sourceRepos:
- 'https://github.com/mycompany/*'
- 'https://gitlab.com/mycompany/*'

การกำหนดค่า Destinations

destinations กำหนดว่า Application ในโปรเจกต์นี้สามารถปรับใช้ได้กับเนมสเปซและเซิร์ฟเวอร์ใด ตัวอย่างขั้นสูง:

destinations:
- namespace: 'frontend-*'
  server: 'https://production-cluster.example.com'
- namespace: 'staging'
  server: 'https://staging-cluster.example.com'
- namespace: 'dev-*'
  server: '*'

clusterResourceWhitelist และ Namespace Resources

clusterResourceWhitelist ระบุทรัพยากร Kubernetes ใดที่สามารถสร้าง อัปเดต หรือลบได้ภายใน AppProject นี้

clusterResourceWhitelist:
- group: ''
  kind: 'Namespace'
- group: ''
  kind: 'Pod'
- group: ''
  kind: 'Service'
- group: ''
  kind: 'ConfigMap'
- group: ''
  kind: 'Secret'
- group: 'apps'
  kind: 'Deployment'
- group: 'apps'
  kind: 'StatefulSet'
- group: 'apps'
  kind: 'DaemonSet'
- group: 'networking.k8s.io'
  kind: 'NetworkPolicy'

นอกจากนี้ คุณยังสามารถใช้ deny list โดยใช้ clusterResourceBlacklist:

clusterResourceBlacklist:
- group: ''
  kind: 'ResourceQuota'
- group: ''
  kind: 'NetworkPolicy'

Roles และ RBAC ภายใน AppProject

Argo CD ให้คุณสามารถกำหนด roles ที่มีสิทธิ์ต่างๆ ภายใน AppProject:

apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: development
  namespace: argocd
spec:
  description: "Project สำหรับสภาพแวดล้อมพัฒนา"
  sourceRepos:
  - 'https://github.com/mycompany/*'
  destinations:
  - namespace: 'dev-*'
    server: 'https://dev-cluster.example.com'
  roles:
  - name: developers
    policies:
    - p, proj:development:developers, applications, get, development/*, allow
    - p, proj:development:developers, applications, sync, development/*, allow
    groups:
    - 'your-org:developers'
  - name: admins
    policies:
    - p, proj:development:admins, applications, *, development/*, allow
    groups:
    - 'your-org:platform-team'

Default Project vs Custom Projects

Argo CD มี default project ที่ชื่อ “default” ซึ่งไม่มีข้อจำกัดใดๆ สำหรับสภาพแวดล้อมการผลิต คุณควรหลีกเลี่ยงการใช้ default project และสร้าง custom projects แทน

ข้อแตกต่างหลัก:

  • Default Project: ไม่มีข้อจำกัด ใช้สำหรับการพัฒนาและการทดสอบเท่านั้น
  • Custom Projects: มีข้อจำกัด RBAC ที่ชัดเจน เหมาะสำหรับการผลิต

จัดการ AppProject ผ่าน CLI

คุณสามารถสร้าง รับ และจัดการ AppProject ผ่าน argocd CLI:

# ดูรายชื่อ projects ทั้งหมด
argocd proj list
# ดูรายละเอียด project
argocd proj get frontend-team
# สร้าง project ใหม่
argocd proj create frontend-team
# เพิ่ม source repository
argocd proj add-source frontend-team https://github.com/mycompany/frontend-repo
# เพิ่ม destination
argocd proj add-destination frontend-team https://dev-cluster.example.com frontend-ns
# สร้าง token สำหรับ role
argocd proj role create-token frontend-team developers

จัดการผ่าน Argo CD UI

นอกจากนี้ คุณสามารถจัดการ AppProject ผ่าน Argo CD web UI โดยไปที่:

  • Settings > Projects
  • คลิก “Create Project” เพื่อสร้าง project ใหม่
  • กำหนด source repositories, destinations, และ RBAC rules
  • บันทึกการเปลี่ยนแปลง

Best Practices สำหรับสภาพแวดล้อมมัลติทีม

เมื่อใช้ ผู้ให้บริการโฮสติ้ง Cloud VPS สำหรับการโฮสต์คลัสเตอร์ Kubernetes หลายตัว นี่คือ best practices ในการจัดการ AppProject:

  1. จัดกลุ่มตามทีม: สร้าง AppProject สำหรับแต่ละทีม ไม่ใช่ตามแอปพลิเคชัน
  2. กำหนดข้อจำกัดแหล่งที่มา: จำกัด Git repository เพื่อป้องกันการแสวงหาทรัพยากร
  3. แยกสภาพแวดล้อม: ใช้ projects ที่แตกต่างกันสำหรับ dev, staging, production
  4. ใช้ RBAC: กำหนด roles อย่างละเอียดและใช้ groups จาก identity provider ของคุณ
  5. ตรวจสอบ Whitelist: อนุญาตเฉพาะ Kubernetes resource types ที่จำเป็น
  6. ใช้ wildcards อย่างชาญฉลาด: wildcards ช่วยให้ยืดหยุ่น แต่อย่าให้ยืดหยุ่นเกินไป
  7. ตรวจสอบสิทธิ์เป็นประจำ: ทบทวนและตรวจสอบ AppProject roles เป็นประจำ
  8. เอกสารประกอบนโยบาย: จดบันทึกว่า AppProject แต่ละตัวมีวัตถุประสงค์อะไร

ตัวอย่างการตั้งค่า Multi-Team

ตัวอย่างตัวจริงของการตั้งค่า AppProject สำหรับหลายทีม:

---
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: backend-services
  namespace: argocd
spec:
  description: "Backend microservices project"
  sourceRepos:
  - 'https://github.com/mycompany/api-gateway'
  - 'https://github.com/mycompany/user-service'
  - 'https://github.com/mycompany/payment-service'
  destinations:
  - namespace: 'backend-*'
    server: 'https://prod-cluster.example.com'
  clusterResourceWhitelist:
  - group: 'apps'
    kind: 'Deployment'
  - group: ''
    kind: 'Service'
  - group: ''
    kind: 'ConfigMap'
  - group: 'batch'
    kind: 'CronJob'
  roles:
  - name: backend-developers
    policies:
    - p, proj:backend-services:backend-developers, applications, get, backend-services/*, allow
    - p, proj:backend-services:backend-developers, applications, sync, backend-services/*, allow
    groups:
    - 'github.com:mycompany:backend-team'
  - name: backend-admins
    policies:
    - p, proj:backend-services:backend-admins, applications, *, backend-services/*, allow
    groups:
    - 'github.com:mycompany:platform-admins'
---
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
  name: frontend-services
  namespace: argocd
spec:
  description: "Frontend web applications project"
  sourceRepos:
  - 'https://github.com/mycompany/web-app'
  - 'https://github.com/mycompany/admin-dashboard'
  destinations:
  - namespace: 'frontend-*'
    server: 'https://prod-cluster.example.com'
  clusterResourceWhitelist:
  - group: 'apps'
    kind: 'Deployment'
  - group: ''
    kind: 'Service'
  - group: 'networking.k8s.io'
    kind: 'Ingress'
  roles:
  - name: frontend-developers
    policies:
    - p, proj:frontend-services:frontend-developers, applications, get, frontend-services/*, allow
    - p, proj:frontend-services:frontend-developers, applications, sync, frontend-services/*, allow
    groups:
    - 'github.com:mycompany:frontend-team'

สรุป

Argo CD Project (AppProject) เป็นเครื่องมือที่ทรงพลังในการจัดกลุ่ม Application และควบคุมสิทธิ์ในสภาพแวดล้อมมัลติทีม โดยการใช้ sourceRepos, destinations, clusterResourceWhitelist และ RBAC roles คุณสามารถสร้างโครงสร้างการควบคุมการเข้าถึงที่ยืดหยุ่นและปลอดภัยได้

เมื่อทำงานกับโครงสร้างพื้นฐาน Kubernetes บน ผู้ให้บริการโฮสติ้ง Cloud VPS การใช้ AppProject จะช่วยให้แน่ใจว่าแต่ละทีมมีสิทธิ์การเข้าถึงที่เหมาะสม และการปรับใช้ Application จะปลอดภัยและมีการดูแลอย่างระมัดระวัง