ELK+filebeat采集java日志

本文档记录了使用ELK(Elasticsearch, Logstash, Kibana)堆栈配合filebeat收集Java应用日志的全过程。在三台服务器上部署Elasticsearch集群,一台服务器上安装Logstash、Kibana和Head插件,两台生产环境服务器安装filebeat。详细步骤包括设置hosts、安装JDK、Elasticsearch集群配置、Logstash与Kibana安装、filebeat配置以及采集结果的验证。" 115109933,10768685,排序数组中的查找:顺序法与二分法,"['算法', '二分法']

前言

此文章是我在生产环境下搭建ELK日志系统的记录,该日志系统主要是采集Java日志,开发人员能通过kibanaWeb页面查找相关主机的指定日志;对于Java日志,filebeat已做多行合并、过滤行处理,更精准的获取需要的日志信息,关于ELK系统的介绍,这里不再赘述。

更多信息可以访问官方网站查询:
Elasticsearch:
https://www.elastic.co/cn/products/elasticsearch
Logstash:
https://www.elastic.co/cn/products/logstash
Kibana:
https://www.elastic.co/cn/products/kibana
Filebeat:
https://www.elastic.co/cn/products/beats/filebeat

环境

主机服务类型主机名内存服务
es1test-machine32Gelasticsearch、logstash、kibana
es2926prod8Gelasticsearch
es3replmysql4Gelasticsearch
web1template-centos716Gfilebeat
web2template-centos716Gfilebeat

ELK使用的版本均为6.4.3,在下面的搭建中提供了下载地址。三台elasticsearch搭建集群处理,可靠传输。logstash、kibana搭建在同一台服务器中,资源足够不影响服务。两台web服务器为生产环境下的主机,安装filebeat软件来采集日志数据,传输至logstash服务器。

步骤

1、设置hosts

[root@test-machine /root]#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.146 testmachine
192.168.1.60 926prod
192.168.1.62 replmysql
[root@test-machine /root]#scp /etc/hosts 926prod:/etc/hosts
[root@test-machine /root]#scp /etc/hosts replmysql:/etc/hosts

2、安装javaJDK

jdk版本需要时1.8及以上

[root@test-machine /root]#yum -y install java
[root@test-machine /root]#java -version
openjdk version "1.8.0_191"

[root@926prod /root]#yum -y install java
[root@926prod /root]#java -version
openjdk version "1.8.0_191"

[root@replmysql /root]#yum -y install java
[root@replmysql /root]#java -version
openjdk version "1.8.0_191"

3、安装elasticsearch(三台机器相同操作)

下载——安装——启动服务

[root@test-machine ~]#curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.3.tar.gz
[root@test-machine ~]#tar -xvf elasticsearch-6.4.3.tar.gz
[root@test-machine ~]#mv elasticsearch-6.4.3 /usr/local/
[root@test-machine ~]#useradd elasticsearch
[root@test-machine ~]#cd /usr/local/elasticsearch-6.4.3/
[root@test-machine bin]#chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-6.4.3/

修改配置文件:

[root@test-machine /root]#grep -v "^#" /etc/elasticsearch/elasticsearch.yml
cluster.name: sunaw-cluster
node.name: test-machine
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.1.146
discovery.zen.ping.unicast.hosts: ["testmachine", "replmysql", "926prod"]
http.enabled: true
http.cors.enabled: true
http.cors.allow-origin: "*"
# 注意node.name、network.host字段,三台机填入各自的名字和ip地址

启动服务:

[root@test-machine bin]#su elasticsearch
[elasticsearch@test-machine bin]$nohup ./elasticsearch &
[elasticsearch@test-machine bin]$exit
[root@test-machine ~]#ss -ntulp | grep -E "9200|9300"
tcp    LISTEN     0      128    127.0.0.1:9200                  *:*                   users:(("java",pid=27279,fd=190))
tcp    LISTEN     0      128    127.0.0.1:9300                  *:*                   users:(("java",pid=27279,fd=183))

※在启动elasticsearch过程中可能会遇到一下问题:
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决:

