您的位置:首页 > 其它

快速搭建ELK集中化日志管理平台

2018-09-19 22:36 316 查看

     由于我们的项目是分布式,服务分布于多个服务器上,每次查看日志都要登录不同服务器查看,而且查看起来还比较麻烦,老大让搭一个集中化日志管理的东西,然后就在网上找到了这个东西ELK

ELK就是elastic公司下的三款产品,elasticsearch,logstash,kibana  

   官网:https://www.elastic.co/cn/products

1.我先解释一下这三个产品的功能

   1: elasticsearch  (ELK核心)

          这是一个Lucene的分布式全搜索框架,可以对日志进行分布式存储

   2:logstash

          它的功能是分布于各个服务器上做日志的收集,比如192.168.1.45上配置了logstash那么他会自动收集你该服务器上的日志传输给elasticsearch

   3: kibana

         它可以把elasticsearch中的数据进行报表形式的展示

   

      知道了这些以后我们就已经大体了解到ELK的大体工作流程了


2.快速搭建(在这里我全都搭建在一台服务器上了)


从上图中我们都能看出elasticsearch是核心的一个东西,我们先从它这里开始配置

1.elasticsearch配置

        elasticsearch是ELK的核心,而且一定要注意他不能使用root账户启动,所以我们直接新创建一个账户来配置elasticsearch,

       首先用useradd 命令创建一个账户  比如我创建的账户是elsearch

   直接用elsearch账户登录,然后去官网下载wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.1.tar.gz

1 1 [elsearch@9378y7s1qz ~]$ tar zxf elasticsearch-6.4.0.tar.gz
2 2 [elsearch@9378y7s1qz ~]$ cd elasticsearch-6.4.0/conf


接下来需要修改elasticsearch.yml

1  network.host: 0.0.0.0
2  http.port: 9200


这个如果不修改,外网是没法访问的

接下来就是启动了到bin目录下运行 ./elasticsearch

这个时候你就会发现启动不了

   提示最大此用户的最大能使用的线程数太小,最少需要4096这个量级才能启动,可是这玩意在哪修改呢? 每个用户能打开的文件句柄数也和允许此用户能打开的线程进程数有关,Linux上一切皆文件嘛,线程、进程也是以文件句柄书相关的方式控制的 ulimit -n可以查看当前用户能同时使用的文件句柄数限制,当然这玩意我们可以配置,但是我们配置的属于软件限制,每个电脑都有其极限,这个极限基于硬件,如果硬件限制1024,那么我们软件调整到65535也是无济于事的 上网查了查 需要 vim /etc/security/limits.conf 添加或修改

1 * soft nofile 65536
2
3 * hard nofile 131072
4
5 * soft nproc 1024
6
7 * hard nproc 4096

提示1024不足,而文件中只有一个1024:* soft nproc 1024,我们按照要求将其更改为2048,然后重新尝试一下

           还是没法启动,这个时候会

ERROR: [1] bootstrap checks failed [1]:max virtual memory areas vm.max_map_count [1024] is too low, increase to at least [262144]

这个错误需要vim /etc/sysctl.conf添加

vm.max_map_count=262144

然后sysctl -p 一下即可

我们再次启动,这次就正常了,到这里我们的elasticsearch已经配置好了,接下来我们开始配置logstash


2.logStash配置

  还是跟之前一样先下载logstash

   wget  https://artifacts.elastic.co/downloads/logstash/logstash-6.4.1.tar.gz

   然后解压

   配置的时候我们需要在config下创建一个配置文件logstash.conf

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

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

1 input {
2      file {
3         type => "log"
4         path => "/usr/local/logs/*.log"  #这里是你需要收集的日志
5         start_position => "beginning"
6     }
7 }
8
9 output {
10   stdout {
11    codec => rubydebug { }
12   }
13
14   elasticsearch {
15     hosts => "127.0.0.1"	#elasticsearch地址
16     index => "log-%{+YYYY.MM.dd}"
17   }
18 }

这些配置完成之后就能到/bin下找到logstash 启动脚本了

在启动的时候需要引用刚才我们创建的配置文件

