如何在 Debian 12 上安装 Prometheus

Linux命令 Edge插件网 1年前 (2023-09-16) 290次浏览 已收录 0个评论

在当今复杂的数字环境中,系统监控至关重要。它使管理员能够在问题升级为关键问题之前主动识别和缓解问题。为此目的,最强大和最广泛采用的工具之一是普罗米修斯。此开源监视和警报工具包以其可靠性和灵活性而闻名。

如何在 Debian 12 上安装 Prometheus

在 Debian 12 书虫上安装 Prometheus

第 1 步。在我们安装任何软件之前,通过在终端中运行以下命令来确保您的系统是最新的非常重要:apt

<span class="pln">sudo apt update</span>

此命令将刷新存储库,允许您安装最新版本的软件包。

第 2 步。为 Prometheus 创建一个非 root 用户。

出于安全原因,不鼓励以根用户身份运行 Prometheus。最佳实践是为 Prometheus 创建一个专用的非 root 用户:

<span class="pln">sudo useradd </span><span class="pun">--</span><span class="kwd">no</span><span class="pun">-</span><span class="pln">create</span><span class="pun">-</span><span class="pln">home </span><span class="pun">--</span><span class="pln">shell </span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">false</span><span class="pln"> prometheus</span>

第 3 步。在 Debian 12安装 Prometheus

普罗米修斯提供了预编译的二进制文件,您可以从官方网站下载。首先,导航到 Prometheus 安装的首选目录,例如 。/opt

<span class="pln">cd </span><span class="pun">/</span><span class="pln">opt</span>

接下来,用于下载最新版本的普罗米修斯。请务必查看普罗米修斯官方下载页面以获取最新版本:wget

<span class="pln">wget https</span><span class="pun">:</span><span class="com">//github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz</span>

下载完成后,使用以下方法提取普罗米修斯存档:tar

<span class="pln">sudo tar </span><span class="pun">-</span><span class="pln">xzf prometheus</span><span class="pun">-</span><span class="lit">2.47</span><span class="pun">.</span><span class="lit">0.linux</span><span class="pun">-</span><span class="pln">amd64</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz</span>

Prometheus 依靠配置文件来确定要监控的目标以及如何从中抓取指标。让我们创建这个配置文件:

<span class="pln">sudo mkdir </span><span class="pun">-</span><span class="pln">p </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">prometheus
sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">prometheus</span><span class="pun">/</span><span class="pln">prometheus</span><span class="pun">.</span><span class="pln">yml</span>

将以下基本配置添加到:prometheus.yml

<span class="kwd">global</span><span class="pun">:</span><span class="pln">
  scrape_interval</span><span class="pun">:</span> <span class="lit">15s</span><span class="pln">

scrape_configs</span><span class="pun">:</span>
  <span class="pun">-</span><span class="pln"> job_name</span><span class="pun">:</span> <span class="str">'prometheus'</span><span class="pln">
    static_configs</span><span class="pun">:</span>
      <span class="pun">-</span><span class="pln"> targets</span><span class="pun">:</span> <span class="pun">[</span><span class="str">'localhost:9090'</span><span class="pun">]</span>

这个最小配置指示普罗米修斯抓取自己的指标。稍后我们将探讨更高级的配置。

第 4 步。创建 Prometheus 系统服务。

为普罗米修斯创建一个新的单元文件:systemd

<span class="pln">sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">prometheus</span><span class="pun">.</span><span class="pln">service</span>

将以下内容添加到单元文件中:

<span class="pun">[</span><span class="typ">Unit</span><span class="pun">]</span>
<span class="typ">Description</span><span class="pun">=</span><span class="typ">Prometheus</span> <span class="typ">Monitoring</span>
<span class="typ">Wants</span><span class="pun">=</span><span class="pln">network</span><span class="pun">-</span><span class="pln">online</span><span class="pun">.</span><span class="pln">target
</span><span class="typ">After</span><span class="pun">=</span><span class="pln">network</span><span class="pun">-</span><span class="pln">online</span><span class="pun">.</span><span class="pln">target

