Bạn có bao giờ cảm thấy như một thám tử đang tìm kiếm manh mối trong mê cung của Kubernetes cluster không? Đừng lo, hôm nay chúng ta sẽ biến bạn thành Sherlock Holmes của DevOps với một hệ thống monitoring hoàn chỉnh chỉ trong 30 phút!
Tại sao cần monitoring cho K8s cluster?
Trước khi bắt đầu cuộc phiêu lưu, hãy tưởng tượng Kubernetes cluster của bạn như một thành phố lớn. Không có hệ thống camera an ninh (monitoring), làm sao bạn biết được có kẻ trộm nào đang “ăn cắp” CPU hay RAM của bạn? Prometheus sẽ là đôi mắt thần, Grafana là màn hình giám sát siêu đẹp, còn ArgoCD sẽ đảm bảo mọi thứ được triển khai một cách GitOps chuẩn chỉ.
Chuẩn bị môi trường – Không cần pha cà phê đâu!
Trước tiên, chúng ta cần đảm bảo có đủ “vũ khí” trong tay:
- Kubernetes cluster đang chạy (miễn sao không phải trên laptop cũ từ thời ông bà)
- Helm 3.x đã cài đặt
- kubectl được cấu hình đúng
- ArgoCD đã sẵn sàng hoạt động
Nếu bạn chưa có ArgoCD, đừng panic! Cài đặt nó nhanh như chớp:
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
Bước 1: Triển khai Prometheus – Thám tử thu thập bằng chứng
Prometheus là “thám tử tư” chuyên nghiệp của chúng ta, luôn âm thầm thu thập metrics từ mọi ngóc ngách trong cluster. Chúng ta sẽ sử dụng Prometheus Operator thông qua Helm chart để việc cài đặt trở nên dễ dàng hơn bao giờ hết.
Tạo namespace riêng cho monitoring (vì ai mà không thích sự gọn gàng):
kubectl create namespace monitoring
Thêm Helm repository và cài đặt kube-prometheus-stack:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack \
--namespace monitoring \
--set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false \
--set prometheus.prometheusSpec.retention=30d
Lúc này, Prometheus sẽ bắt đầu “nghe lén” tất cả các metrics trong cluster của bạn. Thật tuyệt vời phải không?
Bước 2: Cấu hình Grafana – Nghệ sĩ vẽ biểu đồ
Grafana đã được cài đặt cùng với kube-prometheus-stack, nhưng chúng ta cần truy cập vào nó. Để lấy password mặc định:
kubectl get secret --namespace monitoring prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 --decode
Port-forward để truy cập Grafana UI:
kubectl port-forward --namespace monitoring svc/prometheus-grafana 3000:80
Giờ bạn có thể truy cập http://localhost:3000 với username “admin” và password vừa lấy được. Grafana đã tự động cấu hình Prometheus làm data source, thông minh như Einstein vậy!
Bước 3: Tích hợp với ArgoCD – GitOps cho monitoring
Để thực hiện GitOps đúng nghĩa, chúng ta cần tạo ArgoCD Application. Tạo file monitoring-app.yaml:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: monitoring-stack
namespace: argocd
spec:
project: default
source:
repoURL: https://prometheus-community.github.io/helm-charts
chart: kube-prometheus-stack
targetRevision: "*"
helm:
values: |
prometheus:
prometheusSpec:
retention: 30d
serviceMonitorSelectorNilUsesHelmValues: false
destination:
server: https://kubernetes.default.svc
namespace: monitoring
syncPolicy:
automated:
prune: true
selfHeal: true
Apply configuration này và ArgoCD sẽ quản lý monitoring stack của bạn một cách tự động.
Bước 4: Tùy chỉnh Dashboard và Alerts
Giờ đến phần thú vị nhất – tạo dashboard đẹp mắt! Grafana đã có sẵn nhiều dashboard mặc định, nhưng bạn có thể import thêm từ Grafana Dashboard Library. Một số dashboard ID phổ biến:
- 3119 – Kubernetes cluster monitoring
- 6417 – Kubernetes cluster health
- 8588 – Kubernetes Deployment Statefulset Daemonset metrics
Để import, chỉ cần vào Grafana UI, chọn “+” > Import và nhập ID dashboard.
Bước 5: Cấu hình AlertManager – Người gác đêm
AlertManager sẽ là “người gác đêm” thông báo cho bạn khi có vấn đề. Tạo custom alerting rules trong file custom-alerts.yaml:
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: custom-alerts
namespace: monitoring
spec:
groups:
- name: kubernetes-resources
rules:
- alert: KubePodCrashLooping
expr: rate(kube_pod_container_status_restarts_total[5m]) > 0
for: 5m
annotations:
summary: "Pod {{ $labels.pod }} is crash looping"
Troubleshooting – Khi mọi thứ không như ý muốn
Nếu Prometheus không thu thập được metrics, hãy kiểm tra ServiceMonitor configurations. Nếu

