ELK 部署 及 logstash 对接应用

463 阅读13分钟

ELK

ELK 是三个开源项目的首字母缩写,代表 Elasticsearch、Logstash 和 Kibana。这个组合被广泛用于日志管理、安全分析、应用程序监控等领域,提供了一个强大的、可扩展的日志数据分析和可视化平台。

  1. Elasticsearch:Elasticsearch 是一个基于 Lucene 的分布式、RESTful 搜索和分析引擎。它允许你快速、实时地存储、搜索和分析大量数据。Elasticsearch 的特点包括全文搜索、实时分析、分布式存储和可扩展性。它支持复杂的查询和聚合操作,并提供了丰富的 API,使得开发者可以轻松地将其集成到各种应用程序中。
  2. Logstash:Logstash 是一个开源的服务器端数据处理管道,它能够同时从多个源采集数据,进行转换和丰富处理,然后将数据发送到指定的目的地,如 Elasticsearch、文件、数据库等。Logstash 的灵活性在于其强大的插件生态系统,通过插件可以轻松地扩展其功能,支持各种输入、过滤器和输出。
  3. Kibana:Kibana 是一个开源的数据可视化和分析平台,专为 Elasticsearch 设计。它提供了一个直观的界面,允许用户通过简单的点击和拖拽操作来创建图表、仪表板、地图等可视化元素,以便更好地理解和分析存储在 Elasticsearch 中的数据。Kibana 还支持数据探索和搜索,使用户能够轻松地查询和挖掘数据中的价值。

将 Elasticsearch、Logstash 和 Kibana 结合使用时,可以形成一个完整的日志管理和分析解决方案,即 ELK Stack。这个解决方案可以自动收集、解析、存储和可视化来自各种应用程序和系统的日志数据,帮助用户快速发现和解决问题,提高系统的稳定性和性能。

ELK 的工作原理

image.png

  • (1)在所有需要收集日志的服务器上部署Logstash;或者先将日志进行集中化管理在日志服务器上,在日志服务器上部署 Logstash。
  • (2)Logstash 收集日志,将日志格式化并输出到 Elasticsearch 群集中
  • (3)Elasticsearch 对格式化后的数据进行索引和存储
  • (4)Kibana 从 ES 群集中查询数据生成图表,并进行前端数据的展示。

总结:logstash作为日志搜集器,从数据源采集数据,并对数据进行过滤,格式化处理,然后交由Elasticsearch存储,kibana对日志进行可视化处理。

安装部署ELK

前期准备

java环境

java -version                                       #如果没有安装,yum -y install java
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)

关闭防火墙

systemctl disable  --now   firewalld
setenforce 0
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/'  /etc/selinux/config 
​

安装部署 Elasticsearch 软件

安装 Elasticsearch 软件

[root@localhost data]#ls
elasticsearch-6.7.2.rpm  elasticsearch-head-master.zip  node-v8.2.1.tar.gz  phantomjs-2.1.1-linux-x86_64.tar.bz2
​
[root@localhost data]#rpm -ivh elasticsearch-6.7.2.rpm
[root@localhost data]#cd /etc/elasticsearch/
[root@localhost elasticsearch]#mkdir bak 
[root@localhost elasticsearch]#cp -a *.yml  bak/
#备份
​
​

修改配置文件

[root@node1 elasticsearch]#vim elasticsearch.yml
17 cluster.name: my-elk-cluster
#修改集群名字



23 node.name: node1
24 node.master: true
25 node.data: true
#设置 节点名称 主从之间不能一致    24作为主节点   25作为数据节点



45 bootstrap.memory_lock: true
#内存锁开启  禁止使用  swap




59 network.host: 0.0.0.0
#监听地址
60 http.port: 9200
#  默认使用端口
61 transport.tcp.port: 9300
#内部传输端口



73 discovery.zen.ping.unicast.hosts: ["192.168.10.1:9300", "192.168.10.10:9300"]
#自动集群发现,加入主机名  使用单播 类似心跳线



