<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Monitoring on HiDa</title><link>https://www.0niu.cn/tags/monitoring/</link><description>Recent content in Monitoring on HiDa</description><generator>Hugo -- gohugo.io</generator><language>zh</language><lastBuildDate>Thu, 06 Mar 2025 10:43:00 +0800</lastBuildDate><atom:link href="https://www.0niu.cn/tags/monitoring/index.xml" rel="self" type="application/rss+xml"/><item><title>Prometheus 监控平台搭建</title><link>https://www.0niu.cn/posts/prometheus%E7%9B%91%E6%8E%A7%E5%B9%B3%E5%8F%B0%E6%90%AD%E5%BB%BA/</link><pubDate>Thu, 06 Mar 2025 10:43:00 +0800</pubDate><guid>https://www.0niu.cn/posts/prometheus%E7%9B%91%E6%8E%A7%E5%B9%B3%E5%8F%B0%E6%90%AD%E5%BB%BA/</guid><description>&lt;p>本文介绍如何在 Linux 系统上搭建 Prometheus 监控平台。&lt;/p>
&lt;h2 id="安装步骤">安装步骤&lt;/h2>
&lt;h3 id="1-下载并解压-prometheus">1. 下载并解压 Prometheus&lt;/h3>
&lt;p>下载预编译的 Prometheus tar 包并解压到 &lt;code>/opt/prometheus&lt;/code> 文件夹：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 下载最新版本的 Prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.linux-amd64.tar.gz
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 解压到指定目录&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>tar xzf prometheus-*.linux-amd64.tar.gz
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>mkdir -p /opt/prometheus
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cd prometheus-*.linux-amd64
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cp prometheus promtool /opt/prometheus/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cp -r consoles prometheus.yml /opt/prometheus/
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="2-创建系统用户和数据目录">2. 创建系统用户和数据目录&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 创建 prometheus 用户&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo useradd --no-create-home --shell /bin/false prometheus
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 创建数据目录&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo mkdir -p /app/prometheus/data
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo chown -R prometheus:prometheus /app/prometheus
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description><content>&lt;p>本文介绍如何在 Linux 系统上搭建 Prometheus 监控平台。&lt;/p>
&lt;h2 id="安装步骤">安装步骤&lt;/h2>
&lt;h3 id="1-下载并解压-prometheus">1. 下载并解压 Prometheus&lt;/h3>
&lt;p>下载预编译的 Prometheus tar 包并解压到 &lt;code>/opt/prometheus&lt;/code> 文件夹：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 下载最新版本的 Prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>wget https://github.com/prometheus/prometheus/releases/download/v*/prometheus-*.linux-amd64.tar.gz
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 解压到指定目录&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>tar xzf prometheus-*.linux-amd64.tar.gz
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>mkdir -p /opt/prometheus
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cd prometheus-*.linux-amd64
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cp prometheus promtool /opt/prometheus/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>cp -r consoles prometheus.yml /opt/prometheus/
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="2-创建系统用户和数据目录">2. 创建系统用户和数据目录&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 创建 prometheus 用户&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo useradd --no-create-home --shell /bin/false prometheus
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 创建数据目录&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo mkdir -p /app/prometheus/data
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo chown -R prometheus:prometheus /app/prometheus
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="3-编写-systemd-服务文件">3. 编写 Systemd 服务文件&lt;/h3>
&lt;p>创建服务文件 &lt;code>/etc/systemd/system/prometheus.service&lt;/code>：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-ini" data-lang="ini">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">[Unit]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Description&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">Prometheus Monitoring&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">After&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">network-online.target&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Wants&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">network-online.target&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">[Service]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">User&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">WorkingDirectory&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">/app/prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">Restart&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">on-failure&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">RestartSec&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">5s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">ExecStart&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">/opt/prometheus/prometheus \
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74"> --config.file=/opt/prometheus/prometheus.yml \
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74"> --storage.tsdb.path=/app/prometheus/data&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Security hardening&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">NoNewPrivileges&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">true&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">ProtectSystem&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">strict&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">ProtectHome&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">true&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">ReadWritePaths&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">/app/prometheus/data&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Resource limits&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">MemoryMax&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">4G&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">[Install]&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">WantedBy&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">multi-user.target&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="4-修改-selinux-配置">4. 修改 SELinux 配置&lt;/h3>
&lt;p>如果系统启用了 SELinux，需要调整上下文：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>sudo restorecon -Rv /etc/systemd/system/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo chcon -t bin_t /opt/prometheus/prometheus
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="5-启动服务">5. 启动服务&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 重新加载 systemd 配置&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo systemctl daemon-reload
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 启用并启动 Prometheus&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo systemctl enable --now prometheus
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># 检查服务状态&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sudo systemctl status prometheus
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="验证安装">验证安装&lt;/h2>
&lt;p>访问 &lt;code>http://服务器IP:9090&lt;/code> 查看 Prometheus Web 界面。&lt;/p>
&lt;h2 id="配置文件">配置文件&lt;/h2>
&lt;p>主配置文件位于 &lt;code>/opt/prometheus/prometheus.yml&lt;/code>，根据需要添加监控目标和告警规则。&lt;/p>
&lt;h2 id="参考资料">参考资料&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://prometheus.io/docs/">Prometheus 官方文档&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://github.com/prometheus/prometheus/releases">Prometheus GitHub Releases&lt;/a>&lt;/li>
&lt;/ul></content></item></channel></rss>