Lab 13 — ArgoCD Notifications 🔔
🎯 Mục tiêu
Sau Lab này, bạn sẽ:
- Hiểu ArgoCD Notifications dùng để làm gì.
- Biết cách gửi thông báo khi Application Sync thành công hoặc thất bại.
- Cấu hình Notification đến Slack hoặc Email.
- Hiểu
TriggervàTemplatetrong ArgoCD Notifications. - Biết cách kiểm tra và troubleshooting khi notification không được gửi.
🤔 1. Vấn đề thực tế
Ở các Lab trước, chúng ta đã có flow:
Developer
│
│ git push
▼
Git Repository
│
▼
ArgoCD
│
│ Sync
▼
Kubernetes
ArgoCD có thể tự động deploy application.
Nhưng có một vấn đề:
Deploy xong thì ai biết?
Ví dụ lúc 2 giờ sáng:
Developer push code
│
▼
GitHub
│
▼
ArgoCD
│
├── Sync thành công ✅
│
└── Application lỗi ❌
Nếu không có notification, Developer phải tự mở ArgoCD để kiểm tra.
Trong production, điều này không thực tế.
Chúng ta muốn:
┌── Slack
│
ArgoCD ──► Notification ──► Email
│
└── ...
Ví dụ:
🚀
todo-appvừa deploy thành công lên production.
hoặc:
🚨
todo-appSync Failed — Deploymenttodo-backendkhông Healthy.
Đó chính là vấn đề ArgoCD Notifications giải quyết.
🧠 2. Hiểu nhanh
2.1. ArgoCD Notifications là gì?
ArgoCD Notifications là cơ chế giúp ArgoCD gửi thông báo khi Application đạt một trạng thái nào đó.
Ví dụ:
Application
│
├── Sync Succeeded
│ ↓
│ Notification
│ ↓
│ Slack
│
└── Sync Failed
↓
Notification
↓
Email
Bạn có thể hiểu đơn giản:
ArgoCD Notifications = hệ thống chuông báo của ArgoCD.
2.2. Trigger là gì?
Trigger quyết định:
Khi nào cần gửi notification?
Ví dụ:
Trigger:
on-sync-succeeded
Nghĩa là:
Sync thành công
↓
Trigger được kích hoạt
↓
Gửi notification
Một số tình huống thường gặp:
| Trigger | Ý nghĩa |
|---|---|
on-sync-succeeded |
Sync thành công |
on-sync-failed |
Sync thất bại |
on-health-degraded |
Application bị degraded |
on-deployed |
Application đã deploy |
2.3. Template là gì?
Nếu Trigger trả lời:
Khi nào gửi?
thì Template trả lời:
Gửi nội dung gì?
Ví dụ:
Trigger
│
│ Sync thành công
▼
Template
│
│ "Application {{.app.metadata.name}}
│ deployed successfully"
▼
Slack
Vì vậy có thể nhớ:
Trigger = WHEN
Template = WHAT
Đây là hai khái niệm quan trọng nhất trong Lab này.
🏗️ 3. Architecture
Trong Lab này, flow sẽ là:
Git Repository
│
│ Git push
▼
┌─────────┐
│ ArgoCD │
└────┬────┘
│
│ Sync
▼
┌─────────────┐
│ Kubernetes │
└──────┬──────┘
│
│ Application Status
▼
┌─────────────────────┐
│ ArgoCD Notifications│
│ │
│ Trigger │
│ ↓ │
│ Template │
└─────────┬───────────┘
│
▼
┌────────────┐
│ Slack │
│ / Email │
└────────────┘
🛠️ 4. Chuẩn bị
4.1. Kiểm tra ArgoCD
Kiểm tra namespace:
kubectl get ns argocd
Kiểm tra các Pod:
kubectl get pods -n argocd
Bạn cần thấy các component ArgoCD đang chạy.
Đặc biệt, Notifications Controller thường có Pod tương tự:
argocd-notifications-controller-xxxxx
Kiểm tra:
kubectl get pods -n argocd | grep notifications
Nếu có:
argocd-notifications-controller-xxxxx 1/1 Running
là ổn.
🔔 5. Kiểm tra ArgoCD Notifications
5.1. Kiểm tra ConfigMap
ArgoCD Notifications sử dụng ConfigMap để lưu cấu hình như:
- Template
- Trigger
- Service
Kiểm tra:
kubectl get configmap -n argocd
Bạn có thể tìm ConfigMap liên quan:
kubectl get configmap -n argocd | grep notification
Thông thường sẽ thấy:
argocd-notifications-cm
5.2. Kiểm tra Secret
Thông tin nhạy cảm như:
- Slack token
- SMTP password
- API token
không nên đặt trực tiếp trong ConfigMap.
Chúng nên nằm trong Secret.
Kiểm tra:
kubectl get secret -n argocd
💬 6. Cấu hình Slack Notification
Trong Lab này chúng ta sử dụng Slack vì đây là cách dễ hình dung nhất trong môi trường team.
Flow:
ArgoCD
│
│ Notification
▼
Slack
│
▼
#devops-alert
Nếu bạn không muốn sử dụng Slack, phần concept vẫn hoàn toàn giống với Email.
6.1. Tạo Slack Webhook
Trong Slack, tạo một Incoming Webhook cho channel muốn nhận notification.
Sau khi tạo, bạn sẽ có một URL dạng:
https://hooks.slack.com/services/XXX/XXX/XXX
⚠️ Đây là secret.
Không commit URL này vào Git repository.
🔐 7. Lưu Slack Token vào Secret
Tạo Secret:
kubectl create secret generic argocd-notifications-secret \
-n argocd \
--from-literal=slack-token='<YOUR_SLACK_WEBHOOK>'
Kiểm tra:
kubectl get secret argocd-notifications-secret -n argocd
💡 Tại sao phải dùng Secret?
Không nên làm:
ConfigMap
↓
slack-token: https://hooks.slack.com/...
vì ConfigMap không dành cho dữ liệu nhạy cảm.
Thay vào đó:
ConfigMap
│
│ cấu hình
▼
Notifications
Secret
│
│ credentials
▼
Slack
⚙️ 8. Cấu hình Notification Service
Tạo file:
argocd-notifications-cm.yaml
Ví dụ:
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-notifications-cm
namespace: argocd
data:
service.slack: |
token: $slack-token
Ở đây:
service.slack
khai báo:
ArgoCD Notifications có một service tên là Slack.
Còn:
$slack-token
tham chiếu tới credential được lưu trong Secret.
Apply:
kubectl apply -f argocd-notifications-cm.yaml
📝 9. Tạo Notification Template
Bây giờ chúng ta cần nói cho ArgoCD biết:
Khi notification được gửi, nội dung message là gì?
Thêm template:
data:
template.app-sync-succeeded: |
message: |
🚀 Application {{.app.metadata.name}} synced successfully.
Revision: {{.app.status.sync.revision}}
slack:
attachments: |
[{
"title": "{{.app.metadata.name}}",
"text": "Application synced successfully.",
"color": "good"
}]
Template này có tên:
app-sync-succeeded
Nó sẽ tạo message kiểu:
🚀 Application todo-app synced successfully.
Revision: abc123
🎯 10. Tạo Trigger
Template đã có.
Bây giờ chúng ta cần quy định:
Khi nào sử dụng template này?
Thêm:
data:
trigger.on-sync-succeeded: |
- description: Application sync succeeded
send:
- app-sync-succeeded
when: app.status.operationState.phase == 'Succeeded'
Đọc nó như một câu tiếng Anh:
WHEN
Application sync status == Succeeded
THEN
SEND app-sync-succeeded
Đây chính là bản chất của Trigger.
📦 11. Hoàn chỉnh ConfigMap
Bạn có thể cấu hình:
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-notifications-cm
namespace: argocd
data:
service.slack: |
token: $slack-token
template.app-sync-succeeded: |
message: |
🚀 Application {{.app.metadata.name}} synced successfully.
Revision: {{.app.status.sync.revision}}
slack:
attachments: |
[{
"title": "{{.app.metadata.name}}",
"text": "Application synced successfully.",
"color": "good"
}]
trigger.on-sync-succeeded: |
- description: Application sync succeeded
send:
- app-sync-succeeded
when: app.status.operationState.phase == 'Succeeded'
Apply:
kubectl apply -f argocd-notifications-cm.yaml
🔄 12. Gắn Notification vào Application
Bây giờ ArgoCD vẫn chưa biết:
Application nào cần notification?
Chúng ta thêm annotation vào Application.
Ví dụ:
metadata:
name: todo-app
namespace: argocd
annotations:
notifications.argoproj.io/subscribe.on-sync-succeeded.slack: devops-alert
Phần quan trọng:
subscribe.on-sync-succeeded.slack
có thể hiểu:
subscribe
│
├── trigger: on-sync-succeeded
│
└── service: slack
Khi todo-app Sync thành công:
todo-app
│
│ Sync Succeeded
▼
on-sync-succeeded
│
▼
app-sync-succeeded
│
▼
Slack
🧪 13. Thực hành: Trigger Notification
13.1. Kiểm tra Application
kubectl get application -n argocd
Ví dụ:
NAME SYNC STATUS HEALTH STATUS
todo-app Synced Healthy
13.2. Tạo thay đổi
Thay đổi một giá trị trong Git repository.
Ví dụ:
replicas: 2
thành:
replicas: 3
Commit:
git add .
git commit -m "scale todo app"
git push
13.3. Chờ ArgoCD Sync
ArgoCD phát hiện Git repository thay đổi:
Git
│
│ new commit
▼
ArgoCD
│
│ OutOfSync
▼
Sync
│
▼
Succeeded
Nếu Auto Sync được bật:
Git push
↓
ArgoCD
↓
Auto Sync
↓
Notification
👀 14. See Result
Kiểm tra Application:
kubectl get application todo-app -n argocd
Bạn có thể xem chi tiết:
kubectl describe application todo-app -n argocd
Sau khi Sync thành công, Slack sẽ nhận message tương tự:
🚀 Application todo-app synced successfully.
Revision: abc123
Đây chính là kết quả chúng ta muốn.
🚨 15. Tạo Notification khi Sync thất bại
Notification thành công rất hữu ích.
Nhưng trong production, notification quan trọng hơn thường là:
Có lỗi!
Chúng ta tạo thêm template:
template.app-sync-failed: |
message: |
🚨 Application {{.app.metadata.name}} sync failed.
Please check ArgoCD.
slack:
attachments: |
[{
"title": "{{.app.metadata.name}}",
"text": "Application sync failed.",
"color": "danger"
}]
Sau đó tạo trigger:
trigger.on-sync-failed: |
- description: Application sync failed
send:
- app-sync-failed
when: app.status.operationState.phase == 'Failed'
Flow lúc này:
┌── Succeeded ──► ✅ Slack
│
ArgoCD Sync ────┤
│
└── Failed ─────► 🚨 Slack
🧠 16. Understand Result
Sau Lab này, bạn chỉ cần nhớ mô hình:
┌─────────────┐
│ Application │
└──────┬──────┘
│
▼
Trigger
│
WHEN something
│
▼
Template
│
WHAT to send
│
▼
Service
│
▼
Slack / Email
Hay ngắn gọn:
Trigger → Khi nào?
Template → Gửi gì?
Service → Gửi đến đâu?
Đây là bản chất của ArgoCD Notifications.
🔍 17. Troubleshooting
❌ 17.1. Không nhận được Slack message
Kiểm tra Notifications Controller:
kubectl get pods -n argocd | grep notification
Xem logs:
kubectl logs \
-n argocd \
deployment/argocd-notifications-controller
Tìm lỗi liên quan Slack:
kubectl logs \
-n argocd \
deployment/argocd-notifications-controller \
| grep -i slack
❌ 17.2. Kiểm tra Secret
kubectl get secret argocd-notifications-secret -n argocd
Đảm bảo Secret tồn tại:
argocd-notifications-secret
❌ 17.3. Kiểm tra Application Annotation
kubectl get application todo-app \
-n argocd \
-o yaml
Tìm:
notifications.argoproj.io/subscribe
Nếu annotation không tồn tại, Application sẽ không subscribe trigger đó.
❌ 17.4. Notification có thể bị gửi quá nhiều
Đây là vấn đề rất dễ gặp trong production.
Ví dụ:
Deployment
↓
Sync
↓
Notification
↓
Slack
Sync lại
↓
Notification
↓
Slack
Sync lại
↓
Notification
↓
Slack
Channel có thể nhanh chóng trở thành:
#devops-alert
🚀 deploy success
🚀 deploy success
🚀 deploy success
🚀 deploy success
🚀 deploy success
...
💡 Production Tip
Không nên notification mọi event.
Nên ưu tiên:
❌ Mọi Sync
❌ Mọi Health Check
↓
✅ Deployment Failed
✅ Application Degraded
✅ Production Deployment
✅ Important Recovery
Notification tốt không phải là nhiều notification nhất.
Notification tốt là notification giúp con người hành động.
🏭 18. Kinh nghiệm Production
Một hệ thống production có thể chia notification thành:
ArgoCD
│
┌─────────┼─────────┐
│ │ │
▼ ▼ ▼
Success Failed Degraded
│ │ │
▼ ▼ ▼
Slack Slack Slack
Nhưng có thể phân loại channel:
#deployments
│
└── Deployment Success
#devops-alert
│
├── Sync Failed
├── Health Degraded
└── Production Error
#production
│
└── Critical Production Events
Điều này giúp team không bị alert fatigue — tức là nhận quá nhiều cảnh báo đến mức bắt đầu bỏ qua chúng.
🔐 19. Production Tip — Đừng Commit Secret
Đây là một lỗi rất nguy hiểm:
service.slack: |
token: https://hooks.slack.com/services/XXX/XXX/XXX
và commit:
git push
Nếu repository public:
Secret
↓
GitHub
↓
Internet
↓
❌ Credential Leak
Thay vào đó:
Git
│
├── ConfigMap
│ └── Notification config
│
└── Secret
└── Slack credential
Trong production, bạn có thể tiến thêm một bước và quản lý secret bằng các công cụ như External Secrets Operator, Vault hoặc cloud secret manager.
🎯 20. Tổng kết
Trong Lab này, chúng ta đã xây dựng:
Git Push
│
▼
ArgoCD
│
▼
Sync
│
├──────────────┐
│ │
▼ ▼
Succeeded Failed
│ │
▼ ▼
Template Template
│ │
└──────┬───────┘
▼
Slack
Bạn cần nhớ 4 thành phần:
| Thành phần | Vai trò |
|---|---|
| Application | Application cần theo dõi |
| Trigger | Quyết định khi nào gửi |
| Template | Quyết định nội dung |
| Service | Quyết định gửi đến đâu |
Công thức dễ nhớ:
Trigger
↓
"When?"
Template
↓
"What?"
Service
↓
"Where?"
🧪 21. Bài tập thực hành
Exercise 1 — Sync Success
- [ ] Cấu hình Slack notification.
- [ ] Tạo
on-sync-succeeded. - [ ] Deploy Todo App.
- [ ] Kiểm tra notification.
Exercise 2 — Sync Failed
- [ ] Tạo
on-sync-failed. - [ ] Cố tình tạo một manifest Kubernetes lỗi.
- [ ] Push lên Git.
- [ ] Quan sát ArgoCD.
- [ ] Kiểm tra Slack notification.
Exercise 3 — Health Degraded
Tạo trigger cho:
on-health-degraded
Sau đó làm cho Application chuyển sang trạng thái:
Healthy
↓
Degraded
và kiểm tra notification.
Exercise 4 — Production Design
Thiết kế notification strategy:
Development
↓
Chỉ notification khi Failed
Staging
↓
Success + Failed
Production
↓
Success + Failed + Degraded
Hãy tự trả lời:
Nếu bạn là DevOps Engineer của một team 20 người, những event nào thực sự đáng gửi notification?
Đây là câu hỏi quan trọng hơn việc nhớ syntax của ArgoCD Notifications.
All Rights Reserved