GitOps là gì?

GitOps là một evolution của DevOps practice, sử dụng Git như là single source of truth cho declarative infrastructure và applications.Khi tìm hiểu GitOps là gì, thay vì manual deployment hoặc sử dụng CI/CD scripts phức tạp, GitOps định nghĩa mọi thứ trong Git và tự động sync trạng thái thực tế với trạng thái mong muốn.

Nguyên tắc cốt lõi của GitOps

GitOps là gì

Tất cả infrastructure và application configuration phải được declarative – định nghĩa trạng thái mong muốn thay vì các bước để thực hiện.Khi tìm hiểu GitOps là gì, ví dụ: thay vì “chạy commands để create Kubernetes deployment”, bạn định nghĩa Kubernetes manifests và Git trở thành source of truth.

2. Git là Single Source of Truth

Trạng thái mong muốn của hệ thống được lưu trong Git repository.Khi tìm hiểu GitOps là gì, mọi thay đổi phải đi qua Git workflow (commit, review, merge), đảm bảo audit trail đầy đủ và khả năng rollback dễ dàng.

3. Automatic Synchronization

Software agent (như ArgoCD hoặc Flux) liên tục theo dõi Git repository và tự động sync trạng thái thực tế của hệ thống với trạng thái mong muốn trong Git.

GitOps Workflow

  • Developer tạo hoặc update configuration trong Git
  • Pull Request được tạo để review changes
  • Peer Review approve và merge PR
  • Git Agent phát hiện thay đổi và sync xuống cluster
  • Verification agent confirm trạng thái đã được sync

Các GitOps Tools phổ biến

ArgoCD

ArgoCD là một declarative, GitOps continuous delivery tool cho Kubernetes. Nó theo dõi Git repository và tự động deploy applications vào Kubernetes cluster.

# Install ArgoCD
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

# Port forward to access UI
kubectl port-forward svc/argocd-server -n argocd 8080:443

Flux

Flux là một GitOps operator được phát triển bởi Weaveworks, hiện là CNCF project. Flux cung cấp automatic deployment cho Kubernetes dựa trên Git commits.

ArgoCD Example: Application Deployment

# application.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://github.com/your-org/your-repo.git
    targetRevision: main
    path: deploy/k8s
  destination:
    server: https://kubernetes.default.svc
    namespace: production
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

GitOps Best Practices

  • Separate repositories: Dùng separate repo cho infrastructure (Terraform, Kubernetes manifests) và application code
  • Branch strategy: Sử dụng trunk-based development hoặc GitFlow phù hợp với team
  • Small, frequent commits: Thay vì large changesets, commit nhỏ và thường xuyên
  • Comprehensive code review: Review mọi changes trước khi merge
  • Environment promotion: Có promotion pipeline từ dev > staging > production
  • Secrets management: Sử dụng external secrets manager (Vault, Sealed Secrets), không commit secrets vào Git
  • Drift detection: Alert khi trạng thái thực tế khác với Git

Lợi ích của GitOps

  • Enhanced security: Mọi thay đổi được audit, signed commits
  • Improved reliability: Rollback nhanh chóng, consistent environments
  • Developer experience: Developers chỉ cần Git, không cần Kubernetes CLI
  • Compliance: Full audit trail cho compliance requirements
  • Faster deployment: Automated sync giảm manual intervention

Kết luận

GitOps mang lại sự nhất quán, traceable và repeatable cho infrastructure và deployment management. Bắt đầu với một team và project nhỏ, sau đó scale up khi đã quen với workflow. ArgoCD và Flux là hai tools phổ biến nhất để implement GitOps trong Kubernetes environment.

Chào các bạn mình là Quốc Hùng , mình sinh ra thuộc cung song tử ,song tử luôn khẳng định chính mình ,luôn luôn phấn đấu vượt lên phía trước ,mình sinh ra và lớn lên tại vùng đất võ cổ truyền ,đam mê của mình là coder ,ngày đi học tối về viết blog ...