prometheus 中文文档
v2.17
v2.17
  • Prometheus 中文文档
  • introduction
    • 概述
    • 初识 Prometheus
    • 与替代品比较
    • 常见问题
    • 路线图
    • 相关资源
    • 相关术语
  • concepts
    • 数据模型
    • 数据指标类型
    • 作业和实例
  • prometheus
    • 快速开始
    • 安装
    • 配置
      • 配置
      • 定义记录规则
      • 告警规则
      • 模板示例
      • 模板参考
      • 规则的单元测试
    • 查询
      • Prometheus 查询
      • 运算符
      • 函数
      • 查询示例
      • HTTP API
    • 存储
    • 联合
    • 管理 API
    • Prometheus 2.0 迁移指南
    • API 稳定性保证
  • visualization
    • 表达式浏览器
    • Grafana 对 Prometheus 的支持
    • 控制台模板
  • operating
    • 安全模型
    • 集成
  • instrumenting
    • 客户端库
    • 编写客户端库
    • 推送数据指标
    • 数据导出及相关集成
    • 编写数据导出器
    • 公开的格式
  • alerting
    • 告警概述
    • Alertmanager
    • 配置
    • 发送告警
    • 通知模板参考
    • 通知模板示例
    • 管理 API
  • practices
    • 指标和标签命名
    • 控制台和仪表盘
    • 工具
    • Histogram and Summary
    • 告警
    • 记录规则
    • 什么时候使用 Pushgateway
    • 远程写调试
  • guides
    • 使用 cAdvisor 监控 docker 容器数据指标
    • 使用基于文件的服务发现来发现数据采集目标
    • 实现一个 Go 应用
    • 使用 Node Exporter 监控 Linux 主机指标
    • 使用基本身份验证保护 Prometheus API 和 UI 端点
    • 理解并使用 multi-target exporters 模式
    • 使用 TLS 加密 Prometheus API 和 UI 端点
    • 使用 Prometheus 查询日志
由 GitBook 提供支持
在本页
  • nginx 示例
  • nginx 配置
  • Prometheus 配置
  • 测试
  • 小结

这有帮助吗?

  1. guides

使用基本身份验证保护 Prometheus API 和 UI 端点

上一页使用 Node Exporter 监控 Linux 主机指标下一页理解并使用 multi-target exporters 模式

最后更新于5年前

这有帮助吗?

Prometheus 不直接支持与 Prometheus 和 的连接的(也称为"基本身份验证")。如果您想对这些连接强制执行基本身份认证,建议将 Prometheus 与反向代理结合使用,并在代理层应用身份验证。您可以在 Prometheus 中使用任何您喜欢的反向代理服务。本指南中,我们将提供一个 。

Note: 尽管到 Prometheus 实例的连接不支持基本身份认证,但从 Prometheus 实例到的连接均支持基本身份认证。

nginx 示例

假设您要运行 Prometheus 实例作为运行在 localhost:12321 的 服务的后端负载,并且所有 Prometheus 端点都可以通过 /prometheus 端点使用。因此 Prometheus /metrics端点的完整 URL 为:

http://localhost:12321/prometheus/metrics

假设您想要访问 Prometheus 实例的所有用户都需要输入用户名和密码。在此示例中,使用 admin 作为用户名,然后设定您所想要任何密码。

首先,使用 创建一个 .htpasswd 文件用来保存用户名/密码,并将其保存在 /etc/nginx 目录中:

mkdir -p /etc/nginx
htpasswd -c /etc/nginx/.htpasswd admin

Note: 此示例使用 /etc/nginx 作为 nginx 配置文件(包括 .htpasswd 文件)的位置,但这会因安装而异。其它 ,包括 /usr/local/nginx/conf 和 /usr/local/etc/nginx

nginx 配置

如下是一个示例 配置文件 (用户名密码保存在 /etc/nginx/.htpasswd). 使用此配置,nginx 将对与 /prometheus 端点(代理 Prometheus)的所有连接强制执行基本身份验证:

http {
    server {
        listen 12321;

        location /prometheus {
            auth_basic           "Prometheus";
            auth_basic_user_file /etc/nginx/.htpasswd;

            proxy_pass           http://localhost:9090/;
        }
    }
}

events {}

使用上面的配置启动nginx:

nginx -c /etc/nginx/nginx.conf

Prometheus 配置

在 nginx 代理后端运行 Prometheus 时,您需要将外部URL设置为 http://localhost:12321/prometheus 并将路由前缀设置为 /:

prometheus \
  --config.file=/path/to/prometheus.yml \
  --web.external-url=http://localhost:12321/prometheus \
  --web.route-prefix="/"

测试

您可以使用 cURL 与本地 nginx/Prometheus 进行交互,尝试如下请求:

curl --head http://localhost:12321/prometheus/graph

这将返回 401 Unauthorized 响应,因为您未能提供有效的用户名和密码。该响应还将包含 nginx 提供的 WWW-Authenticate: Basic realm="Prometheus" 响应头, 提示 nginx auth_basic 参数指定的 Prometheus 基本认证域。

要使用基本身份验证成功访问 Prometheus 端点,如 /metrics,请使用-u 标志提供正确的用户名,并在出现提示时输入密码:

curl -u admin http://localhost:12321/prometheus/metrics
Enter host password for user 'admin':

然后应该返回 Prometheus 数据指标输出,看起来应该像这样:

# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.0001343
go_gc_duration_seconds{quantile="0.25"} 0.0002032
go_gc_duration_seconds{quantile="0.5"} 0.0004485
...

小结

在本指南中,您将用户名和密码存储在 .htpasswd 文件中,将 nginx 配置为使用该文件中的凭据对访问 Prometheus HTTP 端点的用户进行身份验证,启动 nginx,并为 Prometheus 配置反向代理。

表达式浏览器
HTTP API
基本身份验证
nginx
htpasswd
常用 nginx 配置目录
nginx.conf
nginx 示例
抓取目标