Prometheus和Granafa监控方案
介绍
Prometheus本身不会直接去抓取应用指标,而是通过对应的Exporter来暴露指标。因此,我们需要在服务器上部署相应的Exporter,并确保它们正在运行且暴露指标在特定端口。
Prometheus通过应用暴露的指标获取数据,Grafana连接到Prometheus的数据库,并将其数据可视化的展示,以此达到可视化监控的目的
因云服务器无图形化窗口,本次方案思路:1.Prometheus和所有的exporter只监听本地127.0.0.1,外部无法直接访问2.安装Grafana,配置监听本地3.Grafana中添加数据源4.在本机通过ssh隧道将服务器的Grafana端口(3000)映射到本地的端口(3000)5.本机可通过localhost:3000访问Grafana1.安装Prometheus(监控端)
软件官网: [https://prometheus.io/download/]
1.安装
# 创建监控专用目录(如果不存在)自定义软件安装目录mkdir-p /opt/monitoringcd/opt/monitoringmv软件包位置 /opt/monitoringtar-zxf prometheus-3.7.3.linux-amd64.tar.gzcdprometheus-3.7.3.inux-amd64/2.创建专用用户和目录
# 创建系统用户useradd--no-create-home --shell /bin/false prometheus# 创建必要的目录mkdir-p /etc/prometheusmkdir-p /var/lib/prometheus# 设置目录权限chownprometheus:prometheus /etc/prometheuschownprometheus:prometheus /var/lib/prometheuscdprometheus# 安装二进制文件cpprometheus /usr/local/bin/cppromtool /usr/local/bin/# 设置权限chownprometheus:prometheus /usr/local/bin/prometheuschownprometheus:prometheus /usr/local/bin/promtoolchmod+x /usr/local/bin/prometheuschmod+x /usr/local/bin/promtool3.编辑配置文件
cpprometheus.yml /etc/prometheus/prometheus.ymlvim/etc/prometheus/prometheus.yml global: scrape_interval: 15s# 全局默认抓取间隔evaluation_interval: 15s# 规则评估间隔(如报警规则)scrape_configs:# 监控 Prometheus 自身- job_name:'prometheus'# 覆盖此任务的全局抓取间隔scrape_interval: 15s static_configs: - targets:['127.0.0.1:9090']# Prometheus 自身服务地址,默认为localhost# 设置配置文件权限chownprometheus:prometheus /etc/prometheus/prometheus.yml4.配置service启动文件
cat>/etc/systemd/system/prometheus.service<<'EOF' [Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/prometheus \ --config.file=/etc/prometheus/prometheus.yml \ --storage.tsdb.path=/var/lib/prometheus/ \ --web.listen-address=127.0.0.1:9090 Restart=always [Install] WantedBy=multi-user.target EOF5.启动和测试
# 重新加载 systemdsystemctl daemon-reload# 启用服务systemctlenableprometheus# 启动服务systemctl start prometheus# 检查状态systemctl status prometheus#active即成功6.查看日志(如果有问题)
# 查看实时日志journalctl -u prometheus -f# 或者查看最后20行日志journalctl -u prometheus -n20--no-pager2.安装被监控端
Node Exporter(系统监控)
1.下载,往下翻就有
2.安装 Node Exporter
mkdir-p /opt/monitoringcd/opt/monitoringmv软件包位置 /opt/monitoringtar-zxf node_exporter-1.7.0.linux-amd64.tar.gzcdnode_exporter-1.7.0.linux-amd64# 复制二进制文件cpnode_exporter /usr/local/bin/chmod+x /usr/local/bin/node_exporter# 创建系统用户(如果不存在)useradd--no-create-home --shell /bin/false node_exporter2>/dev/null||truechownnode_exporter:node_exporter /usr/local/bin/node_exporter3.创建 Node Exporter 服务(默认为localhost)
cat>/etc/systemd/system/node_exporter.service<<'EOF' [Unit] Description=Node Exporter After=network.target [Service] User=node_exporter Group=node_exporter Type=simple ExecStart=/usr/local/bin/node_exporter \ --web.listen-address=127.0.0.1:9100 \ --collector.systemd \ --collector.systemd.unit-whitelist="(nginx|mysql|ssh).service" Restart=on-failure [Install] WantedBy=multi-user.target EOF4.启动 Node Exporter
systemctl daemon-reload systemctlenablenode_exporter systemctl start node_exporter systemctl status node_exporterNginx Exporter 服务
1.下载与安装
cd/opt/monitoring# 下载 Nginx Exporterwgethttps://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.11.0/nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gz# 解压tarxzf nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gzcpnginx-prometheus-exporter /usr/local/bin/chmod+x /usr/local/bin/nginx-prometheus-exporter# 创建系统用户(如果不存在)useradd--no-create-home --shell /bin/false nginx_exporter2>/dev/null||truechownnginx_exporter:nginx_exporter /usr/local/bin/nginx-prometheus-exporter2.创建 Nginx Exporter 服务
cat>/etc/systemd/system/nginx_exporter.service<<'EOF' [Unit] Description=Nginx Exporter After=network.target [Service] User=nginx_exporter Group=nginx_exporter Type=simple ExecStart=/usr/local/bin/nginx-prometheus-exporter \ -nginx.scrape-uri http://127.0.0.1:8080/nginx_status \ -web.listen-address 127.0.0.1:9113 Restart=on-failure [Install] WantedBy=multi-user.target EOF3.配置 Nginx 状态页面
vim/usr/local/nginx/conf/nginx.conf#添加一段新serverserver{listen127.0.0.1:8080;server_name localhost;location /nginx_status{stub_status on;access_log off;allow127.0.0.1;deny all;}#重启nginx服务systemctl restart nginx4.启动 Nginx Exporter
systemctl daemon-reload systemctlenablenginx_exporter systemctl start nginx_exporter systemctl status nginx_exporter同理可安装所需exporter使用
3. 更新 Prometheus 配置
vim /etc/prometheus/prometheus.ymlscrape_configs:# 监控 Prometheus 自身-job_name:'prometheus'static_configs:-targets:['127.0.0.1:9090']# 监控系统指标-job_name:'node_exporter'static_configs:-targets:['127.0.0.1:9100']labels:instance:'web-server-01'environment:'production'# 监控 Nginx-job_name:'nginx'static_configs:-targets:['127.0.0.1:9113']labels:instance:'web-server-01'environment:'production'#注意缩进,对照原有的就行# 设置权限chownprometheus:prometheus /etc/prometheus/prometheus.yml# 检查配置语法/usr/local/bin/promtool check config /etc/prometheus/prometheus.yml# 重启 Prometheus 以应用新配置systemctl restart prometheus systemctl status prometheus检查所有服务状态
systemctl status prometheus systemctl status node_exporter systemctl status nginx_exporter有图形化的可以通过设置的监控Prometheus本身的IP和端口访问
如 http://127.0.0.1:9090/targets 访问
4.Grafana可视化
1.下载安装
yuminstall-y grafana#或下载rpm包上传到服务器,使用yun本地安装yum localinstall -y 包名2.启动并启用服务
systemctl daemon-reload systemctl start grafana-server systemctlenablegrafana-server3.检查状态
systemctl status grafana-serve#active就行ss -ntl可以看到服务都成功启动
4.本地浏览器测试
#建立ssh隧道 x是服务器ip,将服务器本地的3000端口映射到本地3000端口(win用cmd就行)ssh-L3000:localhost:3000 root@x.x.x.x 登录后不要关闭cmd 本地浏览器访问localhost:3000 登录默认密码 admin admin 提示改密码,想改可以改,我是skip5.添加数据源
填刚查询的Prometheus地址
保存后添加仪表盘
最后就成功了
可以更换合适的仪表盘
效果图
接下来还可以进行监控报警便不做展示