Xem bài giới thiệu Prometheus tại đây.
Có bao nhiêu cách để cài đặt Prometheus trên Kubernetes?
Về cơ bản, chúng ta có 3 cách triển khai như sau:- Làm thủ công: chuẩn bị toàn bộ configurations yaml cần cho các thành phần của Prometheus (Prometheus sever, AlertManager, Grafana) như là statefulset, deployment, ConfigMap, Secret. Sau đó thực thi cài đặt theo thứ tự với yêu cầu nghiêm ngặt, vì sự phụ thuộc lẫn nhau. Đây là lựa chọn phức tạp và tiêu tốn nhiều công sức nhất! Đòi hỏi chúng ta phải có kiến thức vững vàng đối với từng thành phần tạo nên Prometheus.
- Sử dụng Kubernetes Operator: nhờ áp dụng Operator trong Kubernetes, chúng ta có thể quản lý toàn bộ thành phần riêng biệt, tạo nên hệ sinh thái Prometheus, trong 1 thể thống nhất.
- Sử dụng Helm Chart triển khai Operator: được xây dựng và maintain bởi cộng đồng Helm. Triển khai cài đặt đơn giản và nhanh nhất trong số 3 lựa chọn.
Trong phạm vi bài viết này, chúng ta sẽ chọn giải pháp số 3, để triển khai Prometheus trên Kubernetes,giám sát cơ sở dữ liệu mongoDB.
Helm là gì?
Helm là một trình quản lý gói mã nguồn mở. Helm có khả năng cung cấp, chia sẻ và sử dụng phần mềm được xây dựng cho Kubernetes.
Những người đóng góp cho Helm bao gồm nhiều công ty công nghệ lớn nhất thế giới, chẳng hạn như Google, Microsoft, Samsung SDS, VMware và IBM, cũng như các công ty quy mô trung bình đang phát triển nhanh chóng như Datadog. Sự đóng góp cũng đến từ hàng chục doanh nghiệp nhỏ và các công ty khởi nghiệp, chẳng hạn như codecentric AG.
Helm nằm trong số các dự án thuộc top đầu về tốc độ phát triển.
Helm sử dụng định dạng đóng gói riêng gọi là Chart.
Chart là tập hợp các tài liệu mô tả thành phần, thông số của tất cả chương trình cần triển khai trong 1 gói duy nhất. Tất cả tài liệu được tập hợp trong 1 folder, tên của folder sẽ được hiểu là tên Chart.
Như hình trên, ta có thể hiểu như sau:
- Chart có tên là wordpress.
- Chart.yaml: chứa thông tin liên quan về phiên bản Html và Kubernetes + các chart “con” liên quan có trên Artifact HUB.
- LICENSE, README.md, values.schema.json, templates/NOTES.txt: không bắt buộc.
- values.yaml: chứa thông số cài đặt.
- charts/: folder chứa các chart “con” sẽ được cài đặt thủ công thay vì sử dụng Artifact HUB.
- crds/: hỗ trợ tính năng tạo mẫu tùy chỉnh, custom resource definition, có trong Kubernetes. Tuy nhiên có một số hạn chế như:
- Không hỗ trợ cập nhật CRD khi có thay đổi.
- CRD sẽ không được tạo trong các gói cập nhật. CRD chỉ được tạo trong các gói cài đặt.
- Không hỗ trợ xóa CRD thông qua Heml Chart.
- templates/: tạo mẫu manifest mà Kubernetes có thể hiểu được.
mongoDB là gì?
mongoDB là cơ sở dữ liệu định hướng tài liệu, được phân loại là chương trình cơ sở dữ liệu NoQuery. mongoDB lưu trữ dữ liệu theo định dạng tượng tự như JSON, hỗ trợ mảng và các đối tượng lồng nhau, cho phép tạo ra các lược đồ linh hoạt.
Chi tiết hơn có thể xem trực tiếp trên website mongoDB.
Triển khai Prometheus + Grafana
Điều kiện cần
- Kubernetes 1.16+
- Helm 3+
kube-prometheus stack
kube-prometheus là tập hợp các thành phần tạo nên hệ sinh thái Prometheus, như là: operator, alert manager, grafana, exporter, adapter,...
Chạy lệnh sau để lấy thông tin repository:$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo add stable https://kubernetes-charts.storage.googleapis.com/
$ helm repo update
Cài đặt helm chart
$ helm install prometheus prometheus-community/kube-prometheus-stack
Một số thành phần quan trọng
Statefulsets:
- Prometheus Core
- Alert Manager
Deployments:
- Prometheus Operator
- kube-state-metrics: cho phép Prometheus “cào” các chỉ số liên quan Kubernetes
- Grafana: giao diện web cho end-user
DaemonSet:
- Node Exporter: được triển khai chạy trên toàn bộ Worker Node của Kubernetes. Chịu trách nhiệm chuyển đổi các chỉ số thành định dạng mà Prometheus có thể hiểu.
Giám sát mongoDB với Prometheus + Grafana
Làm cách nào Prometheus tìm và giám sát đối tượng chủ động?
- Prometheus tìm kiếm và phát hiện đối tượng giám sát nhờ vào tính năng Service Discovery.
- ServiceMonitor là một đối tượng dùng để mô tả tập hợp các đối tượng cần giám sát bởi Prometheus.
- Để hiểu rõ hơn, chúng ta hãy xem xét ServiceMonitor được tạo ra bởi Prometheus Operator như bên dưới.
$ kubectl get servicemonitor
NAME AGE
prometheus-kube-prometheus-alertmanager 13d
prometheus-kube-prometheus-apiserver 13d
prometheus-kube-prometheus-coredns 13d
prometheus-kube-prometheus-grafana 13d
prometheus-kube-prometheus-kube-controller-manager 13d
prometheus-kube-prometheus-kube-etcd 13d
prometheus-kube-prometheus-kube-proxy 13d
prometheus-kube-prometheus-kube-scheduler 13d
prometheus-kube-prometheus-kube-state-metrics 13d
prometheus-kube-prometheus-kubelet 13d
prometheus-kube-prometheus-node-exporter 13d
prometheus-kube-prometheus-operator 13d
prometheus-kube-prometheus-prometheus 13d
Xem chi tiết cấu hình 1 servicemonitor bất kỳ, ví dụ:
$ kubectl get servicemonitor prometheus-kube-prometheus-grafana -oyaml
Chúng ta để ý một số thông tin quan trọng sau:
kind: ServiceMonitor
metadata:
...
labels:
...
release: prometheus
spec:
endpoints:
- path: /metrics
...
selector:
matchLabels:
...
app.kubernetes.io/name: grafana
...
Đặc biệt chú ý đến label release: prometheus , thông tin này giúp cho Prometheus tìm kiếm và nhận dạng, và thu thập dữ liệu từ đối tượng cần giám sát.
Trong ví dụ trên thì đối tượng là grafana, có endpoint là /metrics , và label release: prometheus
Để kiểm chứng, chúng ta tìm hiểu thông số được thiết lập trong Prometheus như thế nào
$ kubectl get prometheuses.monitoring.coreos.com -oyaml
Chú ý thông số sau
serviceMonitorSelector:
matchLabels:
release: prometheus
Tạm dịch nôm na là
tìm kiếm tất cả serviceMonitor có label là release: prometheus
Nếu serviceMonitor không có label này thì sẽ không được giám sát bởi Prometheus.
Cài đặt mongoDB
Dùng text editor tạo file mongodb.yml có nội dung như sau:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
Tiến hành cài đặt mongoDB lên kubernetes cluster như sau:
$ kubectl apply -f mongodb.yml
Do chúng ta chỉ quan tâm các vấn đề liên quan Prometheus, nên file config cho mongoDB chỉ đơn giản, và có các giá trị mặc định.
Cài đặt mongoDB Exporter
Nhiệm vụ chính của Exporter là:
- Thu thập dữ liệu từ đối tượng, ví dụ cụ thể là mongoDB.
- Chuyển đổi thành định dạng theo chuẩn Prometheus có thể hiểu được.
- Tạo /metrics endpoint cho Prometheus “cào” dữ liệu.
Đặc điểm quan trọng của Exporter là nó có thể được triển khai độc lập với application. Đây là điểm nhấn quan trọng, vì chúng ta không cần phải thay đổi gì ở application, vừa an toàn vừa tiện lợi. Khi nào không muốn giám sát thì xóa Exporter đi là xong. Exporter vừa được cung cấp dưới dạng open source, và cũng có sẵn Container image.
Để xem danh sách các Exporter có sẵn, theo đường link sau.
Exporter cần có 3 thành phần như sau:
- Pod - Application: thu thập dữ liệu
- Service: Prometheus kết nối đến application thông qua service.
- ServiceMonitor: cho phép Prometheus tìm kiếm Exporter tự động.
Tuy nhiên, thay vì chúng ta phải tạo dựng và thiết lập cấu hình cho từng thành phần cần có của Exporter, thì có lựa chọn khác nhanh-lẹ-dễ dàng hơn, đó là sử dụng helm chart.
Thiết lập repository
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo update
Để biết các fields mà chúng ta có thể thiết lập cho mongodb-exporter, chạy lệnh sau:
$ helm show values prometheus-community/prometheus-mongodb-exporter
Trong số các fields này, chúng ta quan tâm đến và
mongodb:
uri: ""
serviceMonitor:
additionalLabels:{}
Dùng text editor tạo file values.yaml có nội dung như sau:
mongodb:
uri: "mongodb://mongodb-service:27017"
serviceMonitor:
additionalLabels:
release: prometheus
- uri: theo format mongodb://[mongodb_service_name]:mongodb_service_port
- additionalLabels: thêm label release: prometheus, như đã giải thích ở trên. Nhờ vào label này mà sau khi tạo serviceMonitor, Prometheus sẽ tự động nhìn thấy expoter và tiến hành “cào” dữ liệu.
Cài đặt mongoDB exporter
Chạy lệnh sau:
$ helm install mongodb-exporter prometheus-community/prometheus-mongodb-exporter -f values.yml
Nếu không có lỗi xảy ra, chúng ta sẽ nhận thấy có 3 components mới được tạo ra, như sau:
service/mongodb-exporter-prometheus-mongodb-exporter
pod/mongodb-exporter-prometheus-mongodb-exporter-xxxxxxx-xxxxservicemonitor/mongodb-exporter-prometheus-mongodb-exporter
Để kiểm tra xem liệu exporter có kết nối thành công với mongoDB và expose được metrics hay không, chúng ta làm như sau:
$ kubectl port-forward service/mongodb-exporter-prometheus-mongodb-exporter 9216
Forwarding from 127.0.0.1:9216 -> 9216
Forwarding from [::1]:9216 -> 9216
Mở trình duyệt với Url sau: http://127.0.0.1:9216/metrics, nếu thấy tượng tự như bên dưới là OK rồi nhé!
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.0001098
go_gc_duration_seconds{quantile="0.25"} 0.0001836
go_gc_duration_seconds{quantile="0.5"} 0.0002396
go_gc_duration_seconds{quantile="0.75"} 0.0003067
go_gc_duration_seconds{quantile="1"} 0.0009211
go_gc_duration_seconds_sum 0.037733
go_gc_duration_seconds_count 142
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 11
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.11.13"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.288048e+06
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 2.72100816e+08
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 1.479396e+06
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 2.036877e+06
# HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
# TYPE go_memstats_gc_cpu_fraction gauge
go_memstats_gc_cpu_fraction 1.0062350036648507e-05
# HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
# TYPE go_memstats_gc_sys_bytes gauge
go_memstats_gc_sys_bytes 2.371584e+06
...
Grafana
Mở port cho Grafana
$ kubectl port-forward deployment/prometheus-grafana 3000
Mở trình duyệt với Url sau http://localhost:3000/, đăng nhập với user admin, password prom-operator, kết quả như sau:
Giám sát mongoDB với Grafana
=>Chọn pod: mongodb-deployment-xxxxxxxxxxxxxxxxxxxx, ta sẽ thấy các metrics của mongodb được thu thập và biểu diễn dưới dạng đồ thị như sau
Đến đây xem như chúng ta đã hoàn thành các nhiệm vụ sau:
- Triển khai Prometheus
- Triển khai mongoDB + mongoDB Exporter
- Triển khai Grafana