ELK的轻量级搭建使用

作者:邓聪聪

  一般我们需要进行日志分析场景:直接在日志文件中 grep、awk 就可以获得自己想要的信息。但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档、文本搜索太慢怎么办、如何多维度查询。需要集中化的日志管理,所有服务器上的日志收集汇总。常见解决思路是建立集中式日志收集系统,将所有节点上的日志统一收集,管理,访问。

一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块,构建一套集中式日志系统,可以提高定位问题的效率。

一个完整的集中式日志系统,需要包含以下几个主要特点:

  • 收集-能够采集多种来源的日志数据
  • 传输-能够稳定的把日志数据传输到中央系统
  • 存储-如何存储日志数据
  • 分析-可以支持 UI 分析
  • 警告-能够提供错误报告,监控机制

ELK提供了一整套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用。目前主流的一种日志系统。

ELK简介:

ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。

Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。

Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。

Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。

Filebeat隶属于Beats。目前Beats包含四种工具:

    1. Packetbeat(搜集网络流量数据)
    2. Topbeat(搜集系统、进程和文件系统级别的 CPU 和内存使用情况等数据)
    3. Filebeat(搜集文件数据)
    4. Winlogbeat(搜集 Windows 事件日志数据 )

官方文档:

Filebeat:

https://www.elastic.co/cn/products/beats/filebeat
https://www.elastic.co/guide/en/beats/filebeat/5.6/index.html

Logstash:
https://www.elastic.co/cn/products/logstash
https://www.elastic.co/guide/en/logstash/5.6/index.html

Kibana:

https://www.elastic.co/cn/products/kibana

https://www.elastic.co/guide/en/kibana/5.5/index.html

Elasticsearch:
https://www.elastic.co/cn/products/elasticsearch
https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index.html

elasticsearch中文社区:
https://elasticsearch.cn/

  elasticsearch、Logstash均需要jdk支持,故若服务器上没有,需要先安装JDK,5.x级以上版本均需要jdk1.8的支持

安装JDK,并设置JDK的环境变量 

drwxr-xr-x. 8   10  143       255 Jun 17  2014 jdk1.8.0_11
-rw-r--r--. 1 root root 159019376 Jun  6 11:58 jdk-8u11-linux-x64.tar.gz
[root@test_server java]# cd jdk1.8.0_11/
[root@test_server jdk1.8.0_11]# ll
total 25428
drwxr-xr-x. 2 10 143     4096 Jun 17  2014 bin
-r--r--r--. 1 10 143     3244 Jun 17  2014 COPYRIGHT
drwxr-xr-x. 4 10 143      122 Jun 17  2014 db
drwxr-xr-x. 3 10 143      132 Jun 17  2014 include
-rw-r--r--. 1 10 143  4673670 Jun 17  2014 javafx-src.zip
drwxr-xr-x. 5 10 143      185 Jun 17  2014 jre
drwxr-xr-x. 5 10 143      225 Jun 17  2014 lib
-r--r--r--. 1 10 143       40 Jun 17  2014 LICENSE
drwxr-xr-x. 4 10 143       47 Jun 17  2014 man
-r--r--r--. 1 10 143      159 Jun 17  2014 README.html
-rw-r--r--. 1 10 143      525 Jun 17  2014 release
-rw-r--r--. 1 10 143 21047086 Jun 17  2014 src.zip
-rw-r--r--. 1 10 143   110114 Jun 17  2014 THIRDPARTYLICENSEREADME-JAVAFX.txt
-r--r--r--. 1 10 143   178445 Jun 17  2014 THIRDPARTYLICENSEREADME.txt
[root@test_server jdk1.8.0_11]# 

jdk环境设置:

[root@test_server jdk1.8.0_11]# tail -f /etc/profile
done

unset i
unset -f pathmunge
JAVA_HOME=/usr/local/java/jdk1.8.0_11
JRE_HOME=${JAVA_HOME}/jre
CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME CLASSPATH JRE_HOME
ulimit -u 4096

验证版本信息,确认是否安装成功

