Prometheus: 为 Prometheus Node Exporter 添加身份验证

本文件是从 Linux 检索指标指南的一部分。您可以在这里查看完整指南:如何使用 Node Exporter 在 Prometheus 中检索 Linux 服务器指标

👋 欢迎来到 Stackhero 文档!

Stackhero 提供即用型 Prometheus 云 解决方案,具有多种优势,包括:

  • 包含 Alert Manager,可发送警报到 SlackMattermostPagerDuty 等。
  • 专用邮件服务器发送无限制邮件警报
  • Blackbox 用于探测 HTTPICMPTCP 等。
  • 使用在线配置文件编辑器进行轻松配置
  • 只需点击即可轻松更新
  • 专用私有 VM提供的最佳性能和强大安全性

节省时间简化生活:只需 5 分钟即可试用 Stackhero 的 Prometheus 云托管 解决方案!

默认情况下,Node Exporter 不执行身份验证,这意味着任何人都可以检索暴露的指标。为了保护访问,您可以为用户添加基本身份验证。

使用以下命令生成密码:

password=`openssl rand -base64 32`
passwordHashed=`echo ${password} | htpasswd -inBC 10 "" | tr -d ':'`
echo "保留给 Prometheus 服务器的明文密码:${password}"

如果您没有 htpasswd 二进制文件,可以使用 apt-get install --no-install-recommends apache2-utils 在 Debian/Ubuntu 上安装它。

请妥善保管明文密码,因为在配置 Prometheus 时需要用到它。

将以下行附加到配置文件中,以添加一个名为 "prometheus" 的用户,并使用生成的密码哈希:

sudo cat << EOF >> /etc/prometheus_node_exporter/configuration.yml
basic_auth_users:
  prometheus: ${passwordHashed}

EOF

# 重启 Node Exporter
sudo systemctl restart node_exporter

最后,验证身份验证是否正常工作:

  • 运行 curl http://localhost:9100/metrics 应返回 "Unauthorized"。
  • 运行 curl -u prometheus:${password} http://localhost:9100/metrics 应返回指标列表。