用dokcer搭建EFK(7.5.2)

用dokcer搭建EFK(7.5.2)

参考文章:手把手系列 - 使用docker容器搭建efk8 - hukey - 博客园

├── elasticsearch	 # elasticsearch:7.5.2
│ ├── data	# es数据存储目录
│ ├── logs	# es日志目录
│ ├── plugins	# 插件目录
│ └── start.sh # 容器启动脚本
├── filebeat	# filebeat:7.5.2
│ ├── filebeat.docker.yml	# 收集 docker 容器日志
│ └── start.sh	# 容器启动脚本
├── images	# 整体打包的基础镜像(elasticsearch:7.5.2 | filebeat:7.5.2 | kibana:7.5.2)
│ └── efk-images-8.2.2.tar.gz
└── kibana	# kibana:7.5.2
 └── start.sh	# 容器启动脚本

系统版本

[root@master ~]# hostnamectl
 Static hostname: node2
 Icon name: computer-vm
 Chassis: vm
 Machine ID: 1a1578f2f4274659be8e4d3d1367b032
 Boot ID: e810201df9b9455a97249868d1e22b8f
 Virtualization: kvm
 Operating System: CentOS Linux 7 (Core)	#操作系统
 CPE OS Name: cpe:/o:centos:centos:7
 Kernel: Linux 3.10.0-1160.119.1.el7.x86_64	#内核
 Architecture: x86-64

Docker版本

[root@master ~]# docker -v
Docker version 26.1.4, build 5650f9b

主机规划

服务IP地址端口
ElasticSearch192.168.9.2099200
Kibana192.168.9.2085601
Filebeat192.168.9.207

系统初始化

修改主机名

#修改主机名
hostnamectl set-hostname master && bash 
# 配置主机hosts文件,相互之间通过主机名互相访问 
vim/etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.9.207 master
#测试
ping -c 4 -s 1000 master	#指定发送四个数据包,每个数据包1000字节

备注:Linux机器配置主机名的用处:

  1. 标识主机:主机名是用来标识计算机或网络设备的名称。通过配置主机名,可以使得其他计算机或网络服务更容易地识别和定位该主机。在网络环境中,主机名是在进行网络通信时进行标识的重要信息之一。
  2. 管理网络服务:一些网络服务和应用程序可能需要知道主机的名称才能正常运行。通过配置主机名,可以确保这些服务能够正确地识别和绑定到正确的主机上。
  3. 方便管理:在管理多台服务器或设备时,通过为每台设备配置唯一的主机名可以更轻松地进行识别和管理。这在进行系统日志分析、远程管理以及配置文件管理时特别有用。
  4. 日志记录:主机名通常会出现在系统日志中,以标识生成日志的主机。通过主机名,可以更容易地跟踪和管理日志,特别是在分布式系统中。
  5. 安全性:在一些情况下,主机名也用于识别访问权限。例如,通过配置防火墙规则或访问控制列表(ACL)时,可以基于主机名来限制对特定主机的访问。

关闭selinux 和 firewalld

为什么要关闭selinux

SELinux 是 Linux 系统的一种安全机制,可以限制系统资源(如文件、网络等)的访问,提高系统的安全性。在 docker运行过程中,需要访问系统资源,但 SElinux 可能会限制访问,从而影响 docker的运行。

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

备注:修改selinux配置文件之后,重启linux机器,selinux配置才能永久生效,重启之后,登录到机器,执行如下命令:

getenforce

如果显示Disabled说明selinux已经关闭

关闭防火墙:

systemctl stop firewalld

systemctl disable firewalld

配置国内yum源

配置国内安装docker的阿里云在线源

[root@test-abc ~]#yum install yum-utils -y
[root@test-abc~]#yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@test-abc ~]# yum clean all && yum makecache

修改机器内核参数

[root@test-abc ~]#modprobe br_netfilter
[root@test-abc ~]#vim /etc/sysctl.d/docker.conf 

