centos 7 build ELK log analysis system

A, ELK composition

ELK by the ElasticSearch, Logstash and Kiabana three open source tools, whose official website is https://www.elastic.co/cn

  • Elasticsearch: real-time analysis is an open source distributed search engine, built on the library Apache Lucens on the basis of full-text search engine, while hiding the complexity of the Apache Luces. Elasticsearch all the features packed into a separate service, and provides a simple RESTful API interfaces, it has distributed, zero-configuration, auto-discovery, auto-slice index, index replication mechanism, RESTful style interfaces, multiple data sources, automatic search load characteristics;

  • Logstash: is a fully open source tools, mainly for log collection, while the data processing, and outputs the elasticsearch;

  • Kibana: is an open source and free tools, Kibana can provide a graphical log analysis is Logstash and Elasticsearch Web interface, can aggregate, analyze and search for important data logs;

1, the working principle ELK as shown below:

centos 7 build ELK log analysis system
Logstash collected APPServer generated Log, and place the Elasticsearch cluster, and the cluster Kibana query data from the ES to generate charts, return to the Browser.

In simple terms, a log processing and analysis, generally need to go through the following steps:

  • The centralized log management;

  • Log format (Logstash) and output to elasticsearch;

  • Formatted data indexing and storing (elasticsearch);

  • Data show the front end (Kibana);

2, Elasticsearch DESCRIPTION

Elasticsearch是一个基于Lucene的搜索服务器,它稳定、可靠、快速,而且具有比较好的水平扩展能力、为分布式环境设计、在云计算中被广泛应用。Elasticsearch提供了一个分布式多用户能力的全文搜索引擎,基于RESTful Web接口。通过该接口,用户可以通过浏览器和Elasticsearch通信。Elasticsearch使用Java开发的,并作为Apache许可条款下的开放源码分布。Wikipedia、Stack、Overflow、GitHub等基于Elasticsearch来构建搜索引擎,所以具有实时搜索、稳定、可靠、快速、安装使用方便等特点。

3、Elasticsearch的基础核心概念

  • 接近实时(NRT):Elasticsearch是一个搜索速度接近实时的搜索凭条,相应速度非常快,从搜索一个文档直到这个文档能够被搜索到只有一个轻微的延迟(一般情况下是1s);

  • 群集(cluster):群集就是由一个或多个节点组织在一起,在所有的节点上存放用户数据,并一起提供索引和搜索功能,通过选举产生主节点,并提供跨节点的联合索引和搜索的功能。每个群集都有一个标示的名称,默认是Elasticsearch,每个节点是居于群集名字加入到其群集中的,一个群集可以只有一个节点,为了具备更好的容错性,通常配置多个节点,在配置群集时,建议配置为群集模式;

  • 节点(node):是指一台单一的服务器,多个节点组织为一个群集,每个节点都存储数据并参与群集的索引和搜索功能。和群集一样,节点也是通过名字来标识的,默认情况下,在节点启动时会随机分配字符名,也可自定义。通过指定群集名称,节点可以加入到群集中。默认情况下,每个节点都可以加入Elasticsearch群集。如果群集中有多个节点,它们将会自动组建一个名为Elasticsearch的群集;

  • 索引(Index):类似于关系型数据库的中的“库”。当索引一个文档后,就可以使用Elasticsearch搜索到该文档,也可以简单地将索引理解为存储数据的地方,可以方便地进行全文索引。在index下面包含存储数据库的类型(Type),TYPE类似于关系型数据库中的“表”,用来存放具体数据,而Type下面包含文档(Document),文档相当Yui关系型数据库的“记录”,一个文档是一个可被索引的基础信息单元;

  • 分片和副本:Elasticsearch将索引分为若干部分,每个部分称为一个分片,每个分片就是一个全功能的独立的索引。分片的数量一般在索引创建前指定,且创建索引后不能更改;分片的两个最主要的原因:

  • 水平分割扩展,增大存储量;

  • 分布式并行跨分片操作,提高性能和吞吐量;
    一个好的数据存储方案要求无论何种故障(如节点不可用)下数据都可用,并且具有较高的存储效率。为此,Elasticsearch将索引分片复制一份或多份,称为副本。副本是索引的另一个备份,用于数据冗余以及负载分担。默认情况下Elasticsearch自动对索引请求进行负载分担。

