大家好,我是 WeiyiGeek,一名深耕安全运维开发(SecOpsDev)领域的技术从业者,致力于探索DevOps与安全的融合(DevSecOps),自动化运维工具开发与实践,企业网络安全防护,欢迎各位道友一起学习交流、一起进步。
前言简述
安全:授权认证访问
默认情况下,VictoriaLogs 不限制外部客户端访问,以及访问认证等,若主机防火墙、硬件防火墙也未配置对应的IP访问限制策略,这可能有可能导致一系列安全问题,例如,未经授权的访问和数据泄露。
所以作者在自身生产环境中,通常配置 VictoriaLogs 仅在内部网络上运行,并通过硬件防火墙限制外部访问,而且针对且接口读写访问配置适当的认证和授权机制,例如,使用 VictoriaMetrics的 vmauth 插件进行认证,亦或者使用 Nginx 进行反向代理和 auth_basic 配置,此处作者更推荐使用前者。
VictoriaMetrics 提供了一个名为 vmauth 的插件,用于VictoriaMetrics系列产品(VictoriaMetrics、VictoriaLogs等)的认证和授权,vmauth 是一个HTTP代理,它可以跨HTTP组件或任何其他HTTP后端授权、路由和负载平衡请求,此处以官方提供的使用场景及通信图作为示例:
weiyigeek.top-vmauth插件使用场景图
更多使用配置,请参考官方文档:https://docs.victoriametrics.com/victoriametrics/vmauth/
weiyigeek.top-vmauth使用配置图原文链接: https://articles.zsxq.com/id_7vv1aicx4vsz.html
安装 vmauth
描述:可根据需要以及不同环境选择合适的安装方式,支持二进制安装、容器化部署等,下面作者都简单介绍一下:
二进制方式安装
只需从发布页面下载 vmutils-* 档案,解压并将以下标志传递给 vmauth 二进制文件,默认的 vmauth 端口为 8427 可以通过 -httpListenAddr 命令行标志修改监听端口,并根据提供的 -auth.config 参数指定的配置文件来代理它们。
实践环境:
$ cat /etc/openEuler-release
openEuler release 24.03 (LTS)
操作示例:
# https://github.com/VictoriaMetrics/VictoriaMetrics/releases
VERSION=v1.120.0
TMP_DIR=$(mktemp -d)
echo"正在下载 vmutils 版本: $VERSION"
wget -c https://down.avi.gs/https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/${VERSION}/vmutils-linux-amd64-${VERSION}.tar.gz -O ${TMP_DIR}/vmutils.tar.gz
echo"正在解压 vmutils 工具包到 /usr/local/bin/"
tar -zxvf ${TMP_DIR}/vmutils.tar.gz -C /usr/local/bin/
# vmagent-prod
# vmalert-prod
# vmalert-tool-prod
# vmauth-prod
# vmbackup-prod
# vmrestore-prod
# vmctl-prod
# vmbackupmanager-prod
# vmgateway-prod
echo"查看 vmauth (社区) 版本信息"
vmauth-prod --version
vmauth-20250620-151704-tags-v1.120.0-0-g07c8a4a9a7
温馨提示: VictoriaMetrics 官方提供的 VM 系列产品分为企业版(enterprise)和社区版(community),下载时不要下载错了,否则运行时会报错。
{"ts":"2025-07-04T11:22:33.712+0800","level":"error","caller":"VictoriaMetrics/lib/license/copyrights.go:33","msg":"VictoriaMetrics Enterprise license is required. Please obtain it at https://victoriametrics.com/products/enterprise/trial/ and pass it via either -license or -licenseFile command-line flags. See https://docs.victoriametrics.com/enterprise/"}
防火墙配置:
# 防火墙安装(若主机不存在则安装)
# yum install firewalld
# 启用防火墙
firewall-cmd --state
# not running
systemctl enable firewalld --now
firewall-cmd --state
# running
# 配置防火墙规则,允许访问 vmauth 端口
firewall-cmd --add-port=8427/tcp --permanent
firewall-cmd --reload
配置示例:在 vmauth 中通常使用 Basic Auth 认证方式,当然亦可选择Bearer Token auth 认证方式,配置文件如下:
mkdir -vp/etc/vmlogs
tee/etc/vmlogs/vmauth.yml<<EOF
# vmauth.yml 完整配置示例
users:
-username:"write"
password:"weiyigeek.top"
url_map:
-src_paths:
-"/insert/.*"
url_prefix:"http://10.20.172.214:9428"
-username:"read"
password:"weiyigeek.top"
url_map:
-src_paths:
-"/select/.*"
url_prefix:"http://10.20.172.214:9428"
-bearer_token:WEIYIGEEK.TOP
url_prefix:"http://victoria-metrics:8428/"
EOF
启用 vmauth 代理:
# 1.前台启动 vmauth 代理服务(测试)
vmauth-prod -httpListenAddr 10.20.172.214:8427 --auth.config=/etc/vmlogs/vmauth.yml -loggerTimezone=Asia/Shanghai --loggerFormat=json
# 2.使用 systemd 服务管理 vmauth 代理服务(生产)
# 监听IP端口为10.20.172.214:842,指定认证配置文件,指定日志输出格式与时区。
cat > /etc/vmlogs/vmauth.conf <<EOF
ARGS=-httpListenAddr 10.20.172.214:8427 --auth.config=/etc/vmlogs/vmauth.yml -loggerTimezone=Asia/Shanghai --loggerFormat=json
EOF
# vmauth 服务配置文件示例:
cat > /etc/systemd/system/vmauth.service <<EOF
[Unit]
Description=vmauth is an HTTP proxy,which can authorize route and load balance
Documentation=https://docs.victoriametrics.com
After=network.target
[Service]
Type=simple
StartLimitBurst=5
StartLimitInterval=0
Restart=on-failure
RestartSec=5
EnvironmentFile=-/etc/vmlogs/vmauth.conf
ExecStart=/usr/local/bin/vmauth-prod $ARGS
ExecStop=/bin/kill -s SIGTERM $MAINPID
ExecReload=/bin/kill -HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
StandardOutput=journal
StandardError=journal
SyslogIdentifier=vmauth
PrivateTmp=yes
ProtectHome=yes
NoNewPrivileges=yes
ProtectSystem=strict
ProtectControlGroups=true
ProtectKernelModules=true
ProtectKernelTunables=yes
[Install]
WantedBy=multi-user.target
EOF
# 重载 systemd 管理器配置,启动 vmauth 服务
systemctl daemon-reload
systemctl start vmauth.service
systemctl status vmauth.service
weiyigeek.top-前台与后台运行vmauth图
在服务启动成功后便可,验证 vmauth 代理访问认证是否生效:
# (1)为了对比,作者先访问未经 vmauth 代理的 VictoriaLogs 服务,查看是否能正常访问,
# 发送 json 格式[https://jsonlines.org/]日志数据到 VictoriaLogs 服务
echo'{ "log": { "level": "info", "message": "hello world, VictoriaLogs" }, "date": "0", "stream": "stream1" }
{ "log": { "level": "error", "message": "WebSite: www.weiyigeek.top" }, "date": "0", "stream": "stream1" }
{ "log": { "level": "info", "message": "hello world" }, "date": "0", "stream": "stream2" }
' | curl -X POST -H 'Content-Type: application/stream+json' --data-binary @-
'http://10.20.172.214:9428/insert/jsonline?_stream_fields=stream&_time_field=date&_msg_field=log.message'
# 使用 curl 查看 VictoriaLogs 存储的日志数据
curl 'http://10.20.172.214:9428/select/logsql/query'
-X POST
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0'
-H 'AccountID: 0'
-H 'ProjectID: 0'
-d 'query=log.level:*'
# {"_time":"2025-07-04T03:50:57.032545761Z","_stream_id":"0000000000000000356bfe9e3c71128c750d94c15df6b908","_stream":"{stream="stream1"}","_msg":"hello world, VictoriaLogs","stream":"stream1","log.level":"info"}
# {"_time":"2025-07-04T03:50:57.032570587Z","_stream_id":"0000000000000000356bfe9e3c71128c750d94c15df6b908","_stream":"{stream="stream1"}","_msg":"WebSite: www.weiyigeek.top","stream":"stream1","log.level":"error"}
# {"_time":"2025-07-04T03:50:57.032574268Z","_stream_id":"0000000000000000db1269eafe0b151b8f37d371eeaad405","_stream":"{stream="stream2"}","_msg":"hello world","log.level":"info","stream":"stream2"}
# (2) 尝试使用 vmauth 代理访问 VictoriaLogs 服务,查看是否能正常访问,同样进行读操作
curl 'http://10.20.172.214:8427/select/logsql/query'
-X POST
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0'
-H 'AccountID: 0'
-H 'ProjectID: 0'
-d 'query=log.level:*'
# missing 'Authorization' request header # 无法访问,由于未设置认证票据,无法通过认证。
# 添加认证票据 -u 'read:weiyigeek.top' ,再次尝试访问 VictoriaLogs 服务,可以正常访问并获取数据
curl 'http://10.20.172.214:8427/select/logsql/query'
-X POST
-u 'read:weiyigeek.top'
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0'
-H 'AccountID: 0'
-H 'ProjectID: 0'
-d 'query=log.level:*'
weiyigeek.top-使用vmauth代理认证方式访问图
温馨提示:上述命令中使用 -H 'AccountID: 0' -H 'ProjectID: 0' 参数作用的是在多租户环境下,用以区分不同租户的日志数据,缺省的就是 0:0。
知识扩展:vmauth 支持启用 TLS 加密访问,如有这方面要求的朋友,可参考配置方式如下:
/usr/local/bin/vmauth-prod -tls -tlsKeyFile=/path/to/tls_key_file -tlsCertFile=/path/to/tls_cert_file -httpListenAddr=0.0.0.0:443 --auth.config=/etc/vmlogs/vmauth.yml
# 参数介绍
-tls # 启用 TLS 加密访问
-tlsKeyFile # 配置 TLS 私钥文件路径
-tlsCertFile # 配置 TLS 证书文件路径
-httpListenAddr # 配置监听地址与端口,例如:0.0.0.0:443 表示在所有网络接口上监听 443 端口。
--auth.config # 配置访问策略配置文件路径
容器化方式安装
作者推荐使用 Docker 或者 K8S 集群方式部署 vmauth,因为这种方式更简单、易于维护,这里以 K8S 为例,演示如何通过手搓资源配置清单部署 vmauth:
Docker Hub 镜像地址:https://hub.docker.com/r/victoriametrics/vmauth/tags
# 创建 vmauth 配置文件目录
mkdir -vp /storage/app/logging/vmauth/conf
# 创建名称空间
kubectl create ns logging
# 创建 vmauth 资源(Deployment、Services)配置清单文件
apiVersion: v1
kind: Service
metadata:
name: vmauth
namespace: logging
spec:
type: NodePort
selector:
app: vmauth
ports:
- protocol: TCP
port: 8427
targetPort: http
nodePort: 30087
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: vmauth
namespace: logging
labels:
app: vmauth
spec:
replicas: 1
selector:
matchLabels:
app: vmauth
template:
metadata:
labels:
app: vmauth
spec:
containers:
- name: vmauth
image: victoriametrics/vmauth:v1.118.0
args:
- -auth.config=/etc/vmauth/vmauth.yml
- -httpListenAddr=:8427
ports:
- containerPort: 8427
name: http
volumeMounts:
- name: config
mountPath: /etc/vmauth
readOnly: true
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "4000Mi"
readinessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 10
periodSeconds: 30
volumes:
- name: config
hostPath:
path: /storage/app/logging/vmauth/conf
type: Directory
# vmauth.yml 文件配置示例
# 例如:针对 指定路由目录 设置访问策略,外部写操作需要通过 vmauth 代理访问,只允许访问 VictoriaLogs 的上传接口
tee /storage/app/logging/vmauth/conf/vmauth.yml <<EOF
users:
- username: "logs"
password: "weiyigeek.top"
#url_prefix: "http://victorialogs:9428"
url_map:
- src_paths:
- "/insert/.*"
url_prefix: "http://victorialogs:9428"
EOF
部署完成后,查看服务是处于正常运行状态,然后就可以通过 NodePort 方式访问 vmauth 服务了。例如:http://10.20.172.214:30087/insert/
➤ kubectl get pod -n logging vmauth-568f77fddf-vt7x4
NAME READY STATUS RESTARTS AGE
vmauth-568f77fddf-vt7x4 1/1 Running 0 54d
➤ kubectl get svc -n logging vmauth
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
vmauth NodePort 10.96.154.74 <none> 8427:30087/TCP 54d
➤ kubectl logs -f -n logging vmauth-568f77fddf-vt7x4
2025-05-25T14:42:38.538Z info VictoriaMetrics/app/vmauth/auth_config.go:691 loaded information about 1 users from -auth.config="/etc/vmauth/vmauth.yml"
2025-05-25T14:42:38.538Z info VictoriaMetrics/app/vmauth/main.go:117 started vmauth in 0.002 seconds
2025-05-25T14:42:38.539Z info VictoriaMetrics/lib/httpserver/httpserver.go:144 started server at http://0.0.0.0:8427/
对于 VictoriaLogs 服务有那些插入 API 接口,以及测试上传用例前面的文章中已经讲解过了,所以这里不再赘述,搭建可访问【VictoriaLogs | 关键概念及日志数据模拟上传查询】文章地址:https://articles.zsxq.com/id_17n3e5xwamd9.html
温馨提示:由于 Grafana 也是部署在K8S集群中,因此可以直接使用内部服务名称(例如 victorialogs)来访问 VictoriaLogs 服务(读取存储的日志),而外部写操作,为了安全起见,则通过 vmauth 代理来完成,以限制外部客户端上传日志数据时需提供认证票据,并且只能访问到 VictoriaLogs 上传的 HTTP API 接口。
使用 vmauth
现在我们已经成功部署了 vmauth,接下来就可以通过它来代理访问 VictoriaLogs 服务了。
例如:作者现在使用 Promtail 采集器抓取 Nginx 日志,并通过 vmauth 代理上传到 VictoriaLogs 服务中进行存储,作者抽取部分配置如下:
由于作者实践环境是在 Kubernetes ,自然而然在 K8S 集群中部署 Promtail ,并在 Promtail 中使用 GeoIP2 库获取IP地址所属省份城市名称和经纬度坐标,可参考作者前面发布文章,当然也可加入作者知识星球【全栈工程师修炼指南】获取到全套配置文件。
# promtail.yaml 配置文件片段
server:
disable: true
http_listen_address: 127.0.0.1
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /etc/promtail/positions.yaml
sync_period: 30s
# 关键点:使用 vmauth 代理访问 VictoriaLogs 日志上传服务接口,并且指定了上传日志数据的字段(例如:job,app,ip,filename)
clients:
- url: "http://10.10.10.5:30087/insert/loki/api/v1/push?_stream_fields=job,app
,ip,filename"
# 配置 vmauth 认证信息,用以访问 vmauth 代理服务接口时提供认证票据信息。
basic_auth:
username: "logs"
password: "weiyigeek.top"
timeout: 15s
target_config:
sync_period: 10s
scrape_configs:
- job_name: openresty-ali-logs
static_configs:
- targets:
- localhost
labels:
job: openresty-web
__path__: /logs/ali-logs-openresty-web*/*.log
pipeline_stages:
- match:
selector: '{job="openresty-web"}'
stages:
- json:
expressions:
timestamp: timestamp
ip: ip
domain: domain
uri: uri
method: method
status: status
ua: ua
- geoip:
db: "/etc/promtail/GeoIP2/GeoLite2-City.mmdb"
source: "ip"
db_type: "city"
- template:
source: msg
template: '{{ .domain }}{{ .uri }} | {{ .method }} {{ .status }} | {{
.ip }} | {{ .ua | trunc 50 }}'
- template:
source: stream
template: '{app="openresty",filename="{{ .filename }}",domain="{{ .domain }}"}'
- labeldrop:
- geoip_continent_name
- geoip_postal_code
- geoip_timezone
- geoip_subdivision_code
- structured_metadata:
_msg: msg
_stream: stream
上传日志数据成功后,在 Victorialogs VMUI 中查询如下所示:
_time 2025-07-17T10:14:59.151728125Z
_stream_id 00000000000000000d2e91967d7f83147cf8d21448eac712
_stream {app="nginx",filename="/logs/ali-logs-gateway-nginx-2-pvc-ef01643c-8479-4de4-ba20-4fa3f4f90d5c/www-2025-07-17.log",ip="222.179.120.9"}
_msg www.weiyigeek.top/userfiles/fileupload/202504/123456789.pdf | GET 200 | 221.178.120.9| Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb
domain www.weiyigeek.top
filename /logs/ali-logs-gateway-nginx-2-pvc-ef01643c-8479-4de4-ba20-4fa3f4f90d5c/www-2025-07-17.log
geoip_continent_code AS
geoip_country_name China
geoip_location_latitude 34.7732
geoip_location_longitude 113.722
ip 221.178.120.9
job openresty-web
method GET
protocol HTTP/1.1
scheme http
time 0.000
ua Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
client 172.19.13.115
referer https://www.weiyigeek.top/
size 85497
status 200
timestamp 2025-07-17T18:14:56+08:00
uri /userfiles/fileupload/202504/123456789.pdf
最后,作者也在 Grafana 中安装配置了 VictoriaLogs 数据源,并将采集的 Nginx 中间件应用日志利用 Grafana 面板进行可视化展示,看看效果吧,如下图所示:
温馨提示:需要 Grafana 针对 Victorialogs Nginx 日志可视化面板配置,可加入作者知识星球【全栈工程师修炼指南】在网盘中获取,或者访问【可观测性监控实践】专栏在后续持续发布实践,通过 Nginx + Promtai + VictoriaLogs +Grafana 实现可视化展示全流程实践, 敬请期待。
关注我,获取更多运维安全干货! 加入:作者【全栈工程师修炼指南】知识星球
2570