Prometheus: 為 Prometheus Node Exporter 添加身份驗證

本文件是從 Linux 獲取指標指南的一部分。請在此處查看完整指南:如何使用 Node Exporter 在 Prometheus 中獲取 Linux 伺服器指標

👋 歡迎來到 Stackhero 文件!

Stackhero 提供一個即用型的 Prometheus cloud 解決方案,帶來多項好處,包括:

  • 包含 Alert Manager,可發送警報至 SlackMattermostPagerDuty 等。
  • 專用電郵伺服器發送無限電郵警報
  • Blackbox 用於探測 HTTPICMPTCP 等。
  • 使用線上配置文件編輯器進行簡單配置
  • 只需點擊即可輕鬆更新
  • 專用私有 VM提供的最佳性能和強大安全性

節省時間簡化您的生活:只需5 分鐘即可嘗試 Stackhero 的 Prometheus cloud hosting 解決方案!

默認情況下,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 應返回指標列表。