[root@localhost elasticsearch]#grep -v "^#"  elasticsearch.yml 
cluster.name: my-elk-cluster
node.name: node1
node.master: true
node.data: true
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 0.0.0.0 
http.port: 9200
transport.tcp.port: 9300
discovery.zen.ping.unicast.hosts: ["192.168.10.1:9300", "192.168.10.10:9300"]

修改系统配置

性能调优参数

[root@localhost elasticsearch]#vim /etc/security/limits.conf
......
*  soft    nofile          65536
*  hard    nofile          65536
*  soft    nproc           32000
*  hard    nproc           32000
*  soft    memlock         unlimited
*  hard    memlock         unlimited

修改systemd 服务管理器

/etc/systemd/system.conf 文件是用于配置 systemd 的,这是一种用于 Linux 操作系统的系统和服务管理器。通过这个文件,你可以自定义与系统操作、性能和行为相关的各种设置

  • DefaultTimeoutStartSec=:设置启动服务的默认等待时间
  • DefaultTimeoutStopSec=:设置停止服务的默认等待时间
  • DefaultRestartSec=:设置在重新启动服务之前的默认休眠时间
  • DefaultLimitNOFILE=:设置打开文件数量的默认限制
  • DefaultLimitNPROC=:设置进程数量的默认限制
  • DefaultLimitCORE=:设置核心文件大小的默认限制
  • DefaultEnvironment=:指定服务的默认环境变量

实际修改

[root@localhost elasticsearch]#vim /etc/systemd/system.conf
DefaultLimitNOFILE=65536
DefaultLimitNPROC=32000
DefaultLimitMEMLOCK=infinity

修改内核参数

Lucene 是一个高性能、全功能的文本搜索引擎库,用于实现高效的文本索引和搜索。它是用 Java 编写的,可以嵌入到各种应用程序中,以提供强大的全文搜索功能。Lucene 是许多流行搜索平台的核心,如 Apache Solr 和 Elasticsearch。

以下是 Lucene 的一些关键特性和功能:

  1. 文本索引:Lucene 提供了强大的文本索引功能,能够处理大规模文本数据并生成高效的索引。
  2. 全文搜索:支持复杂的查询语法,包括布尔查询、短语查询、通配符查询、模糊查询等。
  3. 可扩展性:Lucene 设计为模块化和可扩展的,允许用户根据需要扩展和定制其功能。
  4. 分词和分析:提供了丰富的分词器和分析器,用于将文本分解为可索引和可搜索的词语。
  5. 排序和评分:支持对搜索结果进行排序和评分,以提高搜索的准确性和相关性。
  6. 多语言支持:支持多种语言的文本处理和搜索。

优化elasticsearch用户拥有的内存权限 由于ES构建基于lucene, 而lucene设计强大之处在于lucene能够很好的利用操作系统内存来缓存索引数据,以提供快速的查询性能。lucene的索引文件segements是存储在单文件中的,并且不可变,对于OS来说,能够很友好地将索引文件保持在cache中,以便快速访问;因此,我们很有必要将一半的物理内存留给lucene ; 另一半的物理内存留给ES(JVM heap )。所以, 在ES内存设置方面,可以遵循以下原则:

  • 当机器内存小于64G时,遵循通用的原则,50%给ES,50%留给操作系统,供lucene使用
  • 当机器内存大于64G时,遵循原则:建议分配给ES分配 4~32G 的内存即可,其它内存留给操作系统,供lucene使用
[root@localhost elasticsearch]#vim /etc/sysctl.conf
#一个进程可以拥有的最大内存映射区域数,参考数据(分配 2g/262144,4g/4194304,8g/8388608)
vm.max_map_count=262144
​
sysctl -p
sysctl -a | grep vm.max_map_count

重启服务器 启动 elasticsearch

reboot
systemctl start elasticsearch.service
systemctl enable elasticsearch.service
netstat -antp | grep 9200

查看节点信息

浏览器访问  
http://192.168.10.1:9200  
http://192.168.10.10:9200 
查看节点 Node1、Node2 的信息。