[root@test-machine /root]#sysctl -w vm.max_map_count=262144
[root@test-machine /root]#vim /etc/sysctl.conf
vm.max_map_count=262144

启动后可以在浏览器中查看服务状态和集群状态:
http://localhost:9200
http://localhost:9200/_cluster/health?pretty,观察status为‘green’和number of nodes为‘3’,说明三个elasticsearch已经组成集群。
在这里插入图片描述
在这里插入图片描述

4、安装head插件

head插件安装,只需要在一台机器安装就可以,ES5.0以上不支持plugin安装,所以这里笔者启动一个服务来安装head插件。
安装node.js环境:

[root@test-machine /root]#wget https://nodejs.org/dist/v10.13.0/node-v10.13.0-linux-x64.tar.xz
[root@test-machine /root]#tar -xf node-v10.13.0-linux-x64.tar.xz -C /usr/local/
[root@test-machine /root]#vim /etc/profile
……
export NODEJS_HOME=/usr/local/node-v10.13.0-linux-x64
export PATH=$PATH:$NODEJS_HOME/bin
[root@test-machine /root]#source /etc/profile
[root@test-machine /root]#npm -v
6.4.1

安装head插件:

[root@test-machine /root]#git clone https://github.com/mobz/elasticsearch-head
[root@test-machine /root]#cd elasticsearch-head/
[root@test-machine /root]#chown -R elasticsearch:elasticsearch /root/elasticsearch-head/
[root@test-machine /root]#npm install -g grunt-cli
[root@test-machine /root]#npm install
[root@test-machine /root]#npm install phantomjs-prebuilt@2.1.16 --ignore-scripts
[root@test-machine /root]#nohup grunt server &
[root@test-machine /root]#ss -ntulp | grep 9100
tcp    LISTEN     0      128       *:9100                  *:*                   users:(("grunt",pid=26754,fd=18))

head插件监听在9100端口,浏览器访问http://localhost:9100可以看到连接到elasticsearch集群的副本状态
在这里插入图片描述

5、安装logstash

logstash安装,一台机器上安装即可:

[root@test-machine /root]#wget https://artifacts.elastic.co/downloads/logstash/logstash-6.4.3.rpm
[root@test-machine /root]#yum -y install logstash-6.4.3.rpm

编辑配置文件,测试能否将信息传入elasticsearch

[root@test-machine /root]#vim /etc/logstash/logstash.conf
input{
    stdin{}
}

output{
    elasticsearch{
      hosts => ["192.168.1.146:9200","192.168.1.60:9200","192.168.1.62:9200"]
    }
   stdout { codec => rubydebug }
}

指定配置文件启动logstash服务,启动服务后直接在终端键入测试信息,然后回车:

