Prometheus 安装
简介
Prometheus 是一个开源系统监控和警报工具包。Prometheus 将其指标收集并存储为时间序列数据,即指标信息与记录的时间戳一起存储,以及称为标签的可选键值对。
本地容器化部署
1 2 3 4 5 6 7 8 9 10 11 12 13
| version: '3'
services: prometheus: image: prom/prometheus:latest environment: TZ: Asia/Shanghai ports: - 9090:9090 volumes: - type: bind source: ./prometheus.yml target: /etc/prometheus/prometheus.yml
|
监控自身(测试)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| global: scrape_interval: 15s
external_labels: monitor: 'codelab-monitor'
scrape_configs: - job_name: 'prometheus'
scrape_interval: 5s
static_configs: - targets: [ 'localhost:9090' ]
|
1
| prometheus_target_interval_length_seconds
|
监控 JMX
Prometheus 官方提供了 JMX 监控的导出工具 JMX exporter,可以访问官网下载 jar 包:
然后即可在 prometheus.yml 文件下添加检测任务:
1 2 3 4
| - job_name: '<name>' scrape_interval: <time_interval> static_configs: - targets: [ '<host>:<port>' ]
|
在项目中编写导出参数项的配置文件 config.yaml
内容样例如下:
然后通过 Java Agent 运行需要监控的程序即可:
1
| java -javaagent:./jmx_prometheus_javaagent-0.17.0.jar=<port>:config.yaml -jar <jar_name>.jar
|
在程序启动后即可访问 http://localhost:port
看到监控数据。
监控 Mysql
新建配置文件 prometheus.yml
:
1 2 3 4 5 6 7
| global: scrape_interval: 15s scrape_configs: - job_name: 'check-web' scrape_interval: 5s static_configs: - targets: [ 'mysqlexporter:9104' ]
|
新建 docker-compose.yaml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| version: '3.8'
services: mysql: image: mysql:latest command: --default-authentication-plugin=mysql_native_password restart: always environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_DATABASE: demo ports: - "3306:3306" mysqlexporter: image: prom/mysqld-exporter:latest ports: - "9104:9104" environment: - DATA_SOURCE_NAME=root:123456@(mysql:3306)/demo depends_on: - mysql prometheus: image: prom/prometheus:latest environment: TZ: Asia/Shanghai ports: - "9090:9090" volumes: - type: bind source: ./prometheus.yml target: /etc/prometheus/prometheus.yml
|
参考资料
官方文档