浏览器访问 
http://192.168.10.1:9200/_cluster/health?pretty  
http://192.168.10.10:9200/_cluster/health?pretty
查看群集的健康情况,可以看到 status 值为 green(绿色), 表示节点健康运行。


浏览器访问 http://192.168.10.1:9200/_cluster/state?pretty  检查群集状态信息

可以安装插件

编译安装 Elasticsearch-head 插件 主从都可以安装

Elasticsearch 在 5.0 版本后,Elasticsearch-head 插件需要作为独立服务进行安装,需要使用npm工具(NodeJS的包管理工具)安装。 安装 Elasticsearch-head 需要提前安装好依赖软件 **node**和 phantomjs。 node:是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 phantomjs:是一个基于 webkit 的JavaScriptAPI,可以理解为一个隐形的浏览器,任何基于 webkit 浏览器做的事情,它都可以做到。

编译安装node组件
#上传软件包 node-v8.2.1.tar.gz 到/opt
yum install gcc gcc-c++ make -y
​
cd /opt
tar zxvf node-v8.2.1.tar.gz
​
cd node-v8.2.1/
./configure
make && make install
安装 phantomjs
#上传软件包 phantomjs-2.1.1-linux-x86_64.tar.bz2 到
cd /opt
tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
cd /opt/phantomjs-2.1.1-linux-x86_64/bin
ln -s  /opt/phantomjs-2.1.1-linux-x86_64/binhantomjs   /usr/bin
安装 Elasticsearch-head 数据可视化工具
#上传软件包 elasticsearch-head-master.zip 到/op
cd /opt
unzip elasticsearch-head-master.zip
cd /opt/elasticsearch-head/
npm install      //安装依赖包
修改 Elasticsearch 主配置文件**
vim /etc/elasticsearch/elasticsearch.yml
......
--末尾添加以下内容--
http.cors.enabled: true             #开启跨域访问支持,默认为 false
http.cors.allow-origin: "*"         #指定跨域访问允许的域名地址为所有


http.cors.enabled: true
http.cors.allow-origin: "*" 

systemctl restart elasticsearch
#重启elasticsearch  服务
启动 elasticsearch-head 服务**

必须在解压后的 elasticsearch-head 目录下启动服务,进程会读取该目录下的 gruntfile.js 文件,否则可能启动失败。

[root@localhost elasticsearch]#cd /opt/elasticsearch-head/
[root@localhost elasticsearch-head]# npm run start &
> elasticsearch-head@0.0.0 start /usr/local/src/elasticsearch-head
> grunt server
​
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100#elasticsearch-head 监听的端口是 9100
netstat -natp |grep 9100
测试插件
192.168.10.1:9100
192.168.10.10:9100
插入索引测试

image.png

