Prometheus: 为 Prometheus Node Exporter 添加身份验证
本文档属于从 Linux 检索指标指南的一部分。您可以在此处查看完整指南:如何使用 Node Exporter 在 Prometheus 中检索 Linux 服务器指标。
👋 欢迎来到 Stackhero 文档!
Stackhero 提供即用型 Prometheus 云 解决方案,具有多种优势,包括:
- 包含
Alert Manager,可发送警报到Slack、Mattermost、PagerDuty等。- 专用电子邮件服务器发送无限制电子邮件警报。
Blackbox用于探测HTTP、ICMP、TCP等。- 使用在线配置文件编辑器进行简单配置。
- 只需点击即可轻松更新。
- 由专用私有 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 时需要用到它。
添加用户并重启 Node Exporter
将以下行附加到配置文件中,以添加一个名为 "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应返回指标列表。