[root@test-machine /root]#/usr/share/logstash/bin/logstash --config.test_and_exit -f /etc/logstash/logstash.conf
Sending Logstash logs to /var/log/logstash which is now configured via log4j2.properties
[2018-11-21T11:57:23,564][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified
Configuration OK
[2018-11-21T11:57:27,561][INFO ][logstash.runner          ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash
[root@test-machine /root]#/usr/share/logstash/bin/logstash -f /etc/logstash/logstash.conf
……
[2018-11-21T11:50:11,976][INFO ][logstash.agent           ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[2018-11-21T11:50:12,268][INFO ][logstash.agent           ] Successfully started Logstash API endpoint {:port=>9600}
测试————肓己

可以在终端中看到输出的信息:
在这里插入图片描述

head插件中可以看到信息:
在这里插入图片描述
在这里插入图片描述

6、安装kibana

kibana 安装,一台机器安装即可:

[root@test-machine /root]#wget https://artifacts.elastic.co/downloads/kibana/kibana-6.4.3-x86_64.rpm
[root@test-machine /root]#yum -y install kibana-6.4.3-x86_64.rpm

修改配置文件

[root@test-machine /root]#vim /etc/kibana/kibana.yml
[root@test-machine /root]#grep -v "^#" /etc/kibana/kibana.yml | grep -v "^$"
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://192.168.1.146:9200"
kibana.index: ".kibana"
kibana.defaultAppId: "discover"
elasticsearch.pingTimeout: 1500
elasticsearch.requestTimeout: 30000
elasticsearch.startupTimeout: 5000

启动服务:

[root@test-machine /root]#systemctl start kibana
[root@test-machine /root]#systemctl status kibana
● kibana.service - Kibana
   Loaded: loaded (/etc/systemd/system/kibana.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-11-15 16:19:40 CST; 5 days ago
Main PID: 24972 (node)
   CGroup: /system.slice/kibana.service
           └─24972 /usr/share/kibana/bin/../node/bin/node --no-warnings /usr/share/kibana/bin/.....

启动服务后,kibana监听在5601端口,访问http://localhost:5601可以在浏览器中配置和管理

设置索引:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
设置完索引后查看数据:在这里插入图片描述
在这里插入图片描述

7、安装filebeat

两台web主机安装filebeat,编辑配置文件,先不要启动服务。
web主机1:

[root@template-centos7 /root]#curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.4.3-x86_64.rpm
[root@template-centos7 /root]#yum -y install filebeat-6.4.3-x86_64.rpm
[root@template-centos7 /root]#vim /etc/filebeat/filebeat.yml
[root@template-centos7 /root]#grep -v "#" /etc/filebeat/filebeat.yml |grep -v "^$"
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /data/logs/sunaw_czd/czd.log
  document_type: czd-log
  fields:
    tag: czdtest
    logtype: czdtest

  exclude_lines: [".*c.b.mybatisplus.mapper.SqlHelper.*"]
  multiline.pattern: '^\s*(\d{4}|\d{2})\-(\d{2}|[a-zA-Z]{3})\-(\d{2}|\d{4})'
  multiline.negate: true
  multiline.match: after
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 3
setup.kibana:
output.logstash:
  hosts: ["192.168.1.146:5044"]

web主机2:

[root@926-test /root]#grep -v "#" /etc/filebeat/filebeat.yml |grep -v "^$"
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /data/logs/jiuerliu.log
  document_type: jiuerliu-log
  fields:
    tag: yqmirotest
    logtype: yqmirotest

  exclude_lines: [".*c.b.mybatisplus.mapper.SqlHelper.*"]
  multiline.pattern: '^\s*(\d{4}|\d{2})\-(\d{2}|[a-zA-Z]{3})\-(\d{2}|\d{4})'
  multiline.negate: true
  multiline.match: after
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 3
setup.kibana:
output.logstash:
  hosts: ["192.168.1.146:5044"]

在logstash主机新建filebeat配置文件,并启动服务:

logstash配置
[root@test-machine /root]#cat /etc/logstash/filebeat.conf
input {
  beats {
    port => 5044
    type => "logs"
    #codec => "json"
  }
}

output {
  if [fields][logtype] == "czdtest"{
    elasticsearch {
      hosts => [ "192.168.1.146:9200","192.168.1.60:9200","192.168.1.62:9200"]        
      index => "czd-test-log-%{+YYYY-MM}"
    }
  }
  if [fields][logtype] == "yqmirotest"{
    elasticsearch {
      hosts => [ "192.168.1.146:9200","192.168.1.60:9200","192.168.1.62:9200"]        
      index => "yqmiro-test-log-%{+YYYY-MM}"
    }
  }
  stdout { codec => rubydebug }
}
[root@test-machine /root]#/usr/share/logstash/bin/logstash -f /etc/logstash/filebeat.conf
[2018-11-21T12:29:21,462][INFO ][org.logstash.beats.Server] Starting server on port: 5044
[2018-11-21T12:29:21,479][INFO ][logstash.agent           ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[2018-11-21T12:29:21,856][INFO ][logstash.agent           ] Successfully started Logstash API endpoint {:port=>9600}

lostash启动后,再去两台web主机中启动filebeat:

[root@template-centos7 /root]#systemctl start filebeat
[root@template-centos7 /root]#systemctl status filebeat
● filebeat.service - Filebeat sends log files to Logstash or directly to Elasticsearch.
   Loaded: loaded (/usr/lib/systemd/system/filebeat.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-11-21 12:31:15 CST; 7s ago
     Docs: https://www.elastic.co/products/beats/filebeat
 Main PID: 11079 (filebeat)
   Memory: 7.4M
   CGroup: /system.slice/filebeat.service
           └─11079 /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -pat...

Nov 21 12:31:15 template-centos7 systemd[1]: Started Filebeat sends log files to Logstash or directly to Elasticsearch..
Nov 21 12:31:15 template-centos7 systemd[1]: Starting Filebeat sends log files to Logstash or directly to Elasticsearch....

8、采集结果查看

head插件和kibanaWeb界面能查看到采集的数据:
在这里插入图片描述
在这里插入图片描述
展开message能查看到详细信息:
在这里插入图片描述

至此,ELK+filebeat采集java日志的搭建过程结束。

<think>好的,我现在要帮助用户了解如何搭建ELK+Kafka+Filebeat日志系统。根据用户提供的信息,步骤包括配置JDK8、ZooKeeper集群、Kafka和Filebeat。我需要先理解每个组件的作用以及它们之间的关系。 首先,ELK指的是Elasticsearch、Logstash和Kibana,这三个通常一起使用来处理和可视化日志。但用户提到的是ELK+Kafka+Filebeat,所以应该是用Filebeat代替Logstash收集日志,然后发送到Kafka,再由Logstash或另一个消费者处理数据并传给Elasticsearch。不过用户提供的步骤中没有提到Logstash,可能需要确认是否需要配置Logstash,但用户的问题可能只关注这四个部分:JDK8、ZooKeeper、Kafka、Filebeat。 用户给出的步骤是1到4点,我需要按照这个顺序来介绍。首先配置JDK8,因为Kafka和ZooKeeper都需要Java环境。然后配置ZooKeeper集群,因为Kafka依赖ZooKeeper来做集群管理。接着安装Kafka,最后配置Filebeat日志发送到Kafka。 接下来,我需要详细每个步骤的具体操作。例如,安装JDK8可能需要下载安装包,设置环境变量。配置ZooKeeper集群需要修改配置文件,设置服务器ID,启动服务等。Kafka的配置可能涉及修改server.properties,设置ZooKeeper的连接地址,创建topic等。Filebeat需要配置输入和输出,指定日志路径和Kafka的地址及topic。 同时,需要注意各个组件之间的依赖关系和通信端口是否开放,比如ZooKeeper默认端口2181,Kafka的9092端口,以及集群节点之间的通信。另外,是否需要配置多个Kafka broker,形成集群,或者单节点。用户可能是在搭建一个测试环境,所以可能只需要单节点,但如果是生产环境,则需要集群配置。但用户的问题中没有明确说明,所以可能需要涵盖基本的集群配置。 还要考虑可能的错误点和常见问题,例如环境变量未正确设置,配置文件中的IP地址或主机名错误,防火墙未开放端口,服务启动顺序是否正确(比如先启动ZooKeeper再启动Kafka)。此外,Filebeat的配置是否正确,比如日志路径是否正确,Kafka输出配置是否正确,是否需要SSL认证等。 另外,用户可能希望了解每个步骤的命令或配置示例,例如如何修改ZooKeeper的zoo.cfg文件,Kafka的server.properties中的配置项,Filebeatfilebeat.yml的设置。需要确保提供的命令和配置示例准确,避免语法错误。 可能还需要验证步骤的正确性,例如安装JDK后通过java -version检查,启动ZooKeeper后查看日志是否有错误,Kafka是否能够创建topic和发送接收消息,Filebeat是否成功将日志发送到Kafka。 最后,是否需要整合ELK的其他组件,比如Elasticsearch和Kibana,用户的问题中没有提到,但可能需要简要说明整个数据流,即Filebeat收集日志发送到Kafka,然后由Logstash或直接由Elasticsearch消费,存储到Elasticsearch,最后用Kibana展示。但根据用户提供的步骤,可能不需要详细配置ELK部分,只关注到Kafka和Filebeat的设置。</think>以下是ELK+Kafka+Filebeat架构的部署流程详解: --- ### **架构组件说明** - **Filebeat**:轻量级日志采集器,负责收集服务器日志 - **Kafka**:高吞吐量消息队列,作为日志缓冲层 - **ZooKeeper**:Kafka的集群协调服务 - **Elasticsearch**:日志存储和搜索引擎 - **Logstash/Kibana**:日志处理和可视化(本教程暂不展开) --- ### **1. 配置JDK8** ```bash # 下载JDK8 wget https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz # 解压安装 tar -zxvf jdk-8u202-linux-x64.tar.gz -C /usr/local/ # 配置环境变量 echo 'export JAVA_HOME=/usr/local/jdk1.8.0_202 export PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile source /etc/profile # 验证安装 java -version ``` --- ### **2. ZooKeeper集群部署** **所有节点操作:** ```bash # 下载安装包 wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz # 解压并创建数据目录 tar -zxvf zookeeper-3.4.14.tar.gz -C /opt/ mkdir -p /data/zookeeper/{data,log} # 配置文件 zoo.cfg cat > /opt/zookeeper-3.4.14/conf/zoo.cfg <<EOF tickTime=2000 initLimit=10 syncLimit=5 dataDir=/data/zookeeper/data dataLogDir=/data/zookeeper/log clientPort=2181 server.1=node1:2888:3888 server.2=node2:2888:3888 server.3=node3:2888:3888 EOF # 创建myid文件(各节点不同) echo "1" > /data/zookeeper/data/myid # node1执行 echo "2" > /data/zookeeper/data/myid # node2执行 echo "3" > /data/zookeeper/data/myid # node3执行 # 启动服务 /opt/zookeeper-3.4.14/bin/zkServer.sh start ``` --- ### **3. Kafka集群部署** **所有节点操作:** ```bash # 下载安装包 wget https://archive.apache.org/dist/kafka/2.4.1/kafka_2.13-2.4.1.tgz # 解压安装 tar -zxvf kafka_2.13-2.4.1.tgz -C /opt/ # 修改配置文件 server.properties vim /opt/kafka_2.13-2.4.1/config/server.properties --- broker.id=1 # 各节点唯一ID listeners=PLAINTEXT://当前节点IP:9092 zookeeper.connect=node1:2181,node2:2181,node3:2181 log.dirs=/data/kafka-logs --- # 创建数据目录 mkdir -p /data/kafka-logs # 启动服务 /opt/kafka_2.13-2.4.1/bin/kafka-server-start.sh -daemon config/server.properties # 创建测试topic /opt/kafka_2.13-2.4.1/bin/kafka-topics.sh --create \ --zookeeper node1:2181 \ --replication-factor 2 \ --partitions 3 \ --topic test-log ``` --- ### **4. Filebeat配置** **日志采集节点操作:** ```yaml # 编辑配置文件 /etc/filebeat/filebeat.yml filebeat.inputs: - type: log enabled: true paths: - /var/log/*.log output.kafka: hosts: ["kafka-node1:9092", "kafka-node2:9092"] topic: "test-log" required_acks: 1 compression: gzip # 启动服务 systemctl start filebeat ``` --- ### **架构数据流向** $$ \text{Filebeat} \xrightarrow{\text{日志采集}} \text{Kafka} \xrightarrow{\text{消息缓冲}} \text{Logstash/ES} \xrightarrow{\text{存储分析}} \text{Kibana} $$ --- ### **关键验证点** 1. ZooKeeper集群状态检查: ```bash echo stat | nc 127.0.0.1 2181 ``` 2. Kafka生产/消费测试: ```bash # 生产者 /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test-log # 消费者 /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-log --from-beginning ``` 3. Filebeat日志验证: ```bash tail -f /var/log/filebeat/filebeat.log ``` --- ### **注意事项** 1. Kafka集群建议至少3节点,ZooKeeper集群节点数应为奇数 2. 生产环境需配置SSL认证和ACL权限控制 3. 日志保留策略需在Kafka中配置`log.retention.hours`参数 4. 建议使用版本匹配:Filebeat 7.x + Kafka 2.x + JDK8
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值