1 [root@iZwz95t3hfncu7anavrafmZ bin]# ./logstash -f ../config/logstash.conf >lostash.log
2 Sending Logstash logs to /usr/local/logstash/logs which is now configured via log4j2.properties
3 [2018-09-19T21:37:07,725][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified
4 [2018-09-19T21:37:08,899][INFO ][logstash.runner          ] Starting Logstash {"logstash.version"=>"6.4.0"}
5 [2018-09-19T21:37:13,749][INFO ][logstash.pipeline        ] Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>1, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50}
6 [2018-09-19T21:37:14,763][INFO ][logstash.outputs.elasticsearch] Elasticsearch pool URLs updated {:changes=>{:removed=>[], :added=>[http://0.0.0.0:9200/]}}
7 [2018-09-19T21:37:14,783][INFO ][logstash.outputs.elasticsearch] Running health check to see if an Elasticsearch connection is working {:healthcheck_url=>http://0.0.0.0:9200/, :path=>"/"}
8 [2018-09-19T21:37:15,125][WARN ][logstash.outputs.elasticsearch] Attempted to resurrect connection to dead ES instance, but got an error. {:url=>"http://47.107.75.26:9200/", :error_type=>LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError, :error=>"Elasticsearch Unreachable: [http://127.0.0.1:9200/][Manticore::SocketException] Connection refused (Connection refused)"}
9 [2018-09-19T21:37:15,194][INFO ][logstash.outputs.elasticsearch] New Elasticsearch output {:class=>"LogStash::Outputs::ElasticSearch", :hosts=>["//47.107.75.26:9200"]}
10 [2018-09-19T21:37:15,756][INFO ][logstash.inputs.file     ] No sincedb_path set, generating one based on the "path" setting {:sincedb_path=>"/usr/local/logstash/data/plugins/inputs/file/.sincedb_71ed980b1f25dc3be65a3d965d78b265", :path=>["/usr/local/logs/*.log"]}
11 [2018-09-19T21:37:15,849][INFO ][logstash.pipeline        ] Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x123f661f run>"}
12 [2018-09-19T21:37:15,956][INFO ][logstash.agent           ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
13 [2018-09-19T21:37:15,986][INFO ][filewatch.observingtail  ] START, creating Discoverer, Watch with file and sincedb collections
14 [2018-09-19T21:37:16,610][INFO ][logstash.agent           ] Successfully started Logstash API endpoint {:port=>9600}
15

在日志中大概可以看到开启了9200,9300端口。现在logstash已经启动了

3.kibana配置

  elasticsearch和logstash已经配置好了,那么我们需要展示就需要kibana了,接下来我们来配置kibana

  还是先下载kibana,

    wget https://artifacts.elastic.co/downloads/kibana/kibana-6.4.1-linux-x86_64.tar.gz

  然后解压,这个依然需要我们在config文件下创建配置文件


1 [root@slave1 config]# vim kibana.yml
2
3 elasticsearch.url: "http://localhost:9200"
4 server.host: 0.0.0.0

然后就是启动bin下的kibana

1 [root@slave1 bin]# ./kibana
2   log   [01:23:27.650] [info][status][plugin:kibana@5.2.0] Status changed from uninitialized to green - Ready
3   log   [01:23:27.748] [info][status][plugin:elasticsearch@5.2.0] Status changed from uninitialized to yellow - Waiting for Elasticsearch
4   log   [01:23:27.786] [info][status][plugin:console@5.2.0] Status changed from uninitialized to green - Ready
5   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)
6   log   [01:23:27.811] [info]
24000
[status][plugin:elasticsearch@5.2.0] Status changed from yellow to green - Kibana index ready
7   log   [01:23:28.250] [info][status][plugin:timelion@5.2.0] Status changed from uninitialized to green - Ready
8   log   [01:23:28.255] [info][listening] Server running at http://0.0.0.0:5601
9   log   [01:23:28.259] [info][status][ui settings] Status changed from uninitialized to green – Ready


4.现在我们已经全部搭建好了

剩下的就是访问了,来看看我的页面


OK,我们现在ELK已经成功搭建了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: