linux/ubuntu安装Prometheus&Grafana

在Linux上安装 PrometheusGrafana 的步骤如下:

1. 安装 Prometheus

Prometheus 是一个开源的系统监控和报警工具,通常用于收集和存储度量数据。

步骤:
  1. 更新系统并安装依赖

    打开终端,首先确保系统是最新的,并安装 wgetcurl 等依赖工具:

    bash 复制代码
    sudo apt update
    sudo apt install -y wget
  2. 下载 Prometheus 二进制文件

    到 Prometheus 官方的 GitHub Releases 页面获取最新版本(Prometheus releases)。

    例如,下载 2.40.0 版本:

    bash 复制代码
    wget https://github.com/prometheus/prometheus/releases/download/v2.40.0/prometheus-2.40.0.linux-amd64.tar.gz
  3. 解压并安装 Prometheus

    解压下载的文件并进入目录:

    bash 复制代码
    tar -xvzf prometheus-2.40.0.linux-amd64.tar.gz
    cd prometheus-2.40.0.linux-amd64
  4. 运行 Prometheus

    启动 Prometheus,使用以下命令:

    bash 复制代码
    ./prometheus --config.file=prometheus.yml

    默认 Prometheus 会在 localhost:9090 上运行,你可以在浏览器中访问 http://localhost:9090 来查看 Prometheus 界面。

可选步骤:配置 Prometheus 为服务

为了让 Prometheus 在系统启动时自动启动,可以将其配置为系统服务。

  1. 创建 Prometheus 用户

    bash 复制代码
    sudo useradd --no-create-home --shell /bin/false prometheus
  2. 创建 Prometheus 配置和数据目录

    bash 复制代码
    sudo mkdir /etc/prometheus
    sudo mkdir /var/lib/prometheus
    sudo cp prometheus.yml /etc/prometheus
    sudo cp -r consoles/ /etc/prometheus
    sudo cp -r console_libraries/ /etc/prometheus
  3. 创建 systemd 服务文件

    创建 /etc/systemd/system/prometheus.service 文件,内容如下:

    ini 复制代码
    [Unit]
    Description=Prometheus
    Documentation=https://prometheus.io/docs/introduction/overview/
    After=network.target
    
    [Service]
    User=prometheus
    ExecStart=/usr/local/bin/prometheus \
      --config.file=/etc/prometheus/prometheus.yml \
      --storage.tsdb.path=/var/lib/prometheus/
    
    [Install]
    WantedBy=default.target
  4. 启动 Prometheus 服务

    bash 复制代码
    sudo systemctl daemon-reload
    sudo systemctl start prometheus
    sudo systemctl enable prometheus

2. 安装 Grafana

Grafana 是一个用于分析和可视化度量数据的开源工具,可以从 Prometheus 等数据源获取数据。

步骤:
  1. 安装 Grafana

    通过官方的 APT 仓库安装 Grafana。

    bash 复制代码
    sudo apt install -y software-properties-common
    sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
    sudo apt update
    sudo apt install grafana
  2. 启动 Grafana

    启动 Grafana 服务:

    bash 复制代码
    sudo systemctl start grafana-server
    sudo systemctl enable grafana-server
  3. 访问 Grafana Web 界面

    Grafana 默认会在 localhost:3000 上运行,访问 http://localhost:3000 即可。初始登录用户名和密码都是 admin,登录后可以设置新的密码。

  4. 配置 Grafana 连接 Prometheus

    1. 在 Grafana 页面中,点击左侧菜单的 Add data source
    2. 选择 Prometheus
    3. HTTP URL 处输入 Prometheus 的地址(如果你是在本地运行 Prometheus,填写 http://localhost:9090)。
    4. 点击 Save & Test,验证连接是否成功。
  5. 创建 Dashboard

    配置完 Prometheus 数据源后,你可以创建自己的仪表盘(Dashboard)来可视化监控数据。

3. 可选:使用 Docker 安装 Prometheus 和 Grafana

如果你不想直接在系统中安装,你也可以使用 Docker 安装 Prometheus 和 Grafana。

安装 Prometheus(Docker)
bash 复制代码
docker run -d --name prometheus -p 9090:9090 prom/prometheus
安装 Grafana(Docker)
bash 复制代码
docker run -d --name grafana -p 3000:3000 grafana/grafana

然后,你可以访问 Grafana 界面并配置 Prometheus 作为数据源,过程与上面描述的相同。


总结

通过以上步骤,你可以在 Linux 上成功安装并配置 Prometheus 和 Grafana。Prometheus 用于收集和存储度量数据,而 Grafana 则用于将这些数据可视化。两者结合使用能帮助你更好地监控和管理你的系统和应用性能。

相关推荐
梦星辰.42 分钟前
VSCode CUDA C++进行Linux远程开发
linux·c++·vscode
远方16091 小时前
0x-2-Oracle Linux 9上安装JDK配置环境变量
java·linux·oracle
cui_win1 小时前
每日一令:Linux 极简通关指南 - 汇总
linux·运维·服务器
知星小度S1 小时前
Linux权限探秘:驾驭权限模型,筑牢系统安全
linux·运维·服务器
黄交大彭于晏1 小时前
发送文件脚本源码版本
java·linux·windows
搞Linux的杰仔2 小时前
Ubuntu20.04基础配置安装——系统安装(一)
linux·嵌入式开发
Kaede64 小时前
如何应对Linux云服务器磁盘空间不足的情况
linux·运维·服务器
草上爬6 小时前
OpenWrt:使用ALSA实现边录边播
ubuntu·openwrt·record·alsa·play
apocelipes10 小时前
Linux c 运行时获取动态库所在路径
linux·c语言·linux编程
努力学习的小廉11 小时前
深入了解linux系统—— 进程池
linux·运维·服务器