Elastic Stack 索引管理、Kibana 与 Filebeat
Elastic Stack 索引管理、Kibana 与 Filebeat 实践
1. 查询示例
1.1 测试数据
假设已有商品索引 demo-products,主要字段如下:
| 字段 | 含义 | 建议类型 |
|---|---|---|
title | 商品标题 | text |
price | 商品价格 | float 或 scaled_float |
producer | 数据生产者 | keyword |
type | 商品类别 | keyword |
group | 分组编号 | byte 或 keyword |
item | 商品链接 | text 或 keyword |
1.2 查询分组 2 中价格最高的一条商品
GET http://192.0.2.11:9200/demo-products/_search
Content-Type: application/json
{
"query": {
"term": {
"group": 2
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
],
"_source": [
"title",
"price",
"producer",
"type",
"group",
"item"
],
"from": 0,
"size": 1
}
1.3 查询分组 1 中价格最低的一条商品
GET http://192.0.2.11:9200/demo-products/_search
Content-Type: application/json
{
"query": {
"term": {
"group": 1
}
},
"sort": [
{
"price": {
"order": "asc"
}
}
],
"_source": [
"title",
"price",
"producer",
"type",
"group",
"item"
],
"from": 0,
"size": 1
}
1.4 查询价格区间
GET http://192.0.2.11:9200/demo-products/_search
Content-Type: application/json
{
"query": {
"bool": {
"filter": [
{
"term": {
"group": 1
}
},
{
"range": {
"price": {
"gte": 3000,
"lte": 50000
}
}
}
]
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
要点:
from和size应使用数字,不使用字符串。match常用于全文检索,term更适合keyword或精确值查询。- 多个条件组合时,优先使用
bool.filter,不会计算相关性分数,适合筛选。
2. Kibana 部署
2.1 下载并安装
# 将版本变量替换为与 Elasticsearch 一致的版本
wget "https://artifacts.elastic.co/downloads/kibana/kibana-${ELASTIC_VERSION}-amd64.deb"
sudo dpkg -i "kibana-${ELASTIC_VERSION}-amd64.deb"
2.2 配置
配置文件:/etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
i18n.locale: "zh-CN"
server.host: "0.0.0.0"会监听所有网卡,只适合受控实验网络。生产环境应配置 TLS、身份认证、访问控制和防火墙。
2.3 启动并验证
sudo systemctl enable --now kibana.service
sudo systemctl status kibana.service
sudo journalctl -u kibana.service -f
sudo ss -ntlp | grep ':5601'
浏览器访问:
http://192.0.2.11:5601/
验收检查:
- Kibana 服务状态正常。
5601端口处于监听状态。- Kibana 能连接 Elasticsearch。
- Elasticsearch 中已有可查询的数据。
3. Elasticsearch 索引打开与关闭
3.1 关闭索引
POST http://192.0.2.11:9200/demo-products/_close
索引关闭后:
- 索引不可访问,不能正常读写。
- 数据仍然保留在磁盘上。
- 会释放与索引活动状态相关的部分资源。
3.2 打开索引
POST http://192.0.2.11:9200/demo-products/_open
Elasticsearch 新建索引默认处于打开状态。
3.3 查看索引状态
GET http://192.0.2.11:9200/_cat/indices/demo-products?v
也可以在返回结果中查看 status 字段,确认索引是 open 还是 close。
4. 索引模板
4.1 作用
索引模板用于规定匹配索引在创建时应采用的设置、映射和别名。
- 只对之后新创建的索引生效。
- 不会自动修改已经存在的索引。
- 模板名和匹配模式应便于识别,避免误匹配生产索引。
4.2 查看索引模板
GET http://192.0.2.11:9200/_index_template
4.3 创建模板
PUT http://192.0.2.11:9200/_index_template/demo-template
Content-Type: application/json
{
"index_patterns": [
"demo-template-*"
],
"template": {
"settings": {
"number_of_shards": 5,
"number_of_replicas": 0
}
}
}
4.4 查看单个模板
GET http://192.0.2.11:9200/_index_template/demo-template
4.5 创建测试索引
PUT http://192.0.2.11:9200/demo-template-001
查看分片和副本:
GET http://192.0.2.11:9200/demo-template-001/_settings
索引应使用模板中的 5 个主分片和 0 个副本。
4.6 修改模板
PUT http://192.0.2.11:9200/_index_template/demo-template
Content-Type: application/json
{
"index_patterns": [
"demo-template-*"
],
"template": {
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
}
随后新建第二个索引:
PUT http://192.0.2.11:9200/demo-template-002
预期结果:
demo-template-001仍保持原设置。demo-template-002使用修改后的 3 个主分片、2 个副本设置。
4.7 删除模板
DELETE http://192.0.2.11:9200/_index_template/demo-template
删除模板不会删除已经创建的索引。
5. 组件模板与索引模板组合
组件模板用于封装可复用的设置或映射,再由索引模板通过 composed_of 组合。
5.1 查看组件模板
GET http://192.0.2.11:9200/_component_template
5.2 创建组件模板
PUT http://192.0.2.11:9200/_component_template/demo-settings
Content-Type: application/json
{
"template": {
"settings": {
"number_of_shards": 10
}
}
}
5.3 创建组合模板
PUT http://192.0.2.11:9200/_index_template/demo-composed
Content-Type: application/json
{
"index_patterns": [
"composed-*"
],
"composed_of": [
"demo-settings"
],
"template": {
"settings": {
"number_of_replicas": 2
}
}
}
5.4 验证组合结果
PUT http://192.0.2.11:9200/composed-001
GET http://192.0.2.11:9200/composed-001/_settings
预期索引使用 10 个主分片和 2 个副本。若多个模板同时匹配索引,应明确检查优先级、版本和最终合并结果。
6. 索引别名
6.1 作用
索引别名是索引的逻辑访问名称,常用于:
- 对外隐藏真实索引名称。
- 一次查询多个索引。
- 通过原子切换别名完成索引迁移或滚动更新。
别名不是重命名索引,也不会复制数据。
6.2 准备测试索引
PUT http://192.0.2.11:9200/logs-2026-01
PUT http://192.0.2.11:9200/logs-2026-02
PUT http://192.0.2.11:9200/logs-2026-03
批量写入示例:
POST http://192.0.2.11:9200/_bulk
Content-Type: application/x-ndjson
{ "index": { "_index": "logs-2026-01" } }
{ "name": "用户 A", "tags": ["阅读", "运动"] }
{ "index": { "_index": "logs-2026-02" } }
{ "name": "用户 B", "tags": ["音乐", "摄影"] }
{ "index": { "_index": "logs-2026-03" } }
{ "name": "用户 C", "tags": ["学习", "跑步"] }
6.3 添加别名
POST http://192.0.2.11:9200/_aliases
Content-Type: application/json
{
"actions": [
{
"add": {
"index": "logs-2026-01",
"alias": "logs-read"
}
},
{
"add": {
"index": "logs-2026-02",
"alias": "logs-read"
}
},
{
"add": {
"index": "logs-2026-03",
"alias": "logs-read"
}
}
]
}
查询别名可跨索引读取:
GET http://192.0.2.11:9200/logs-read/_search
如果多个索引共享同一个可写别名,应设置唯一 is_write_index: true,避免写请求产生歧义。
6.4 查看别名
# 查看全部别名
GET http://192.0.2.11:9200/_alias
# 查看指定索引的别名
GET http://192.0.2.11:9200/logs-2026-01/_alias
# 查看指定别名关联的索引
GET http://192.0.2.11:9200/logs-read/_alias
6.5 删除别名
POST http://192.0.2.11:9200/_aliases
Content-Type: application/json
{
"actions": [
{
"remove": {
"index": "logs-2026-01",
"alias": "logs-read"
}
}
]
}
删除别名不会删除索引和文档。
6.6 原子切换别名
将 logs-current 从旧索引切换到新索引:
POST http://192.0.2.11:9200/_aliases
Content-Type: application/json
{
"actions": [
{
"remove": {
"index": "logs-2026-02",
"alias": "logs-current"
}
},
{
"add": {
"index": "logs-2026-03",
"alias": "logs-current"
}
}
]
}
同一次 _aliases 请求中的多个动作会作为整体执行,适合索引迁移和读写入口切换。
7. 数据映射
7.1 映射的作用
映射用于定义字段类型、日期格式、分词方式和检索行为。
- 明确字段类型可避免动态映射把数字、日期或 IP 识别错误。
- 字段类型一旦写入并建立索引,修改通常需要重建索引。
- 不需要全文检索的枚举、标签和 ID 通常定义为
keyword。
7.2 IP 类型
创建索引:
PUT http://192.0.2.11:9200/demo-ip
Content-Type: application/json
{
"mappings": {
"properties": {
"ip_addr": {
"type": "ip"
}
}
},
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
}
}
写入测试数据:
POST http://192.0.2.11:9200/_bulk
Content-Type: application/x-ndjson
{ "create": { "_index": "demo-ip" } }
{ "ip_addr": "192.0.2.10" }
{ "create": { "_index": "demo-ip" } }
{ "ip_addr": "192.0.2.20" }
{ "create": { "_index": "demo-ip" } }
{ "ip_addr": "198.51.100.10" }
{ "create": { "_index": "demo-ip" } }
{ "ip_addr": "203.0.113.10" }
按网段查询:
GET http://192.0.2.11:9200/demo-ip/_search
Content-Type: application/json
{
"query": {
"term": {
"ip_addr": "192.0.2.0/24"
}
}
}
删除测试索引:
DELETE http://192.0.2.11:9200/demo-ip
7.3 日期类型
创建索引并指定日期格式:
PUT http://192.0.2.11:9200/demo-date
Content-Type: application/json
{
"mappings": {
"properties": {
"birthday": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
写入测试数据:
POST http://192.0.2.11:9200/_bulk
Content-Type: application/x-ndjson
{ "create": { "_index": "demo-date" } }
{ "name": "用户 A", "birthday": "1991-10-01" }
{ "create": { "_index": "demo-date" } }
{ "name": "用户 B", "birthday": "2003-05-01" }
{ "create": { "_index": "demo-date" } }
{ "name": "用户 C", "birthday": "1996-05-01" }
按出生日期升序排列:
GET http://192.0.2.11:9200/demo-date/_search
Content-Type: application/json
{
"sort": [
{
"birthday": {
"order": "asc"
}
}
]
}
升序表示日期较早的文档排在前面,即出生更早的人排在前面。
7.4 地理坐标类型
创建 geo_point 索引:
PUT http://192.0.2.11:9200/demo-map
Content-Type: application/json
{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
写入坐标:
POST http://192.0.2.11:9200/_bulk
Content-Type: application/x-ndjson
{ "create": { "_index": "demo-map" } }
{ "location": { "lat": 39.9042, "lon": 116.4074 } }
{ "create": { "_index": "demo-map" } }
{ "location": { "lat": 31.2304, "lon": 121.4737 } }
{ "create": { "_index": "demo-map" } }
{ "location": { "lat": 30.5728, "lon": 104.0668 } }
要点:
lat表示纬度,范围为-90到90。lon表示经度,范围为-180到180。- 使用对象形式时字段含义更清楚,不建议依赖经纬度顺序记忆。
- Kibana Maps 可基于
geo_point字段创建地理可视化。
8. Filebeat 部署与架构
8.1 下载并安装
wget "https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${ELASTIC_VERSION}-amd64.deb"
sudo dpkg -i "filebeat-${ELASTIC_VERSION}-amd64.deb"
检查版本和帮助:
filebeat version
filebeat -h
8.2 核心组成
| 部分 | 作用 |
|---|---|
| Input | 指定数据来源,如文件、标准输入或 TCP |
| Processors | 添加、删除或转换字段 |
| Queue | 在输入与输出之间缓存事件 |
| Output | 指定数据发送目标 |
8.3 配置文件检查
sudo filebeat test config -c /etc/filebeat/filebeat.yml
sudo filebeat test output -c /etc/filebeat/filebeat.yml
修改配置后应至少完成语法配置和输出连通性检查。
9. Filebeat 基础管道
9.1 标准输入输出到控制台
配置:/etc/filebeat/config/01-stdin-console.yml
filebeat.inputs:
- type: stdin
output.console:
pretty: true
前台运行:
sudo filebeat -e -c /etc/filebeat/config/01-stdin-console.yml
在终端输入内容并回车,Filebeat 会将事件打印为 JSON。
9.2 标准输入写入 Elasticsearch
配置:/etc/filebeat/config/02-stdin-es.yml
filebeat.inputs:
- type: stdin
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
运行:
sudo filebeat -e -c /etc/filebeat/config/02-stdin-es.yml
输入几行测试内容后,到 Kibana 的 Discover 页面查询新数据。
9.3 TCP 输入写入 Elasticsearch
配置:/etc/filebeat/config/03-tcp-es.yml
filebeat.inputs:
- type: tcp
host: "0.0.0.0:9000"
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
运行:
sudo filebeat -e -c /etc/filebeat/config/03-tcp-es.yml
从另一台实验主机发送测试数据:
printf 'https://example.com/test\n' | nc 192.0.2.12 9000
示例 TCP 输入未启用认证和 TLS,只适用于隔离的实验网络。生产环境必须限制来源地址并配置访问控制。
10. 后台运行 Filebeat
10.1 临时后台运行
仅用于短时测试:
sudo nohup filebeat -e -c /etc/filebeat/config/03-tcp-es.yml \
> /var/log/filebeat-demo.log 2>&1 &
缺点:
- 进程退出后需要人工恢复。
- 缺少统一的重启、状态和日志管理。
- 容易因重复启动产生多个实例。
10.2 使用 systemd 托管
创建数据目录:
sudo install -d -o filebeat -g filebeat /var/lib/filebeat-tcp-demo
创建服务文件:/etc/systemd/system/filebeat-tcp-demo.service
[Unit]
Description=Filebeat TCP to Elasticsearch Demo
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=filebeat
Group=filebeat
ExecStart=/usr/bin/filebeat -e -c /etc/filebeat/config/03-tcp-es.yml --path.data /var/lib/filebeat-tcp-demo
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
启用服务:
sudo systemctl daemon-reload
sudo systemctl enable --now filebeat-tcp-demo.service
sudo systemctl status filebeat-tcp-demo.service
查看日志:
sudo journalctl -u filebeat-tcp-demo.service -f
11. Filebeat 自定义索引
配置:/etc/filebeat/config/04-tcp-custom-index.yml
filebeat.inputs:
- type: tcp
host: "0.0.0.0:9000"
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
index: "demo-app-tcp"
# 禁用 ILM,否则默认索引策略可能覆盖自定义索引名
setup.ilm.enabled: false
# 自动创建的 Filebeat 索引模板
setup.template.name: "demo-filebeat"
setup.template.pattern: "demo-app-tcp*"
setup.template.overwrite: false
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
测试配置并启动:
sudo filebeat test config -c /etc/filebeat/config/04-tcp-custom-index.yml
sudo filebeat test output -c /etc/filebeat/config/04-tcp-custom-index.yml
sudo filebeat -e -c /etc/filebeat/config/04-tcp-custom-index.yml
发送数据:
printf 'demo request from tcp\n' | nc 192.0.2.12 9000
验证:
GET http://192.0.2.11:9200/_cat/indices/demo-app-tcp*?v
GET http://192.0.2.11:9200/demo-app-tcp/_search?pretty&size=3
注意:
- 自定义静态索引不会自动按天滚动,长期使用应考虑 ILM、数据流或 rollover alias。
setup.template.overwrite: false可避免覆盖现有模板。- 修改索引模板后,旧索引不会自动获得新的设置。
12. Filebeat 采集文本日志
配置:/etc/filebeat/config/05-app-log.yml
filebeat.inputs:
- type: log
paths:
- /var/log/demo-app.log
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
index: "demo-app-log"
setup.ilm.enabled: false
setup.template.name: "demo-filebeat-log"
setup.template.pattern: "demo-app-log*"
setup.template.overwrite: false
setup.template.settings:
index.number_of_shards: 5
index.number_of_replicas: 0
权限检查:
sudo -u filebeat test -r /var/log/demo-app.log && echo "readable" || echo "permission denied"
启动:
sudo filebeat -e -c /etc/filebeat/config/05-app-log.yml
向文件中追加测试日志:
printf '%s INFO demo started\n' "$(date '+%Y-%m-%dT%H:%M:%S%z')" | sudo tee -a /var/log/demo-app.log
Filebeat 文件输入的基本行为:
- 默认按行读取文本日志。
- 会记录已采集文件的偏移量,避免重复读取。
- Debian/Ubuntu 软件包默认数据目录通常为
/var/lib/filebeat/。 - 也可通过
--path.data指定独立数据目录。 - 删除或修改 Filebeat 数据目录可能造成重复采集或漏采。
13. 采集 Ubuntu 系统日志
13.1 常见日志路径
| 日志 | 路径 |
|---|---|
| Ubuntu 系统日志 | /var/log/syslog |
| 认证日志 | /var/log/auth.log |
| 内核日志 | /var/log/kern.log |
| 软件包日志 | /var/log/dpkg.log |
| 云初始化日志 | /var/log/cloud-init.log |
| CentOS 系统日志 | /var/log/messages |
| CentOS 安全日志 | /var/log/secure |
13.2 配置示例
filebeat.inputs:
- type: log
paths:
- /var/log/syslog
- /var/log/auth.log
- /var/log/*.log
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
index: "demo-system-log"
setup.ilm.enabled: false
setup.template.name: "demo-system-filebeat"
setup.template.pattern: "demo-system-log*"
setup.template.overwrite: false
setup.template.settings:
index.number_of_shards: 5
index.number_of_replicas: 0
/var/log/*.log可能匹配大量文件。生产环境应收窄路径范围,并根据日志量设置过滤、忽略和生命周期策略。
13.3 KQL 查询示例
在 Kibana Discover 中:
message: "successfully"
常用 KQL:
| 目的 | KQL |
|---|---|
| 字段包含文本 | message: "successfully" |
| 精确匹配状态 | status: 200 |
| 查询错误状态 | status >= 400 |
| 查询指定路径 | url.path: "/pc" |
| 多条件组合 | status >= 400 and url.path: "/pc" |
| 排除条件 | not message: "healthcheck" |
14. Kibana 使用流程
14.1 创建索引模式
在 Kibana 中依次进入:
Stack Management → Index Patterns
较新版本中该功能可能称为 Data Views。
创建方法:
- 输入与数据对应的索引模式,例如
demo-app-log*。 - 选择正确的日期字段,例如
@timestamp。 - 保存后进入 Discover。
14.2 查询数据
在 Discover 页面:
- 选择索引模式。
- 设置时间范围。
- 使用 KQL 或筛选器缩小结果范围。
- 查看字段、时间分布和单条文档详情。
14.3 创建基础可视化
进入 Visualize Library:
- 新建可视化。
- 选择图表类型,例如折线图、柱状图或饼图。
- 选择索引模式。
- 配置时间桶、聚合字段和指标。
- 保存可视化。
14.4 创建 Dashboard
进入 Dashboard:
- 新建仪表盘。
- 添加已保存的可视化。
- 设置全局时间范围和筛选条件。
- 调整布局并保存。
15. 验证与排错
15.1 Elasticsearch 侧
GET http://192.0.2.11:9200/_cluster/health?pretty
GET http://192.0.2.11:9200/_cat/indices?v
GET http://192.0.2.11:9200/_cat/templates?v
GET http://192.0.2.11:9200/_alias
常见检查项:
- 索引是否已经创建。
- 文档总数是否符合预期。
- 索引映射是否符合字段类型。
- 自定义索引是否被 ILM 或默认模板覆盖。
- 模板匹配模式和优先级是否正确。
- 别名是否关联到正确索引。
15.2 Filebeat 侧
sudo filebeat test config -c /etc/filebeat/filebeat.yml
sudo filebeat test output -c /etc/filebeat/filebeat.yml
sudo journalctl -u filebeat.service -f
sudo ss -ntlp | grep ':9000'
sudo ls -l --time-style=long-iso /var/lib/filebeat/
常见问题:
| 现象 | 排查方向 |
|---|---|
| Filebeat 启动失败 | YAML 格式、配置权限、路径和端口冲突 |
| 无法连接 Elasticsearch | 网络、端口、认证、TLS 和地址配置 |
| 日志文件无数据 | 文件权限、路径、inode、偏移记录和文件轮转 |
| 数据写入错误索引 | 检查 output.elasticsearch.index、ILM 和模板 |
| 数据重复 | 数据目录被清理、多个 Filebeat 实例或输出重试 |
| TCP 测试不通 | 监听地址、防火墙、端口和 nc 客户端 |
15.3 Kibana 侧
sudo systemctl status kibana.service
sudo journalctl -u kibana.service -f
sudo ss -ntlp | grep ':5601'
curl -s http://192.0.2.11:5601/api/status
检查 Kibana 是否能连接 Elasticsearch,以及索引模式是否匹配已有字段。
16. 综合练习
- 在商品索引中查询指定分组、指定价格区间并按价格排序。
- 创建一个匹配
practice-*的索引模板。 - 修改模板副本数,再新建索引并比较模板修改前后的设置。
- 创建组件模板,并通过
composed_of组合到索引模板。 - 为三个月度索引添加同一个读别名,并验证别名可查询全部数据。
- 使用无歧义的写别名完成一次索引切换。
- 创建 IP 索引并按 CIDR 查询地址段。
- 创建日期索引并按日期排序。
- 创建地理坐标索引,并在 Kibana Maps 中展示。
- 使用 Filebeat 将文本日志实时写入 Elasticsearch。
- 在 Kibana 中创建索引模式、KQL 查询、可视化并加入 Dashboard。
17. Nginx 日志采集作业
17.1 数据生成要求
- 生成 50,000 条模拟 Nginx 访问日志。
- 时间覆盖最近 3 年。
- 客户端地址使用已获授权的测试地址,不冒用真实第三方公网 IP。
- 请求路径包含:
/pc
/iphone
/miaosha
/618
/11
/12
/cuxiao
/baiyibutie
- 客户端设备类型至少 20 种。
- 响应大小随机生成。
- 状态码包含
200、201、301、302、304、401、403、405、500、502等。 - 输出路径为
/tmp/nginx-access.log。 - 按每 1~3 秒一条的节奏写入。
- 使用 Filebeat 实时采集到 Elasticsearch,并在 Kibana 中查询。
17.2 示例日志格式
192.0.2.10 - - [01/Jan/2026:10:44:36 +0800] "GET /pc HTTP/1.1" 200 512 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0 Safari/537.36" "-"
17.3 实现思路
#!/usr/bin/env bash
set -euo pipefail
log_file=/tmp/nginx-access.log
paths=(/pc /iphone /miaosha /618 /11 /12 /cuxiao /baiyibutie)
statuses=(200 201 301 302 304 401 403 405 500 502)
for ((i = 1; i <= 50000; i++)); do
# 实际作业应让随机时间分布在最近三年
request_time=$(date '+%d/%b/%Y:%H:%M:%S %z')
path=${paths[RANDOM % ${#paths[@]}]}
status=${statuses[RANDOM % ${#statuses[@]}]}
size=$((RANDOM * RANDOM % 5000000))
printf '192.0.2.%d - - [%s] "GET %s HTTP/1.1" %d %d "-" "Mozilla/5.0 DemoClient/%d" "-"\n' \
"$((RANDOM % 250 + 2))" "$request_time" "$path" "$status" "$size" "$((RANDOM % 20 + 1))" \
>> "$log_file"
sleep "$((RANDOM % 3 + 1))"
done
注意:
- 按 1~3 秒生成 50,000 条会持续约 28 小时,适合后台运行。
- 示例中的客户端地址属于文档保留地址,只适合模拟数据。
- 若需要覆盖最近三年,应单独生成随机时间戳,而不是全部使用当前时间。
- 不同设备类型应准备至少 20 个 User-Agent 模板。
- 建议先启动 Filebeat 监听日志,再运行生成脚本。
17.4 Filebeat 配置示例
filebeat.inputs:
- type: log
paths:
- /tmp/nginx-access.log
output.elasticsearch:
hosts:
- "http://192.0.2.11:9200"
- "http://192.0.2.12:9200"
- "http://192.0.2.13:9200"
index: "demo-nginx-access"
setup.ilm.enabled: false
setup.template.name: "demo-nginx-filebeat"
setup.template.pattern: "demo-nginx-access*"
setup.template.overwrite: false
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
17.5 验收
GET http://192.0.2.11:9200/demo-nginx-access/_count?pretty
GET http://192.0.2.11:9200/demo-nginx-access/_search?pretty&size=3
在 Kibana 中可验证:
- 日志总数是否持续增长。
- 4xx、5xx 状态码的分布。
- 各请求路径的访问量。
- 不同时间段的访问趋势。
- User-Agent 或设备类型分布。
18. 扩展 数据流索引
数据流(Data Stream)面向持续追加的时间序列数据,通常由多个后备索引和写索引组成。
适合场景:
- 日志。
- 指标。
- 安全事件。
- 其他持续追加、较少更新的数据。
调研时可重点关注:
- 数据流与普通索引的区别。
- 数据流的命名约束和写索引。
- 索引模板如何配置数据流。
- 数据流如何配合 ILM 完成滚动和保留。
- 为什么数据流不适合频繁按 ID 更新文档。
19. 总结
- 索引模板只影响之后新建的索引。
- 组件模板用于复用配置,索引模板通过
composed_of组合组件模板。 - 别名是逻辑访问名称,多索引别名适合读查询,写入时应明确唯一写索引。
- 关闭索引后数据仍保留,但索引暂时不可访问。
- IP、日期和地理坐标应显式配置映射,避免动态映射偏差。
- Filebeat 的核心链路是
Input → Queue → Output。 - Filebeat 采集文件时依赖持久化数据目录记录读取偏移。
- 自定义索引名时通常需要关闭 ILM,并正确配置模板名称与匹配模式。
- Kibana 的基本流程是索引模式、Discover、Visualization 和 Dashboard。
- 排查链路应按
日志源 → Filebeat → Elasticsearch → Kibana逐段验证。