VictoriaLogs Grafana Filebeat - 构建轻量高性能日志管理平台

本文最后更新于 2025年2月20日 下午

VFG 技术架构

Filebeat 接收Syslog ,并进行日志分段,VictoriaLogs  持久化存储日志 ,Grafana 可视化、数据查询、告警、数据导出。

为什么要用VictoriaLogs ?

  • 与Elasticsearch /Grafana Loki相比几十倍的CPU/内存/存储资源占用的差距,能极大的节省硬件资源。

  • VGF可以实现ELK的Web查询、日志压缩存储、syslog 日志接收。

简介

  • VictoriaLogs 兼容支持多种数据输入软件,Filebeat 也支持多种数据输入。

  • VictoriaLogs 的Web UI很简陋,所以要用Grafana。

  • VictoriaLogs 是HTTP访问是无认证的,需要套其他软件来实现。(默认端口9428)

  • VictoriaLogs 的数据过期时间是全局的,所以如果有需求,只能部署多个实例。

  • Filebeat 相对Logstash 性能更好,也比VictoriaLogs 自带的Syslog输入功能更多。

Docker 国内安装

https://mirror.nju.edu.cn/mirrorz-help/docker-ce/?mirror=NJU

Docker Hub国内加速

1
2
3
4
5
6
7
8
9
10
11
12
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

部署VictoriaLogs

创建 victoria-logs-data 文件夹

1
2
3
4
5
6
7
8
9
10
11
12
docker run -d --restart always \
-p 9428:9428 \
-v ./victoria-logs-data:/victoria-logs-data \
--name victoria-logs-syslog-songxwn.com \
docker.io/victoriametrics/victoria-logs:latest \
--retentionPeriod=365d





# by songxwn.com

  • 9428/tcp 对外HTTP访问

  • 数据保留365天

  • ./victoria-logs-data 为存储目录

部署Grafana

1
2
3
4
5
docker run -d --name=grafana -p 3000:3000 \
-e GF_INSTALL_PLUGINS=victoriametrics-logs-datasource \
--name Grafana \
grafana/grafana-enterprise

Grafana 接入VictoriaLogs 数据源

插件已默认安装,添加对应数据源即可,目标为 http://192.168.1.1:9428

Filebeat 部署 - 接收Syslog,输出到VL

vim filebeat.docker.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
filebeat.inputs:
- type: udp
enabled: true
max_message_size: 10KiB
host: "0.0.0.0:514"
fields:
type: udp

output.elasticsearch:
hosts: ["http://127.0.0.1:9428/insert/elasticsearch/"]
parameters:
_msg_field: "message"
_time_field: "@timestamp"
_stream_fields: "host.hostname"
allow_older_versions: true

PS:注意 127.0.0.1 替换为宿主机IP.

运行

1
2
3
4
5
6
7
docker run -d  \
--name Filebeat \
--user=root \
--volume="$(pwd)/filebeat.docker.yml:/usr/share/filebeat/filebeat.yml:ro" \
-p 514:514/udp \
docker.elastic.co/beats/filebeat:8.17.2

Docker compose 一把梭

创建并进入 /opt/VFG文件夹

1
2
3
mkdir /opt/VFG

cd /opt/VFG

filebeat配置文件 - vim filebeat.docker.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
http:
enabled: true
host: "http://0.0.0.0:5066"


filebeat.inputs:
- type: udp
enabled: true
max_message_size: 10KiB
host: "0.0.0.0:514"
fields:
type: udp

output.elasticsearch:
hosts: ["http://syslog-victoria-logs:9428/insert/elasticsearch/"]
parameters:
_msg_field: "message"
_time_field: "@timestamp"
_stream_fields: "host.hostname"
allow_older_versions: true

PS:如果需要字段分割,可以参考如下。增加到输入输出中间即可。

