什么是Prometheus
本答案由Kimi生成,请仔细甄别。
Prometheus是一个开源系统监控和警报工具包,它由SoundCloud公司开发,现在由Cloud Native Computing Foundation(CNCF)维护。Prometheus特别适用于记录实时的时间序列数据,如指标或事件,通过拉取(pull)或推送(push)的方式收集数据,并通过其强大的查询语言(PromQL)进行数据查询。
Step 1 下载安装Prometheus(主控机)
第一步,下载最新版Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v3.4.0-rc.0/prometheus-3.4.0-rc.0.linux-amd64.tar.gz
第二步,解压缩安装包
tar -zxvf prometheus-3.4.0-rc.0.linux-amd64.tar.gz
第三步,创建文件夹,符合运维规范
mv prometheus-3.4.0-rc.0.linux-amd64.tar.gz prometheus
cd prometheus
mkdir bin data conf
mv prometheus bin/
mv prometheus.yml conf/
第四步,创建环境变量
vim /etc/profile.d/promrtheus.sh
export PROMETHEUS_HOME=/usr/local/prometheus
export PATH=${PROMETHEUS_HOME}/bin:$PATH
source /etc/profile.d/promrtheus.sh
第五步,检查语法错误
./promtool check config conf/prometheus.yml
第六步,创建守护进程
编写新的 prometheus.service。
vi /etc/systemd/system/prometheus.service
写入以下内容
[Unit]
Description=prometheus
Documentation=prometheus
After=network.target
[Service]
User root
WorkingDirector=/usr/local/prometheus
ExecStart=/usr/local/prometheus/bin/prometheus --config.file=/usr/local/prometheus/conf/prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
最后一步,启动服务
mv prometheus /usr/local/prometheus
systemctl start prometheus
Step 2 安装代理Node_exporter(被控机)
第一步,下载最新版node_exporter,并解压
wget https://github.com/prometheus/node_exporter/releases/download/v1.9.1/node_exporter-1.9.1.linux-amd64.tar.gz
tar -zxvf node_exporter-1.9.1.linux-amd64.tar.gz
第二步,创建文件夹,符合运维规范
cd node_exporter-1.9.1.linux-amd64
mkdir bin
mv node_exporter bin/
第三步,创建守护进程
编写新的 node_exporter.service。
vi /etc/systemd/system/node_exporter.service
写入以下内容
[Unit] # 描述服务的元数据
Description=node_exporter # 服务的描述信息
After=network.target # 依赖关系:在网络服务启动之后启动
[Service] # 服务运行参数
type=simple # 表示服务的类型为简单类型,即服务进程将直接运行并在主机上监听。
ExecStart=/usr/local/node_exporter/bin/node_exporter # 服务启动命令路径
Restart=on-failure
[Install] # 安装参数
WantedBy=multi-user.target # 服务所属的目标(multi-user.target表示多用户命令行模式)
最后一步,启动服务
mv node_exporter-1.9.1.linux-amd64 /usr/local/node_exporter
systemctl start node_exporter
Step 3 配置Prometheus(主控机)
在Prometheus服务器上添加监控节点
vim /usr/local/prometheus/conf/prometheus.yml
在下面添加
- job_name: "server" # 资源对象名称
static_configs:
- targets: ["localhost:9100"] # 资源地址及端口
检查语法错误
cd /usr/local/prometheus/
./promtool check config conf/prometheus.yml
重启Prometheus服务
systemctl restart prometheus
Step 4 下载安装Grafana
yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-12.0.0-1.x86_64.rpm
启动grafana
systemctl start grafana-server
查看grafana接口
ss -tlnp
Step 5 配置Grafana反代
第一步,修改grafana.ini,并重启grafana
[server]
domain = example.com
重启grafana
systemctl restart grafana-server
第二步,修改nginx配置文件并重启nginx
location / {
proxy_set_header Host $host;
proxy_pass http://grafana;
}
# Proxy Grafana Live WebSocket connections.
location /api/live/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_pass http://grafana;
}
重启nginx
systemctl restart nginx