安装普罗米修斯和grafana过程
以下是安装 Prometheus 和 Grafana 的过程。这里我们假设使用的是基于 Linux 的服务器(如 Ubuntu 或 CentOS),并使用二进制文件安装 Prometheus 和 YUM 或 APT 包管理器安装 Grafana。
一、安装 Prometheus
1. 下载并解压 Prometheus
在 Prometheus 的 官方发布页面 中找到最新的版本,并下载对应的二进制文件。
# 切换到 /opt 目录
cd /opt
# 下载最新版本的 Prometheus (以 v2.47.0 为例)
wget https://github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz
# 解压文件
tar -xvf prometheus-2.47.0.linux-amd64.tar.gz
cd prometheus-2.47.0.linux-amd64
2. 配置 Prometheus
Prometheus 使用 prometheus.yml
配置文件,默认配置文件已经存在于解压后的目录中。可以按需修改。
# prometheus.yml 示例内容
global:
scrape_interval: 15s # 设置抓取间隔时间
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
3. 启动 Prometheus
将 Prometheus 作为后台服务运行,可以在 /etc/systemd/system/
下创建一个服务文件:
sudo vim /etc/systemd/system/prometheus.service
内容如下:
[Unit]
Description=Prometheus Monitoring System
Wants=network-online.target
After=network-online.target
[Service]
User=root
ExecStart=/opt/prometheus-2.47.0.linux-amd64/prometheus --config.file=/opt/prometheus-2.47.0.linux-amd64/prometheus.yml
Restart=always
[Install]
WantedBy=multi-user.target
保存文件后,启动 Prometheus 服务并设置为开机启动:
# 刷新 systemd
sudo systemctl daemon-reload
# 启动 Prometheus
sudo systemctl start prometheus
# 设置开机启动
sudo systemctl enable prometheus
4. 验证 Prometheus
访问 http://<服务器IP>:9090
,看到 Prometheus 的 web 界面即表示安装成功。
二、安装 Grafana
1. 添加 Grafana 仓库并安装 Grafana
对于 Ubuntu/Debian 系统:
# 添加 Grafana 仓库
sudo apt-get install -y software-properties-common
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
# 更新并安装 Grafana
sudo apt-get update
sudo apt-get install -y grafana
对于 CentOS/RHEL 系统:
# 添加 Grafana 仓库
sudo cat <<EOF | sudo tee /etc/yum.repos.d/grafana.repo
[grafana]
name=Grafana OSS
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslverify=1
EOF
# 安装 Grafana
sudo yum install -y grafana
2. 启动并配置 Grafana
安装完成后,启动 Grafana 并设置开机启动:
# 启动 Grafana 服务
sudo systemctl start grafana-server
# 设置开机启动
sudo systemctl enable grafana-server
3. 验证 Grafana
在浏览器中访问 http://<服务器IP>:3000
,默认用户名和密码为 admin
。首次登录时需要更改密码。
三、在 Grafana 中配置 Prometheus 数据源
- 登录 Grafana,点击左侧菜单中的 Configuration > Data Sources。
- 选择 Add data source。
- 选择 Prometheus。
- 在 URL 栏中填写 Prometheus 的地址,例如
http://localhost:9090
。 - 点击 Save & Test,确保 Grafana 能够连接到 Prometheus。
四、在 Grafana 中创建仪表板
- 点击左侧菜单中的 Create > Dashboard。
- 选择 Add new panel。
- 选择数据源为 Prometheus,在查询栏中输入 Prometheus 的查询语句,例如
up
,以测试服务器的运行状态。 - 配置其他图表参数后,点击 Apply,将图表添加到仪表板中。
总结
通过以上步骤,您成功安装并配置了 Prometheus 和 Grafana,并且将 Prometheus 设置为 Grafana 的数据源,可以在 Grafana 中监控 Prometheus 收集的指标数据。
作者:严锋 创建时间:2024-10-12 08:45
最后编辑:严锋 更新时间:2025-06-07 16:36
最后编辑:严锋 更新时间:2025-06-07 16:36