VictoriaMetrics 完全指南 / 11 - 安全配置
11 · 安全配置
本章目标
- 了解 VictoriaMetrics 的安全模型
- 配置 Basic 认证和 Bearer Token
- 启用 TLS 加密通信
- 使用 vmauth 实现访问控制
- 掌握生产环境安全加固措施
11.1 安全模型概览
VictoriaMetrics 单节点版本身不提供内建认证,安全通过以下方式实现:
┌──────────────────────────────────────────────┐
│ 安全架构 │
│ │
│ 客户端 ──▶ Nginx/vmauth ──▶ VictoriaMetrics │
│ (认证层) (数据层) │
│ │
│ ┌─────────────────────────────────────────┐ │
│ │ 安全层次 │ │
│ │ 1. 网络层 - 防火墙 / VPC │ │
│ │ 2. 传输层 - TLS │ │
│ │ 3. 应用层 - 认证 (Basic/Token) │ │
│ │ 4. 授权层 - 多租户隔离 │ │
│ │ 5. 审计层 - 访问日志 │ │
│ └─────────────────────────────────────────┘ │
└──────────────────────────────────────────────┘
11.2 vmauth 认证代理
11.2.1 安装 vmauth
VM_VERSION="v1.106.0"
curl -LO "https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/${VM_VERSION}/vmauth-linux-amd64-${VM_VERSION}.tar.gz"
tar xzf "vmauth-linux-amd64-${VM_VERSION}.tar.gz"
sudo mv vmauth-prod /usr/local/bin/vmauth
chmod +x /usr/local/bin/vmauth
11.2.2 配置文件
# /etc/vmauth/auth.yml
# 用户认证配置
users:
# admin 用户 - 完全访问权限
- username: "admin"
password: "strong-password-here"
url_prefix: "http://localhost:8428"
# 只读用户 - 只能查询
- username: "readonly"
password: "another-strong-password"
url_prefix:
- src_paths: ["/api/v1/query", "/api/v1/query_range", "/api/v1/series", "/vmui/*"]
url_prefix: "http://localhost:8428"
- src_paths: ["/api/v1/write"]
url_prefix: "" # 空前缀 = 拒绝访问
# 写入专用用户 - 只能写入
- username: "writer"
password: "writer-password"
url_prefix:
- src_paths: ["/api/v1/write", "/api/v1/import/*"]
url_prefix: "http://localhost:8428"
- src_paths: ["/api/v1/query*", "/api/v1/series*"]
url_prefix: ""
# 多租户用户
- username: "tenant1"
password: "tenant1-password"
url_prefix: "http://localhost:8481/select/1001/0/prometheus"
# Bearer Token 认证
- bearer_token: "my-secret-bearer-token"
url_prefix: "http://localhost:8428"
11.2.3 启动 vmauth
vmauth \
-auth.config=/etc/vmauth/auth.yml \
-httpListenAddr=:8427 \
-reloadAuthKey=my-reload-secret
11.2.4 测试认证
# 使用正确凭据
curl -u admin:strong-password-here http://localhost:8427/api/v1/query?query=up
# → 200 OK
# 使用错误凭据
curl -u admin:wrong-password http://localhost:8427/api/v1/query?query=up
# → 401 Unauthorized
# 使用 Bearer Token
curl -H "Authorization: Bearer my-secret-bearer-token" \
http://localhost:8427/api/v1/query?query=up
# → 200 OK
# 只读用户尝试写入
curl -u readonly:another-strong-password \
-d 'test_metric 42' \
http://localhost:8427/api/v1/import/prometheus
# → 403 Forbidden (因为 url_prefix 为空)
11.3 Nginx 认证
11.3.1 基础 Nginx 配置
# /etc/nginx/sites-available/victoriametrics
server {
listen 80;
server_name vm.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name vm.example.com;
# TLS 配置
ssl_certificate /etc/ssl/vm.example.com.crt;
ssl_certificate_key /etc/ssl/vm.example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 写入端点 - 使用 IP 白名单
location /api/v1/write {
allow 10.0.0.0/8;
allow 172.16.0.0/12;
deny all;
proxy_pass http://127.0.0.1:8428;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /api/v1/import/ {
allow 10.0.0.0/8;
deny all;
proxy_pass http://127.0.0.1:8428;
}
# 查询端点 - Basic 认证
location /api/v1/query {
auth_basic "VictoriaMetrics";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://127.0.0.1:8428;
}
location /api/v1/query_range {
auth_basic "VictoriaMetrics";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://127.0.0.1:8428;
}
# VMUI - Basic 认证
location /vmui/ {
auth_basic "VictoriaMetrics";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://127.0.0.1:8428;
}
# 拒绝其他所有请求
location / {
deny all;
}
}
11.3.2 创建 htpasswd 文件
# 安装 htpasswd 工具
sudo apt install apache2-utils
# 创建密码文件
sudo htpasswd -c /etc/nginx/.htpasswd admin
# 输入密码
# 添加更多用户
sudo htpasswd /etc/nginx/.htpasswd readonly
11.4 TLS 配置
11.4.1 自签名证书(测试环境)
# 生成自签名证书
openssl req -x509 -nodes -days 365 \
-newkey rsa:2048 \
-keyout /etc/ssl/vm.key \
-out /etc/ssl/vm.crt \
-subj "/CN=vm.example.com"
# 或使用 Let's Encrypt(生产环境)
# certbot certonly --standalone -d vm.example.com
11.4.2 集群组件间 TLS
# vminsert 启用 TLS
vminsert \
-tls \
-tlsCertFile=/etc/ssl/vminsert.crt \
-tlsKeyFile=/etc/ssl/vminsert.key \
-storageNode=https://vmstorage1:8400
# vmstorage 启用 TLS
vmstorage \
-tls \
-tlsCertFile=/etc/ssl/vmstorage.crt \
-tlsKeyFile=/etc/ssl/vmstorage.key \
-vminsertAddr=:8400 \
-vmselectAddr=:8401
11.4.3 mTLS(双向 TLS)
# vmstorage 要求客户端证书
vmstorage \
-tls \
-tlsCertFile=/etc/ssl/vmstorage.crt \
-tlsKeyFile=/etc/ssl/vmstorage.key \
-mtls \
-mtlsCAFile=/etc/ssl/ca.crt
11.5 网络安全
11.5.1 防火墙配置
# UFW(Ubuntu)
sudo ufw default deny incoming
sudo ufw allow ssh
sudo ufw allow from 10.0.0.0/8 to any port 8428 # 只允许内网访问 VM
sudo ufw enable
# iptables
sudo iptables -A INPUT -p tcp --dport 8428 -s 10.0.0.0/8 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8428 -j DROP
11.5.2 绑定地址
# 只监听本地(通过反向代理访问)
victoria-metrics -httpListenAddr=127.0.0.1:8428
# 监听所有地址(不推荐生产环境)
victoria-metrics -httpListenAddr=:8428
11.6 访问控制最佳实践
11.6.1 角色划分
| 角色 | 权限 | 实现方式 |
|---|
| 管理员 | 完全读写 + 管理 | vmauth admin 用户 |
| 运维 | 读取 + 部分写入 | vmauth 运维用户 |
| 开发 | 只读查询 | vmauth readonly 用户 |
| 采集代理 | 只写 | vmauth writer 用户 |
| 外部集成 | 受限读取 | vmauth + IP 白名单 |
11.6.2 生产环境安全清单
| # | 检查项 | 推荐措施 |
|---|
| 1 | 绑定地址 | -httpListenAddr=127.0.0.1:8428 |
| 2 | 认证 | 使用 vmauth 或 Nginx Basic Auth |
| 3 | TLS | 生产环境必须启用 TLS |
| 4 | 防火墙 | 只允许必要的网络访问 |
| 5 | 密码强度 | ≥ 16 字符,包含大小写+数字+特殊字符 |
| 6 | 定期轮换 | 每 90 天更换密码/Token |
| 7 | 审计日志 | 启用 Nginx/vmauth 访问日志 |
| 8 | 最小权限 | 每个角色只授予必要的权限 |
| 9 | 禁用 API | 关闭不需要的 API 端点 |
| 10 | 监控 | 监控认证失败次数 |
本章小结
| 要点 | 内容 |
|---|
| vmauth | 官方认证代理,支持 Basic/Bearer/多租户 |
| Nginx | 灵活的反向代理,支持 IP 白名单 |
| TLS | 支持 mTLS,集群组件间加密 |
| 网络安全 | 防火墙 + 绑定地址 + 最小权限 |
| 最佳实践 | 分角色、最小权限、定期审计 |
扩展阅读