文章目录
前言
什么是prometheus
Prometheus 是一个开源的系统监控和报警工具,最早由 SoundCloud 开发,现已成为独立的开源项目并且是 Cloud Native Computing Foundation 的孵化项目。
环境
虚拟机
Ip | 主机名 | cpu | 内存 | 硬盘 |
---|---|---|---|---|
192.168.10.14 | prometheus-server | 2c | 2G | 100G |
192.168.10.15 | prometheus-agent | 2c | 2G | 100G |
版本 centos7.9
初始化已完成(防火墙,沙盒,主机名)
一、Prometheus安装
prometheus-server主机操作
1.Prometheus包获取
wget https://github.com/prometheus/prometheus/releases/download/v2.37.8/prometheus-2.37.8.linux-amd64.tar.gz
2.Prometheus安装
tar xf prometheus-2.37.8.linux-amd64.tar.gz
mv prometheus-2.37.8.linux-amd64 /usr/local/prometheus
3.Prometheus启动
cd /usr/local/prometheus/
ls
可以看到包含以下文件与目录
编辑yml文件
vim Prometheus.yml
翻到最后
把targets后的localhost改为本机ip即192.168.10.14
修改前
修改后
这里我们使用 nohup 命令启动 Prometheus 并在后台运行
注意:在/usr/local/prometheus中执行
nohup ./prometheus --config.file=prometheus.yml &
查找与Prometheus相关的进程
ps aux | grep prometheus
也可以把Prometheus托管给systemd,注册成系统服务,方便管理,如下所示
先停止进程
pkill prometheus
注册为系统服务
cat > /usr/lib/systemd/system/prometheus.service << EOF
[Service]
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
[Install]
WantedBy=multi-user.target
[Unit]
Description=prometheus
After=network.target
EOF
重载/开机自启/查看状态/启动
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus
running了就行
查看一下进程
ps -ef | grep prometheus
二、Prometheus UI界面访问
可以通过运行Prometheus server节点IP+9090端口对Prometheus进行访问。
这里是
192.168.10.14:9090
正常会看到以下页面
可以看到日志内容
搜索一下刚刚看到的日志
go_gc_duration_seconds
可以看到搜索出来了
这里使用的语法是PromQL (Prometheus Query Language)
一种用于查询 Prometheus 监控数据的专用语言
感兴趣的可以去了解一下,这里不过多描述
点击Graph可以看到图形化界面
五、使用Prometheus监控Prometheus server及其它主机
1.对Prometheus server主机监控
下载node_exporter
cd ~
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
安装node_exporter
tar xf node_exporter-1.6.0.linux-amd64.tar.gz
mv node_exporter-1.6.0.linux-amd64 /usr/local/node_exporter
cd /usr/local/node_exporter/
ls
可以看到有如下文件与目录
启动node_exporter
注册为systemd管理的系统服务
cat > /usr/lib/systemd/system/node_exporter.service << EOF
[Service]
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
[Unit]
Description=node_exporter
After=network.target
EOF
重载/开机自启/查看状态/启动
systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
systemctl status node_exporter
修改Prometheus Server配置文件添加node节点
cd /usr/local/prometheus/
vim prometheus.yml
尾部添加
- job_name: "prometheus-server"
static_configs:
- targets: ["192.168.10.14:9100"]
重启prometheus
systemctl restart prometheus
web页面
已经监控到了prometheus-server主机了
2.其它主机监控
prometheus-agent主机操作
tar xf node_exporter-1.6.0.linux-amd64.tar.gz
mv node_exporter-1.6.0.linux-amd64 /usr/local/node_exporter
cd /usr/local/node_exporter/
ls
启动node_exporter
注册为系统服务
cat > /usr/lib/systemd/system/node_exporter.service << EOF
[Service]
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
[Unit]
Description=node_exporter
After=network.target
EOF
重载/开机自启/查看状态/启动
systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
systemctl status node_exporter
修改Prometheus Server配置文件添加node节点
回到prometheus-server操作
cd /usr/local/prometheus/
vim prometheus.yml
末端添加
- job_name: "prometheus-agent"
static_configs:
- targets: ["192.168.10.15:9100"]
重启prometheus
systemctl restart prometheus
查看web界面
完成
后面会有Grafana的讲解