输入如下内容:

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1

[root@test-abc~]#sysctl -p /etc/sysctl.d/docker.confsysctl 将读取 /etc/sysctl.d/docker.conf文件中的参数,并将其应用到当前系统。

这些参数的含义是:

  1. net.bridge.bridge-nf-call-ip6tables = 1

这个参数启用了 IPv6 的iptables netfilter hook,允许 Linux 内核在网络层面上进行 IPv6 数据包的过滤和处理。在 Kubernetes 中,这个参数通常需要启用,因为 Kubernetes 网络通常是基于 iptables 实现的,而且在一些网络插件中可能会使用到 IPv6。这个参数的启用确保了 Linux 内核在处理 IPv6 数据包时会经过 iptables 过滤,从而确保网络功能的正常运行。

  1. net.bridge.bridge-nf-call-iptables = 1

这个参数启用了 IPv4 的iptables netfilter hook,类似于前一个参数,但是针对 IPv4 数据包。它允许 Linux 内核在网络层面上进行 IPv4 数据包的过滤和处理。在 Kubernetes 中,这个参数通常需要启用,因为 Kubernetes 网络通常是基于 iptables 实现的,而且在一些网络插件中可能会使用到 Ipv4。这个参数的启用确保了 Linux 内核在处理 Ipv4 数据包时会经过 iptables 过滤,从而确保网络功能的正常运行。

  1. net.ipv4.ip_forward = 1

这个参数启用了 Linux 内核的 IP 转发功能,允许 Linux 主机将收到的数据包从一个网络接口转发到另一个网络接口。在 Kubernetes 中,Pod 可能会跨越多个节点进行通信。例如,当一个 Pod 需要访问另一个 Pod 或外部服务时,网络流量可能需要通过不同的节点进行路由。启用 IP 转发功能允许 Linux 主机将收到的数据包从一个网络接口转发到另一个网络接口,从而实现跨节点通信。

校对时间

#使用chronyd服务从网络同步时间
#企业中建议配置内部的时间同步服务器
#安装chrony服务
sudo yum install chrony -y
#启动服务
sudo systemctl start chronyd
#开机自启动
sudo systemctl enable chronyd
#检查chronyd服务是否存在
systemctl list-units --type=service | grep chronyd
#验证时间
[root@master ~]# date
Wed Apr 23 17:07:32 CST 2025

Dokcer的安装

