news 2026/7/18 10:23:31

filebeat+elasticsearch+kibana日志分析

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
filebeat+elasticsearch+kibana日志分析

1 默认配置

1.1 filebeat

filebeat-7.17.yml,从网关中下载k8s的配置,指定es和kibana的配置

通过kibana查询可以查询到日志了,但此时还不知道具体怎么用。

1.2 kibana

在Discover中创建索引格式:filebeat-*,得到如下图,可以看出acc-statement-server的日志最多。但里面的字段太多了,下面应该怎么看呢?

再跟进查看日志,filebeat应该是每一样记录一次,这个浪费了很多存储空间。另外排查问题也并不好查。

2 多行合并输出

如果使用默认的配置,每一行日志,就会产生一条记录。

2.1 filebeat

增加多行规则匹配

设置索引,符合条件走自己的索引,否则则为默认索引

output.elasticsearch: hosts: ['10.101.10.2:9200','10.101.10.3:9200','10.101.10.4:9200'] username: ${ELASTICSEARCH_USERNAME} password: ${ELASTICSEARCH_PASSWORD} indices: - index: acc-accountbook-server-%{+yyyy.MM.dd} when.contains: kubernetes.container.name: acc-accountbook-server - index: acc-analysis-server-%{+yyyy.MM.dd} when.contains: kubernetes.container.name: acc-analysis-server index: filebeat-7.17.25-%{+yyyy.MM.dd}

在kibana中跟进日志,发现少部分日志输出成功,大多失败,这是什么原因呢?

为什么有些可以添加数据,有些不能呢

调试发现,我在索引前面加上了eayc就可以了,看来问题就出现在索引策略

2.2 logback

上面的时间分割,是需要logback配置与之对应。如我的系统日志打印出来的是这个,那么filebeat中就无法实现多行合并了。

如下图修改logback配置。

2.3 pipeline

在elasticsearch中创建pipeline,参考了【ELK】到【EFK】,【Filebeat】收集【SpringBoot】日志,但最终还是放弃了,还是按照k8s自带的格式,这样便于处理,不需要非得自定义格式。

PUT _ingest/pipeline/eayc_log_pipeline { "description": "岁月云日志管道", "processors": [ { "remove": { "field": "agent.name", "ignore_missing": true } }, { "remove": { "field": "agent.ephemeral_id", "ignore_missing": true } }, { "remove": { "field": "log.file.path", "ignore_missing": true } }, { "remove": { "field": "input.type", "ignore_missing": true } }, { "remove": { "field": "kubernetes.node.labels.kubernetes_io/hostname", "ignore_missing": true } }, { "remove": { "field": "kubernetes.labels.k8s_kuboard_cn/layer", "ignore_missing": true } }, { "remove": { "field": "kubernetes.deployment.name", "ignore_missing": true } }, { "remove": { "field": "container.runtime", "ignore_missing": true } }, { "remove": { "field": "ecs.version", "ignore_missing": true } }, { "remove": { "field": "host.architecture", "ignore_missing": true } }, { "remove": { "field": "host.containerized", "ignore_missing": true } }, { "remove": { "field": "host.mac", "ignore_missing": true } }, { "remove": { "field": "host.os.codename", "ignore_missing": true } }, { "remove": { "field": "host.os.name", "ignore_missing": true } }, { "remove": { "field": "host.os.platform", "ignore_missing": true } }, { "remove": { "field": "kubernetes.labels.k8s_kuboard_cn/name", "ignore_missing": true } }, { "remove": { "field": "kubernetes.labels.pod-template-hash", "ignore_missing": true } }, { "remove": { "field": "kubernetes.namespace_uid", "ignore_missing": true } }, { "remove": { "field": "kubernetes.node.labels.beta_kubernetes_io/arch", "ignore_missing": true } }, { "remove": { "field": "kubernetes.node.labels.beta_kubernetes_io/os", "ignore_missing": true } }, { "remove": { "field": "log.flags", "ignore_missing": true } }, { "remove": { "field": "log.offset", "ignore_missing": true } }, { "remove": { "field": "kubernetes.container.id", "ignore_missing": true } }, { "remove": { "field": "kubernetes.pod.uid", "ignore_missing": true } } ] }

创建索引策略

PUT _ilm/policy/eayc_log_policy { "policy": { "phases": { "hot": { "min_age": "0ms", "actions": { "rollover": { "max_size": "50gb", "max_age": "30d" } } }, "delete": { "min_age": "90d", "actions": { "delete": {} } } } } }

2.4 elasticsearch

在k8s中启动filebeat中,查看filebeat的日志发现

2024-10-29T07:55:33.935Z ERROR [elasticsearch] elasticsearch/client.go:226 failed to perform any bulk index operations: 500 Internal Server Error: {"error":{"root_cause":[{"type":"illegal_state_exception","reason":"There are no ingest nodes in this cluster, unable to forward request to an ingest node."}],"type":"illegal_state_exception","reason":"There are no ingest nodes in this cluster, unable to forward request to an ingest node."},"status":500}

