เชื่อมต่อ Git Repository กับ Argo CD (GitHub, GitLab, Bitbucket)

Argo CD เป็นเครื่องมือ Continuous Deployment ที่ได้รับความนิยมอย่างมากในวงการ DevOps และ Kubernetes ในปัจจุบัน โดยอาศัยพลังของ Git Repository เป็นศูนย์กลางในการจัดการการ Deploy แอปพลิเคชัน การเชื่อมต่อ Git Repository เช่น GitHub GitLab หรือ Bitbucket กับ Argo CD ถือเป็นขั้นตอนพื้นฐานและสำคัญที่สุด ที่จะทำให้คุณสามารถ Automate การ Deploy และการจัดการ Infrastructure ได้อย่างมีประสิทธิภาพ บทความนี้จะคู่มือเรียนรู้ครบถ้วนเกี่ยวกับการเชื่อมต่อ Git Repository กับ Argo CD ตั้งแต่การตั้งค่าพื้นฐาน การเชื่อมต่อ Repository แบบ Public และ Private จนถึงการแก้ไขปัญหาทั่วไป

ทำไมต้องเชื่อมต่อ Git Repository กับ Argo CD

การเชื่อมต่อ Git Repository กับ Argo CD มีความสำคัญหลายประการ ในการพัฒนาแอปพลิเคชันสมัยใหม่:

  • Source of Truth: Git Repository ทำหน้าที่เป็น Single Source of Truth สำหรับการกำหนดค่า (Configuration) และ Infrastructure ของแอปพลิเคชัน ทุกการเปลี่ยนแปลงจะบันทึกไว้ใน Git History
  • Automated Deployment: Argo CD จะ Monitor Git Repository อย่างต่อเนื่อง เมื่อมีการ Commit หรือ Push ใหม่ ระบบจะ Deploy แอปพลิเคชันโดยอัตโนมัติ
  • GitOps Workflow: แนวทาง GitOps ช่วยให้ทีมพัฒนาสามารถจัดการ Infrastructure เหมือนจัดการ Application Code ผ่านทาง Git Version Control
  • Audit Trail: ทุกการเปลี่ยนแปลงจะมีการบันทึก Commit Message และ Committer Information ทำให้สามารถ Audit และ Track Changes ได้อย่างชัดเจน
  • Disaster Recovery: หากเกิดปัญหาใด ๆ สามารถ Rollback ไปยังเวอร์ชันก่อนหน้าได้ง่าย ๆ ผ่านทาง Git

ความเข้าใจพื้นฐานเกี่ยวกับ Argo CD Repository Connection

ก่อนที่จะเริ่มเชื่อมต่อ Git Repository กับ Argo CD จำเป็นต้องทำความเข้าใจกับแนวคิดพื้นฐาน:

  • Repository URL: ที่อยู่ของ Git Repository ที่ต้องการเชื่อมต่อ เช่น https://github.com/username/repo.git
  • Credentials: ข้อมูลการพิสูจน์ตัวตน (Authentication) ซึ่งอาจเป็น Personal Access Token (PAT) Username Password หรือ SSH Key
  • Namespace: Kubernetes Namespace ที่ Argo CD ติดตั้งไว้ โดยค่าเริ่มต้นคือ argocd
  • Secret Object: Kubernetes Secret ที่เก็บ Credentials สำหรับการเชื่อมต่อ Git Repository

การเชื่อมต่อ Public Git Repository

การเชื่อมต่อ Public Git Repository ที่ไม่ต้องการการพิสูจน์ตัวตนเป็นขั้นตอนที่ง่ายที่สุด:

