Prometheus不直接支持連接到Prometheus表達式瀏覽器和HTTP API的Basic認證。如果你希望對這些連接執行Basic認證,我們建議將Prometheus與反向代理結合使用,並在代理層添加身份認證。你可以使用Prometheus的任何反向代理,但在本指南中,我們將提供一個nginx示例。
注意:雖然不支持連接至Prometheus實例的Basic認證,但是支持Prometheus實例與抓取目標之間的Basic認證。
nginx示例
假設你希望在nginx服務器後端運行一個Prometheus實例 localhost:12321
,並且,所有Prometheus端點都通過/prometheus
端點進行訪問。因此,Prometheus的/metrics
端點的完整URL應該是:
http://localhost:12321/prometheus/metrics
我們還假設所有訪問Prometheus實例的用戶都需要提供用戶名和密碼。對於本例,使用admin
作爲用戶名並選擇你想要的任何密碼。
首先,使用htpasswd
工具創建一個.htpasswd
文件來存儲用戶名和密碼,並將其存儲在/etc/nginx
目錄中:
mkdir -p /etc/nginx
htpasswd -c /etc/nginx/.htpasswd admin
注意:本例使用/etc/nginx
作爲nginx配置文件(包括.htpasswd
文件)的目錄,但是根據安裝的不同而有所不同。其他常見的nginx配置目錄包括/usr/local/nginx/conf
和/usr/local/etc/nginx
。
nginx配置
下面是一個示例nginx.conf
配置文件(存儲在/etc/nginx/.htpasswd
)。有了這個配置,nginx將強制所有連接到/prometheus
端點的請求執行Basic認證:
http {
server {
listen 12321;
location /prometheus {
auth_basic "Prometheus";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://localhost:9090/;
}
}
}
events {}