ELK框架日志收集
一、Filebeat
- 测试数据
192.168.227.1 - - [02/Aug/2023:18:20:22 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://192.168.227.20/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
- 安装
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.13.2-linux-x86_64.tar.gz
tar xzf filebeat-7.13.2-linux-x86_64.tar.gz -C /usr/local/
mv /usr/local/filebeat-7.13.2-linux-x86_64/ /usr/local/filebeat
- systemd方式的Filebeat启动管理文件
#vim /usr/lib/systemd/system/filebeat.service
[Unit]
Description=Filebeat
Documentation=https://www.elastic.co/products/beats/filebeat
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start filebeat && systemctl status filebeat
- 配置Filebeat的输入和输出
将其他output
输出地方注释掉
![]() |
---|
- 出现错误
Exiting: error loading config file: stat filebeat.yml: no such file or director
通过-c指令配置文件
/usr/local/filebeat/filebeat -c /usr/local/filebeat/filebeat.yml
专用日志搜索模块
启动ngingx模块
./filebeat moudules enable nginx
情景1:使用模块默认的日志路径
修改dodules.d/nginx.yml
文件内容如下:
- module: nginx
access:
enabled: true
error:
enabled: true
nginx 模块模块收集日志的默认路径是:
/var/log/nginx/access.log
/var/log/nginx/error.log
情景2:使用非默认路径(适合所有模块)
如果日志真实的路径和日志手机模块默认的路径不一样之,可以配置var.paths:
属性进行配置。
- moudlue: nginx
access:
enabled: true
var.paths: ["/opt/nginx/log/nginx/access.log","/opt/nginx/log/nginx/error.log"]
配置output
Filebeat适用于搜集日志的,之后把日志推送到接受的系统中,这些系统或者是装置再Filebeat中称为output
output类型
console
终端屏幕es
存放日志,并提供查询logstash
进一步对日志数据进行处理kafka
消息队列
filebeat推送日志,只能是唯一系统不能多个
输出完整JSON数据
ouput.console:
pretty: true
只输出指定字段
output.console:
codec.format:
string: '%{[@timestamp]} %{[message]}'
输出到ES
output.elasticsearch:
hosts: ['http://es01:9200','http://es02:9200']
输出道logstash
output.logstash:
hosts: ["localhost:5044"]
重读日志文件
假如出现如下报错,请删除安装目录中的data
文件夹
![]() |
---|
处理器Processors
- 减少导出字段的数量
- 谁用其他元数据增强事件
- 执行其他处理和解码
去除某行
删除所有以 DBG 开头的行
processors:
- drop_event:
when:
regexp:
message: "^DBG" # message为自定义字段
添加某些自定义字段
processors:
- add_fields:
target: project
fields:
name: myproject
id: "asdflkjasdflkjadslkf"
从事件中删除某些字段
processors:
- drop_fields:
fields: ["field1","field2",....]
ignore_missing: false
二、Logstash
1、安装及基本配置
- 安装logstash
# 下载软件包
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.13.2-linux-x86_64.tar.gz
tar xf logstash-7.13.2-linux-x86_64.tar.gz -C /usr/local
mv /usr/local/logstash-7.13.2 /usr/local/logstash
- 编写管道配置文件
input{
stdin { }
}
output{
stdout { }
}
- 检查配置文件是否正确
bin/logstash -f config/first-pipline.conf --config.test_and_exit
![]() |
---|
- 启动
bin/logstash -f config/first-pipline.conf
- 收集httpd.log文件日志
vim /usr/local/logstash/config/first-pipline.conf
input{
file {
path => ["/var/log/httpd.log"]
start_position => "beginning"
}
}
filter {
grok {
#对web日志文件进行分割
match => {"message" => "%{COMBINEDAPACHELOG}"}
}
}
output{
stdout { }
}
你会发现原来的非结构化数据,变为结构化的数据了。
细心的你一定发现原来的 message
字段仍然存在,假如你不需要它,可以使用 grok 中提供的常用选项之一remove_file
来移除这个字段。
remove_field
可以移除任意的字段,它可以接收的值是一个数组。rename可以重新命名字段
rename可以冲i性能命名字段
vim /usr/local/logstash/config/first-pipline.conf
input{
file {
path => ["/var/log/httpd.log"]
start_position => "beginning"
}
}
filter {
grok {
# 对web日志文件进行分割
match => {"message" => "%{COMBINEDAPACHELOG}"}
}
mutate {
# 重写字段
rename => {
"clientip" => "cip"
}
}
mutate {
#去掉没用字段
remove_field => ["message","input_type","@version","fields"]
}
}
output{
stdout { }
}
2、配置接受Beats的输入
- 修改logstash主配置文件
# vim /usr/local/logstash/config/first-pipline.conf
input{
beats {
port => 5044
}
}
filter {
grok {
# 对web日志文件进行分割
match => {"message" => "%{COMBINEDAPACHELOG}"}
}
}
output{
stdout { }
}
- 修改filebeat配置文件
output.logstash:
hosts:["logstash地址:5044"]
- 清空缓存
rm -rf /usr/local/filebeat/data/
- 运行filebeat
./filebeat -c /usr/local/fileat/filebeat.yml
在filebeat和logstash联动的时候启动顺序
- filebeat
- logstash
三、ES
1、安装及集群搭建
- 配置
elasticsearch.repo
# vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/yum
gpgcheck=0
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
- 安装
# 刷新yum 源
yum install elasticsearch
- 启动
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
systemctl stop elasticsearch.service
- 配置文件
# node1 节点的配置文件
cluster.name: elk
node.name: ela1 #S
node.data: true
network.host: 0.0.0.0
http.port: 9200
discovery.seed hosts: #官方指定写法如下,3台机器一样
- ela1 #节点1主机名称
- 192.168.122.106:9300 #节点2的ip加端口
- 192.168.122.218 #节点3的
ipcluster.initial master _nodes:["ela1","ela2","ela3"]
三个节点的配置文件都是相同的,唯一不同的就是
node.name
注意:三台节点要按顺序启动依次启动否则会出现脑裂的现象
- 启动
systemctl start elasticsearch
- 检查集群状态
curl -X GET "localhost:9200/_cat/health?v"
curl -X GET "localhost:9200/_cat/nodes?v"
如果出现了缺少节点的情况,此时就要:
- 删除
/var/lib/elasticsearch/nodes/*
黄色
: 如果您仅运行单个Elasticsearch实例,则集群状态将保持黄色。单节点群集具有完整的功能,但是无法数据复制到另一个节点以提供弹性绿色
:副本分片必须可用,群集状态为绿色红色
:如果群集状态为红色,则某些数据不可用
2、ELK搭建
-
修改
Filebeat
配置文件 -
修改
Logstash
配置文件
input{
beats {
port => 5044
}
}
filter {
grok {
match => {"message" => "%{COMBINEDAPACHELOG}"}
}
}
output{
stdout {
codec => rubydebug
}
elasticsearch {
hosts => ["192.168.227.20:9200", "192.168.227.21:9200"]
}
}
- 验证是否收到
curl -X GET "192.168.19.20:9200/_cat/indices"
![]() |
---|
- 修改输出内容
output{
stdout {
codec => rubydebug
}
if [log][file][path] == "/var/log/nginx/access.log"(
elasticsearch {
hosts => ["192.168.19.20:9200","192.168.19.21:9200","192.168.19.22:9200"]index =>"%([host][hostname]]-nginx-access-%[+YYYY.MM.dd)
} else if [log][file][path] == "/var/log/nginx/error.log"{
elasticsearch {
hosts => ["192.168.19.20:9200" "192.168.19,21:9200" "192.168.19.22:9200"]index => "%([host][hostname]}-nginx-error-%(+YYYY.MM.dd}"
}
}
四、Kibana
- 安装
curl -L -0 https://artifacts.elastic.co/downloads/kibana/kibana-7.13.2-linux-x86_64.tar.gz
#解压并修改名字
tar zxf kibana-7.13.2-linux-x86_64.tar.gz -C /usr/local
mv /usr/local/kibana-7.13.2-linux-x86_64.tar.gz /usr/local kibana
- 修改配置文件
server.port: 5601 #改
server.host:"..e.e" #改
#用于连接到 ES 集群的地址和端口
elasticsearch.hosts: ["http://es@1:9200"] #改
#日志文件路径 logging.dest: stdout
logging.dest: /var/log/kibana/kibana.log #改
#设置页面的字体为中文
i18n.locale:"zh-CN"#改
- 创建用于运行 kibana的普通用户
默认情况下,kibana 不允许使用 root 用户运行,所以这里创建一个普通用户
useradd ela
创建程序使用到的目录并赋予权限
mkdir /run/kibana /var/log/kibana/
授权
# chown ela.ela /run/kibana /var/log/kibana/ /usr/local/kibana -R
- 运行
使用普通用户运行
运行于前台
# su - ela
# /usr/local/kibana/bin/kibana
运行于后台
# nohup /usr/local/kibana/bin/kibana &
使用 root 用户运行
如果使用root用户运行需要使用如下命令
nohup /usr/local/kibana/bin/kibana--allow-root &
- 使用浏览器访问:http://server_ip:5601
![]() |
五、Kafka
zookeeper
- 安装
yum -y install java-1.8.0-openjdk
tar zxf kafka_2.12-2.8.0.tgz -C /usr/local
mv /usr/local/kafka_2.12-2.8.0/ /usr/local/kafka
- 域名
# vim /etc/hosts
192.168.227.20 node1
192.168.227.21 node2
- 文件配置
# 将来所有的内容注释
sed -i 's/^[^#]/#&/' /usr/local/kafka/config/zookeeper.properties
vim /usr/local/kafka/config/zookeeper.properties
dataDir=/opt/data/zookeeper/data # 需要创建,所有节点一致
dataLogDir=/opt/data/zookeeper/logs # 需要创建,所有节点一致
clientPor=2181
tickTime=2000
ininLimit=20
syncLimit=10
server.1=192.168.227.20:2888:3888
server.2=192.168.227.21:2888:3888
- 创建日志存放文件
mkdir -p /opt/data/zookeeper/{data,logs}
echo 1 > /opt/data/zookeeper/data/myid
两台结点配置都相同惟独
/opt/data/zookeeper/data/myid
不相同,按照顺序一次写入“1,2,3”
- 参数解析
dataDir ZK数据存放目录
ZK 日志存放目录。
dataLogDir 客户端连接ZK服务的端口。clientPort
tickTimeZK 服务器之间或客户端与服务器之间维持心跳的时间间隔允许
follower连接并同步到Leader的初始化连接时间,当初始化连接时间超过该值,则表示连接失败
initLimitLeader与Follower之间发送消息时如果follower在设置时间内不能与eader通信,那么此follower将会syncLimit被丢弃。
server.1=192.168.19.20:2888:3888
2888是follower与leader交换信息的端口,3888是当leader挂了时用来执行选举时服务器相互通信的端口。
kafka集群搭建
- 配置文件
config/server.properties
broker.id=1
listeners=PLAINTEXT://192.168.227.20:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=node1:2181,node2:2181
zookeeper.connection.timeout.ms=18000
group.initial.rebalance.delay.ms=0
两台配置主机配置一样,只有
borker.id
和listeners
不同
- 后台启动
nohup bin/zookeeper-server-start.sh config/zookeeper.properties &
- 查看端口
netstat -anput
- 启动kafka
nohup bin/kafka-server-start.sh config/server.properties &
- 验证
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1-partitions1--topic testtopic
在第二太主机上
bin/kafka-topics.sh --zookeeper 192.168,19.20:2181 --list