ขั้นตอนการเชื่อมต่อ Public Repository โดยใช้ Argo CD Web UI

  1. เข้าสู่ Argo CD Web UI (โดยปกติอยู่ที่ https://argocd.example.com)
  2. ไปที่ Settings → Repositories
  3. คลิก “CONNECT REPO” ปุ่มสีขาว
  4. เลือก Connection method เป็น HTTPS
  5. กรอกข้อมูล Repository URL: https://github.com/your-username/your-repo.git
  6. คลิก “CONNECT” เพื่อทดสอบการเชื่อมต่อ
  7. หากสำเร็จ Repository จะแสดงในรายการ Connected Repositories

การเชื่อมต่อ Public Repository โดยใช้ Argo CD CLI

สำหรับผู้ที่ชื่นชอบการใช้ Command Line Interface (CLI) สามารถใช้คำสั่งต่อไปนี้:

argocd repo add https://github.com/your-username/your-repo.git \
  --insecure-skip-server-verification

การเชื่อมต่อ Private Git Repository

การเชื่อมต่อ Private Repository นั้นต้องการการพิสูจน์ตัวตน (Authentication) เพื่อให้ Argo CD สามารถเข้าถึง Repository ได้ มีหลายวิธีในการทำเช่นนี้:

วิธีที่ 1: ใช้ HTTPS Personal Access Token (PAT)

Personal Access Token เป็นวิธีที่ปลอดภัยและสะดวกในการเชื่อมต่อ Private Repository ขั้นตอนการสร้าง PAT นั้นแตกต่างกันสำหรับแต่ละแพลตฟอร์ม:

สำหรับ GitHub:

  1. เข้าไปที่ GitHub Settings → Developer settings → Personal access tokens
  2. คลิก “Generate new token”
  3. ตั้งชื่อ Token เช่น “Argo CD Token”
  4. เลือก Scopes: repo (full control of private repositories)
  5. คลิก “Generate token” และ Copy Token

หลังจากได้ Token สามารถเชื่อมต่อกับ Argo CD ได้:

argocd repo add https://github.com/your-username/your-repo.git \
  --username your-github-username \
  --password ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

สำหรับ GitLab:

  1. เข้าไปที่ GitLab Settings → Access Tokens
  2. คลิก “Add new token”
  3. ตั้งชื่อ และเลือก Scopes: api read_repository
  4. คลิก “Create personal access token” และ Copy Token
argocd repo add https://gitlab.com/your-username/your-repo.git \
  --username your-gitlab-username \
  --password glpat_xxxxxxxxxxxxxxxxxxxxxx

สำหรับ Bitbucket:

  1. เข้าไปที่ Bitbucket Settings → Personal settings → App passwords
  2. คลิก “Create app password”
  3. ตั้งชื่อ และเลือก Permissions: repositories (read)
  4. คลิก “Create” และ Copy Password
argocd repo add https://bitbucket.org/your-workspace/your-repo.git \
  --username your-bitbucket-username \
  --password app_password_xxxxxxxxxxxxxx

วิธีที่ 2: ใช้ SSH Key

SSH Key เป็นวิธีที่ปลอดภัยมากกว่าสำหรับการพิสูจน์ตัวตน

ขั้นตอนสร้าง SSH Key:

ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_argo_cd

เพิ่ม SSH Public Key เข้า Git Repository:

สำหรับ GitHub: ไปที่ Settings → SSH and GPG keys → New SSH key

สำหรับ GitLab: ไปที่ Settings → SSH Keys

สำหรับ Bitbucket: ไปที่ Settings → SSH keys → Add key

เชื่อมต่อ Argo CD ด้วย SSH Key:

kubectl create secret generic argocd-ssh-key \
  --from-file=ssh-privatekey=$HOME/.ssh/id_argo_cd \
  -n argocd
argocd repo add [email protected]:your-username/your-repo.git \
  --ssh-private-key-path ~/.ssh/id_argo_cd

วิธีที่ 3: ใช้ Kubernetes Secret manifest

apiVersion: v1
kind: Secret
metadata:
  name: github-repo-credentials
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: repository
type: Opaque
stringData:
  type: git
  url: https://github.com/your-username/your-repo.git
  password: ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  username: your-github-username

การตั้งค่า Repository สำหรับแต่ละแพลตฟอร์ม

การเชื่อมต่อ GitHub Repository

GitHub เป็นแพลตฟอร์ม Repository ที่ได้รับความนิยมมากที่สุด การเชื่อมต่อหลาย Repository ของ GitHub Organization:

argocd repo add https://github.com/your-organization \
  --username your-bot-username \
  --password ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

การเชื่อมต่อ GitLab Repository

สำหรับ Self-hosted GitLab:

argocd repo add https://gitlab.example.com/your-group/your-repo.git \
  --username bot-username \
  --password glpat_xxxxxxxxxxxxx \
  --insecure-skip-server-verification

การยืนยันการเชื่อมต่อ Repository

argocd repo list
kubectl get secrets -n argocd -l argocd.argoproj.io/secret-type=repository

การใช้งาน Repository ใน Argo CD Application

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/your-username/your-repo.git
    targetRevision: HEAD
    path: kustomize
  destination:
    server: https://kubernetes.default.svc
    namespace: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
    - CreateNamespace=true

แก้ไขปัญหาทั่วไป

ปัญหา: “Repository not found” หรือ “Invalid credentials”

ตรวจสอบ Repository URL และ Credentials ว่าถูกต้อง ดู Argo CD logs:

kubectl logs -n argocd deployment/argocd-repo-server -f

ปัญหา: SSH Key authentication ไม่สำเร็จ

ทดสอบ SSH Connection:

ssh -i ~/.ssh/id_argo_cd [email protected]

ปัญหา: “Host key verification failed”

ssh-keyscan -t rsa gitlab.example.com >> ~/.ssh/known_hosts

Best Practices สำหรับการเชื่อมต่อ Repository

  • ใช้ Bot Account: สร้าง Bot Account สำหรับ Argo CD แทนที่จะใช้ Personal Account
  • Limited Permissions: ให้สิทธิ์แบบ Read-only เท่านั้น
  • Rotate Credentials: เปลี่ยน PAT หรือ SSH Key เป็นระยะ
  • Use SSH Keys: สำหรับ Private Repository ควรใช้ SSH Keys มากกว่า Password
  • Monitor Access Logs: ติดตามการเข้าถึง Repository

การเชื่อมต่อกับ ผู้ให้บริการโฮสติ้ง Cloud VPS

หากคุณใช้บริการ ผู้ให้บริการโฮสติ้ง Cloud VPS (https://de.co.th/cloud-vps) ในการติดตั้ง Kubernetes Cluster สำหรับ Argo CD การเชื่อมต่อ Git Repository นั้นทำงานได้เหมือนกัน คุณสามารถติดตั้ง Argo CD บน Kubernetes Cluster และเชื่อมต่อกับ Repository ที่จัดเก็บไว้บน GitHub GitLab หรือ Bitbucket ได้อย่างง่ายดาย

ผู้ให้บริการโฮสติ้ง Cloud VPS นั้นมีประสิทธิภาพสูง มีความเสถียรดี และรองรับการทำงานของ Argo CD ได้เป็นอย่างดี

สรุป

การเชื่อมต่อ Git Repository กับ Argo CD นั้นเป็นขั้นตอนพื้นฐานและสำคัญมากในการใช้งาน Argo CD โดยเต็มที่ ไม่ว่าคุณจะใช้ HTTPS Personal Access Token หรือ SSH Key การเชื่อมต่อ Repository นั้นก็เป็นเรื่องง่าย เมื่อคุณเข้าใจขั้นตอนและ Best Practices แล้ว คุณจะสามารถ Automate การ Deploy แอปพลิเคชันได้อย่างมีประสิทธิภาพและปลอดภัย และหากใช้บริการ ผู้ให้บริการโฮสติ้ง Cloud VPS คุณจะได้ฐานโครงสร้าง (Infrastructure) ที่เสถียรและปลอดภัยสำหรับ Argo CD ได้