总之,索引可以将分为若干个分片。这些分片也可以被复制0次(没有复制)或多次,当有副本存在是,作为复制源的分片称为主分片,而作为复制目标的分片称为复制分片。分片和副本的数量可以在索引创建时指定。在索引创建后,可以改变副本的数量,但是不能改变分片的数量。默认情况,Elasticsearch中的每个索引被分片为5个主分片和1个副本。在两个节点的场景下,每个索引都将会有5个主分片和另外5个副本分片,每个索引总共就有10个分片。

4、Logstash介绍

Logstash有JRuby语言编写,运行在Java虚拟机(JVM)上,是一款强大的数据处理工具,可以实现数据传输、格式处理、格式化输出。Logstash具有强大的插件功能,常用于日志处理。Logstash可配置单一的代理端,与其他开源软件结合,实现不同的功能。

Logstash的理念很简单,它只做三件事:数据输入、数据加工(如过滤、改写等)以及数据输出。通过组合输入和输出,可以实现锁种需求。Logstash处理日志时,典型的部署架构图如下:
centos 7 build ELK log analysis system

Logstash的主要组件:

  • Shipper:日志收集者。负责监控本地日志文件的变化,及时收集最新的日志文件内容。通常,远程代理端(agent)只需要运行这个组件即可;

  • Indexer:日志存储者。负责接收日志并写入到本地文件;

  • Broker:日志Hub。负责连接多个Shipper和Indexer;

  • Search and Stronage:允许对事件进行搜索和存储;

  • Web Interface:基于Web的展示界面;

正是由于以上组件在Logstash架构中可独立部署,才提供了更好的群集扩展性。
Logstash使用管道方式进行日志的搜集处理和输出,优点类似Linux系统的管道命令,将前一个流程的处理结构发送到后一个流程继续处理。在Logstash中,包括了三个阶段,分别是输入(input)、处理(Filter,非必需)和输出(output),三者的关系如图:
centos 7 build ELK log analysis systemcentos 7 build ELK log analysis system
如图中,整个流程为Input收集数据,Filter处理数据、Output输出数据。每个阶段也可以指定多种方式,如输出既可以输出到Elasticsearch中,也可以指定到stdout在控制台打印。这种插件式的组织方式,使得Logstash的扩展和定制非常方便。

5、Kibana介绍

Kibana是一个针对Elasticsearch的开源分析及可视化平台,主要设计用来和Elasticsearch一起工作,可以搜索、查看存储在Elasticsearch索引中的数据,并通过各种图表进行高级数据分析和展示。Kibana可以让数据看起来一目了然。它的操作十分简单,基于浏览器的用户界面可以让用户在任何位置都可以实时浏览。Kibana可以快速创建仪表板实时显示查询动态。Kibana使用非常简单,只需添加索引就可以检测Elasticsearch索引数据。

1)Kibana的主要功能

  • Elasticsearch无缝之集成:Kibana架构是为了Elasticsearch定制的,可以将任何(结构化和非结构化)数据加入Elasticsearch索引。Kibana还充分李永乐Elasticsearch强大的搜索和分析功能;

  • 整合数据:Kibana可以让海量数量变得更容易理解,根据数据内容可以创建形象的柱形图、折线图、散点图、直方图、饼图和地图等,以便用户查看;

  • 复杂数据分析:Kibana提升了Elasticsearch的分析能力,能够更加智能地分析数据,执行数据转换并且根据要求对数据切割分块;

  • 让更多团队成员受益:强大的数据库可视化接口让各业务岗位都能够从数据集合受益;

  • 接口灵活,分享更容易:使用Kibana可以更加方便地创建、保存、分享数据,并将可视化数据快速交流;

  • 配置简单:Kibana的配置和启用非常简单,用户体验非常友好。Kibana自带Web服务器,可以快速启动运行;

  • 可视化多数据源:Kibana可以非常方便地把来自Logstash、ES-Hadoop、Beats多第三方技术的数据整合到Elasticsearch,支持的第三方技术包括Apache、Flume、Fluentd等;

简单数据导出:Kibana可以方便地导出感兴趣的数据,与其他数据集合并融合后快速建模分析,发现新结果;

二、搭建ELK平台

案例环境如下:
centos 7 build ELK log analysis system

准备安装环境:
准备三台服务器,分别根据上述环境表配置网络参数,我这里是192.168.100.10-30。其次关闭防火墙和Selinux。其中Centos01和Centos02节点分配4GB(>2GB)内存,Apache节点分配1GB内存。此案例自行连接广域网。

本案例实现功能如下:

  • 配置ELK日志分析群集;
  • 使用Logsttash收集日志;
  • 使用Kibana查看分析日志;

本案例用到的所有程序包请访问:https://pan.baidu.com/s/1OK49eAIwbvwIV5swe0-8-w
提取码:yiad

1、centos01基础环境配置

[root@localhost ~]# hostnamectl set-hostname centos01
[root@localhost ~]# bash
[root@centos01 ~]# 
[root@centos01 ~]# vim /etc/hosts
192.168.100.10 centos01
192.168.100.20 centos02
[root@centos01 ~]# java -version
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)
[root@centos01 ~]# vim /etc/sysconfig/selinux 
SELINUX=disabled
[root@centos01 ~]# systemctl stop firewalld
[root@centos01 ~]# reboot

2、centos02基础环境配置

[root@localhost ~]# hostnamectl set-hostname centos02
[root@localhost ~]# bash
[root@centos02 ~]# 
[root@centos02 ~]# vim /etc/hosts
192.168.100.10 centos01
192.168.100.20 centos02
[root@centos02 ~]# java -version
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)
[root@centos02 ~]# vim /etc/sysconfig/selinux 
SELINUX=disabled
[root@centos02 ~]# systemctl stop firewalld
[root@centos02 ~]# reboot

3、centos01安装Elasticsearch软件

[root@centos01 ~]# ls            <!--通过rz命令上传elasticsearch包-->
anaconda-ks.cfg  elasticsearch-5.6.16.rpm  initial-setup-ks.cfg
[root@centos01 ~]# yum -y install elasticsearch-5.6.16.rpm   <!--安装elasticsearch程序-->
[root@centos01 ~]# vim /etc/elasticsearch/elasticsearch.yml <!--修改elasticsearch配置文件-->
cluster.name: ELK        <!--定义群集名称-->
node.name: centos01      <!--定义节点名称-->
network.host: 192.168.100.10          <!--提供服务绑定的IP地址-->
http.port: 9200                                                        <!--服务监听端口为9200-->
discovery.zen.ping.unicast.hosts: ["centos01", "centos02"] <!--群集发现通过单播的方式-->
[root@centos01 ~]# systemctl daemon-reload <!--配置以守护进程方式运行elasticsearch-->
[root@centos01 ~]# /etc/init.d/elasticsearch start       <!--启动elasticsearch服务-->
[root@centos01 ~]# systemctl start elasticsearch        <!--启动elasticsearch服务-->
[root@centos01 ~]# systemctl enable elasticsearch       <!--设置开机自动启动-->
[root@centos01 ~]# netstat -anptu | grep 9200  
            <!--监听elasticsearch服务是否启动成功(需要耐心等待几秒才可以监听到)-->
tcp6       0      0 192.168.100.10:9200     :::*                    LISTEN      1557/java  

4、centos02安装Elasticsearch软件

[root@centos02 ~]# ls            <!--通过rz命令上传elasticsearch包-->
anaconda-ks.cfg  elasticsearch-5.6.16.rpm  initial-setup-ks.cfg
[root@centos02 ~]# yum -y install elasticsearch-5.6.16.rpm   <!--安装elasticsearch程序-->
[root@centos02 ~]# vim /etc/elasticsearch/elasticsearch.yml <!--修改elasticsearch配置文件-->
cluster.name: ELK        <!--定义群集名称-->
node.name: centos02      <!--定义节点名称-->
network.host: 192.168.100.20          <!--提供服务绑定的IP地址-->
http.port: 9200                                                        <!--服务监听端口为9200-->
discovery.zen.ping.unicast.hosts: ["centos01", "centos02"] <!--群集发现通过单播的方式-->
[root@centos02 ~]# systemctl daemon-reload <!--配置以守护进程方式运行elasticsearch-->
[root@centos02 ~]# /etc/init.d/elasticsearch start       <!--启动elasticsearch服务-->
[root@centos02 ~]# systemctl start elasticsearch        <!--启动elasticsearch服务-->
[root@centos02 ~]# systemctl enable elasticsearch       <!--设置开机自动启动-->
[root@centos02 ~]# netstat -anptu | grep 9200  
            <!--监听elasticsearch服务是否启动成功(需要耐心等待几秒才可以监听到)-->
tcp6       0      0 192.168.100.20:9200     :::*                    LISTEN      1557/java  

5、通过客户端访问两个节点

配置客户端IP地址通过浏览器访问centos01
centos 7 build ELK log analysis system

