安全配置 #
一、安全概述 #
1.1 安全风险 #
text
Prometheus安全风险:
┌─────────────────────────────────────────────┐
│ 1. 未授权访问 │
├─────────────────────────────────────────────┤
│ • 默认无认证 │
│ • 任何人可访问 │
│ • 敏感数据泄露 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 2. 数据篡改 │
├─────────────────────────────────────────────┤
│ • 管理API未保护 │
│ • 可删除数据 │
│ • 可修改配置 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 3. 中间人攻击 │
├─────────────────────────────────────────────┤
│ • 通信未加密 │
│ • 数据可被窃听 │
│ • 可被篡改 │
└─────────────────────────────────────────────┘
1.2 安全措施 #
text
安全措施:
┌─────────────────────────────────────────────┐
│ 1. 网络隔离 │
├─────────────────────────────────────────────┤
│ • 限制访问IP │
│ • 使用防火墙 │
│ • 内网部署 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 2. 认证授权 │
├─────────────────────────────────────────────┤
│ • 添加认证 │
│ • 使用反向代理 │
│ • RBAC控制 │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│ 3. 加密通信 │
├─────────────────────────────────────────────┤
│ • 启用TLS │
│ • 使用HTTPS │
│ • 证书验证 │
└─────────────────────────────────────────────┘
二、网络隔离 #
2.1 防火墙配置 #
bash
# iptables配置
# 只允许特定IP访问
iptables -A INPUT -p tcp --dport 9090 -s 10.0.0.0/8 -j ACCEPT
iptables -A INPUT -p tcp --dport 9090 -j DROP
# firewalld配置
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" port protocol="tcp" port="9090" accept'
firewall-cmd --reload
2.2 绑定内网地址 #
bash
# 只监听内网地址
prometheus --web.listen-address=10.0.0.1:9090
2.3 Kubernetes网络策略 #
yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: prometheus-network-policy
namespace: monitoring
spec:
podSelector:
matchLabels:
app: prometheus
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: monitoring
- podSelector:
matchLabels:
app: grafana
ports:
- protocol: TCP
port: 9090
三、反向代理认证 #
3.1 Nginx反向代理 #
nginx
# nginx.conf
server {
listen 80;
server_name prometheus.example.com;
# 重定向到HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name prometheus.example.com;
# SSL配置
ssl_certificate /etc/nginx/ssl/prometheus.crt;
ssl_certificate_key /etc/nginx/ssl/prometheus.key;
# 基本认证
auth_basic "Prometheus";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
proxy_pass http://localhost:9090;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
3.2 创建密码文件 #
bash
# 创建htpasswd文件
htpasswd -c /etc/nginx/.htpasswd admin
# 添加用户
htpasswd /etc/nginx/.htpasswd user1
3.3 OAuth认证 #
nginx
# 使用oauth2-proxy
server {
listen 443 ssl;
server_name prometheus.example.com;
ssl_certificate /etc/nginx/ssl/prometheus.crt;
ssl_certificate_key /etc/nginx/ssl/prometheus.key;
location /oauth/ {
proxy_pass http://oauth2-proxy:4180;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location / {
auth_request /oauth/auth;
auth_request_set $user $upstream_http_x_auth_request_user;
proxy_pass http://localhost:9090;
proxy_set_header X-User $user;
}
}
四、TLS加密 #
4.1 生成证书 #
bash
# 生成私钥
openssl genrsa -out prometheus.key 2048
# 生成证书签名请求
openssl req -new -key prometheus.key -out prometheus.csr
# 生成自签名证书
openssl x509 -req -days 365 -in prometheus.csr -signkey prometheus.key -out prometheus.crt
# 或使用Let's Encrypt
certbot certonly --standalone -d prometheus.example.com
4.2 Prometheus TLS配置 #
bash
# 启动Prometheus with TLS
prometheus \
--config.file=prometheus.yml \
--web.tls.config=tls.yml
yaml
# tls.yml
tls_server_config:
cert_file: /etc/prometheus/ssl/prometheus.crt
key_file: /etc/prometheus/ssl/prometheus.key
# 客户端证书验证(可选)
client_ca_file: /etc/prometheus/ssl/ca.crt
client_auth_type: VerifyClientCertIfGiven
# 最小TLS版本
min_version: TLS12
# 加密套件
cipher_suites:
- TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
- TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
4.3 采集目标TLS配置 #
yaml
# prometheus.yml
scrape_configs:
- job_name: 'https-targets'
scheme: https
tls_config:
# 跳过证书验证(不推荐生产使用)
insecure_skip_verify: true
# 或指定CA证书
ca_file: /etc/prometheus/ssl/ca.crt
# 客户端证书
cert_file: /etc/prometheus/ssl/client.crt
key_file: /etc/prometheus/ssl/client.key
# 服务器名称
server_name: target.example.com
static_configs:
- targets: ['target.example.com:443']
五、管理API保护 #
5.1 禁用管理API #
bash
# 默认禁用管理API
prometheus --web.enable-admin-api=false
5.2 启用管理API(谨慎使用) #
bash
# 启用管理API
prometheus --web.enable-admin-api=true
5.3 限制管理API访问 #
nginx
# nginx.conf
# 只允许特定IP访问管理API
location ~ ^/api/v1/admin/ {
allow 10.0.0.0/8;
deny all;
proxy_pass http://localhost:9090;
}
六、Alertmanager安全 #
6.1 访问控制 #
bash
# 绑定内网地址
alertmanager --web.listen-address=10.0.0.1:9093
6.2 静默权限控制 #
yaml
# alertmanager.yml
global:
resolve_timeout: 5m
route:
receiver: 'default'
receivers:
- name: 'default'
email_configs:
- to: 'admin@example.com'
# 静默需要认证(通过反向代理实现)
七、安全最佳实践 #
7.1 检查清单 #
text
安全检查清单:
□ 网络隔离
├── 限制访问IP
├── 使用防火墙
└── 内网部署
□ 认证授权
├── 启用认证
├── 使用反向代理
└── RBAC控制
□ 加密通信
├── 启用TLS
├── 使用HTTPS
└── 证书验证
□ API保护
├── 禁用管理API
├── 限制API访问
└── 审计日志
□ 数据保护
├── 定期备份
├── 数据加密
└── 访问审计
7.2 安全监控 #
promql
# 监控认证失败
rate(prometheus_http_requests_total{code="401"}[5m])
# 监控异常访问
rate(prometheus_http_requests_total{code="403"}[5m])
# 监控API使用
rate(prometheus_http_requests_total{handler=~"/api/v1/admin/.*"}[5m])
八、总结 #
安全配置要点:
| 措施 | 说明 |
|---|---|
| 网络隔离 | 限制访问IP、防火墙 |
| 认证授权 | 反向代理、基本认证 |
| TLS加密 | HTTPS、证书验证 |
| API保护 | 禁用管理API、限制访问 |
配置文件:
| 文件 | 说明 |
|---|---|
| tls.yml | TLS配置 |
| nginx.conf | 反向代理配置 |
| prometheus.yml | 采集配置 |
下一步,让我们学习故障排查!
最后更新:2026-03-27