本篇文章為大家展示了如何實(shí)現(xiàn)Prometheus應(yīng)用監(jiān)控,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),融水網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:融水等地區(qū)。融水做網(wǎng)站價(jià)格咨詢:18982081108
監(jiān)控Nginx
nginx:1.16.1、nginx-vts-exporter:0.10.3、nginx-module-vts:1.6.3
監(jiān)控Nginx主要用到以下三個(gè)模塊:
nginx-module-vts:Nginx的監(jiān)控模塊,能夠提供JSON格式的數(shù)據(jù)產(chǎn)出。
nginx-vts-exporter:主要用于收集Nginx的監(jiān)控?cái)?shù)據(jù),并給Prometheus提供監(jiān)控接口,默認(rèn)端口號9913。
Prometheus:監(jiān)控Nginx-vts-exporter提供的Nginx數(shù)據(jù),并存儲(chǔ)在時(shí)序數(shù)據(jù)庫中,可以使用PromQL對時(shí)序數(shù)據(jù)進(jìn)行查詢和聚合
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編譯時(shí)添加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的配置,添加監(jiān)控接口/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語法并啟動(dòng)
$ /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數(shù)據(jù)
$ curl http://127.0.0.1/status/format/json
7.安裝nginx-vts-exporter
下載地址:https://github.com/hnlq715/nginx-vts-exporter/releases
$ 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/ //啟動(dòng)命令 nginx-vts-exporter -nginx.scrape_uri=http://127.0.0.1/status/format/json
6.將nginx-vts-exporter
添加system系統(tǒng)服務(wù)
$ 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.啟動(dòng)添加的system服務(wù)
$ systemctl daemon-reload $ systemctl start nginx-vts-exporter.service $ systemctl enable nginx-vts-exporter.service // nginx-vts-exporter 默認(rèn)端口 - 9913 $ netstat -lntup | grep "9913" tcp6 0 0 :::9913 :::* LISTEN 9195/nginx-vts-expo
8.使用prometheus監(jiān)控修改監(jiān)控端配置文件
$ vim /usr/local/prometheus/prometheus.yml - job_name: 'nginx' static_configs: - targets: ['10.0.0.171:9913']
9.啟動(dòng)prometheus
服務(wù)
$ systemctl restart prometheus.service
10.查看監(jiān)控端是否介入
11.Granfana導(dǎo)入Nginx監(jiān)控圖表
https://grafana.com/grafana/dashboards/2949
監(jiān)控MySQL
文中主要監(jiān)控MySQL/MySQL主從信息
版本:mysql-5.7,mysql_exporter-0.12.1
mysql_exporter下載地址:https://github.com/prometheus/mysqld_exporter/releases
1、下載mysql_exporter并解壓
$ tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz // 將mysql_exporter二進(jìn)制文件拷貝至/usr/local/bin $ cp mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter /usr/local/bin/
2.需要授權(quán)用戶給exporter使用
> CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'abc12345' WITH MAX_USER_CONNECTIONS 3; // 可查看主從運(yùn)行情況查看線程,及所有數(shù)據(jù)庫。 > GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
為該用戶設(shè)置最大連接數(shù)為了避免監(jiān)控?cái)?shù)據(jù)過大導(dǎo)致服務(wù)器超載
3.修改mysql配置文件,添加剛才創(chuàng)建的exporter用戶和密碼
$ vim /etc/my.cnf [client] user=exporter password=abc12345
4.啟動(dòng)exporter客戶端,需指定mysql配置文件,讀取exporter用戶和密碼
$ mysqld_exporter --config.my-cnf=/etc/my.cnf 常用參數(shù): // 選擇采集innodb --collect.info_schema.innodb_cmp // innodb存儲(chǔ)引擎狀態(tài) --collect.engine_innodb_status // 指定配置文件 --config.my-cnf="/etc/my.cnf"
5.添加system系統(tǒng)服務(wù)
$ 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.啟動(dòng)添加的system服務(wù)
$ systemctl daemon-reload $ systemctl start mysql_exporter.service $ systemctl enable mysql_exporter.service // mysql_export默認(rèn)端口 - 9104 $ netstat -lntup | grep "9104" tcp6 0 0 :::9104 :::* LISTEN 34137/mysqld_export
7.curl查看捕獲mysql數(shù)據(jù)
curl http://localhost:9104/metrics
8.使用prometheus監(jiān)控修改監(jiān)控端配置文件
$ vim /usr/local/prometheus/prometheus.yml - job_name: 'mysql' scrape_interval: 5s # 靜態(tài)添加node static_configs: - targets: ['10.0.0.171:9104']
9.啟動(dòng)prometheus
服務(wù)
$ systemctl restart prometheus.service
10.查看監(jiān)控端是否介入
11.Granfana導(dǎo)入MySQL監(jiān)控圖表
https://grafana.com/grafana/dashboards/8564/revisions
監(jiān)控redis
本文主要監(jiān)控redis信息
版本:redis-3.2.2,redis_exporter-1.3.4
安裝部署redis:https://blog.51cto.com/12643266/2349586
redis_exporter下載地址:https://github.com/oliver006/redis_exporter/releases/
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.啟動(dòng)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系統(tǒng)服務(wù)
$ 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.啟動(dòng)添加的system服務(wù)
$ systemctl daemon-reload $ systemctl start redis_exporter.service $ systemctl enable redis_exporter.service // redis_exporter默認(rèn)端口 - 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.yml加入redis節(jié)點(diǎn),然后重啟prometheus
:
- job_name: 'redis' static_configs: - targets: ['10.0.0.171:9121']
7.重啟prometheus
服務(wù)端
systemctl restart prometheus.service
8.查看監(jiān)控端是否介入
9.Granfana導(dǎo)入Redis監(jiān)控圖表
redis節(jié)點(diǎn)監(jiān)控:https://grafana.com/grafana/dashboards/2751
redis哨兵監(jiān)控:https://grafana.com/grafana/dashboards/9570
上述內(nèi)容就是如何實(shí)現(xiàn)Prometheus應(yīng)用監(jiān)控,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。