配置客户端IP地址通过浏览器访问centos02
centos 7 build ELK log analysis system

6、安装elasticsearch-head图形化管理ELK工具

1)centos01节点

<!--上传node压缩包,解压缩安装到/usr/local/中 -->
[root@centos01 ~]# ls
anaconda-ks.cfg  elasticsearch-5.6.16.rpm  initial-setup-ks.cfg  node-v4.2.2-linux-x64.tar.gz
[root@centos01 ~]# tar zxvf node-v4.2.2-linux-x64.tar.gz -C /usr/local/   <!---->
[root@centos01 local]# mv node-v4.2.2-linux-x64/ node   <!--修改名字为node-->
[root@centos01 local]# ln -s /usr/local/node/bin/npm /usr/local/bin/npm<!--连接管理命令-->
[root@centos01 local]# ln -s /usr/local/node/bin/node /usr/local/bin/node<!--连接管理命令-->
[root@centos01 ~]# vim /etc/profile       <!--配置环境变量加载node-->
export NODE_HOME=/usr/local/node
export PATH=$PATH:$NODE_HOME/bin
export BODE_PATH=$NODE_HOME/lib/node_modules/
[root@centos01 ~]# source /etc/profile  <!--刷新环境变量-->
[root@centos01 ~]# vim /etc/elasticsearch/elasticsearch.yml   
                   <!--修改elasticsearch连接elasticsearch-head-->
http.cors.enabled: true 
http.cors.allow-origin: "*" 

<!--接下来安装elasticsearch-head-->

[root@centos01 ~]# git clone git://github.com/mobz/elasticsearch-head.git   <!--配置安装源-->
[root@centos01 ~]# ls
anaconda-ks.cfg  elasticsearch-5.6.16.rpm  elasticsearch-head  initial-setup-ks.cfg  node-v4.2.2-linux-x64.tar.gz
[root@centos01 ~]# mv elasticsearch-head /usr/local/<!--修改elasticsearch-head安装位置-->
[root@centos01 ~]# cd /usr/local/elasticsearch-head/
[root@centos01 elasticsearch-head]# npm install -g grunt-cli    <!--安装grunt-cli程序-->
[root@centos01 elasticsearch-head]# grunt -version    <!--检查grunt是否安装成功-->
grunt-cli v1.3.2
[root@centos01 ~]# vim /usr/local/elasticsearch-head/Gruntfile.js    
                       <!--修改elasticsearch-head的Gruntfile.js配置文件-->
99                                         keepalive: true, 
100                                         hostname: "*  
[root@centos01 ~]# vim /usr/local/elasticsearch-head/_site/app.js   
                         <!--修改elasticsearch-head的app.js配置文件-->
4374                         this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") 
|| "http://192.168.100.10:9200";    <!--将http后面的localhost改为此服务器IP地址-->
<!--接下来开始安装npm-->
[root@centos01 ~]# cd /usr/local/elasticsearch-head/    <!---->
[root@centos01 elasticsearch-head]# npm install     <!--安装npm -->
[root@centos01 elasticsearch-head]# grunt server&<!--启动grunt服务,将服务运行在后台-->
[root@centos01 ~]# /etc/init.d/elasticsearch restart    <!--重启elasticsearch服务-->
[root@centos01 ~]# netstat -anptu | grep 9200    <!--监听elasticsearch服务是否启动-->
tcp6       0      0 192.168.100.10:9200     :::*                    LISTEN      1557/java           
[root@centos01 ~]# netstat -anptu | grep 9100    <!--监听grunt服务是否启动-->
tcp6       0      0 :::9100                 :::*                    LISTEN      3400/grunt

centos02节点和centos01节点配置相同,就是改一下IP地址即可,参考centos01节点自行配置即可。

现在就可以通过浏览器访问http://192.168.100.10:9100 去查看群集信息了
centos 7 build ELK log analysis system

7、安装logstash

<!--上传logstash压缩包到服务器 -->
[root@centos01 ~]# ls
anaconda-ks.cfg  elasticsearch-5.6.16.rpm  initial-setup-ks.cfg  logstash-5.5.1.rpm  node-v4.2.2-linux-x64.tar.gz
[root@centos01 ~]# rpm -ivh logstash-5.5.1.rpm    <!--安装logstash-->
[root@centos01 ~]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/<!--优化安装命令-->
[root@centos01 ~]# mkdir -p /usr/share/logstash/config   <!--创建存储数据目录-->
[root@centos01 ~]# ln -s /etc/logstash/* /usr/share/logstash/config/<!--配置存储数据目录-->
[root@centos01 ~]# systemctl start logstash    <!--启动logstash服务-->
[root@centos01 ~]# systemctl enable logstash   <!--设置开机自动启动-->
[root@centos01 ~]# logstash -e 'input { stdin {} } output { stdout {} }'   
             <!--写入测试数据,测试日志服务器-->
