🚀 Chapter 5 — Platform: Phần F — Quản lý nhiều môi trường Dev, Staging và Production bằng Helm và Argo CD trên Amazon EKS
Ở Chapter 5E, chúng ta đã triển khai Google Online Boutique lên Amazon EKS bằng Argo CD và GitOps.
Flow hiện tại:
Developer
↓
GitHub
↓
Argo CD
↓
Helm
↓
Amazon EKS
↓
Google Online Boutique
Tuy nhiên, hệ thống hiện tại mới chỉ có một môi trường:
EKS
└── online-boutique
Trong hệ thống thực tế, chúng ta thường cần nhiều môi trường:
Development
Staging
Production
Mỗi môi trường có thể sử dụng:
- Số lượng replica khác nhau
- Image tag khác nhau
- Resource request/limit khác nhau
- Domain khác nhau
- Cấu hình khác nhau
- Quy tắc triển khai khác nhau
Trong Chapter 5F, chúng ta sẽ mở rộng GitOps từ một môi trường thành nhiều môi trường bằng Helm + Argo CD + Amazon EKS.
1. Mục tiêu
Sau khi hoàn thành Chapter 5F, chúng ta sẽ có:
- Một Helm chart dùng chung cho Online Boutique.
- Ba môi trường
dev,stagingvàproduction. - Cấu hình riêng cho từng môi trường.
- Một Argo CD Application cho mỗi môi trường.
- Dev tự động đồng bộ từ Git.
- Staging và Production có thể yêu cầu approval trước khi triển khai.
- Kiểm tra được trạng thái từng môi trường.
- Thay đổi image tag bằng Git.
- Rollback bằng Git revert.
Kiến trúc cuối Chapter:
GitHub
│
┌──────────────┼──────────────┐
▼ ▼ ▼
Dev Staging Production
│ │ │
▼ ▼ ▼
Argo CD Argo CD Argo CD
│ │ │
└──────────────┼──────────────┘
▼
Amazon EKS
│
┌──────────────┼──────────────┐
▼ ▼ ▼
Namespace Namespace Namespace
dev staging production
Trong lab này, chúng ta sử dụng một EKS cluster với ba namespace để tiết kiệm chi phí:
Amazon EKS
├── online-boutique-dev
├── online-boutique-staging
└── online-boutique-production
Mô hình một cluster, nhiều namespace phù hợp cho lab. Trong production thực tế, có thể sử dụng nhiều EKS cluster để cô lập các môi trường.
2. Điều kiện trước khi bắt đầu
Bạn cần hoàn thành Chapter 5E và có:
- Amazon EKS đang ở trạng thái
ACTIVE. kubectlkết nối được với EKS.- Helm đã được cài đặt.
- Argo CD đã được cài đặt trên EKS.
- Argo CD CLI đã được cài đặt.
- Google Online Boutique đã được triển khai thành công.
- Git đã được cài đặt.
- GitHub repository
online-boutique-gitops.
Kiểm tra công cụ:
aws --version
kubectl version --client
helm version
argocd version --client
git --version
Kiểm tra EKS:
aws eks describe-cluster \
--name online-boutique-eks \
--region ap-northeast-1 \
--query 'cluster.status' \
--output text
Kết quả mong đợi:
ACTIVE
Kiểm tra node:
kubectl get nodes
Kết quả mong đợi:
NAME STATUS ROLES AGE VERSION
ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal Ready <none> ... ...
Kiểm tra Argo CD:
kubectl get pods -n argocd
Kiểm tra Helm chart trong repository:
cd online-boutique-gitops
helm lint ./helm-chart
Kết quả mong đợi:
1 chart(s) linted, 0 chart(s) failed
3. Kiểm tra cấu trúc repository hiện tại
Repository sau Chapter 5E có thể có cấu trúc:
online-boutique-gitops/
├── helm-chart/
│ ├── Chart.yaml
│ ├── values.yaml
│ └── templates/
│ ├── frontend.yaml
│ ├── cartservice.yaml
│ ├── paymentservice.yaml
│ └── ...
│
├── environments/
│ └── dev/
│ └── values.yaml
│
└── argocd/
└── online-boutique-dev.yaml
Trong Chapter 5F, chúng ta sẽ mở rộng thành:
online-boutique-gitops/
├── helm-chart/
│ ├── Chart.yaml
│ ├── values.yaml
│ ├── values-dev.yaml
│ ├── values-staging.yaml
│ ├── values-production.yaml
│ └── templates/
│ ├── frontend.yaml
│ ├── cartservice.yaml
│ ├── paymentservice.yaml
│ ├── checkoutservice.yaml
│ └── ...
│
└── argocd/
├── online-boutique-dev.yaml
├── online-boutique-staging.yaml
└── online-boutique-production.yaml
Trong phần này, chúng ta đặt các file values trong thư mục helm-chart để Helm và Argo CD dễ dàng xử lý.
4. Tạo branch mới cho Chapter 5F
Di chuyển vào GitOps repository:
cd online-boutique-gitops
Kiểm tra branch hiện tại:
git branch
Tạo branch mới:
git checkout -b chapter-5f-multi-environment
5. Tạo file values cho môi trường Dev
Tạo file:
touch helm-chart/values-dev.yaml
Mở file:
nano helm-chart/values-dev.yaml
Thêm nội dung mẫu:
frontend:
replicaCount: 1
cartservice:
replicaCount: 1
paymentservice:
replicaCount: 1
Các field như
replicaCountphải tồn tại trong Helm chart của bạn. Nếu chart không hỗ trợ field này, cần sửa template trước khi sử dụng.
Kiểm tra cấu hình hiện tại:
helm show values ./helm-chart
Render chart:
helm template online-boutique-dev ./helm-chart \
--namespace online-boutique-dev \
-f ./helm-chart/values-dev.yaml
Nếu không có lỗi, tiếp tục bước tiếp theo.
6. Tạo file values cho môi trường Staging
Tạo file:
touch helm-chart/values-staging.yaml
Mở file:
nano helm-chart/values-staging.yaml
Thêm nội dung:
frontend:
replicaCount: 2
cartservice:
replicaCount: 2
paymentservice:
replicaCount: 1
Render chart:
helm template online-boutique-staging ./helm-chart \
--namespace online-boutique-staging \
-f ./helm-chart/values-staging.yaml
Kiểm tra kết quả:
helm template online-boutique-staging ./helm-chart \
--namespace online-boutique-staging \
-f ./helm-chart/values-staging.yaml \
> /tmp/online-boutique-staging.yaml
Kiểm tra số lượng replica:
grep -n "replicas:" /tmp/online-boutique-staging.yaml
7. Tạo file values cho môi trường Production
Tạo file:
touch helm-chart/values-production.yaml
Mở file:
nano helm-chart/values-production.yaml
Thêm nội dung:
frontend:
replicaCount: 3
cartservice:
replicaCount: 2
paymentservice:
replicaCount: 2
Render chart:
helm template online-boutique-production ./helm-chart \
--namespace online-boutique-production \
-f ./helm-chart/values-production.yaml
Kiểm tra chart:
helm lint ./helm-chart
Kiểm tra cả ba file values:
helm template online-boutique-dev ./helm-chart \
-f ./helm-chart/values-dev.yaml > /tmp/dev.yaml
helm template online-boutique-staging ./helm-chart \
-f ./helm-chart/values-staging.yaml > /tmp/staging.yaml
helm template online-boutique-production ./helm-chart \
-f ./helm-chart/values-production.yaml > /tmp/production.yaml
Nếu cả ba lệnh đều chạy thành công, tiếp tục.
8. Tạo namespace cho ba môi trường
Chạy:
kubectl create namespace online-boutique-dev
kubectl create namespace online-boutique-staging
kubectl create namespace online-boutique-production
Kiểm tra:
kubectl get namespaces
Kết quả cần có:
NAME STATUS AGE
online-boutique-dev Active ...
online-boutique-staging Active ...
online-boutique-production Active ...
Nếu namespace đã tồn tại, có thể bỏ qua lỗi.
9. Tạo Argo CD Application cho Dev
Tạo thư mục:
mkdir -p argocd
Tạo file:
touch argocd/online-boutique-dev.yaml
Mở file:
nano argocd/online-boutique-dev.yaml
Thêm nội dung:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: online-boutique-dev
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/<YOUR_GITHUB_USERNAME>/online-boutique-gitops.git
targetRevision: chapter-5f-multi-environment
path: helm-chart
helm:
valueFiles:
- values-dev.yaml
destination:
server: https://kubernetes.default.svc
namespace: online-boutique-dev
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
Thay:
<YOUR_GITHUB_USERNAME>
bằng GitHub username của bạn.
Dev sẽ sử dụng:
Branch: chapter-5f-multi-environment
Values: values-dev.yaml
Namespace: online-boutique-dev
10. Tạo Argo CD Application cho Staging
Tạo file:
touch argocd/online-boutique-staging.yaml
Mở file:
nano argocd/online-boutique-staging.yaml
Thêm nội dung:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: online-boutique-staging
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/<YOUR_GITHUB_USERNAME>/online-boutique-gitops.git
targetRevision: chapter-5f-multi-environment
path: helm-chart
helm:
valueFiles:
- values-staging.yaml
destination:
server: https://kubernetes.default.svc
namespace: online-boutique-staging
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
Staging sẽ sử dụng:
Branch: chapter-5f-multi-environment
Values: values-staging.yaml
Namespace: online-boutique-staging
11. Tạo Argo CD Application cho Production
Tạo file:
touch argocd/online-boutique-production.yaml
Mở file:
nano argocd/online-boutique-production.yaml
Thêm nội dung:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: online-boutique-production
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/<YOUR_GITHUB_USERNAME>/online-boutique-gitops.git
targetRevision: chapter-5f-multi-environment
path: helm-chart
helm:
valueFiles:
- values-production.yaml
destination:
server: https://kubernetes.default.svc
namespace: online-boutique-production
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
Production sẽ sử dụng:
Branch: chapter-5f-multi-environment
Values: values-production.yaml
Namespace: online-boutique-production
Trong phần lab cơ bản, Production cũng đang bật
automated sync. Ở phần nâng cao, chúng ta sẽ tắt automated sync để mô phỏng quy trình approval trước khi triển khai Production.
12. Kiểm tra cấu hình YAML
Kiểm tra các file:
cat argocd/online-boutique-dev.yaml
cat argocd/online-boutique-staging.yaml
cat argocd/online-boutique-production.yaml
Nếu máy đã cài yamllint, có thể chạy:
yamllint argocd/
Kiểm tra Helm chart:
helm lint ./helm-chart
Kiểm tra từng environment:
helm template online-boutique-dev ./helm-chart \
-f ./helm-chart/values-dev.yaml \
--namespace online-boutique-dev
helm template online-boutique-staging ./helm-chart \
-f ./helm-chart/values-staging.yaml \
--namespace online-boutique-staging
helm template online-boutique-production ./helm-chart \
-f ./helm-chart/values-production.yaml \
--namespace online-boutique-production
13. Commit và push GitOps repository
Kiểm tra thay đổi:
git status
Thêm file:
git add helm-chart argocd
Commit:
git commit -m "add multi-environment GitOps configuration"
Push branch:
git push -u origin chapter-5f-multi-environment
Kiểm tra branch trên GitHub.
Cấu trúc repository hiện tại:
online-boutique-gitops/
├── helm-chart/
│ ├── Chart.yaml
│ ├── values.yaml
│ ├── values-dev.yaml
│ ├── values-staging.yaml
│ ├── values-production.yaml
│ └── templates/
│
└── argocd/
├── online-boutique-dev.yaml
├── online-boutique-staging.yaml
└── online-boutique-production.yaml
14. Apply ba Argo CD Applications
Apply Dev:
kubectl apply \
-f argocd/online-boutique-dev.yaml
Apply Staging:
kubectl apply \
-f argocd/online-boutique-staging.yaml
Apply Production:
kubectl apply \
-f argocd/online-boutique-production.yaml
Kiểm tra:
kubectl get applications -n argocd
Kết quả tương tự:
NAME SYNC STATUS HEALTH STATUS
online-boutique-dev Synced Healthy
online-boutique-staging Synced Healthy
online-boutique-production Synced Healthy
Trạng thái ban đầu có thể là:
OutOfSync
Hãy đợi Argo CD thực hiện quá trình sync.
15. Kiểm tra bằng Argo CD CLI
Danh sách Application:
argocd app list
Xem Dev:
argocd app get online-boutique-dev
Xem Staging:
argocd app get online-boutique-staging
Xem Production:
argocd app get online-boutique-production
Thông tin cần kiểm tra:
Project: default
Server: https://kubernetes.default.svc
Namespace: online-boutique-dev
Sync Status: Synced
Health Status: Healthy
Kiểm tra riêng từng Application:
argocd app wait online-boutique-dev \
--sync \
--health
argocd app wait online-boutique-staging \
--sync \
--health
argocd app wait online-boutique-production \
--sync \
--health
16. Kiểm tra các Deployment trong từng môi trường
Dev:
kubectl get deployments \
-n online-boutique-dev
Staging:
kubectl get deployments \
-n online-boutique-staging
Production:
kubectl get deployments \
-n online-boutique-production
Kiểm tra Pod:
kubectl get pods \
-n online-boutique-dev
kubectl get pods \
-n online-boutique-staging
kubectl get pods \
-n online-boutique-production
Kiểm tra Service:
kubectl get services \
-n online-boutique-dev
kubectl get services \
-n online-boutique-staging
kubectl get services \
-n online-boutique-production
Kiểm tra số lượng replica của frontend:
kubectl get deployment frontend \
-n online-boutique-dev
kubectl get deployment frontend \
-n online-boutique-staging
kubectl get deployment frontend \
-n online-boutique-production
Kết quả dự kiến:
Dev → 1 replica
Staging → 2 replicas
Production → 3 replicas
17. Kiểm tra ba môi trường trên Argo CD UI
Nếu Argo CD đang được port-forward:
kubectl port-forward \
svc/argocd-server \
-n argocd \
8080:443
Mở trình duyệt:
https://localhost:8080
Bạn sẽ thấy ba Application:
online-boutique-dev
online-boutique-staging
online-boutique-production
Mỗi Application có thể có trạng thái:
Synced
Healthy
Kiểm tra:
- Repository
- Branch
- Helm chart path
- Values file
- Namespace
- Sync policy
- Resource tree
- Deployment
- Service
- Pod
18. Thử nghiệm thay đổi cấu hình Dev
Mở file:
nano helm-chart/values-dev.yaml
Thay đổi:
frontend:
replicaCount: 1
thành:
frontend:
replicaCount: 2
Commit:
git add helm-chart/values-dev.yaml
git commit -m "scale frontend in dev environment"
Push:
git push origin chapter-5f-multi-environment
Theo dõi Deployment:
kubectl get deployment frontend \
-n online-boutique-dev \
-w
Kết quả mong đợi:
READY
2/2
Kiểm tra Pod:
kubectl get pods \
-n online-boutique-dev
Kiểm tra Argo CD:
argocd app get online-boutique-dev
Kết quả:
Sync Status: Synced
Health Status: Healthy
19. Kiểm tra tính độc lập giữa các môi trường
Thay đổi Dev:
frontend:
replicaCount: 2
Không thay đổi:
frontend:
replicaCount: 3
trong Production.
Kiểm tra Dev:
kubectl get deployment frontend \
-n online-boutique-dev
Kiểm tra Production:
kubectl get deployment frontend \
-n online-boutique-production
Kết quả:
Dev → 2 replicas
Production → 3 replicas
Điều này chứng minh rằng mỗi environment có cấu hình riêng nhưng vẫn dùng chung Helm chart.
20. Thử nghiệm GitOps drift
Thay đổi thủ công Deployment trong Dev:
kubectl scale deployment frontend \
--replicas=5 \
-n online-boutique-dev
Kiểm tra:
kubectl get deployment frontend \
-n online-boutique-dev
Kết quả tạm thời:
5 replicas
Do Application có:
automated:
selfHeal: true
Argo CD sẽ phát hiện trạng thái thực tế khác với Git và đưa Deployment về cấu hình được khai báo trong Git.
Kiểm tra lại:
kubectl get deployment frontend \
-n online-boutique-dev
Kết quả sẽ quay lại giá trị trong Git:
2 replicas
Kiểm tra trạng thái:
argocd app get online-boutique-dev
21. Tạo quy trình Production thủ công
Trong production thực tế, không nên để mọi commit đều tự động triển khai ngay vào Production.
Chúng ta sẽ tắt automated sync cho Production.
Mở file:
nano argocd/online-boutique-production.yaml
Thay phần:
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
bằng:
syncPolicy:
syncOptions:
- CreateNamespace=true
Apply lại cấu hình:
kubectl apply \
-f argocd/online-boutique-production.yaml
Kiểm tra:
argocd app get online-boutique-production
Production hiện tại không còn tự động sync.
Flow mới:
Developer
↓
GitHub
↓
Argo CD phát hiện thay đổi
↓
Production OutOfSync
↓
Kiểm tra / Approval
↓
Manual Sync
↓
Production
22. Thử nghiệm thay đổi Production
Mở file:
nano helm-chart/values-production.yaml
Thay đổi:
frontend:
replicaCount: 3
thành:
frontend:
replicaCount: 4
Commit:
git add helm-chart/values-production.yaml
git commit -m "scale frontend in production"
Push:
git push origin chapter-5f-multi-environment
Kiểm tra Application:
argocd app get online-boutique-production
Trạng thái dự kiến:
Sync Status: OutOfSync
Kiểm tra Deployment:
kubectl get deployment frontend \
-n online-boutique-production
Deployment vẫn giữ cấu hình cũ vì Production chưa được sync.
23. Manual Sync Production
Sau khi kiểm tra thay đổi, thực hiện sync thủ công:
argocd app sync online-boutique-production
Theo dõi quá trình:
argocd app wait online-boutique-production \
--sync \
--health
Kiểm tra Deployment:
kubectl get deployment frontend \
-n online-boutique-production
Kết quả mong đợi:
4 replicas
Kiểm tra trạng thái:
argocd app get online-boutique-production
Kết quả:
Sync Status: Synced
Health Status: Healthy
24. Rollback Production bằng Git revert
Giả sử thay đổi Production gây ra lỗi.
Xem lịch sử commit:
git log --oneline --max-count=5
Ví dụ:
a81c92f scale frontend in production
b71d120 add multi-environment GitOps configuration
Rollback commit gần nhất:
git revert a81c92f
Push thay đổi:
git push origin chapter-5f-multi-environment
Kiểm tra Application:
argocd app get online-boutique-production
Nếu Production đang sử dụng manual sync, thực hiện:
argocd app sync online-boutique-production
Kiểm tra replica:
kubectl get deployment frontend \
-n online-boutique-production
Production sẽ quay lại cấu hình trước đó.
Với GitOps, rollback được thực hiện bằng cách rollback cấu hình trong Git, sau đó để Argo CD đồng bộ trạng thái mới vào cluster.
25. Kiểm tra toàn bộ hệ thống
Kiểm tra Application:
argocd app list
Kiểm tra namespace:
kubectl get namespaces | grep online-boutique
Kiểm tra Pod của Dev:
kubectl get pods \
-n online-boutique-dev
Kiểm tra Pod của Staging:
kubectl get pods \
-n online-boutique-staging
Kiểm tra Pod của Production:
kubectl get pods \
-n online-boutique-production
Kiểm tra Deployment:
kubectl get deployments \
-A | grep online-boutique
Kiểm tra Service:
kubectl get services \
-A | grep online-boutique
Kết quả cuối cùng:
online-boutique-dev
├── Helm chart
├── Argo CD Application
└── Namespace riêng
online-boutique-staging
├── Helm chart
├── Argo CD Application
└── Namespace riêng
online-boutique-production
├── Helm chart
├── Argo CD Application
└── Namespace riêng
26. Kiến trúc cuối Chapter 5F
Developer
│
▼
GitHub
│
┌────────────┼────────────┐
▼ ▼ ▼
Dev config Staging config Production config
│ │ │
└────────────┼────────────┘
▼
Helm Chart
│
▼
Argo CD
│
┌───────────────┼───────────────┐
▼ ▼ ▼
Dev App Staging App Production App
│ │ │
▼ ▼ ▼
Namespace Namespace Namespace
│ │ │
└───────────────┼───────────────┘
▼
Amazon EKS
│
▼
Google Online Boutique
27. Những gì đã đạt được
Sau Chapter 5F, chúng ta đã chuyển từ:
Một Helm release
↓
Một namespace
↓
Một môi trường
sang:
Một Helm chart
↓
Nhiều values files
↓
Nhiều Argo CD Applications
↓
Nhiều môi trường
↓
Một GitOps Platform
Các kiến thức chính:
- Helm values theo môi trường
- Argo CD Application
- Multi-environment deployment
- Namespace isolation
- Automated sync
- Manual sync
- Self-healing
- GitOps drift detection
- Git-based rollback
- Production deployment control
28. Kết luận
Trong Chapter 5E, chúng ta đã chuyển deployment từ:
Developer
↓
helm upgrade
↓
Amazon EKS
sang:
Developer
↓
GitHub
↓
Argo CD
↓
Amazon EKS
Trong Chapter 5F, chúng ta tiếp tục mở rộng thành:
GitHub
↓
Helm Chart
↓
Argo CD
├── Dev
├── Staging
└── Production
↓
Amazon EKS
Từ đây, Git không chỉ lưu source code mà còn lưu desired state của toàn bộ platform.
Đây là nền tảng để tiếp tục xây dựng các mô hình nâng cao hơn:
- App of Apps
- ApplicationSet
- Multi-cluster GitOps
- ECR image promotion
- Helm release promotion
- Progressive delivery
- Canary deployment
- Blue/Green deployment
- Production approval workflow
All rights reserved