本文讲述如何实现 Prometheus 的应用监控
监控 Nginx
nginx:1.16.1,nginx-vts-exporter:0.10.3,nginx-module-vts:1.6.3
监控 Nginx 主要用到以下三个模块:
nginx-module-vts:Nginx 的监控模块, 能够提供 JSON 格式的数据产出.
nginx-vts-exporter: 主要用于收集 Nginx 的监控数据, 并给 Prometheus 提供监控接口, 默认端口号 9913.
Prometheus: 监控 Nginx-vts-exporter 提供的 Nginx 数据, 并存储在时序数据库中, 可以使用 PromQL 对时序数据进行查询和聚合
nginx-module-vts 模块编译
nginx_vts_exporter 依赖 nginx-module-vts 模块, 安装此模块无需任何其他依赖. 模块与 Nginx 的版本兼容性如下:
- 1.11.x (last tested: 1.11.10)
- 1.10.x (last tested: 1.10.3)
- 1.8.x (last tested: 1.8.0)
- 1.6.x (last tested: 1.6.3)
- 1.4.x (last tested: 1.4.7)
1. 安装依赖包
- $ yum -y install gcc gcc-c++ autoconf automake make
- $ yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2. 添加 www 用户
- $ groupadd -f www
- $ useradd -g www www
3. 下载 nginx(版本 1.16.1)
- $ wget http://nginx.org/download/nginx-1.16.1.tar.gz
- $ tar xf nginx-1.16.1.tar.gz
3. 克隆 vts 模块仓库
- https://github.com/vozlt/nginx-module-vts/releases
- $ Git clone Git://GitHub.com/vozlt/nginx-module-vts.Git
4. 编译 nginx 配置, 添加 vts 模块
在 nginx 编译时添加 vts 模块:--add-module=/path/to/nginx-module-vts
- $ cd nginx-1.16.1/
- # 配置 nginx
- # --prefix 指定安装的目录
- # /usr/local/nginx 是安装目录, 不能和自己下载的文件目录重了
- #./configure --prefix=/usr/local/nginx
- $ ./configure --user=www --group=www \
- --prefix=/usr/local/nginx \
- --with-http_stub_status_module \
- --with-http_ssl_module \
- --with-stream \
- --with-http_gzip_static_module \
- --with-http_sub_module \
- --add-module=/root/nginx-module-vts
- $ make && make install
5.Nginx config 配置
更改 Nginx Conf 的配置, 添加监控接口 / status
- $ VIM /usr/local/nginx/conf/nginx.conf
- user www;
- worker_processes 1;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local]"$request" '
- '$status $body_bytes_sent"$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log logs/access.log main;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 65;
- # enabled nginx status
- vhost_traffic_status_zone;
- gzip on;
- server {
- listen 80;
- server_name localhost;
- location /status {
- vhost_traffic_status_display;
- vhost_traffic_status_display_format html;
- # Only allow local access the url
- allow 127.0.0.1;
- }
- }
- }
检查 nginx 语法并启动
- $ /usr/local/nginx/sbin/nginx -t
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- $ /usr/local/nginx/sbin/nginx
6.curl 查看捕获 nginx 数据
$ curl http://127.0.0.1/status/format/json
7. 安装 nginx-vts-exporter
下载地址:
- $ tar xf nginx-vts-exporter-0.10.3.Linux-amd64.tar.gz
- $ cp nginx-vts-exporter-0.10.3.Linux-amd64/nginx-vts-exporter /usr/local/bin/
- // 启动命令
- nginx-vts-exporter -nginx.scrape_uri=http://127.0.0.1/status/format/json
6. 将 nginx-vts-exporter 添加 system 系统服务
- $ VIM /usr/lib/systemd/system/nginx-vts-exporter.service
- [Unit]
- Description=Prometheus
- Wants=network-online.target
- After=network-online.target
- [Service]
- User=root
- Group=root
- Type=simple
- ExecStart=/usr/local/bin/nginx-vts-exporter \
- -nginx.scrape_uri=http://127.0.0.1/status/format/json
- [Install]
- WantedBy=multi-user.target
7. 启动添加的 system 服务
- $ systemctl daemon-reload
- $ systemctl start nginx-vts-exporter.service
- $ systemctl enable nginx-vts-exporter.service
- // nginx-vts-exporter 默认端口 - 9913
- $ netstat -lntup | grep "9913"
- tcp6 0 0 :::9913 :::* LISTEN 9195/nginx-vts-expo
8. 使用 prometheus 监控修改监控端配置文件
- $ VIM /usr/local/prometheus/prometheus.YAML
- - job_name: 'nginx'
- static_configs:
- - targets: ['10.0.0.171:9913']
9. 启动 prometheus 服务
$ systemctl restart prometheus.service
10. 查看监控端是否介入
11.Granfana 导入 Nginx 监控图表
https://grafana.com/grafana/dashboards/2949
监控 MySQL
文中主要监控 MySQL/MySQL 主从信息
版本: MySQL-5.7,mysql_exporter-0.12.1
mysql_exporter 下载地址:
1, 下载 mysql_exporter 并解压
- $ tar xf mysqld_exporter-0.12.1.Linux-amd64.tar.gz
- // 将 mysql_exporter 二进制文件拷贝至 / usr/local/bin
- $ cp mysqld_exporter-0.12.1.Linux-amd64/mysqld_exporter /usr/local/bin/
2. 需要授权用户给 exporter 使用
- > CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'abc12345' WITH MAX_USER_CONNECTIONS 3;
- // 可查看主从运行情况查看线程, 及所有数据库.
- > GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
为该用户设置最大连接数为了避免监控数据过大导致服务器超载
3. 修改 MySQL 配置文件, 添加刚才创建的 exporter 用户和密码
- $ VIM /etc/my.cnf
- [client]
- user=exporter
- password=abc12345
4. 启动 exporter 客户端, 需指定 MySQL 配置文件, 读取 exporter 用户和密码
$ mysqld_exporter --config.my-cnf=/etc/my.cnf
常用参数:
- // 选择采集 innodb
- --collect.info_schema.innodb_cmp
- // innodb 存储引擎状态
- --collect.engine_innodb_status
- // 指定配置文件
- --config.my-cnf="/etc/my.cnf"
5. 添加 system 系统服务
- $ VIM /usr/lib/systemd/system/mysql_exporter.service
- [Unit]
- Description=Prometheus
- Wants=network-online.target
- After=network-online.target
- [Service]
- User=root
- Group=root
- Type=simple
- ExecStart=/usr/local/bin/mysqld_exporter \
- --config.my-cnf=/etc/my.cnf
- [Install]
- WantedBy=multi-user.target
6. 启动添加的 system 服务
- $ systemctl daemon-reload
- $ systemctl start mysql_exporter.service
- $ systemctl enable mysql_exporter.service
- // mysql_export 默认端口 - 9104
- $ netstat -lntup | grep "9104"
- tcp6 0 0 :::9104 :::* LISTEN 34137/mysqld_export
7.curl 查看捕获 MySQL 数据
curl http://localhost:9104/metrics
8. 使用 prometheus 监控修改监控端配置文件
- $ VIM /usr/local/prometheus/prometheus.YAML
- - job_name: 'mysql'
- scrape_interval: 5s
- # 静态添加 node
- static_configs:
- - targets: ['10.0.0.171:9104']
9. 启动 prometheus 服务
$ systemctl restart prometheus.service
10. 查看监控端是否介入
11.Granfana 导入 MySQL 监控图表
监控 Redis
本文主要监控 Redis 信息
版本: Redis-3.2.2,redis_exporter-1.3.4
安装部署 Redis:https://blog.51cto.com/12643266/2349586
redis_exporter 下载地址:
1. 下载 redis_exporter 插件
代理插件不一定非要安装在 Redis 端
- $ wget https://github.com/oliver006/redis_exporter/releases/download/v1.3.4/redis_exporter-v1.3.4.linux-amd64.tar.gz
- $ tar xf redis_exporter-v1.3.4.Linux-amd64.tar.gz
- $ cp redis_exporter-v1.3.4.Linux-amd64/redis_exporter /usr/local/bin/
2. 启动 redis_exporter 登陆 Redis
- // 无密码
- redis_exporter -Redis.addr 127.0.0.1:6379
- // 有密码
- redis_exporter -Redis.addr 172.0.0.1:6379 -Redis.password 123456
3. 添加 system 系统服务
- $ VIM /usr/lib/systemd/system/redis_exporter.service
- [Unit]
- Description=Prometheus
- Wants=network-online.target
- After=network-online.target
- [Service]
- User=root
- Group=root
- Type=simple
- ExecStart=/usr/local/bin/redis_exporter \
- -Redis.addr 127.0.0.1:6379
- [Install]
- WantedBy=multi-user.target
4. 启动添加的 system 服务
- $ systemctl daemon-reload
- $ systemctl start redis_exporter.service
- $ systemctl enable redis_exporter.service
- // redis_exporter 默认端口 - 9121
- $ netstat -lntup | grep 9121""
- tcp6 0 0 :::9121 :::* LISTEN 4465/redis_exporter
5. 本地 curl 访问
curl http://127.0.0.1:9121/metrics
6.prometheus.YAML 加入 Redis 节点, 然后重启 prometheus:
- - job_name: 'redis'
- static_configs:
- - targets: ['10.0.0.171:9121']
7. 重启 prometheus 服务端
systemctl restart prometheus.service
8. 查看监控端是否介入
9.Granfana 导入 Redis 监控图表
Redis 节点监控: https://grafana.com/grafana/dashboards/2751
Redis 哨兵监控: https://grafana.com/grafana/dashboards/9570
来源: http://blog.51cto.com/12643266/2459530