程序说明:
Prometheus:用于收集 node_exporter 数据信息
node_exporter:用于监控服务器,收集服务器资源信息
Grafana: 把 Prometheus 收集的数据进行界面展示
使用版本:
CentOS 7.8-2003 (2核 4G 30G)
prometheus 版本 2.21.0 监听 9090 端口
node_exporter 版本 监听 9100 端口
Grafana 版本 9.3.2监听 3000 端口
准备工作:
服务器:192.168.233.130 centos7.8
客户端:192.168.233.134 centos7.8
一.系统安装后初始步骤(优化服务器):
yum install wget yum-utils net-tools vim unzip bash-completion –y
yum update –y
ulimit -n
sed -i "s/* soft nofile 65535/ /g" /etc/security/limits.conf
sed -i "s/* hard nofile 65535/ /g" /etc/security/limits.conf
echo "* soft nofile 65535" >>/etc/security/limits.conf
echo "* hard nofile 65535" >>/etc/security/limits.conf
ulimit -n 65535
echo "修改后文件数量"
ulimit –n
echo "优化内核参数"
echo "net.ipv4.ip_local_port_range = 1024 65535" >>/etc/sysctl.conf
echo "net.ipv4.tcp_syncookies = 1" >>/etc/sysctl.conf
echo "net.ipv4.tcp_tw_reuse = 1" >>/etc/sysctl.conf
echo "net.ipv4.tcp_tw_recycle = 1" >>/etc/sysctl.conf
echo "net.ipv4.tcp_fin_timeout = 30" >>/etc/sysctl.conf
echo "net.core.somaxconn = 20480" >>/etc/sysctl.conf
echo "net.core.netdev_max_backlog = 20480" >>/etc/sysctl.conf
echo "net.ipv4.tcp_max_syn_backlog = 20480" >>/etc/sysctl.conf
echo "net.ipv4.tcp_max_tw_buckets = 800000" >>/etc/sysctl.conf
sysctl -p
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
setenforce 0
systemctl disabled firewalld
systemctl stop firewalld
二.安装prometheus服务器
#下载
wget https://github.com/prometheus/prometheus/releases/download/v2.21.0/prometheus-2.21.0.linux-amd64.tar.gz
#安装
tar -xvf prometheus-2.21.0.linux-amd64.tar.gz
#重命名
mv prometheus-2.21.0.linux-amd64 /opt/Prometheus
#修改环境变量
cat > /etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus
After=network.target
[Service]
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/opt/prometheus/data
User=prometheus
[Install]
WantedBy=multi-user.target
EOF
#开机启动
systemctl daemon-reload
systemctl enable Prometheus
systemctl start prometheus
当启动 Prometheus 后,便可以通过 9090 端口来访问 Prometheus 自带的 UI 界面:
在浏览器打开 http://192.168.233.130:9090/,即可访问UI界面。
三.安装node_exporter
在192.168.233.134,下载安装node_exporter。
#下载
wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
#解压
tar -zxvf node_exporter-1.3.1.linux-amd64.tar.gz
#重命名
mv node_exporter-1.3.1.linux-amd64 node_exporter
#进入node_exporter文件夹启动
nohup ./node_exporter &
此处用nohup让程序一直运行下去,且在后台运行
#检查端口
lsof -i:9100
启动后,默认监听端口为9100,在浏览器输入,192.168.233.134:9100可以查看访问 Client 的监控指标。
四.服务端连接客户端
只需要在prometheus服务器配置文件中,添加项目名称job_name 和目标IP地址+端口即可。
#更改服务器配置文件
vim prometheus.yml
在最后面添加即可。[‘192.168.233.134:9100’]为客户端信息。
- job_name: 'linux'
static_configs:
- targets: ['192.168.233.134:9100']
重启服务器端。
pkill Prometheus
./prometheus &
登录浏览器UI界面,在Status >> Targets 即可查看到所有客户端信息,状态UP即可正常。
五.安装grafana
由于prometheus自带UI界面相对比较简陋,所以我们需要用到第三方工具Grafana在管理,这样更友好,更直观。
这里我们安装在192.168.233.130服务器端,也可以单独部署在其他机器上。
#下载
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.3.2-1.x86_64.rpm
#安装
yum install grafana-enterprise-9.3.2-1.x86_64.rpm
#开机自启动
systemctl enable grafana-server.service
systemctl start grafana-server.service
启动Grafana,并添加prometheus;
启动Grafana,默认端口为3000;
访问Grafana,登录http://192.168.233.130:3000/,默认账号密码:admin/admin。
添加数据 add data source
选择prometheus
添加prometheus的服务器IP地址,默认端口为9090
其他默认,保存即可
Grafana配置添加Dashboards
官方提供了很多模板,可以自己根据需求喜好来选择。当然只能选择prometheus的模板。
官方模块地址:https://grafana.com/grafana/dashboards/
我们选择最多人使用的,代号为1860。
回到Grafana,在Dashboards中,选择Import导入即可。
添加完成后,即可按host和时间在选择图形观看了。