1
2
3
4
5
processors:
- dissect:
tokenizer: "<%{syslog_pri}>%{timestamp} %{hostname} %%{log_level}/%{log_code}/%{log_action}(l):IPVer=%{ipver},Protocol=%{protocol},SourceIP=%{source_ip},DestinationIP=%{destination_ip},SourcePort=%{source_port},DestinationPort=%{destination_port},SourceNatIP=%{source_nat_ip},SourceNatPort=%{source_nat_port},BeginTime=%{begin_time},EndTime=%{end_time},S>
field: "message"
target_prefix: "parsed"

victoria-logs 数据目录

1
2
3
mkdir victoria-logs-data

chown -R 472:472 victoria-logs-data

Grafana 数据目录和配置文件

1
2
3
mkdir grafana-data

chown -R 472:472 grafana-data

数据源配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mkdir -p ./provisioning/datasources

vim ./provisioning/datasources/vm.yml

apiVersion: 1
datasources:
# <string, required> Name of the VictoriaLogs datasource
# displayed in Grafana panels and queries.
- name: VictoriaLogs-songxwn.com
# <string, required> Sets the data source type.
type: victoriametrics-logs-datasource
# <string, required> Sets the access mode, either
# proxy or direct (Server or Browser in the UI).
access: proxy
# <string> Sets URL for sending queries to VictoriaLogs server.
# see https://docs.victoriametrics.com/victorialogs/querying/
url: http://syslog-victoria-logs:9428
# <string> Sets the pre-selected datasource for new panels.
# You can set only one default data source per organization.
isDefault: true

vim docker-compose.yml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
services:
victoria-logs:
image: victoriametrics/victoria-logs:latest
container_name: syslog-victoria-logs
volumes:
- ./victoria-logs-data:/victoria-logs-data
restart: always
command: [
"--retentionPeriod=365d"
]
environment:
- TZ=Asia/Shanghai
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1:9428/health"]
interval: 1m30s
timeout: 10s
retries: 3

grafana:
image: grafana/grafana-enterprise
container_name: syslog-grafana
ports:
- "3000:3000"
environment:
- GF_INSTALL_PLUGINS=victoriametrics-logs-datasource
- GF_SECURITY_ADMIN_PASSWORD=Songxwn.com
- TZ=Asia/Shanghai
volumes:
- ./grafana-data:/var/lib/grafana
- ./provisioning:/etc/grafana/provisioning
restart: always
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
interval: 1m30s
timeout: 10s
retries: 3

filebeat:
image: docker.elastic.co/beats/filebeat:8.17.2
container_name: syslog-filebeat
ports:
- "514:514/udp"
volumes:
- ./filebeat.docker.yml:/usr/share/filebeat/filebeat.yml:ro
restart: always
environment:
- TZ=Asia/Shanghai
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5066"]
interval: 60s
timeout: 10s
retries: 3

compose 启动命令 (需要Docker Engine 19.03.0以上版本)

1
2
3

docker compose up -d

停止、升级

1
2
3
4
5
6
7

docker compose down

dcker compose pull

docker compose up -d

compose文件说明

1. Victoria-Logs 服务

  • 服务名称: victoria-logs
  • 镜像: 使用 victoriametrics/victoria-logs:latest 镜像。
  • 容器名称: syslog-victoria-logs
  • 数据卷: 挂载本地目录 ./victoria-logs-data 到容器内的 /victoria-logs-data,用于持久化存储。
  • 重启策略: 设置为 always,确保容器在运行失败后自动重启。
  • 启动命令: 指定命令参数 --retentionPeriod=365d,表示日志保留期为365天。
  • 环境变量: 设置时区为 Asia/Shanghai
  • 健康检查:
    • 执行 wget -q --spider http://127.0.0.1:9428/health 检查服务的健康状态。
    • 1分30秒 执行一次检查。
    • 如果检测不到健康状态,等待 10秒 后超时。
    • 重试 3次 失败后标记为非健康。

