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

相关推荐
WoY20204 小时前
opencv-python在ubuntu系统中缺少依赖
python·opencv·ubuntu
ICscholar6 小时前
ExaDigiT/RAPS
linux·服务器·ubuntu·系统架构·运维开发
sim20206 小时前
systemctl isolate graphical.target命令不能随便敲
linux·mysql
米高梅狮子7 小时前
4. Linux 进程调度管理
linux·运维·服务器
再创世纪8 小时前
让USB打印机变网络打印机,秀才USB打印服务器
linux·运维·网络
fengyehongWorld9 小时前
Linux ssh端口转发
linux·ssh
知识分享小能手10 小时前
Ubuntu入门学习教程,从入门到精通, Ubuntu 22.04中的Shell编程详细知识点(含案例代码)(17)
linux·学习·ubuntu
Xの哲學11 小时前
深入解析 Linux systemd: 现代初始化系统的设计与实现
linux·服务器·网络·算法·边缘计算
龙月11 小时前
journalctl命令以及参数详解
linux·运维
EndingCoder12 小时前
TypeScript 的基本类型:数字、字符串和布尔
linux·ubuntu·typescript