5月27日 17:51

Prometheus 安全认证怎么配置?Basic Auth 与 RBAC 实战

Prometheus 默认不启用认证,9090 端口一旦暴露,任何人都能访问 /metrics 和 /api/v1/query,造成监控数据泄露甚至配置被篡改。下面从 scrape 认证、服务端防护、K8s RBAC 三个层面说明如何配置 Prometheus 的安全认证和访问控制。

一、Scrape 侧认证:让 Prometheus 访问受保护的 Target

1.1 Basic Auth

当 Target(如 pushgateway 或其他 exporter)启用了 Basic Auth 时,Prometheus 抓取时需携带用户名密码:

yaml
scrape_configs: - job_name: 'pushgateway' basic_auth: username: admin password: <your-password> static_configs: - targets: ['localhost:9091']

若需从文件读取密码,使用 password_file 替代 password,避免密钥明文写入配置。

1.2 Bearer Token

在 Kubernetes 环境中,Prometheus 使用 ServiceAccount Token 访问 kube-apiserver:

yaml
scrape_configs: - job_name: 'kubernetes-apiservers' bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token scheme: https tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt kubernetes_sd_configs: - role: endpoints

1.3 TLS 双向认证

当 Target 要求客户端证书时:

yaml
scrape_configs: - job_name: 'etcd' scheme: https tls_config: ca_file: /etc/prometheus/tls/ca.crt cert_file: /etc/prometheus/tls/client.crt key_file: /etc/prometheus/tls/client.key insecure_skip_verify: false

二、服务端防护:保护 Prometheus 自身的 UI 和 API

Prometheus 从 2.24 版本开始支持内置 Basic Auth 和 TLS,通过 web.config.file 参数加载。

2.1 生成 bcrypt 密码哈希

bash
# 安装工具 apt-get install -y python3-bcrypt # 生成哈希 python3 -c " import bcrypt print(bcrypt.hashpw(b'your-secure-password', bcrypt.gensalt()).decode()) " # 输出类似:$2b$12$Wxn...

2.2 编写 web-config.yml

yaml
basic_auth_users: admin: '$2b$12$Wxn...' # 上一步生成的哈希 tls_config: cert_file: /etc/prometheus/tls/cert.pem key_file: /etc/prometheus/tls/key.pem

验证配置文件语法:

bash
promtool check web-config web-config.yml

2.3 启动时加载

bash
prometheus --config.file=/etc/prometheus/prometheus.yml --web.config.file=/etc/prometheus/web-config.yml

启动后访问 http://localhost:9090 将弹出 Basic Auth 登录框,未认证请求返回 401。

2.4 Docker 部署示例

yaml
# docker-compose.yml services: prometheus: image: prom/prometheus:v2.53.0 command: - '--config.file=/etc/prometheus/prometheus.yml' - '--web.config.file=/etc/prometheus/web-config.yml' volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml - ./web-config.yml:/etc/prometheus/web-config.yml ports: - '9090:9090'

2.5 Nginx 反向代理认证(适用 kube-prometheus-stack)

当前 kube-prometheus-stack 不直接支持内置 Basic Auth,可通过 Ingress + Nginx 实现:

yaml
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: prometheus-ingress annotations: nginx.ingress.kubernetes.io/auth-type: basic nginx.ingress.kubernetes.io/auth-secret: prometheus-basic-auth nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required' spec: rules: - host: prometheus.example.com http: paths: - path: / pathType: Prefix backend: service: name: prometheus-operated port: number: 9090

创建对应的 Secret:

bash
htpasswd -c auth admin kubectl create secret generic prometheus-basic-auth --from-file=auth -n monitoring

三、Kubernetes RBAC:限制 Prometheus 的访问范围

3.1 创建 ServiceAccount

yaml
apiVersion: v1 kind: ServiceAccount metadata: name: prometheus namespace: monitoring

3.2 定义 Role(最小权限)

yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: prometheus namespace: monitoring rules: - apiGroups: [''] resources: ['pods', 'services', 'endpoints'] verbs: ['get', 'list', 'watch']

仅授权 monitoring 命名空间下的资源读取,不授予写权限和跨命名空间权限。

3.3 绑定 Role 和 ServiceAccount

yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: prometheus namespace: monitoring roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: prometheus subjects: - kind: ServiceAccount name: prometheus namespace: monitoring

3.4 ClusterRole(如需跨命名空间监控)

yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus rules: - apiGroups: [''] resources: ['nodes', 'pods', 'services', 'endpoints'] verbs: ['get', 'list', 'watch'] - nonResourceURLs: ['/metrics'] verbs: ['get'] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: prometheus roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: prometheus subjects: - kind: ServiceAccount name: prometheus namespace: monitoring

四、Grafana 对接待认证的 Prometheus

配置 Basic Auth 后,Grafana 数据源需同步修改:

yaml
# grafana datasources config apiVersion: 1 datasources: - name: Prometheus type: prometheus url: http://prometheus:9090 basicAuth: true basicAuthUser: admin secureJsonData: basicAuthPassword: your-secure-password editable: true

五、安全加固清单

措施说明
启用 Basic Auth防止未授权访问 UI 和 API
启用 TLS防止传输层窃听
网络策略隔离Kubernetes NetworkPolicy 限制 Pod 间访问
防火墙规则仅允许 Grafana/Alertmanager 所在网段访问 9090
密钥不硬编码使用 password_file 或 Kubernetes Secrets
定期轮换密钥每 90 天更换 Basic Auth 密码和 TLS 证书
审计日志通过请求日志监控异常访问模式
及时更新版本关注 Prometheus 安全公告,修补已知漏洞
标签:Prometheus