curl -X PUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
​
-x 指定方法
-H 添加请求头
-d 请求体
​
​
[root@node2 elasticsearch-head-master]#curl -X PUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
{
  "_index" : "index-demo",
  "_type" : "test",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
​

刷新页面可以看到有新的数据

在应用服务器部署 Logstash

在 Apache 节点上操作

更改主机名

[root@localhost elasticsearch]# hostnamectl set-hostname apache

安装服务

yum -y install java
java -version
yum -y install httpd
systemctl start httpd

安装logstash

cd /opt
[root@localhost opt]#  rpm -ivh logstash-6.7.2.rpm                          #开启服务
systemctl enable --now logstash.service
​
[root@localhost opt]# ln -s  /usr/share/logstash/bin/logstash   /usr/bin/
# 做软连接

使用logstash

Logstash 命令常用选项: -f:通过这个选项可以指定 Logstash 的配置文件,根据配置文件配置 Logstash 的输入和输出流。 -e:从命令行中获取,输入、输出后面跟着字符串,该字符串可以被当作 Logstash 的配置(如果是空,则默认使用 stdin 作为输入,stdout 作为输出)。 -t:测试配置文件是否正确,然后退出。

例子:在命令行中收集日志数据

#输入采用标准输入,输出采用标准输出(类似管道),新版本默认使用 rubydebug 格式输出

[root@localhost opt]# logstash -e 'input { stdin{} } output { stdout{} }'
# 等待时间较长
WARNING: Could not find logstash.yml which is typically located in $LS_HOME/config or /etc/logstash. You can specify the path using --path.settings. Continuing using the defaults
Could not find log4j2 configuration at path /usr/share/logstash/config/log4j2.properties. Using default config which logs errors to the console
[INFO ] 2024-07-16 00:42:00.411 [main] writabledirectory - Creating directory {:setting=>"path.queue", :path=>"/usr/share/logstash/data/queue"}
[INFO ] 2024-07-16 00:42:00.468 [main] writabledirectory - Creating directory {:setting=>"path.dead_letter_queue", :path=>"/usr/share/logstash/data/dead_letter_queue"}
[WARN ] 2024-07-16 00:42:01.196 [LogStash::Runner] multilocal - Ignoring the 'pipelines.yml' file because modules or command line options are specified
[INFO ] 2024-07-16 00:42:01.211 [LogStash::Runner] runner - Starting Logstash {"logstash.version"=>"6.7.2"}
[INFO ] 2024-07-16 00:42:01.269 [LogStash::Runner] agent - No persistent UUID file found. Generating new UUID {:uuid=>"31865788-3782-4fb6-9472-c755f27c98ed", :path=>"/usr/share/logstash/data/uuid"}
[INFO ] 2024-07-16 00:42:09.884 [Converge PipelineAction::Create<main>] pipeline - Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>2, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50}
[INFO ] 2024-07-16 00:42:10.147 [Converge PipelineAction::Create<main>] pipeline - Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x6a4c4acf sleep>"}
The stdin plugin is now waiting for input:
[INFO ] 2024-07-16 00:42:10.244 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[INFO ] 2024-07-16 00:42:10.635 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
​
# 此处输入需要的信息
hello world
#############
/usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/awesome_print-1.7.0/lib/awesome_print/formatters/base_formatter.rb:31: warning: constant ::Fixnum is deprecated
{
       "message" => "hello world",
          "host" => "localhost.localdomain",
      "@version" => "1",
    "@timestamp" => 2024-07-15T16:42:54.195Z
}
​

例子:#使用 rubydebug 输出详细格式显示,codec 为一种编解码器

logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
结果与上面一样

使用 Logstash 将信息写入 Elasticsearch 中

[root@localhost opt]#logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.10.1:9200"] } }'
                                        对接
                                        
//结果不在标准输出显示,而是发送至 Elasticsearch 中,可浏览器访问 http://192.168.10.1:9100/ 查看索引信息和数据浏览。


logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.10.1:9200","192.168.10.10:9200"]} }'
............................
[INFO ] 2024-07-16 00:50:12.096 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
# 输入信息

hello world

使用配置文件

Logstash 配置文件基本由三部分组成:input、output 以及 filter(可选,根据需要选择使用)

  • input:表示从数据源采集数据,常见的数据源如Kafka、日志文件等 file beats kafka redis stdin
  • filter:表示数据处理层,包括对数据进行格式化处理、数据类型转换、数据过滤等,支持正则表达式 grok 对若干个大文本字段进行再分割成一些小字段 (?<字段名>正则表达式) 字段名: 正则表达式匹配到的内容 date 对数据中的时间格式进行统一和格式化 mutate 对一些无用的字段进行剔除,或增加字段 mutiline 对多行数据进行统一编排,多行合并或拆分
  • output:表示将Logstash收集的数据经由过滤器处理之后输出到Elasticsearch。 elasticsearch stdout
#格式如下:
input {...}
filter {...}
output {...}