则需要在elasticsearch.yml中增加配置

node.roles: [ingest]

创建组合模板

PUT _component_template/filebeat_settings { "template": { "settings": { "number_of_shards": 1, "number_of_replicas": 1 } } } PUT _component_template/filebeat_mappings { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" }, "message": { "type": "text" } } } } } PUT _component_template/eayc_mappings { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" }, "message": { "type": "text" }, "custom_field": { "type": "keyword" } } } } } PUT _component_template/acc_mappings { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" }, "message": { "type": "text" }, "custom_field": { "type": "keyword" } } } } } PUT _index_template/filebeat { "index_patterns": ["filebeat-*"], "composed_of": ["filebeat_settings", "filebeat_mappings"], "priority": 100, "template": { "settings": { "index.lifecycle.name": "eayc_log_policy", "index.lifecycle.rollover_alias": "filebeat-write" } } } PUT _index_template/eayc { "index_patterns": ["eayc-*"], "composed_of": ["filebeat_settings", "eayc_mappings"], "priority": 100, "template": { "settings": { "index.lifecycle.name": "eayc_log_policy", "index.lifecycle.rollover_alias": "filebeat-write" } } } PUT _index_template/acc { "index_patterns": ["acc-*"], "composed_of": ["filebeat_settings", "acc_mappings"], "priority": 100, "template": { "settings": { "index.lifecycle.name": "eayc_log_policy", "index.lifecycle.rollover_alias": "filebeat-write" } } }

接着再看acc添加进去了

再看日志数据出来了

3 环境相关

3.1 cgroup

绝对性的控制CPU/内存在一个范围内,使用cgroup来实现。

下面是centos7.9

[root@master1 ~]# cat /proc/mounts | grep cgroup tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0 cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0 cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0 cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0 cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0 cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpuacct,cpu 0 0 cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0 cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0 cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_prio,net_cls 0 0 cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0 cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0 cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0

下面是rockylinux9.5,使用的是较新的内核,默认启用了 cgroup v2,可能是filebeat7.17不能兼容。下载安装8.17就没有报这个错误了。

[root@master8 ~]# cat /proc/mounts | grep cgroup cgroup2 /sys/fs/cgroup cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot 0 0 none /run/calico/cgroup cgroup2 rw,relatime,nsdelegate,memory_recursiveprot 0 0

3.2 挂载目录

将版本从8.17回退到8.10,针对docker的目录,如果docker的目录发生改变,则下面两个地方更改

在Kubernetes中,Filebeat 通常从宿主机上的/var/log/containers目录获取日志

3.3 日志没有写入elastic

这个仍然是filebeat与elastic版本造成,增加allow_older_versions: true配置即可

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/18 10:22:55

poissonsearch-py错误处理:Elasticsearch异常捕获与调试技巧

poissonsearch-py错误处理:Elasticsearch异常捕获与调试技巧 【免费下载链接】poissonsearch-py Official Python client for Elasticsearch.The original name is elasticsearch-py, and the name is changed to poissonsearch-py for self-maintenance. 项目地址…

作者头像 李华
网站建设 2026/7/18 10:22:33

Qt多线程定时器:解决GUI阻塞,实现后台任务与界面流畅响应

1. 项目概述:为什么我们需要“会飞”的子线程定时器?在C和Qt的GUI应用开发中,定时器(QTimer)是我们再熟悉不过的老朋友了。无论是界面元素的周期性刷新、后台任务的轮询检查,还是简单的动画效果&#xff0c…

作者头像 李华
网站建设 2026/7/18 10:22:21

昇腾C掩码操作API使用指南

如何使用掩码操作API 【免费下载链接】asc-devkit 本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。 项目地址: https://gi…

作者头像 李华
网站建设 2026/7/18 10:21:42

Local RAG完全指南:如何在本地构建私有化AI知识库

Local RAG完全指南:如何在本地构建私有化AI知识库 【免费下载链接】local-rag Ingest files for retrieval augmented generation (RAG) with open-source Large Language Models (LLMs), all without 3rd parties or sensitive data leaving your network. 项目地…

作者头像 李华
网站建设 2026/7/18 10:21:34

CardKit主题与布局系统:创建可定制化的图像设计模板

CardKit主题与布局系统:创建可定制化的图像设计模板 【免费下载链接】cardkit A simple, powerful and fully configurable image editor for web browsers and servers. Optional UI included. 项目地址: https://gitcode.com/gh_mirrors/ca/cardkit CardKi…

作者头像 李华
网站建设 2026/7/18 10:21:01

5分钟掌握跨平台输入法词库转换:深蓝词库转换完全指南

5分钟掌握跨平台输入法词库转换:深蓝词库转换完全指南 【免费下载链接】imewlconverter ”深蓝词库转换“ 一款开源免费的输入法词库转换程序 项目地址: https://gitcode.com/gh_mirrors/im/imewlconverter 你是否曾在更换电脑或操作系统时,发现自…

作者头像 李华