🎯 Mục tiêu: Cài Flux v2, bootstrap với GitHub, cấu hình image automation tự update image tag trong Git khi có image mới, thực hiện promotion thủ công qua PR từ dev → staging → prod.
🧰 Công cụ / nền tảng: flux CLI, kubectl, GitHub account + Personal Access Token (PAT), cluster Kubernetes.
📦 Chuẩn bị: Cài flux CLI. GitHub PAT với quyền repo read/write.
# Cài flux CLI (Linux)
curl -s https://fluxcd.io/install.sh | sudo bash
# Windows (PowerShell)
# winget install fluxcd.flux
flux version
▶️ Các bước:
# === PHẦN 1: Bootstrap Flux vào GitHub ===
export GITHUB_TOKEN=
export GITHUB_USER=
export GITHUB_REPO=flux-gitops-lab
# 1. Bootstrap: Flux tự tạo repo (nếu chưa có), commit config vào đó, cài controller vào cluster
flux bootstrap github \
--owner=$GITHUB_USER \
--repository=$GITHUB_REPO \
--branch=main \
--path=clusters/dev \
--personal \
--token-auth
# 2. Kiểm tra Flux controllers
flux check
kubectl get pods -n flux-system
# source-controller, kustomize-controller, helm-controller, notification-controller
# 3. Clone repo Flux vừa tạo
git clone https://github.com/$GITHUB_USER/$GITHUB_REPO.git
cd $GITHUB_REPO
# === PHẦN 2: Deploy app qua Flux ===
# 4. Tạo GitRepository source
flux create source git podinfo \
--url=https://github.com/stefanprodan/podinfo \
--branch=master \
--interval=1m \
--export > clusters/dev/podinfo-source.yaml
# 5. Tạo Kustomization (deploy app)
flux create kustomization podinfo \
--target-namespace=default \
--source=podinfo \
--path="./kustomize" \
--prune=true \
--interval=5m \
--export > clusters/dev/podinfo-kustomization.yaml
git add clusters/
git commit -m "feat: add podinfo app via Flux"
git push
# 6. Theo dõi reconciliation
flux get kustomizations --watch
# podinfo True Applied revision: master/...
kubectl get pods -n default | grep podinfo
# === PHẦN 3: Image Automation ===
# 7. Thêm image-reflector và image-automation controller
flux bootstrap github \
--owner=$GITHUB_USER \
--repository=$GITHUB_REPO \
--branch=main \
--path=clusters/dev \
--personal \
--token-auth \
--components-extra=image-reflector-controller,image-automation-controller
# 8. Tạo ImageRepository (scan Docker Hub cho podinfo)
flux create image repository podinfo \
--image=ghcr.io/stefanprodan/podinfo \
--interval=1m \
--export > clusters/dev/podinfo-registry.yaml
# 9. Tạo ImagePolicy (chọn tag semver tăng dần)
flux create image policy podinfo \
--image-ref=podinfo \
--select-semver=">=6.0.0 <7.0.0" \
--export > clusters/dev/podinfo-policy.yaml
# 10. Sửa deployment để Flux biết chỗ nào update image tag
# Trong file kustomize/deployment.yaml của repo podinfo, thêm marker:
# image: ghcr.io/stefanprodan/podinfo:6.0.0 # {"$imagepolicy": "flux-system:podinfo"}
# (Flux sẽ tìm marker này và replace tag)
# 11. Tạo ImageUpdateAutomation
flux create image update flux-system \
--git-repo-ref=flux-system \
--git-repo-path=./clusters/dev \
--checkout-branch=main \
--push-branch=main \
--author-name=flux-bot \
[email protected] \
--commit-template="chore: update image tag to {{range .Updated.Images}}{{.}}{{end}}" \
--export > clusters/dev/image-update-automation.yaml
git add clusters/
git commit -m "feat: enable image automation for podinfo"
git push
# 12. Xem kết quả automation
flux get images repository podinfo # scan status
flux get images policy podinfo # latest tag detected
# Kiểm tra commit do flux-bot tạo trong repo
git log --oneline | head -5
# sẽ thấy commit "chore: update image tag to ..."
# === PHẦN 4: Manual Promotion dev → staging ===
# 13. Tạo branch staging, copy manifest từ dev và chỉnh replicas
git checkout -b staging
cp -r clusters/dev clusters/staging
# Sửa replicas cho staging
cat > clusters/staging/podinfo-kustomization.yaml << 'EOF'
---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: podinfo
namespace: flux-system
spec:
interval: 5m0s
path: ./kustomize
prune: true
sourceRef:
kind: GitRepository
name: podinfo
targetNamespace: staging
patches:
- patch: |
- op: replace
path: /spec/replicas
value: 3
target:
kind: Deployment
name: podinfo
EOF
git add clusters/staging/
git commit -m "feat: add staging environment config"
git push -u origin staging
# 14. Tạo Pull Request từ staging → main (promotion)
# gh pr create --base main --head staging \
# --title "promote: podinfo staging → main" \
# --body "Promotes latest image tag from dev to staging"
# 15. Sau merge PR, Flux tự deploy lên staging
flux get kustomizations
✅ Kết quả mong đợi: flux check all OK. kubectl get pods thấy podinfo running. Image automation tự tạo commit với tag mới khi có release mới trên registry. Promotion qua PR đảm bảo toàn bộ lịch sử thay đổi nằm trong Git — mọi team member đều thấy ai promote gì khi nào.
🧹 Cleanup: flux uninstall --silent — xóa toàn bộ Flux khỏi cluster. Xóa repo GitHub nếu muốn: gh repo delete $GITHUB_USER/$GITHUB_REPO --yes. kind delete cluster --name gitops-lab.