[root@node1 ~]# sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
[root@node1 ~]# sudo mkdir -p /etc/docker
#阿里云的镜像加速器经常被封,无法使用!!!
[root@node1 ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
{
 "registry-mirrors": [ "https://docker.1ms.run",
 "https://docker.xuanyuan.me"] 
}
EOF
[root@node1 ~]# sudo systemctl daemon-reload
[root@node1 ~]# sudo systemctl restart docker
[root@node1 ~]# systemctl enable docker
 
#docker -v验证docker是否安装成功

ElasticSearch的安装配置

参考文章:https://www.amjun.com/1551.html

注意:这里指定容器名称需要指定为 elasticsearch,因为 kibana 容器的配置文件 kibana.yml 中默认 es 的地址为 elasticsearch。否则,需要修改 kibana 的配置文件。

docker pull elasticsearch:7.5.2
docker run -d \
--name elasticsearch \
-p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
-v /home/docker/es/plugins:/usr/share/elasticsearch/plugins \
-v /home/docker/es/data:/usr/share/elasticsearch/data \
elasticsearch:7.5.2

如果提示报错:

ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes

这是因为 es 没有权限,执行如下代码即可。

chmod 777 /home/docker/es/data && docker restart elasticsearch

配置

#进入容器
[root@master ~]# docker exec -it elasticsearch /bin/bash
[root@f33dde804d9f elasticsearch]# ls
LICENSE.txt NOTICE.txt README.asciidoc bin config data jdk lib logs modules plugins
#修改配置文件
[root@f33dde804d9f config]# cd config
[root@f33dde804d9f config]# vi elasticsearch.yml 
network.host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"

安装完成后访问 192.168.9.209:9200

ElasticSearch的基本信息

版本IP地址端口
7.5.2192.168.9.2099200

Kibana安装配置

参考文章:Kibana 基础入门 | 月光中的污点

安装 kibana

docker pull kibana:7.5.2 #切记!!!!!!必须和elasticsearch版本一样
docker run -d \
--name kibana \
-p 5601:5601 \
-e "ELASTICSEARCH_HOSTS=http://192.168.9.209:9200" \
 kibana:7.5.2
#进入容器
[root@techtest9-230 ~]# docker exec -it kibana /bin/bash
bash-4.4$ ls
LICENSE.txt NOTICE.txt README.txt bin config data	node node_modules package.json plugins src	x-pack
bash-4.4$ cd config/
bash-4.4$ ls
kibana.yml node.options
bash-4.4$ vi kibana.yml 
#修改配置文件
server.host: "0.0.0.0"
server.port: 5601
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
kibana.index: ".kibana"
i18n.locale: "zh-CN"

安装后访问 192.168.9.208:5601

如果报错,Kibana server is not ready yet

参考文章:https:blog.csdn.net/weixin_45956631/article/details/130636880?fromshare=blogdetail&sharetype=blogdetail&sharerId=130636880&sharerefer=PC&sharesource=2301_76820728&sharefrom=from_link
!

Kibana的基本信息

VersionIP addressPort
7.5.2192.168.9.2085601

Filebeat基础入门

参考文章:Docker安装部署ELK教程 (Elasticsearch+Kibana+Logstash+Filebeat) - 万能付博 - 博客园

docker安装filebeat - 肖祥 - 博客园

docker安装elk7.6.0版本,配合filebeat收集日志 - 謙謙 - 博客园

Beats的介绍

Beat就是采集数据,并上报到Logstash或Elasticsearch

Beats对于收集数据非常有用。它们位于你的服务器上,将数据集中在Elasticsearch中,Beats也可以发送到Logstash来进行转换和解析。

Beats可以直接(或者通过Logstash)将数据发送到Elasticsearch,在那里你可以进一步处理和增强数据,然后在Kibana中将其可视化。

通过logstash将数据发送到Elasticsearch,以及集群的目的都是为了达到高可用性。

Filebeat的安装配置

参考文章:Filebeat 6.2.3 rpm方式安装启动 - 2240930501 - 博客园

Docker搭建EFK日志收集系统,并自定义es索引名 - 等风来~~ - 博客园

#下载rpm二进制包
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.5.2-x86_64.rpm
#安装
sudo rpm -vi filebeat-7.5.2-x86_64.rpm
#启动filebeat
systemctl start filebeat
#查看filebeat的状态
systemctl status filebeat
#设置开机自启动
systemctl enable filebeat.service
#查看filebeat的服务
[root@master ~]# rpm -ql filebeat | more
/etc/filebeat/fields.yml
/etc/filebeat/filebeat.reference.yml
/etc/filebeat/filebeat.yml
/etc/filebeat/modules.d/apache.yml.disabled
/etc/filebeat/modules.d/auditd.yml.disabled
/etc/filebeat/modules.d/aws.yml.disabled
/etc/filebeat/modules.d/azure.yml.disabled
/etc/filebeat/modules.d/cef.yml.disabled
/etc/filebeat/modules.d/cisco.yml.disabled
/etc/filebeat/modules.d/coredns.yml.disabled
/etc/filebeat/modules.d/elasticsearch.yml.disabled
/etc/filebeat/modules.d/envoyproxy.yml.disabled
/etc/filebeat/modules.d/googlecloud.yml.disabled
/etc/filebeat/modules.d/haproxy.yml.disabled
/etc/filebeat/modules.d/ibmmq.yml.disabled
/etc/filebeat/modules.d/icinga.yml.disabled
/etc/filebeat/modules.d/iis.yml.disabled
/etc/filebeat/modules.d/iptables.yml.disabled
/etc/filebeat/modules.d/kafka.yml.disabled
/etc/filebeat/modules.d/kibana.yml.disabled
/etc/filebeat/modules.d/logstash.yml.disabled
/etc/filebeat/modules.d/misp.yml.disabled
/etc/filebeat/modules.d/mongodb.yml.disabled
/etc/filebeat/modules.d/mssql.yml.disabled
/etc/filebeat/modules.d/mysql.yml.disabled
/etc/filebeat/modules.d/nats.yml.disabled
/etc/filebeat/modules.d/netflow.yml.disabled
/etc/filebeat/modules.d/nginx.yml.disabled
/etc/filebeat/modules.d/osquery.yml.disabled
/etc/filebeat/modules.d/panw.yml.disabled
/etc/filebeat/modules.d/postgresql.yml.disabled
--More--

修改配置文件

#默认索引名字为“filebeat-*”,所有的日志都收集在这个索引下,所以我们需要自定义,实现索引开头一致,中间是各自的程序名,看名字就知道是哪个程序日志。
[root@node1 filebeat-7.5.2]# vim filebeat.yml     
#=========================== Filebeat inputs =============================
filebeat.inputs:
#定义日志收集路径
- type: log
 enabled: true
 paths:
	 - /opt/tomcat/logs/catalina*.log # 收集 catalina 日志
 - /opt/tomcat/logs/localhost*.log # 收集 localhost 日志
 - /opt/tomcat/logs/manager*.log # 收集 manager 日志
 - /opt/tomcat/logs/host-manager*.log # 收集 host-manager 日志
 - /opt/tomcat/logs/localhost_access_log*.txt # 收集访问日志
 # 可选:设置日志文件的编码(如 UTF-8)
 encoding: utf-8
 # 可选:排除某些文件(如当前正在写入的 catalina.out)
 exclude_files: ['.gz$'] # 排除压缩的日志文件
 fields:	
 source: xqkhuahua
 filetype: tomcat #自定义字段,用来区分多个类型日志
#fields用于向收集的日志数据中添加自定义的字段(键值对)。这些字段会被注入到每条日志事件中,通常用于标记日志的来源、环境或其他元信息,便于后续的日志处理和分析(例如在 Elasticsearch 或 Logstash 中进行分类或过滤)。
#============================= Filebeat modules ===============================
filebeat.config.modules:
 path: ${path.config}/modules.d/*.yml
 reload.enabled: true
#==================== Elasticsearch template setting ========================== 
setup.template.settings:
 index.number_of_shards: 1
 
#============================== Kibana ========================================
setup.kibana:
 host: "192.168.9.208:5601"
 
#定义模板的相关信息 
setup.template.name: "tomcat_log_template"
setup.template.pattern: "tomcat-logs-*"
setup.template.overwrite: true
setup.template.enabled: true
#自定义ES的索引需要把ilm设置为false
setup.ilm.enabled: false
 
#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
 hosts: ["192.168.9.209:9200"]
 index: "tomcat-logs-%{+yyyy.MM.dd}"
 
 #================================ Logging =====================================
processors:
 - add_host_metadata: ~
 - add_cloud_metadata: ~
 
 #================================ Logging =====================================
logging.level: info # 日志级别(可选:debug/info/warning/error)
logging.to_files: true # 是否将日志写入文件
logging.files:
 path: /var/log/filebeat # 日志文件路径
 name: filebeat # 日志文件名前缀
 keepfiles: 7 # 保留的日志文件数量

Filebeat的基本信息

版本IP地址端口
7.5.2192.168.9.207

配置可视化界面

访问192.168.9.208:5601






可以根据字段搜索

更多功能还在探索中.....

作者:想去看花花吗原文地址:https://www.cnblogs.com/xqkhuahua/p/18850190

%s 个评论

要回复文章请先登录注册