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 则用于将这些数据可视化。两者结合使用能帮助你更好地监控和管理你的系统和应用性能。

相关推荐
likfishdn17 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean37 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=39 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
若云止水1 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
sz66cm2 小时前
Linux基础 -- ARM 32 位架构动态注入代码技术文档
linux·arm开发·架构