Giới thiệu Pushgateway

Phần này mình sẽ hướng dẫn các bạn dùng bash script để đẩy metric lên Prometheus thông qua Pushgateway. Pushgateway nó như 1 bộ nhớ tạm để chứa các metric và các metric này sẽ được Prometheus pull về định kỳ.

Cụ thể mình sẽ hướng dẫn các bạn viết 1 đoạn script nhỏ để lấy thông tin CPU và Memory thông qua top của hệ thống, sau khi đẩy được metric lên Prometheus mình cũng sẽ hướng dẫn các bạn tạo 1 dashboard để chúng ta có 1 cái nhìn trực quan hơn.

Lưu ý:

  • Máy cần giám sát là máy ngoài MacOS với ip 192.168.13.205
  • Hoặc có thể sử dụng máy Linux có kết nối được tới PushGateway và gửi metrics qua Curl
  • Phụ thuộc: curl, ps, docker, vim

Triển khai mô hình

Phần này, mình sẽ hướng dẫn nhanh chứ không phải thích chi tiết về tác dụng của từng thành phần.

Tạo 1 file config prometheus tại ~/prometheus/prometheus.yml

mkdir /prometheus
cd /prometheus
vi prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'pushgateway'
    static_configs:
      - targets: ['pushgateway:9091']

Tạo Prometheus:

docker run -d --name prometheus -p 9090:9090 -v /prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

Tạo Pushgateway:

docker run -d --name pushgateway -p 9091:9091 prom/pushgateway

Tạo Grafana:

docker run -d --name grafana -p 3000:3000 grafana/grafana

Kiểm tra:

$ docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED            STATUS                 PORTS                                                                                                               NAMES
8fa3517c6eab   prom/pushgateway          "/bin/pushgateway"       15 seconds ago     Up 14 seconds          0.0.0.0:9091->9091/tcp, :::9091->9091/tcp                                                                           pushgateway
744c9ab6dc1d   grafana/grafana           "/run.sh"                6 minutes ago      Up 6 minutes           0.0.0.0:3000->3000/tcp, :::3000->3000/tcp                                                                           grafana
03c5619941de   prom/prometheus           "/bin/prometheus --c…"   2 minutes ago      Up 2 minutes           0.0.0.0:9090->9090/tcp, :::9090->9090/tcp                                                                           prometheus

Truy cập Prometheus, đảm bảo các Targets đều up.

Truy cập Grafana, nhập Data Source là Prometheus với url là http://prometheus:9090 (vì đang sử dụng Docker nên các container truy cập với nhau qua hostname).

Viết script đẩy metric lên PushGateway

Lấy thông tin cho CPU

Mình sẽ tạo 1 file có tên cpu-script và phân quyền thực thi cho nó.

touch cpu-script
chmod u+x cpu-script
vi cpu-script
#!/bin/bash
z=$(ps aux)
while read -r z
do
   var=$var$(awk '{print "cpu_usage{process=\""$11"\", pid=\""$2"\"}", $3z}');
done <<< "$z"
curl -X POST -H  "Content-Type: text/plain" --data "$var
" http://192.168.13.205:9091/metrics/job/top/instance/machine

Giờ mình sẽ chạy file cpu-script cứ 1 giây lặp lại 1 lần để lấy metric.

while sleep 1; do ./cpu-script; done;

Lấy thông tin cho Memory

Tạo file có tên memory-script và phân quyền thực thi cho nó.

touch memory-script
chmod u+x memory-script
vi memory-script
#!/bin/bash
z=$(ps aux)
while read -r z
do
   var=$var$(awk '{print "memory_usage{process=\""$11"\", pid=\""$2"\"}", $4z}');
done <<< "$z"
curl -X POST -H  "Content-Type: text/plain" --data "$var
" http://192.168.13.205:9091/metrics/job/top/instance/machine

Giờ mình sẽ chạy file memory-script cứ 1 giây lặp lại 1 lần để lấy metric

while sleep 1; do ./memory-script; done;

Hiển thị trên Grafana

Kết quả trên trang console của Pushgateway

Đây là kết quả khi show metric trên Prometheus

Truy cập Grafana, tạo một Dashboard mới.

Chọn virtualization là Gauge, ở dòng metric hãy sửu dụng phép tính sum(cpu_usage) để tính tổng phần trăm CPU đã được sử dụng. Đặt tên cho panel ở mục 1 và mở mục 2 hãy chọn last * để nếu grafana không lấy được giá trị gần nhất thì nó sẽ lấy giá trị cuối cùng để hiện lên dashboard.

Áp dụng tương tự cho Average CPU Usage và Current Memory Usage ta được kết quả như sau.

Tiếp theo mình sẽ tạo 1 panel mới để hiện thị top 10 services sử dụng CPU nhiều nhất với topk(10, cpu_usage), lần này mình sẽ lựa chọn virtualization là Bar gauge. Chọn kiểu hiển thi là Horizontal, kiểu hiện thị là phần trăm.

Áp dụng tương tự cho Memory với công thức topk(10, memory_usage)

Tiếp theo nếu dashboard của bạn đang ít thì bạn có thể thiết kế thêm 1 virtualization loại Time series hiển thị topk(10, cpu_usage) với các services như sau.

Làm tương tự cho phần Memory bạn sẽ có 1 dashboard hiển thị thông tin resource của CPU và Memory như dưới.

Hướng dẫn Test hiệu năng

Test CPU:

sha1sum /dev/zero $

Test memory:

tail /dev/zero

Hoặc cài đặt công cụ Stress

sudo apt install stress -y

Sử dụng CPU trong 15 giây và sử dụng 4 core

$ stress --cpu 4  --timeout 15
stress: info: [29562] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
stress: info: [29562] successful run completed in 15s

Sử dụng I/O

$ stress --io 4
stress: info: [30329] dispatching hogs: 0 cpu, 4 io, 0 vm, 0 hdd
stress: info: [30329] successful run completed in 15s

Sử dụng Memory

$ stress --vm 2 --vm-bytes 128M --vm-hang 10
stress: info: [31207] dispatching hogs: 0 cpu, 0 io, 2 vm, 0 hdd
stress: info: [31207] successful run completed in 15s

Leave a Reply

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.