共計 6455 個字符,預計需要花費 17 分鐘才能閱讀完成。
這篇文章主要為大家展示了“如何利用 prometheus 監控 zookeeper”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓丸趣 TV 小編帶領大家一起研究并學習一下“如何利用 prometheus 監控 zookeeper”這篇文章吧。
prometheus 監控 zookeeper
官方介紹:主要有兩種方式,四字命令和 jmx
1、四字命令:
https://zookeeper.apache.org/doc/r3.4.14/zookeeperAdmin.html#sc_zkCommands 其中最常用的三個 stat、srvr、cons
Three of the more interesting commands: stat gives some general information about the server and connected clients, while srvr and cons give extended details on server and connections respectively.
$ echo srvr | nc svc-zk.o.svc.mhc.local 2181
Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
Latency min/avg/max: 0/0/802
Received: 5833450
Sent: 6074801
Connections: 84
Outstanding: 0
Zxid: 0x2d9407
Mode: standalone
Node count: 12977
使用四字命令對已經在生產運行的 zookeeper 監控起來比較方便,就是向 zookeeper 服務發送 mntr 之類的命令
具體實現參考:https://github.com/jiankunking/zookeeper_exporter
按照上述參考文檔構建鏡或者使用打好的鏡像 carlpett/zookeeper_exporter,并啟動 exporter
docker run -it -p 9141:9141 --name=zookeeper_exporter_m -d xxx.xxx.com/library/zookeeper_exporter:1.0.2 -zookeeper svc-zk.m.svc.uuu.local:2181
curl http://127.0.0.1:9141/metrics
......
# HELP zk_avg_latency Average latency of requests
# TYPE zk_avg_latency gauge
zk_avg_latency 0
# HELP zk_ephemerals_count Number of ephemeral nodes
# TYPE zk_ephemerals_count gauge
zk_ephemerals_count 2625
# HELP zk_max_file_descriptor_count Maximum number of open file descriptors
# TYPE zk_max_file_descriptor_count counter
zk_max_file_descriptor_count 1.048576e+06
# HELP zk_max_latency Maximum seen latency of requests
# TYPE zk_max_latency gauge
zk_max_latency 1
# HELP zk_min_latency Minimum seen latency of requests
# TYPE zk_min_latency gauge
zk_min_latency 0
# HELP zk_num_alive_connections Number of active connections
# TYPE zk_num_alive_connections gauge
zk_num_alive_connections 82
# HELP zk_open_file_descriptor_count Number of open file descriptors
# TYPE zk_open_file_descriptor_count gauge
zk_open_file_descriptor_count 107
# HELP zk_outstanding_requests Number of outstanding requests
# TYPE zk_outstanding_requests gauge
zk_outstanding_requests 0
# HELP zk_packets_received Number of packets received
# TYPE zk_packets_received counter
zk_packets_received 40
# HELP zk_packets_sent Number of packets sent
# TYPE zk_packets_sent counter
zk_packets_sent 40
# HELP zk_server_state Server state (leader/follower)
# TYPE zk_server_state untyped
zk_server_state{state= standalone} 1
# HELP zk_up Exporter successful
# TYPE zk_up gauge
zk_up 1
# HELP zk_watch_count Number of watches
# TYPE zk_watch_count gauge
zk_watch_count 22108
# HELP zk_znode_count Number of znodes
# TYPE zk_znode_count gauge
zk_znode_count 13105
# HELP zookeeper_exporter_build_info A metric with a constant 1 value labeled by version, revision, branch, and goversion from which zookeeper_exporter was built.
# TYPE zookeeper_exporter_build_info gauge
zookeeper_exporter_build_info{branch= ,goversion= go1.10.8 ,revision= ,version=} 1
prometheus 配置內容添加如下配置,此處同時添加了自定義標簽 env:
- job_name: zk-u
static_configs:
- targets:
- 172.21.11.101:9141
labels:
env: u
- job_name: zk-m
static_configs:
- targets:
- 172.21.11.101:9142
labels:
env: m
- job_name: zk-h
static_configs:
- targets:
- 172.21.11.101:9143
labels:
env: h
- job_name: zk-c
static_configs:
- targets:
- 172.21.11.101:9144
labels:
env: c
reload 生效配置
curl -XPOST http://prometheus:9090/-/reload
prometheus 頁面上查看 target:
grafana 展示:
2、jmx 監控
是利用 jvm_exporter 暴露指標,參考 prometheus 官方 jmx_exporter https://github.com/prometheus/jmx_exporter
jvm-exporter 從基于 jvm 的應用讀取 jmx 數據并以 http 的方式暴露給 prometheus 抓取
jmx 是一種常用的技術,輸出運行中應用的狀態信息,并且可以控制它(例如用 jmx 觸發 gc)
jvm-exporter 是用 jmx apis 收集 app 和 jvm metrics 的 Java 應用,以 Java agent 方式運行在同一個 jvm 里面。
jvm-exporter 用 Java 寫的,以 jar 包的方式發布,下載地址
有一個 ansible role(https://github.com/alexdzyoba/ansible-jmx-exporter),配置文件包含將 JMX MBean 重寫為 Prometheus 展示格式度量標準的規則。基本上它是將 MBeans 字符串轉換為 Prometheus 字符串的 regexp 集合。
zookeeper 是很多系統的關鍵組成部分,例如 Hadoop, Kafka and Clickhouse,因此監控是必不可少的。
2.1、以 java-agent 方式運行(官方推薦):
zookeeper 啟動的時候傳入參數啟用,不需要開啟 jmx
$ cat java.env
export SERVER_JVMFLAGS= -javaagent:/root/jmx_prometheus_javaagent-0.12.0.jar=7070:/root/config.yaml $SERVER_JVMFLAGS
查看啟動日志并檢查結果:
$ netstat -tlnp | grep 7070
tcp 0 0 0.0.0.0:7070 0.0.0.0:* LISTEN 892/java
$ curl -s localhost:7070/metrics | head
# HELP jvm_threads_current Current thread count of a JVM
# TYPE jvm_threads_current gauge
jvm_threads_current 16.0
# HELP jvm_threads_daemon Daemon thread count of a JVM
# TYPE jvm_threads_daemon gauge
jvm_threads_daemon 12.0
# HELP jvm_threads_peak Peak thread count of a JVM
# TYPE jvm_threads_peak gauge
jvm_threads_peak 16.0
# HELP jvm_threads_started_total Started thread count of a JVM
With jmx-exporter you can scrape the metrics of running JVM applications. jmx-exporter runs as a Java agent (inside the target JVM) scrapes JMX metrics, rewrite it according to config rules and exposes it in Prometheus exposition format.
容器中啟用 jvm_exporter
基于官方 zookeeper 的 github 倉庫,修改官方 dockerfile 以加入啟動參數
下面鏈接是我修改后的,親測可用
https://github.com/weifan01/zookeeper-docker/tree/master/3.4.14
構建好的 zk-3.4.14 版本鏡像:docker pull 3070656869/zookeeper:3.4.14-jvm-exporter
啟動 OK 后,訪問 8080 端口查看結果
2.2、以 http 方式獨立運行
參考官方文檔:https://github.com/prometheus/jmx_exporter
git clone https://github.com/prometheus/jmx_exporter.git
cd jmx_exporter
mvn package
# 修改配置文件 example_configs/httpserver_sample_config.yml
#修改啟動腳本里面的監聽地址和端口
sh run_sample_httpserver.sh
我測試配置如下:
$ cat http_server_config.yaml
# jmx 地址和端口
hostPort: 172.21.10.248:6666
username:
password:
rules:
# replicated Zookeeper
- pattern: org.apache.ZooKeeperService name0=ReplicatedServer_id(\\d+) (\\w+)
name: zookeeper_$2
- pattern: org.apache.ZooKeeperService name0=ReplicatedServer_id(\\d+), name1=replica.(\\d+) (\\w+)
name: zookeeper_$3
labels:
replicaId: $2
- pattern: org.apache.ZooKeeperService name0=ReplicatedServer_id(\\d+), name1=replica.(\\d+), name2=(\\w+) (\\w+)
name: zookeeper_$4
labels:
replicaId: $2
memberType: $3
- pattern: org.apache.ZooKeeperService name0=ReplicatedServer_id(\\d+), name1=replica.(\\d+), name2=(\\w+), name3=(\\w+) (\\w+)
name: zookeeper_$4_$5
labels:
replicaId: $2
memberType: $3
# standalone Zookeeper
- pattern: org.apache.ZooKeeperService name0=StandaloneServer_port(\\d+) (\\w+)
name: zookeeper_$2
- pattern: org.apache.ZooKeeperService name0=StandaloneServer_port(\\d+), name1=InMemoryDataTree (\\w+)
name: zookeeper_$2
啟動:
java -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=6667 -jar jmx_prometheus_httpserver-0.12.1-SNAPSHOT-jar-with-dependencies.jar 172.21.10.248:6668 http_server_config.yaml
訪問:
以上是“如何利用 prometheus 監控 zookeeper”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!