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

相关推荐
2401_826097625 小时前
JavaEE-Linux环境部署
java·linux·java-ee
(:满天星:)6 小时前
第31篇:块设备与字符设备管理深度解析(基于OpenEuler 24.03)
linux·运维·服务器·网络·centos
爱莉希雅&&&6 小时前
shell编程之awk命令详解
linux·服务器·git
笑稀了的野生俊6 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
渡我白衣6 小时前
Linux操作系统之文件(四):文件系统(上)
linux
ZZH1120KQ6 小时前
Linux系统安全及应用
linux·运维·系统安全
程序漫游人7 小时前
centos8.5安装jdk21详细安装教程
java·linux
小小小糖果人7 小时前
Linux云计算基础篇(5)
linux·运维·服务器
small_wh1te_coder7 小时前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
小张是铁粉8 小时前
docker在Linux的安装遇到的问题
linux·docker·容器