Prometheus 的 Node Exporter 并沒有提供任何認證支持。不過,借助 Nginx 作為反向代理服務器,我們可以很容易地為 Node Exporter 添加 HTTP Basic Auth 功能。
首先,啟動 Node Exporter,監聽 9090 端口。
然后,在 /etc/nginx
(可能你的 Nginx 配置目錄在其他路徑,請做相應修改)目錄下,使用 apache2-utils
提供的 htpasswd
工具創建一個用戶文件,需要填入用戶名和密碼:
$ htpasswd -c .htpasswd yuankun
New password:
Re-type new password:
Adding password for user yuankun
接下來,在 Nginx 配置文件中添加下面的配置,這里我們使用 19090 作為代理端口:
http {
server {
listen 0.0.0.0:19090;
location / {
proxy_pass http://localhost:9090/;
auth_basic "Prometheus";
auth_basic_user_file ".htpasswd";
}
}
保存配置文件,然后重新載入 Nginx 服務。此時在瀏覽器中訪問 server:19090,瀏覽器會要求你輸入用戶名和密碼。
最后一步是修改 prometheus.yml
文件,將我們的 Node Exporter 服務添加進去:
- job_name: 'node-exporter'
static_configs:
- targets: ['your-ip:19090']
basic_auth:
username: yuankun
password: your-password
重啟 Prometheus 服務,就大功告成了。