Trong bài viết này, mình sẽ giới thiệu về cách sử dụng Prometheus và Grafana để giám sát và phân tích hiệu suất của cụm Amazon EKS (Elastic Kubernetes Service). Bằng cách sử dụng Prometheus, bạn có thể thu thập và lưu trữ dữ liệu giám sát từ các thành phần khác nhau của cụm EKS, bao gồm các pod, node và các nguồn tài nguyên khác. Grafana cung cấp một giao diện người dùng trực quan để tạo và tùy chỉnh các biểu đồ, bảng điều khiển và cảnh báo dựa trên dữ liệu được thu thập bởi Prometheus.
Cài đặt hệ thống monitoring bằng helm
Cấu hình helm repo
Add thêm repo để cài đặt Prometheus:
helm repo add stable <https://charts.helm.sh/stable> helm repo add prometheus-community <https://prometheus-community.github.io/helm-charts> helm repo update
Tìm kiếm và cài đặt prometheus từ bộ kube-prometheus-stack
:
helm search repo prometheus-community
Cài đặt Prometheus bằng tham số mặc định
Lệnh cài đặt:
helm install prometheus-release prometheus-community/kube-prometheus-stack
Output khi cài bằng lệnh helm như sau:
ubuntu@ip-172-31-43-103:~$ helm install prometheus-release prometheus-community/kube-prometheus-stack NAME: prometheus-release LAST DEPLOYED: Wed Jan 4 16:23:59 2023 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: kube-prometheus-stack has been installed. Check its status by running: kubectl --namespace default get pods -l "release=prometheus-release" Visit <https://github.com/prometheus-operator/kube-prometheus> for instructions on how to create & configure Alertmanager and Prometheus instances using the Operator.
Kiểm tra kết quả cài đặt sẽ sinh ra các Pod ở namespace mặc định (do lúc cài chúng ta không chỉ định namespace):
ubuntu@ip-172-31-43-103:~$ k get pods NAME READY STATUS RESTARTS AGE alertmanager-prometheus-release-kube-pr-alertmanager-0 2/2 Running 1 (37s ago) 49s prometheus-prometheus-release-kube-pr-prometheus-0 2/2 Running 0 48s prometheus-release-grafana-5ccf8474-2fc6r 3/3 Running 0 62s prometheus-release-kube-pr-operator-697c5ddbf5-7vcc2 1/1 Running 0 62s prometheus-release-kube-state-metrics-7f75fc8b84-tp6cg 1/1 Running 0 62s prometheus-release-prometheus-node-exporter-q6ljd 1/1 Running 0 62s prometheus-release-prometheus-node-exporter-qtgnr 1/1 Running 0 62s prometheus-release-prometheus-node-exporter-xh85v 1/1 Running 0 62s
💡 Giải thích một chút!
- alertmanager-prometheus-release-kube-pr-alertmanager-0 ⇒ Đây là thành phần AlertManager
- prometheus-prometheus-release-kube-pr-prometheus-0 ⇒ Đây là Prometheus Server
- prometheus-release-grafana-5ccf8474-2fc6r ⇒ Đây là Grafana
- prometheus-release-kube-pr-operator-697c5ddbf5-7vcc2 ⇒ Đây là Operator cho cả stack này. Khi thực hiện update gì cho helm-release này mà bị lỗi hoặc cần debug gì thì check log của Operator này sẽ rõ
- prometheus-release-prometheus-node-exporter-* ⇒ Làm nhiệm vụ lấy metric của node trong cluster. Trên mỗi node sẽ có đúng 01 Node Exporter chạy
💡 Với việc cài hoàn toàn bằng tham số mặc định ⇒ Tất cả các service đều sinh ra dạng ClusterIP. Ta muốn truy cập vào ứng dụng từ bên ngoài Cluster thì phải edit service tương ứng thành NodePort hoặc LoadBalancer
Danh sách service:
ubuntu@ip-172-31-43-103:~$ k get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 5m25s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 66m prometheus-operated ClusterIP None <none> 9090/TCP 5m24s prometheus-release-grafana ClusterIP 10.107.64.16 <none> 80/TCP 5m38s prometheus-release-kube-pr-alertmanager ClusterIP 10.110.37.219 <none> 9093/TCP 5m38s prometheus-release-kube-pr-operator ClusterIP 10.107.79.72 <none> 443/TCP 5m38s prometheus-release-kube-pr-prometheus ClusterIP 10.99.135.176 <none> 9090/TCP 5m38s prometheus-release-kube-state-metrics ClusterIP 10.97.148.240 <none> 8080/TCP 5m38s prometheus-release-prometheus-node-exporter ClusterIP 10.111.51.24 <none> 9100/TCP 5m38s
Trong đó:
- prometheus-release-kube-pr-prometheus ⇒ Service của Prometheus
- prometheus-release-grafana ⇒ Service của Grafana
Cấu hình cho phép kết nối tới NodePort của các worker node
💡 Lưu ý: Để có thể kết nối được vào range của NodePort (30000-32767) ta cần allow range port này trong Security Group của các worker node
Mở service EC2 vào danh sách instance và chọn vào instance là worker-node.
Trong giao diện chi tiết của Security Group chọn tab Inbound rules và chọn Edit Inbound rules:
Ấn Add rule để tạo thêm một rule mới và chọn Type là Custom TCP ⇒ 30000-33000 ⇒ 0.0.0.0/0 ⇒ Allow NodePort ⇒ Chọn Save rules:

