Prometheus使用Basic認證保護Prometheus API和UI端點

完整譯文請訪問http://www.coderdocument.com/docs/prometheus/v2.14/guides/securing_prometheus_api_and_ui_endpoints_using_basic_auth.html

Prometheus不直接支持連接到Prometheus表達式瀏覽器HTTP APIBasic認證。如果你希望對這些連接執行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 {}

完整譯文請訪問http://www.coderdocument.com/docs/prometheus/v2.14/guides/securing_prometheus_api_and_ui_endpoints_using_basic_auth.html

發佈了124 篇原創文章 · 獲贊 91 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章