[root@test_server jdk1.8.0_11]# java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
[root@test_server jdk1.8.0_11]# 

安装ELK软件,主要有Elasticsearch,Logstash,Kibana三个软件,安装时要保证版本的一致性

[root@test_server elk]# ll
total 89284
drwxr-xr-x  9 elk elk      155 Jun  7 20:53 elasticsearch-6.3.0
-rw-r--r--  1 elk elk 91423553 Jun  6 16:46 elasticsearch-6.3.0.tar.gz
drwxr-xr-x 11 elk elk      229 Jun  6 18:05 kibana-6.3.0-linux-x86_64
drwxr-xr-x 14 elk elk      321 Jun  7 00:32 logstash-6.3.0

创建ELK运行用户

[root@test_server]# groupadd elk
[root@test_server]# groupadd elk
#软件存放及启动的目录
[root@test_server ]# mkdir /elk
[root@test_server ]# chown -R elk:elk /elk
#关闭seliunx
[root@test_server ]# getenforce 
Disabled

配置配置Elasticsearch,并启动

以下为我的修改项:
[root@test_server config]# cat elasticsearch.yml|grep -v ^"#"
cluster.name: my-application
node.name: node-1
network.host: x.x.x.x
http.port: 9200
#启动 elasticsearch放后台运行任务
/elk/elasticsearch-6.3.0/bin/elasticsearch &
#查看是否运行成功
[root@test_server config]# ss -ntl
State      Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
LISTEN     0      128                             *:10022                                       *:*                  
LISTEN     0      128                             *:80                                          *:*                  
LISTEN     0      100                     127.0.0.1:25                                          *:*                  
LISTEN     0      128                  118.188.20.5:5601                                        *:*                  
LISTEN     0      128                            :::10022                                      :::*                  
LISTEN     0      128           ::ffff:x.x.x.x:9200 //运行端口                                       :::*                  
LISTEN     0      128           ::ffff:x.x.x.x:9300 //运行端口                                       :::*                  
LISTEN     0      100                           ::1:25                                         :::*                  
LISTEN     0      50               ::ffff:127.0.0.1:9600                                       :::*                  
[root@test_server config]# 

配置logstash

[root@test_server config]# cat logstash.yml|grep -v ^"#"
path.data: /elk/logstash-6.3.0/data
path.config: /elk/logstash-6.3.0/config
#启动
[root@test_server config]# /elk/logstash-6.3.0/bin/logstash -f /elk/logstash-6.3.0/config/yourfile.conf &

logstash的日志收集文件配置,这个我syslog的配置参考

[root@test_server config]# cat syslog.conf 
input {
  file {
    path => "/var/log/boot.log"          
    start_position => "beginning"
    type => "test" 
  }
}

filter {
    grok {
      match => { "message" => "(?:%{SYSLOGTIMESTAMP:timestamp}|%{TIMESTAMP_ISO8601:timestamp8601}) (?:%{SYSLOGFACILITY} )?%{SYSLOGHOST:logsource}+(?: %{SYSLOGPROG}:|)" }
  }
}

output {
  elasticsearch {
    hosts => "x.x.x.x:9200"
    index => "blog2"
        document_type => "test"
  }
    stdout { codec => rubydebug }  //屏幕输出
}

配置kibana,以下是我的修改项

[root@test_server elk]# cat kibana-6.3.0-linux-x86_64/config/kibana.yml|grep -Ev ^"#|^$"
server.port: 5601
server.host: "x.x.x.x"
elasticsearch.url: "http://x.x.x.x:9200"
#启动kinaba
[root@test_server elk]# /elk/kibana-6.3.0-linux-x86_64/bin/kibana &

调用nginx代理的配置,使其具有验证功能,在nginx的配置文件添加以下内容

        include /etc/nginx/default.d/*.conf;
        location / {
            auth_basic "secret";
            auth_basic_user_file /etc/nginx/passwd.db;
            proxy_pass http://x.x.x.x:5601;
            proxy_set_header Host $host:5601;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Via "nginx";
        }

猜你喜欢

转载自www.cnblogs.com/dengcongcong/p/10990780.html