#在每个部分中,也可以指定多个访问方式。例如,若要指定两个日志来源文件,则格式如下:
input {
	file { path =>"/var/log/messages" type =>"syslog"}
	file { path =>"/var/log/httpd/access.log" type =>"apache"}
	
vim system.conf
input {
    file{
        path =>"/var/log/messages"
        type =>"system"
        start_position =>"beginning"
		# ignore_older => 604800
        sincedb_path => "/etc/logstash/sincedb_path/log_progress"
        add_field => {"log_hostname"=>"${HOSTNAME}"}
    }
}
#path表示要收集的日志的文件位置
#type是输入ES时给结果增加一个叫type的属性字段
#start_position可以设置为beginning或者end,beginning表示从头开始读取文件,end表示读取最新的,这个要和ignore_older一起使用
#ignore_older表示了针对多久的文件进行监控,默认一天,单位为秒,可以自己定制,比如默认只读取一天内被修改的文件
#sincedb_path表示文件读取进度的记录,每行表示一个文件,每行有两个数字,第一个表示文件的inode,第二个表示文件读取到的位置(byteoffset)。默认为$HOME/.sincedb*
#add_field增加属性。这里使用了${HOSTNAME},即本机的环境变量,如果要使用本机的环境变量,那么需要在启动命令上加--alow-env

output {
    elasticsearch {												#输出到 elasticsearch
        hosts => ["192.168.10.1:9200","192.168.10.10:9200"]	#指定 elasticsearch 服务器的地址和端口
        index =>"system-%{+YYYY.MM.dd}"							#指定输出到 elasticsearch 的索引格式
    }
}

实际例子:

[root@localhost log]# vim /etc/logstash/conf.d/system-log.conf 

input {
    file {
      path => "/var/log/messages"
      type => "system"
      start_position => "beginning"
    }


}


output {
  elasticsearch {
  hosts => [ "192.168.10.1:9200","192.168.10.10:9200" ]
  index => "system-%{+YYYY.MM.dd}"
  }

}



[root@localhost conf.d]# chmod +r /var/log/messages 
#添加权限

[root@localhost conf.d]# logstash   -f  system-log.conf 
#启动logstash
........................................................................
[INFO ] 2024-07-16 01:02:41.716 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9601}

刷新页面

安装 kibana 无所谓哪台服务器

安装

root@localhost opt]# rpm -ivh kibana-6.7.2-x86_64.rpm 

修改配置

[root@localhost opt]# cd  /etc/kibana/
[root@localhost kibana]# cp kibana.yml  kibana.yml.bak -a

[root@localhost kibana]# vim kibana.yml

2   		 server.port: 5601          		#打开端口
7    server.host: "0.0.0.0"      	#监听端口
28   elasticsearch.hosts: ["http://192.168.10.1:9200", "http://192.168.10.10:9200"]  #el服务器地址
37   kibana.index: ".kibana"         #打开索引
96   logging.dest: /var/log/k.log      #指定日志文件, 需要手动建立文件
114  i18n.locale: "zh-CN"              #中文设置

[root@localhost kibana]# chown kibana:kibana /var/log/k.log

启动 kibana

[root@localhost kibana]# systemctl enable --now kibana.service
[root@localhost kibana]# ss -nap |grep 5601
tcp    LISTEN     0      128       *:5601                  *:*                   users:(("node",pid=42235,fd=19))
​

访问测试

192.168.10.20:5601

image.png

image.png

image.png

image.png

加入nginx日志

