
Introduction
In traditional CI/CD, the deploy script runs kubectl apply directly to the cluster. Who's running what? What state is the cluster currently in? How to rollback?
GitOps reverses this model: Git is the source of truth for cluster state. The operator (ArgoCD) continuously syncs the cluster to the correct state declared in Git.
1. GitOps Principles
1.1 Core Principles
1. DECLARATIVE
Toàn bộ trạng thái hệ thống được khai báo trong Git
(YAML manifests, Helm values, Kustomize overlays)
2. VERSIONED & IMMUTABLE
Git history = audit log đầy đủ của mọi thay đổi
"Ai thay đổi gì, lúc nào, tại sao" (commit message, PR)
3. PULLED AUTOMATICALLY
Agent (ArgoCD/Flux) pull từ Git và apply
Không có CI push thẳng vào cluster (không cần cluster credentials trong CI)
4. CONTINUOUSLY RECONCILED
Agent liên tục compare desired state (Git) với actual state (cluster)
Tự động "heal" nếu ai đó thay đổi trực tiếp trên cluster
1.2 Why is GitOps better than "kubectl apply in CI"?
| Traditional CI Push | GitOps (ArgoCD) | |
|---|---|---|
| Credentials | CI needs cluster credentials | Only ArgoCD needed, CI not |
| Audit trails | CI logs (easy to lose) | Git history (permanent) |
| Rollback | Re-run CI with old version | git revert → auto deploy |
| Drift detection | No | Automatically detect and fix |
| Multi-cluster | Complex | ApplicationSet handles it |
| Review process | Ad-hoc scripts | Pull Request workflow |
2. ArgoCD Architecture
┌─────────────────────────────────────────────────────────────────┐
│ ArgoCD │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌───────────────────────┐ │
│ │ API Server │ │ Repo Server │ │ Application │ │
│ │ (REST+gRPC) │ │ (Git clone, │ │ Controller │ │
│ │ CLI/UI/CD │ │ render │ │ (reconcile loop) │ │
│ └──────┬───────┘ │ manifests) │ └──────────┬────────────┘ │
│ │ └──────────────┘ │ │
│ ┌──────▼─────────────────────────────────────┐ │ │
│ │ Redis Cache │ │ │
│ └─────────────────────────────────────────────┘ │ │
└──────────────────────────────────────────────────┼──────────────┘
│
┌──────────────▼──────┐
Pull │ │ Kubernetes │
from │ │ Cluster │
Git │ └─────────────────────┘
▼
┌──────────────────┐
│ Git Repository │
│ (k8s-manifests) │
└──────────────────┘
Repo Server: Clone Git repo, render manifests (Helm/Kustomize/plain YAML)
Application Controller: Compare desired state (Git) with live state (Cluster), create sync actions
API Server: REST API for UI, CLI, CI/CD webhooks
3. Install ArgoCD
kubectl create namespace argocd
kubectl apply -n argocd \
-f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
# Lấy initial admin password
kubectl -n argocd get secret argocd-initial-admin-secret \
-o jsonpath="{.data.password}" | base64 -d
# Port-forward UI
kubectl port-forward svc/argocd-server -n argocd 8080:443
# Cài ArgoCD CLI
brew install argocd
argocd login localhost:8080
4. Application Manifest
4.1 Application CRD
# order-service-app.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: order-service-prod
namespace: argocd
# Đừng xóa khi app bị xóa (cascade protection)
finalizers:
- resources-finalizer.argocd.argoproj.io
spec:
project: backend-services
source:
repoURL: https://github.com/myorg/k8s-manifests.git
targetRevision: main
# Thư mục trong repo
path: services/order-service/overlays/production
destination:
server: https://kubernetes.default.svc
namespace: services-prod
syncPolicy:
# Tự động sync khi Git thay đổi
automated:
prune: true # Xóa resources không còn trong Git
selfHeal: true # Tự fix nếu ai kubectl edit trực tiếp
allowEmpty: false # Không sync nếu manifest rỗng (safety)
syncOptions:
- CreateNamespace=true
- PrunePropagationPolicy=foreground
- RespectIgnoreDifferences=true
# Retry nếu sync fail
retry:
limit: 5
backoff:
duration: 5s
factor: 2
maxDuration: 3m
# Bỏ qua một số fields khi so sánh (thường do auto-scaling thay đổi)
ignoreDifferences:
- group: apps
kind: Deployment
jsonPointers:
- /spec/replicas # HPA manage replicas, bỏ qua drift này
4.2 ArgoCD Project — permissions management
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: backend-services
namespace: argocd
spec:
description: "Backend microservices"
# Chỉ cho phép source từ các repos này
sourceRepos:
- 'https://github.com/myorg/k8s-manifests.git'
# Chỉ deploy vào namespace này
destinations:
- namespace: services-prod
server: https://kubernetes.default.svc
- namespace: services-staging
server: https://kubernetes.default.svc
# Chỉ allow các resource kinds này
clusterResourceWhitelist:
- group: ''
kind: Namespace
namespaceResourceWhitelist:
- group: 'apps'
kind: Deployment
- group: ''
kind: Service
- group: autoscaling
kind: HorizontalPodAutoscaler
# Không cho phép ClusterRole, CRD, etc.
roles:
- name: developer
description: Deploy access for developers
policies:
- p, proj:backend-services:developer, applications, sync, backend-services/*, allow
- p, proj:backend-services:developer, applications, get, backend-services/*, allow
groups:
- github:myorg:backend-team
5. Kustomize — Multi-environment Configuration
5.1 Directory structure
k8s-manifests/
services/
order-service/
base/ ← Shared manifests
deployment.yaml
service.yaml
hpa.yaml
kustomization.yaml
overlays/
staging/ ← Staging overrides
kustomization.yaml
replica-patch.yaml
env-patch.yaml
production/ ← Production overrides
kustomization.yaml
replica-patch.yaml
env-patch.yaml
resource-limits-patch.yaml
5.2 Base manifests
# base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
- hpa.yaml
commonLabels:
app: order-service
managed-by: argocd
# base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
spec:
replicas: 1 ← Base, override per environment
template:
spec:
containers:
- name: order-service
image: registry.example.com/order-service:latest ← CI sẽ update tag này
resources:
requests:
cpu: "250m"
memory: "512Mi"
limits:
cpu: "500m"
memory: "1Gi"
5.3 Staging overlay
# overlays/staging/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: staging-
namespace: services-staging
bases:
- ../../base
patches:
- path: replica-patch.yaml
- path: env-patch.yaml
images:
- name: registry.example.com/order-service
newTag: sha-abc1234 ← CI cập nhật field này
# overlays/staging/replica-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
spec:
replicas: 2
# overlays/staging/env-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
spec:
template:
spec:
containers:
- name: order-service
env:
- name: ENVIRONMENT
value: staging
- name: LOG_LEVEL
value: DEBUG
- name: DB_HOST
valueFrom:
secretKeyRef:
name: order-db-staging
key: host
5.4 Production overlay
# overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: services-prod
bases:
- ../../base
patches:
- path: replica-patch.yaml
- path: resource-limits-patch.yaml
- path: env-patch.yaml
images:
- name: registry.example.com/order-service
newTag: 1.2.3 ← Semantic version cho production
# overlays/production/replica-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: order-service
spec:
replicas: 5 ← Production needs more replicas
6. CI Update Image Tag (GitOps Flow)
# Trong CI pipeline (GitHub Actions)
- name: Update image tag in GitOps repo
run: |
# Clone manifest repo
git clone https://x-access-token:${{ secrets.GITOPS_TOKEN }}@github.com/myorg/k8s-manifests.git
cd k8s-manifests
# Update image tag cho staging
cd services/order-service/overlays/staging
kustomize edit set image registry.example.com/order-service:sha-${{ github.sha }}
# Commit và push
git config user.email "[email protected]"
git config user.name "CI Bot"
git add .
git commit -m "chore(order-service): deploy sha-${{ github.sha }} to staging
Triggered by: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
git push
# ArgoCD sẽ auto-detect và deploy
Promote to Production (manual step):
- name: Promote to production (manual approval required)
environment: production # GitHub Environment với required reviewers
run: |
cd k8s-manifests/services/order-service/overlays/production
kustomize edit set image registry.example.com/order-service:${{ needs.build.outputs.version }}
git add . && git commit -m "chore: promote order-service ${{ needs.build.outputs.version }} to prod"
git push
7. ApplicationSet — Multi-cluster, Multi-tenant
# Tự động tạo Application cho mỗi cluster/env combination
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: order-service
namespace: argocd
spec:
generators:
- matrix:
generators:
- list:
elements:
- cluster: staging
url: https://staging-cluster:6443
namespace: services-staging
- cluster: prod-us
url: https://prod-us-cluster:6443
namespace: services-prod
- cluster: prod-eu
url: https://prod-eu-cluster:6443
namespace: services-prod
- list:
elements:
- service: order-service
- service: payment-service
- service: inventory-service
template:
metadata:
name: '{{service}}-{{cluster}}'
spec:
project: backend-services
source:
repoURL: https://github.com/myorg/k8s-manifests.git
targetRevision: main
path: 'services/{{service}}/overlays/{{cluster}}'
destination:
server: '{{url}}'
namespace: '{{namespace}}'
syncPolicy:
automated:
prune: true
selfHeal: true
8. Rollback
GitOps does visual rollback:
# Option 1: git revert (recommended — tạo revert commit, không rewrite history)
git revert abc1234 --no-edit
git push
# ArgoCD auto-deploys revert commit
# Option 2: ArgoCD UI — click "History and Rollback" → chọn revision cũ → sync
# Option 3: ArgoCD CLI
argocd app history order-service-prod
argocd app rollback order-service-prod 5 # Rollback về revision 5
Summary
| Concept | Purpose |
|---|---|
| GitOps | Git = source of truth for cluster state |
| ArgoCD Application | Declare "app X = code from Git path Y, deploy to namespace Z" |
| automated sync | ArgoCD automatically syncs every time Git changes |
| selfHeal | Automatically revert manual changes on cluster |
| Kustomize overlays | Customize manifest per environment without duplicate |
| ApplicationSet | Deploy the same app to multiple clusters/environments |
| Git revert = rollback | Safe rollback, with audit trail |
Next article: Deployment Strategies — Canary, Blue/Green & Progressive Delivery