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

相关推荐
抛砖者20 小时前
1、Ubuntu上MySQL安装,密码设置,远程访问,端口修改
mysql·ubuntu
小兔薯了20 小时前
11. Linux firewall 防火墙管理
linux·运维·服务器
Linux技术芯21 小时前
浅谈SCSI寻址机制与工作阶段深度解析?
linux
☼←安于亥时→❦1 天前
Linux 系统日志‘/var/log/syslog‘ 和 ‘/var/log/messages‘ 详解
linux
袁气满满~_~1 天前
Ubuntu下配置PyTorch
linux·pytorch·ubuntu
倦王1 天前
Linux看ip,改用户名字加权限,在单独用户下设置miniconda
linux·服务器·tcp/ip
少废话h1 天前
Flume Kafka源与汇的topic覆盖问题解决
java·linux·kafka·flume
凤凰战士芭比Q1 天前
Docker安装与常用命令
linux·运维·docker·容器
ASKED_20191 天前
常用 Linux 命令大全(文件、网络、时间、进程、数据库、工具全覆盖)
linux·网络·数据库
a123560mh1 天前
国产信创操作系统银河麒麟常见软件适配(MongoDB、 Redis、Nginx、Tomcat)
linux·redis·nginx·mongodb·tomcat·kylin