阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

通过ELK快速搭建集中化日志平台

169次阅读
没有评论

共计 10615 个字符,预计需要花费 27 分钟才能阅读完成。

在项目初期的时候,大家都是赶着上线,一般来说对日志没有过多的考虑,当然日志量也不大,所以用 log4net 就够了,随着应用的越来越多,日志散落在各个服务器的 logs 文件夹下,确实有点不大方便,这个时候就想到了,在 log4net 中配置 mysql 的数据源,不过这里面有一个坑,熟悉 log4net 的同学知道写入 mysql 有一个 batch 的阈值,比如说 batchcache 中有 100 条,才写入 mysql,这样的话,就有一个延迟的效果,而且如果 batchcache 中不满 100 条的话,你在 mysql 中是看不到最新的 100 条日志。而且采用中心化的 mysql,涉及到 tcp 传输,其中的性能大家也应该明白,而且 mysql 没有一个好的日志界面,只能自己去写 UI,所以还还得继续寻找其他的解决方案,也就是本篇的 ELK。

一:ELK 名字解释

ELK 就是 ElasticSearch + LogStash + Kibana,这三样搭配起来确实非常不错,先画张图给大家看一下。

通过 ELK 快速搭建集中化日志平台

1. LogStash

它可以流放到各自的服务器上收集 Log 日志,通过内置的 ElasticSearch 插件解析后输出到 ES 中。

2.ElasticSearch

这是一个基于 Lucene 的分布式全文搜索框架,可以对 logs 进行分布式存储,有点像 hdfs 哈。。。

3. Kibana

所有的 log 日志都到 ElasticSearch 之后,我们需要给他展示出来,对吧?这个时候 Kibana 就出手了,它可以多维度的展示 es 中的数据。这也解决了

用 mysql 存储带来了难以可视化的问题。

二:快速搭建

上面只是名词解释,为了演示,我只在一台 CentOS 上面搭建了。

1.  官方下载 :https://www.elastic.co/cn/products,在下面这张图上,我们找到对应的三个产品,进行下载就好了。

通过 ELK 快速搭建集中化日志平台

[root@slave1 myapp]# ls
elasticsearch              kafka_2.11-1.0.0.tgz              nginx-1.13.6.tar.gz
elasticsearch-5.6.4.tar.gz  kibana                            node
elasticsearch-head          kibana-5.2.0-linux-x86_64.tar.gz  node-v8.9.1-linux-x64.tar.xz
images                      logstash                          portal
Java                        logstash-5.6.3.tar.gz            service
jdk1.8                      logstash-tutorial-dataset        sql
jdk-8u144-linux-x64.tar.gz  nginx
kafka                      nginx-1.13.6
[root@slave1 myapp]#

我这里下载的是 elasticsearch 5.6.4,kibana5.2.0,logstash5.6.3 三个版本。。。然后用 tar -xzvf 解压一下。

2. logstash 配置

解压完之后,我们到 config 目录中新建一个 logstash.conf 配置。

[root@slave1 config]# ls
jvm.options  log4j2.properties  logstash.conf  logstash.yml  startup.options
[root@slave1 config]# pwd
/usr/myapp/logstash/config
[root@slave1 config]# vim logstash.conf

然后做好 input,filter,output 三大块,其中 input 是吸取 logs 文件下的所有 log 后缀的日志文件,filter 是一个过滤函数,这里不用配置,output 配置了导入到

hosts 为 127.0.0.1:9200 的 elasticsearch 中,每天一个索引。