The stdin plugin is now waiting for input:
www.baidu.com         <!--自行输入,完成后强制退出-->
2019-12-19T07:44:26.487Z centos01 www.baidu.com

[root@centos01 ~]# logstash -e 'input { stdin {} } output { stdout { codec=> rubydebug } }' 
<!--写入测试数据,测试日志服务器-->
The stdin plugin is now waiting for input:
www.baidu.com   <!--自行输入,完成后强制退出-->
{
    "@timestamp" => 2019-12-19T07:48:34.006Z,
      "@version" => "1",
          "host" => "centos01",
       "message" => "www.baidu.com"
}

[root@centos01 ~]# logstash -e 'input { stdin {} } output { elasticsearch { hosts =>
["192.168.100.10:9200"] } }'  <!--写入测试数据,测试日志服务器-->
The stdin plugin is now waiting for input:
www.baidu.com  <!--自行输入,完成后强制退出-->

centos02和centos01是相同配置,请自行配置。

接下来通过客户端浏览器访问9100查看日志
centos 7 build ELK log analysis system

centos 7 build ELK log analysis system

8、安装kibana

<!--上传kibana-5.5.1-x86_64.rpm包到服务器-->
[root@centos01 ~]# ls
anaconda-ks.cfg           initial-setup-ks.cfg     logstash-5.5.1.rpm
elasticsearch-5.6.16.rpm  kibana-5.5.1-x86_64.rpm  node-v4.2.2-linux-x64.tar.gz
[root@centos01 ~]# rpm -ivh kibana-5.5.1-x86_64.rpm   <!--安装kibana-->
[root@centos01 ~]# vim /etc/kibana/kibana.yml  <!--修改kibana主配置文件-->
server.port: 5601   <!--服务监听的端口号-->
server.host: "0.0.0.0"   <!--允许所有网段-->
elasticsearch.url: "http://192.168.100.10:9200"   <!--连接elasticsearch的url-->
[root@centos01 ~]# systemctl start kibana   <!--启动kibana服务-->
[root@centos01 ~]# systemctl enable kibana   <!--设置开机自动启动-->

centos02和上面相同配置,将连接elasticsearch的url的IP地址改为centos02的IP地址即可,自行配置。

现在可以通过客户端浏览器访问5601端口
centos 7 build ELK log analysis system

9、配置监控网站服务器

[root@centos03 ~]# yum -y install httpd   <!--安装httpd-->
[root@centos03 ~]# systemctl start httpd    <!--启动服务-->
[root@centos03 ~]# systemctl enable httpd   <!--设置开机自动启动-->

<!--上传logstash到网站服务器,安装logstash-->

[root@centos03 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  logstash-5.5.1.rpm  
[root@centos03 ~]# rpm -ivh logstash-5.5.1.rpm   <!--安装logstash-->
[root@centos03 ~]# vim /etc/logstash/conf.d/apache_error.conf <!--配置监控网站的错误日志-->
input {
        file {
                path => "/var/log/httpd/error_log"
                type => "error"
                start_position => "beginning"
                }
}
output {
        if [type] == "error" {
                elasticsearch {
                        hosts => ["192.168.100.10:9200"]
                        index => "apache_error-%{+YYYY.MM.dd}"
                        }
                }
}  
[root@centos03 ~]# systemctl start logstash.service    <!--启动logstash服务-->
[root@centos03 ~]# systemctl enable logstash.service  <!--设置开机自动启动-->
[root@centos03 ~]# /usr/share/logstash/bin/logstash -f 
/etc/logstash/conf.d/apache_error.conf   <!--启动监控网站服务器-->

1)现在可以通过浏览器访问验证是否监控成功

centos 7 build ELK log analysis system

2)查看监控网站服务器的日志

centos 7 build ELK log analysis system

3)重启网站服务器和监控服务;浏览器再次查看监控网站服务器的日志

centos 7 build ELK log analysis system
自行在kibana服务器添加索引。

------ This concludes the article, thanks for reading ------

Guess you like

Origin blog.51cto.com/14156658/2460844