</span><span class="pun">[</span><span class="typ">Service</span><span class="pun">]</span>
<span class="typ">User</span><span class="pun">=</span><span class="pln">prometheus
</span><span class="typ">ExecStart</span><span class="pun">=</span><span class="str">/opt/</span><span class="pln">prometheus</span><span class="pun">-</span><span class="lit">2.47</span><span class="pun">.</span><span class="lit">0.linux</span><span class="pun">-</span><span class="pln">amd64</span><span class="pun">/</span><span class="pln">prometheus \
  </span><span class="pun">--</span><span class="pln">config</span><span class="pun">.</span><span class="pln">file</span><span class="pun">=</span><span class="str">/etc/</span><span class="pln">prometheus</span><span class="pun">/</span><span class="pln">prometheus</span><span class="pun">.</span><span class="pln">yml \
  </span><span class="pun">--</span><span class="pln">storage</span><span class="pun">.</span><span class="pln">tsdb</span><span class="pun">.</span><span class="pln">path</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">prometheus</span><span class="pun">/</span><span class="pln"> \
  </span><span class="pun">--</span><span class="pln">web</span><span class="pun">.</span><span class="pln">console</span><span class="pun">.</span><span class="pln">templates</span><span class="pun">=</span><span class="str">/opt/</span><span class="pln">prometheus</span><span class="pun">-</span><span class="lit">2.47</span><span class="pun">.</span><span class="lit">0.linux</span><span class="pun">-</span><span class="pln">amd64</span><span class="pun">/</span><span class="pln">consoles \
  </span><span class="pun">--</span><span class="pln">web</span><span class="pun">.</span><span class="pln">console</span><span class="pun">.</span><span class="pln">libraries</span><span class="pun">=</span><span class="str">/opt/</span><span class="pln">prometheus</span><span class="pun">-</span><span class="lit">2.47</span><span class="pun">.</span><span class="lit">0.linux</span><span class="pun">-</span><span class="pln">amd64</span><span class="pun">/</span><span class="pln">console_libraries

</span><span class="pun">[</span><span class="typ">Install</span><span class="pun">]</span>
<span class="typ">WantedBy</span><span class="pun">=</span><span class="pln">multi</span><span class="pun">-</span><span class="pln">user</span><span class="pun">.</span><span class="pln">target</span>

重新加载以应用更改并启动普罗米修斯:systemd

<span class="pln">sudo systemctl daemon</span><span class="pun">-</span><span class="pln">reload
sudo systemctl start prometheus
sudo systemctl enable prometheus</span>

您可以检查 Prometheus 服务的状态,以确保其正常运行:

<span class="pln">sudo systemctl status prometheus</span>

第5步。配置防火墙。

现在,我们使用 Prometheus 设置了一个简单防火墙 (UFW),以允许在默认 Web 端口 9090 上进行公共访问:

<span class="pln">sudo ufw allow </span><span class="typ">OpenSSH</span><span class="pln">
sudo ufw allow </span><span class="lit">9090</span><span class="pun">/</span><span class="pln">tcp
sudo ufw enable</span>

第 6 步。访问普罗米修斯网页用户界面。

默认情况下,Prometheus 的 Web 界面可在 本地访问。要使其可从 Web 浏览器访问,您需要配置反向代理或在防火墙中打开端口。出于安全原因,请考虑将反向代理与 HTTPS 一起使用。http://localhost:9090

如何在 Debian 12 上安装 Prometheus

步骤 7.故障 排除。

A. 常见的安装和配置问题 √

  • 服务未启动:检查日志中的错误,并确保单元文件中的文件路径正确。systemd
  • 配置错误:验证语法错误。prometheus.yml
  • 防火墙阻止:确保防火墙规则允许与 Prometheus 和导出器的传入连接。

B. 使用 Prometheus 日志对√进行故障排除

普罗米修斯记录有价值的信息,可以帮助诊断问题。使用以下命令检查日志:

<span class="pln">journalctl </span><span class="pun">-</span><span class="pln">u prometheus</span>

感谢您使用本教程在 Debian 12 书虫上安装最新版本的 Prometheus 监控工具。如需其他帮助或有用信息,我们建议您查看普罗米修斯官方网站


Edge插件网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在 Debian 12 上安装 Prometheus
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址