Prometheus 的 Node Exporter 并沒有提供任何認(rèn)證支持见转。不過水醋,借助 Nginx 作為反向代理服務(wù)器旗笔,我們可以很容易地為 Node Exporter 添加 HTTP Basic Auth 功能。
首先拄踪,啟動(dòng) Node Exporter蝇恶,監(jiān)聽 9090 端口。
然后惶桐,在 /etc/nginx
(可能你的 Nginx 配置目錄在其他路徑撮弧,請(qǐng)做相應(yīng)修改)目錄下,使用 apache2-utils
提供的 htpasswd
工具創(chuàng)建一個(gè)用戶文件姚糊,需要填入用戶名和密碼:
$ 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 服務(wù)贸辈。此時(shí)在瀏覽器中訪問 server:19090,瀏覽器會(huì)要求你輸入用戶名和密碼肠槽。
最后一步是修改 prometheus.yml
文件擎淤,將我們的 Node Exporter 服務(wù)添加進(jìn)去:
- job_name: 'node-exporter'
static_configs:
- targets: ['your-ip:19090']
basic_auth:
username: yuankun
password: your-password
重啟 Prometheus 服務(wù),就大功告成了秸仙。