[root@localhost ~]# yum install  epel-release.noarch -y 
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# vim /etc/nginx/nginx.conf
http {
# 添加在 http  语句块中
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';

access_log  /var/log/nginx/access.log  access_json;'"status":"$status"}';
#修改默认  日志格式







[root@localhost ~]# systemctl  start  nginx

[root@localhost ~]# vim /etc/logstash/conf.d/nginx-log.conf 

input{
   file {
    path =>  "/var/log/nginx/access.log"
    type =>  "nginx"
    start_position => "beginning"
    
     }
}

output {
   elasticsearch  {
        hosts => [ "192.168.10.1:9200", "192.168.10.10:9200"]
        index => "nginx-%{+YYYY.MM.dd}"
     }

http.port: 9200


[root@localhost conf.d]# logstash -f  nginx-log.conf
  • 收集日志成功后可以 制作可视化图形

logstash 对接 redis

image.png 7-3

[root@apache conf.d]# cat  redis-push.conf 
input {
  file {
    path => "/usr/local/tomcat/logs/localhost_access_log.*.log"
    type => "tomcat-acceslog"
    start_position => "beginning"
    stat_interval => "3"
  }

}


output {
    redis {
      data_type => list
      key => "tomcat-accesslog-40"
      host => "192.168.10.40"
      port => "6379"
      db => "0"
  }}

7-5 redis

image.png

在 7-4 配置


安装logstash
[root@localhost ~]#vim /etc/logstash/conf.d/redis.conf 

input {
    redis {
      data_type => list
      key => "tomcat-accesslog-40"
      host => "192.168.10.40"
      port => "6379"
      db => "0"
  }
}


output {
  elasticsearch {
     hosts => ["192.168.10.1:9200", "192.168.10.10:9200"]
     index => "logstash-tomcat40-accesslog-%{+YYYY.MM.dd}"
}
}

image.png

Filebeat

Filebeat:轻量级的开源日志文件数据搜集器。通常在需要采集数据的客户端安装 Filebeat,并指定目录与日志格式,Filebeat 就能快速收集数据,并发送给 logstash 进行解析,或是直接发给 Elasticsearch 存储,性能上相比运行于 JVM 上的 logstash 优势明显,是对它的替代。常应用于 EFLK 架构当中。

filebeat 结合 logstash 带来好处: 1)通过 Logstash 具有基于磁盘的自适应缓冲系统,该系统将吸收传入的吞吐量,从而减轻 Elasticsearch 持续写入数据的压力 2)从其他数据源(例如数据库,S3对象存储或消息传递队列)中提取 3)将数据发送到多个目的地,例如S3,HDFS(Hadoop分布式文件系统)或写入文件 4)使用条件数据流逻辑组成更复杂的处理管道

●缓存/消息队列(redis、kafka、RabbitMQ等):可以对高并发日志数据进行流量削峰和缓冲,这样的缓冲可以一定程度的保护数据不丢失,还可以对整个架构进行应用解耦。

安装 Filebeat

安装 Filebeat

#上传软件包 filebeat-6.7.2-linux-x86_64.tar.gz 到/opt目录
tar zxvf filebeat-6.7.2-linux-x86_64.tar.gz
mv filebeat-6.7.2-linux-x86_64/ /usr/local/filebeat

修改配置文件

vim filebeat.yml

24   enabled: ture
27   paths:
28     - /var/log/nginx/access.log
30   tags: ["filebeat"]
31   fields:
32      service_name: nginx
33      log_type: access
34      from: 192.168.10.30
注释以下行
151 #output.elasticsearch:
152   # Array of hosts to connect to.
153  # hosts: ["localhost:9200"]

164 output.logstash:
165   # The Logstash hosts
166   hosts: ["192.168.10.20:5044"]




filebeat.inputs:
- type: log         #指定 log 类型,从日志文件中读取消息
  enabled: true
  paths:
    - /var/log/messages       #指定监控的日志文件
    - /var/log/*.log
  tags: ["sys"]		#设置索引标签   这两行注意对齐否则启动不了
  fields:           #可以使用 fields 配置选项设置一些参数字段添加到 output 中
    service_name: filebeat
    log_type: syslog
    from: 192.168.80.13
    
--------------Elasticsearch output-------------------
(全部注释掉)

----------------Logstash output---------------------
output.logstash:
  hosts: ["192.168.10.30:5044"]      #指定 logstash 的 IP 和端口

启动 filebeat

nohup ./filebeat -e -c filebeat.yml > filebeat.out &
#-e:输出到标准输出,禁用syslog/文件输出
#-c:指定配置文件
#nohup:在系统后台不挂断地运行命令,退出终端不会影响程序的运行

对接logstash

cd /etc/logstash/conf.d

vim filebeat.conf
input {
  beats { port => "5044" }


}



output {
    elasticsearch  {
       hosts => ["192.168.10.1:9200", "192.168.10.10:9200"]
       index => "woshishuaige-YYYY.MM.dd}"
}

    stdout {
      codec => rubydebug

}

}


启动 logstash

#启动 logstash
logstash -f filebeat.conf -t  检查语法

浏览器访问 http://192.168.10.20:5601 登录 Kibana


logstash -f filebeat.conf

image.png

image.png