Prometheus+Grafana可视化监控【Nginx状态】

文章目录

一、安装Docker

注意:我这里使用之前写好脚本进行安装Docker,如果已经有Docker请省略此步骤,安装Docker是为了方便部署Nginx WEB服务器,如果已经有数据库前两步骤都可以省略。

点击获取Docker离线安装脚本

bash 复制代码
tar zxf docker20.10.14Install.tar.gz
cd docker20.10.14Install
bash install.sh

查看Docker状态如下图表示没问题:

bash 复制代码
systemctl status docker

二、安装Nginx(Docker容器方式)

1、前提准备:

bash 复制代码
docker pull nginx:1.18
mkdir /home/software/nginx/{conf,logs} -p

systemctl stop firewalld
systemctl disable firewalld
iptables -F

2、创建配置文件

bash 复制代码
cat >> /home/software/nginx/conf/nginx.conf << EOF
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        location /nginx_status {
            stub_status on;
            access_log off;
            allow all;
        }
        location / {
            return 200 'Hello, Nginx!';
        }
    }
}
EOF

2、运行容器:

bash 复制代码
docker run -itd --name nginx \
    -v /home/software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
    -v /etc/localtime:/etc/localtime \
    -v /home/software/nginx/logs:/var/log/nginx \
    -p 80:80 --restart=always nginx:1.18

3、验证Nginx服务是否正常

bash 复制代码
curl http://127.0.0.1/nginx_status

如下图:返回Nginx状态信息,表示无误。

三、安装Prometheus

1、时间和时区同步

bash 复制代码
timedatectl set-timezone Asia/Shanghai
yum -y install ntpdate
/usr/sbin/ntpdate -u ntp1.aliyun.com

配置计划任务定时同步时间

bash 复制代码
echo "0 5 * * * /usr/sbin/ntpdate -u ntp1.aliyun.com >/dev/null &" >> /var/spool/cron/root
crontab -l

2、安装Prometheus

bash 复制代码
wget https://github.com/prometheus/prometheus/releases/download/v2.42.0/prometheus-2.42.0.linux-amd64.tar.gz

tar zxf prometheus-2.42.0.linux-amd64.tar.gz 
mv prometheus-2.42.0.linux-amd64 /usr/local/prometheus

3、配置systemd管理

bash 复制代码
cat > /usr/lib/systemd/system/prometheus.service << EOF
[Unit]

[Service]
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
Alias=dbus-org.fedoraproject.FirewallD1.service
EOF

4、启动并设置开机自启

bash 复制代码
systemctl enable prometheus --now
systemctl status prometheus

显示如上图,表示Prometheus也没问题了,默认端口是9090,我们可以浏览器访问一下

点击 Status > Targets 可以查看到Prometheus自己的metrics如下图:

四、安装Grafana

1、安装Grafana

bash 复制代码
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.4.1-1.x86_64.rpm
sudo yum install grafana-enterprise-9.4.1-1.x86_64.rpm -y

systemctl enable grafana-server.service --now

2、WEB页面验证

默认端口是3000,第一次访问会提示重新设置密码,如下图:

五、Pronetheus和Grafana相关联



六、安装nginx_exporter

1、安装nginx_exporter

bash 复制代码
wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.11.0/nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gz
mkdir  /usr/local/nginx-exporter
tar zxf nginx-prometheus-exporter_0.11.0_linux_amd64.tar.gz -C /usr/local/nginx-exporter/

2、配置systemd管理

注意:-nginx.scrape-uri参数设置自己的nginx地址,只要能访问到 /nginx_status路由即可,我这里是本机,我写的是http://127.0.0.1:80/nginx_status

bash 复制代码
cat > /usr/lib/systemd/system/nginx_exporter.service << EOF
[Unit]
Description=nginx_exporter Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/nginx-exporter/nginx-prometheus-exporter -nginx.scrape-uri http://127.0.0.1:80/nginx_status
ExecReload=/bin/kill -HUP \$MAINPID
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=multi-user.target
EOF

启动并加入开机自启动

bash 复制代码
systemctl enable nginx_exporter --now
systemctl status  nginx_exporter

默认端口为9113

bash 复制代码
netstat -anput |grep 9113

4、Prometheus配置文件中指定nginx_exporter信息

yml 复制代码
vim /usr/local/prometheus/prometheus.yml

  - job_name: "Nginx_115"
    static_configs:
      - targets: ["16.32.15.115:9113"]

添加完成配置文件后使用命令测试格式上是否有问题

bash 复制代码
cd  /usr/local/prometheus/
./promtool check config prometheus.yml

重启Prometheus

bash 复制代码
systemctl restart prometheus.service

七、Grafana添加Nginx监控模板

这里使用Nginx的模板ID:11199

点击 下方红圈地方 > import > 输入模板ID

最终效果图如下:

相关推荐
m0_719084111 小时前
nginx的一些配置的意思
运维·服务器·nginx
大大小小聪明14 小时前
Nginx中root与alias的区别及用法
服务器·网络·nginx
大得36920 小时前
安装openresty使用nginx+lua,openresty使用jwt解密
nginx·lua·openresty
酷爱码2 天前
Nginx 配置 HTTPS 与 WSS 完整指南(最新推荐)
nginx·https·iphone
mqiqe2 天前
Nginx 代理Https服务
nginx·https·iphone
Alex_81D2 天前
Https流式输出一次输出一大段,一卡一卡的-解决方案
nginx·http·https
π大星星️2 天前
LVS + Keepalived + Nginx 高可用负载均衡系统实验
nginx·负载均衡·lvs
一个小白5552 天前
nginx 的反向代理 负载均衡 动静分离 重写
linux·运维·前端·nginx·centos·firefox
果子⌂2 天前
Nginx核心服务
运维·nginx
2301_803297752 天前
nginx网站服务
linux·运维·nginx