Cấu hình kết nối Prometheus từ bên ngoài Cluster
Sửa service của Prometheus để kết nối từ bên ngoài Cluster:
kubectl edit svc prometheus-release-kube-pr-prometheus
Sửa tham số như sau rồi lưu lại:
spec: clusterIP: 10.99.135.176 clusterIPs: - 10.99.135.176 internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - name: http-web port: 9090 protocol: TCP targetPort: 9090 nodePort: 30090 #Khai bao them dong nay selector: app.kubernetes.io/name: prometheus prometheus: prometheus-release-kube-pr-prometheus sessionAffinity: None type: NodePort #Doi ClusterIP thanh NodePort status: loadBalancer: {}
Kiểm tra kết quả sau khi sửa:
ubuntu@ip-172-31-43-103:~$ k get svc prometheus-release-kube-pr-prometheus NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE prometheus-release-kube-pr-prometheus NodePort 10.99.135.176 <none> 9090:30090/TCP 12m
Lấy thông tin Public IP của một work-node và kết nối vào theo địa chỉ [node-public-ip]:[prometheus-service-nodeport]. Trong trường hợp này public IP của node là 13.212.76.199 và NodePort tạo ở bước trên là 30090

Như vậy ta đã kết nối được vào Prometheus Server từ máy cá nhân của chúng ta.
Cấu hình kết nối Grafana từ bên ngoài Cluster
Sửa service của Grafana để kết nối từ bên ngoài Cluster:
kubectl edit svc prometheus-release-grafana
Sửa tham số như sau rồi lưu lại:
spec: clusterIP: 10.107.64.16 clusterIPs: - 10.107.64.16 externalTrafficPolicy: Cluster internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - name: http-web nodePort: 30091 #Khai bao them dong nay port: 80 protocol: TCP targetPort: 3000 selector: app.kubernetes.io/instance: prometheus-release app.kubernetes.io/name: grafana sessionAffinity: None type: NodePort #Doi ClusterIP thanh NodePort status: loadBalancer: {}
Kiểm tra kết quả sau khi sửa:
ubuntu@ip-172-31-43-103:~$ k get svc prometheus-release-grafana NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE prometheus-release-grafana NodePort 10.107.64.16 <none> 80:30091/TCP 25m
Hoàn toàn tương tự ta kết nối vào Grafana bằng public IP của worker-node và NodePort 30091:

Tên truy cập và mật khẩu mặc định của Grafana là admin/prom-operator
Chúc các bạn thành công!!!