安装 Prometheus监控主机服务

一、安装 Prometheus

下载 Prometheus

首先,访问 Prometheus 官网 获取最新版本的下载链接,然后使用 wget 下载:

 

wget https://github.com/prometheus/prometheus/releases/download/v3.2.1/prometheus-3.2.1.linux-amd64.tar.gz

解压并安装

 

解压下载的文件:

 

tar -xvzf prometheus-3.2.1.linux-amd64.tar.gz

将解压后的文件夹移动到 /opt/prometheus:

 

mv prometheus-3.2.1.linux-amd64 /opt/prometheus

安装 htpasswd 来生成密码 hash

 

#Ubuntu
 
apt install apache2-utils -y
 
#centos
 
yum install httpd-tools -y

在 prometheus目录下执行

 

# htpasswd -nBC 12 '' | tr -d ':\n'
New password:
Re-type new password:
 
$2y$12$LWDB21oY/67kz3lu8Y7bGOcerRodeAK4SfOSmielNq.BzMlrjw1/q

创建配置文件

 

vim /opt/prometheus/config.yml
basic_auth_users:
admin: $2y$12$LWDB21oY/67kz3lu8Y7bGOcerRodeAK4SfOSmielNq.BzMlrjw1/q

创建 Prometheus 用户
为了安全,创建一个专用用户来运行 Prometheus:

 
sudo useradd --no-create-home --shell /bin/false prometheus
sudo chown -R prometheus:prometheus /opt/prometheus

配置 Prometheus

编辑 Prometheus 配置文件 /opt/prometheus/prometheus.yml:

 

sudo vim /opt/prometheus/prometheus.yml

添加以下内容:

 
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
 
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
 
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
 
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
basic_auth:
username: admin
password: Yy5lC64W4qtF9y0uOsk
 
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
 
static_configs:
- targets: ["localhost:9090"]
- job_name: "node_exporter"
basic_auth:
username: admin
password: Yy5lC64W4wtF9y0uOsk
static_configs:
- targets: ["localhost:9100"]
 
创建 Systemd 服务

创建 Systemd 服务文件 /etc/systemd/system/prometheus.service:

 

sudo vim /etc/systemd/system/prometheus.service

添加以下内容:

 
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
 
[Service]
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/prometheus \
--config.file=/opt/prometheus/prometheus.yml \
--storage.tsdb.path=/opt/prometheus/data \
--web.config.file=/opt/prometheus/config.yml
Restart=always
 
[Install]
WantedBy=multi-user.target
 
启动 Prometheus

启动并设置开机启用 Prometheus 服务:

 
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
 
验证 Prometheus

访问

二、安装 Node Exporter

Node Exporter 用于收集系统指标(如 CPU、内存使用率)。

下载 Node Exporter

 

wget https://github.com/prometheus/node_exporter/releases/download/v1.9.0/node_exporter-1.9.0.linux-amd64.tar.gz

解压并安装

 
tar -xvzf node_exporter-1.9.0.linux-amd64.tar.gz
sudo mv node_exporter-1.9.0.linux-amd64/node_exporter /usr/local/bin/

 

node_exporter目录下执行

 

# htpasswd -nBC 12 '' | tr -d ':\n'
New password:
Re-type new password:
 
$2y$12$LWDB21oY/67kz3lu8Y7bGOcerRodeAK4SfOSmielNq.BzMlrjw1/q

创建配置文件

 

mkdir /opt/node_exporter
vim /opt/node_exporter/config.yml
basic_auth_users:
admin: $2y$12$LWDB21oY/67kz3lu8Yw7bGOcerRodeAK4SfOSmielNq.BzMlrjw1/q

创建 Systemd 服务

创建 Systemd 服务文件 /etc/systemd/system/node_exporter.service:

 

 

 

sudo vim /etc/systemd/system/node_exporter.service

添加以下内容:

 

 
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
 
[Service]
User=node_exporter
Group=node_exporter
ExecStart=/usr/local/bin/node_exporter --web.config.file=/opt/node_exporter/config.yml
 
[Install]
WantedBy=multi-user.target
 
为用户和组增加权限
 
sudo useradd -rs /bin/false node_exporter
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
sudo chown -R node_exporter:node_exporter /opt/node_exporter
sudo chmod 755 /usr/local/bin/node_exporter

启动 Node Exporter

 
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter

验证 Node Exporter

访问 http://<服务器IP>:9100/metrics,如果看到系统指标数据,说明安装成功。

三、安装 Grafana

下载并安装 Grafana

 

sudo yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-11.5.2-1.x86_64.rpm

启动 Grafana服务

 

 
sudo systemctl start grafana-server
sudo systemctl enable grafana-server

web界面访问

访问 Grafana
访问 http://<服务器IP>:3000,使用默认账号 admin 和密码 admin 登录。

四、配置 Grafana 仪表盘

添加 Prometheus 数据源

登录 Grafana,点击左侧菜单的 Configuration > Data Sources。
点击 Add data source,

选择 Prometheus

在 URL 中输入 http://localhost:9090,点击 Save & Test。

导入 Node Exporter 仪表盘

在 dashboad页面点击 Create > Import。

在 Import via grafana.com 中输入仪表盘 ID 1860(Node Exporter 官方仪表盘)。

选择 Prometheus 数据源,点击 Import。

查看仪表盘

导入成功后,你可以看到一个完整的系统监控仪表盘,包含 CPU、内存、磁盘、网络等指标。

posted @ 2025-03-20 22:02  happy峰  阅读(81)  评论(0)    收藏  举报