input {
    file {
        type => “log”
        path => “/logs/*.log”
        start_position => “beginning”
    }
}

output {
  stdout {
  codec => rubydebug {}
  }

  elasticsearch {
    hosts => “127.0.0.1”
    index => “log-%{+YYYY.MM.dd}”
  }
}

配置完了之后,我们就可以到 bin 目录下启动 logstash 了,配置文件设置为 conf/logstash.conf,从下图中可以看到,当前开启的是 9600 端口。

[root@slave1 bin]# ls
cpdump            logstash      logstash.lib.sh  logstash-plugin.bat  setup.bat
ingest-convert.sh  logstash.bat  logstash-plugin  ruby                system-install
[root@slave1 bin]# ./logstash -f ../config/logstash.conf
Sending Logstash’s logs to /usr/myapp/logstash/logs which is now configured via log4j2.properties
[2017-11-28T17:11:53,411][INFO][logstash.modules.scaffold] Initializing module {:module_name=>”fb_apache”, :directory=>”/usr/myapp/logstash/modules/fb_apache/configuration”}
[2017-11-28T17:11:53,414][INFO][logstash.modules.scaffold] Initializing module {:module_name=>”netflow”, :directory=>”/usr/myapp/logstash/modules/netflow/configuration”}
[2017-11-28T17:11:54,063][INFO][logstash.outputs.elasticsearch] Elasticsearch pool URLs updated {:changes=>{:removed=>[], :added=>[http://127.0.0.1:9200/]}}
[2017-11-28T17:11:54,066][INFO][logstash.outputs.elasticsearch] Running health check to see if an Elasticsearch connection is working {:healthcheck_url=>http://127.0.0.1:9200/, :path=>”/”}
[2017-11-28T17:11:54,199][WARN][logstash.outputs.elasticsearch] Restored connection to ES instance {:url=>”http://127.0.0.1:9200/”}
[2017-11-28T17:11:54,244][INFO][logstash.outputs.elasticsearch] Using mapping template from {:path=>nil}
[2017-11-28T17:11:54,247][INFO][logstash.outputs.elasticsearch] Attempting to install template {:manage_template=>{“template”=>”logstash-*”, “version”=>50001, “settings”=>{“index.refresh_interval”=>”5s”}, “mappings”=>{“_default_”=>{“_all”=>{“enabled”=>true, “norms”=>false}, “dynamic_templates”=>[{“message_field”=>{“path_match”=>”message”, “match_mapping_type”=>”string”, “mapping”=>{“type”=>”text”, “norms”=>false}}}, {“string_fields”=>{“match”=>”*”, “match_mapping_type”=>”string”, “mapping”=>{“type”=>”text”, “norms”=>false, “fields”=>{“keyword”=>{“type”=>”keyword”, “ignore_above”=>256}}}}}], “properties”=>{“@timestamp”=>{“type”=>”date”, “include_in_all”=>false}, “@version”=>{“type”=>”keyword”, “include_in_all”=>false}, “geoip”=>{“dynamic”=>true, “properties”=>{“ip”=>{“type”=>”ip”}, “location”=>{“type”=>”geo_point”}, “latitude”=>{“type”=>”half_float”}, “longitude”=>{“type”=>”half_float”}}}}}}}}
[2017-11-28T17:11:54,265][INFO][logstash.outputs.elasticsearch] New Elasticsearch output {:class=>”LogStash::Outputs::ElasticSearch”, :hosts=>[“//127.0.0.1”]}
[2017-11-28T17:11:54,266][INFO][logstash.pipeline] Starting pipeline {“id”=>”main”, “pipeline.workers”=>2, “pipeline.batch.size”=>125, “pipeline.batch.delay”=>5, “pipeline.max_inflight”=>250}
[2017-11-28T17:11:54,427][INFO][logstash.pipeline] Pipeline main started
[2017-11-28T17:11:54,493][INFO][logstash.agent] Successfully started Logstash API endpoint {:port=>9600}

3. elasticSearch 

这个其实也是 ELK 中的核心,启动的时候一定要注意,因为 es 不可以进行 root 账户启动,所以你还需要开启一个 elsearch 账户。

groupadd elsearch                  #新建 elsearch 组
useradd elsearch -g elsearch -p elasticsearch  #新建一个 elsearch 用户
chown -R elsearch:elsearch  ./elasticsearch    #指定 elasticsearch 所属 elsearch 组

接下来我们默认启动就好了,什么也不用配置,然后在日志中大概可以看到开启了 9200,9300 端口。

[elsearch@slave1 bin]$ ./elasticsearch
[2017-11-28T17:19:36,893][INFO][o.e.n.Node] [] initializing …
[2017-11-28T17:19:36,973][INFO][o.e.e.NodeEnvironment] [0bC8MSi] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [17.9gb], net total_space [27.6gb], spins? [unknown], types [rootfs]
[2017-11-28T17:19:36,974][INFO][o.e.e.NodeEnvironment] [0bC8MSi] heap size [1.9gb], compressed ordinary object pointers [true]
[2017-11-28T17:19:36,982][INFO][o.e.n.Node] node name [0bC8MSi] derived from node ID [0bC8MSi_SUywaqz_Zl-MFA]; set [node.name] to override
[2017-11-28T17:19:36,982][INFO][o.e.n.Node] version[5.6.4], pid[12592], build[8bbedf5/2017-10-31T18:55:38.105Z], OS[Linux/3.10.0-327.el7.x86_64/amd64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_144/25.144-b01]
[2017-11-28T17:19:36,982][INFO][o.e.n.Node] JVM arguments [-Xms2g, -Xmx2g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionSUSECanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/usr/myapp/elasticsearch]
[2017-11-28T17:19:37,780][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [aggs-matrix-stats]
[2017-11-28T17:19:37,780][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [ingest-common]
[2017-11-28T17:19:37,780][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [lang-expression]
[2017-11-28T17:19:37,780][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [lang-groovy]
[2017-11-28T17:19:37,780][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [lang-mustache]
[2017-11-28T17:19:37,780][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [lang-painless]
[2017-11-28T17:19:37,780][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [parent-join]
[2017-11-28T17:19:37,780][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [percolator]
[2017-11-28T17:19:37,781][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [reindex]
[2017-11-28T17:19:37,781][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [transport-netty3]
[2017-11-28T17:19:37,781][INFO][o.e.p.PluginsService] [0bC8MSi] loaded module [transport-netty4]
[2017-11-28T17:19:37,781][INFO][o.e.p.PluginsService] [0bC8MSi] no plugins loaded
[2017-11-28T17:19:39,782][INFO][o.e.d.DiscoveryModule] [0bC8MSi] using discovery type [zen]
[2017-11-28T17:19:40,409][INFO][o.e.n.Node] initialized
[2017-11-28T17:19:40,409][INFO][o.e.n.Node] [0bC8MSi] starting …
[2017-11-28T17:19:40,539][INFO][o.e.t.TransportService] [0bC8MSi] publish_address {192.168.23.151:9300}, bound_addresses {[::]:9300}
[2017-11-28T17:19:40,549][INFO][o.e.b.BootstrapChecks] [0bC8MSi] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
[2017-11-28T17:19:43,638][INFO][o.e.c.s.ClusterService] [0bC8MSi] new_master {0bC8MSi}{0bC8MSi_SUywaqz_Zl-MFA}{xcbC53RVSHajdLop7sdhpA}{192.168.23.151}{192.168.23.151:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2017-11-28T17:19:43,732][INFO][o.e.h.n.Netty4HttpServerTransport] [0bC8MSi] publish_address {192.168.23.151:9200}, bound_addresses {[::]:9200}
[2017-11-28T17:19:43,733][INFO][o.e.n.Node] [0bC8MSi] started
[2017-11-28T17:19:43,860][INFO][o.e.g.GatewayService] [0bC8MSi] recovered [1] indices into cluster_state
[2017-11-28T17:19:44,035][INFO][o.e.c.r.a.AllocationService] [0bC8MSi] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[.kibana][0]] …]).

4. kibana

它的配置也非常简单,你需要在 kibana.yml 文件中指定一下你需要读取的 elasticSearch 地址和可供外网访问的 bind 地址就可以了。

[root@slave1 config]# pwd
/usr/myapp/kibana/config

[root@slave1 config]# vim kibana.yml

elasticsearch.url: “http://localhost:9200”
server.host: 0.0.0.0

然后就是启动,从日志中可以看出,当前开了 5601 端口。

[root@slave1 kibana]# cd bin
[root@slave1 bin]# ls
kibana  kibana-plugin  nohup.out
[root@slave1 bin]# ./kibana
  log  [01:23:27.650] [info][status][plugin:kibana@5.2.0] Status changed from uninitialized to green – Ready
  log  [01:23:27.748] [info][status][plugin:elasticsearch@5.2.0] Status changed from uninitialized to yellow – Waiting for Elasticsearch
  log  [01:23:27.786] [info][status][plugin:console@5.2.0] Status changed from uninitialized to green – Ready
  log  [01:23:27.794] [warning] You’re running Kibana 5.2.0 with some different versions of Elasticsearch. Update Kibana or Elasticsearch to the same version to prevent compatibility issues: v5.6.4 @ 192.168.23.151:9200 (192.168.23.151)
  log  [01:23:27.811] [info][status][plugin:elasticsearch@5.2.0] Status changed from yellow to green – Kibana index ready
  log  [01:23:28.250] [info][status][plugin:timelion@5.2.0] Status changed from uninitialized to green – Ready
  log  [01:23:28.255] [info][listening] Server running at http://0.0.0.0:5601
  log  [01:23:28.259] [info][status][ui settings] Status changed from uninitialized to green – Ready

5. 浏览器中输入:http://192.168.23.151:5601/ 你就可以打开 kibana 页面了,,默认让我指定一个查看的 Index。

通过 ELK 快速搭建集中化日志平台

接下来我们在本机的 /logs 文件夹下创建一个简单的 1.log 文件,内容为“hello world”,然后在 kibana 上将 logstash-*  改成 log*,Create 按钮就会自动出来。

[root@slave1 logs]# echo ‘hello world’ > 1.log

通过 ELK 快速搭建集中化日志平台

进入之后,点击 Discover,你就可以找到你输入的内容啦~~~~ 是不是很帅气。。。

通过 ELK 快速搭建集中化日志平台

如果你装了 head 安装包,你还可以看到它确实带了日期模式的 Index 索引,还自带 5 个默认分片数。

通过 ELK 快速搭建集中化日志平台

好了,本篇就说这么多吧,希望对你有帮助。

更多 ELK 相关教程见以下内容

基于 CentOS 6.9 搭建 ELK 环境指南  https://www.linuxidc.com/Linux/2017-07/145636.htm
Linux 日志分析 ELK 环境搭建  https://www.linuxidc.com/Linux/2017-07/145494.htm
Logstash 监控日志文件时应对日志文件名改变的原理  https://www.linuxidc.com/Linux/2016-08/133994.htm
使用 Elasticsearch + Logstash + Kibana 搭建日志集中分析平台实践  https://www.linuxidc.com/Linux/2015-12/126587.htm
ELK 日志管理平台搭建教程  https://www.linuxidc.com/Linux/2018-07/153136.htm
CenttOS 6.6 下部署 ELK 日志告警系统 https://www.linuxidc.com/Linux/2018-05/152192.htm
CentOS 7 单机部署 ELK  https://www.linuxidc.com/Linux/2017-11/148222.htm
CentOS 安装 Logstash 2.2.0 和 Elasticsearch 2.2.0  https://www.linuxidc.com/Linux/2016-03/128794.htm

ELK 日志管理平台搭建教程  https://www.linuxidc.com/Linux/2018-07/153136.htm

ElasticSearch 的详细介绍:请点这里
ElasticSearch 的下载地址:请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计10615字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中