2. Grafana 服务

  • 镜像: 使用 grafana/grafana-enterprise 镜像。
  • 容器名称: syslog-grafana
  • 端口映射: 将主机的 3000 端口映射到容器的 3000 端口,供外部访问 Grafana。
  • 环境变量:
    • 安装插件 victoriametrics-logs-datasource
    • 设置初始管理员密码为 Songxwn.com
    • 设置时区为 Asia/Shanghai
  • 数据卷:
    • 挂载本地目录 ./grafana-data 到容器中的 /var/lib/grafana,用于持久化 Grafana 的数据。
    • 挂载本地目录 ./provisioning/etc/grafana/provisioning,用于配置预置的数据源和仪表盘。
  • 重启策略: 设置为 always
  • 健康检查:
    • 执行 curl -f http://localhost:3000/api/health 检查服务的健康状态。
    • 1分30秒 执行一次检查。
    • 如果检测不到健康状态,等待 10秒 后超时。
    • 重试 3次 失败后标记为非健康。

3. Filebeat 服务

  • 镜像: 使用 docker.elastic.co/beats/filebeat:8.17.2 镜像。
  • 容器名称: syslog-filebeat
  • 端口映射: 将主机的 514/udp 端口映射到容器的 514/udp 端口,用于接收 Syslog。
  • 数据卷: 将本地的 filebeat.docker.yml 配置文件挂载到容器内的 /usr/share/filebeat/filebeat.yml,且为只读。
  • 重启策略: 设置为 always
  • 环境变量: 设置时区为 Asia/Shanghai
  • 健康检查:
    • 执行 curl -f http://localhost:5066 检查服务的健康状态。
    • 60秒 执行一次检查。
    • 如果检测不到健康状态,等待 10秒 后超时。
    • 重试 3次 失败后标记为非健康。

这些配置用来部署和管理 Victoria-Logs、Grafana 和 Filebeat 服务,确保它们处于良好运行状态,且在故障发生时可以自动恢复。各自的健康检查提供了针对服务运行状态的简易确认方式,进一步提高系统的稳定性和可靠性。

VFG Compose 部署使用

  • 发送日志到udp/514 端口,即可存储到数据库。 echo "songxwn.com\!" | nc -4u 202.182.109.11 514 命令用来测试发送

  • 访问到Grafana Web 3000端口,使用admin/Songxwn.com 登录。

  • 可以通过已经预先创建的数据源VictoriaLogs-songxwn.com,通过Explore 查询日志(LogsQL语法,* 匹配所有 )和导出为CSV文件。

  • Grafana 也可以进行日志告警和构建可视化仪表盘。

Grafana 示例

资源占用 - 有较高日志输入的情况下

1
2
3
4
5
CONTAINER ID   NAME                   CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O        PIDS
1c7fe73fc896 syslog-filebeat 0.52% 63.14MiB / 3.822GiB 1.61% 14.1MB / 5.38MB 0B / 8.19kB 15
4a6ee8b19475 syslog-grafana 0.61% 147.3MiB / 3.822GiB 3.76% 9.85MB / 12.8MB 299kB / 305MB 33
a80d91af294c syslog-victoria-logs 0.04% 195.7MiB / 3.822GiB 5.00% 5.4MB / 604kB 520kB / 6.86MB 14

通过Git下载Compose文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

git clone https://github.com/Songxwn/VGF-Docker-compose.git

cd VGF-Docker-compose

chown -R 472:472 grafana-data

chown -R 472:472 victoria-logs-data

chown -R 472:472 provisioning

chown 472:472 filebeat.docker.yml


docker compose up -d

docker ps

# 查看状态,两分钟之后应该都是healthy

# 群晖需要进入SSH下配置

运维技术交流群

发送邮件到 ➡️ [email protected]

或者关注WX公众号:网工格物

微信扫码

博客(最先更新)

https://songxwn.com/


VictoriaLogs Grafana Filebeat - 构建轻量高性能日志管理平台
https://songxwn.com/Filebeat-Grafana-VictoriaLogs_Syslog/
作者
Song
发布于
2025年2月13日
更新于
2025年2月20日
许可协议