ELK日志分析平台搭建和使用

ELK介绍

ELK是三个开源工具组成,简单解释如下:

  • Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
  • Logstash是一个完全开源的工具,它可以对你的日志进行收集、过滤,并将其存储供以后使用(如,搜索)。
  • Kibana 也是一个开源和免费的工具,它可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。

架构图

系统环境

[root@n4 ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@n4 ~]# uname -r
3.10.0-862.el7.x86_64
[root@n4 ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@n4 ~]# getenforce
Disabled

节点安排

n1 192.168.231.60 apache           客户端       httpd、filebeat
n2 192.168.231.61 nginx            客户端       nginx、filebeat
n3 192.168.231.62 logstash         日志处理分析  logstash、filebeat
n4 192.168.231.63 elasticsearch    存储数据      elasticsearch head
n6 192.168.231.65 elasticsearch    存储数据      elasticsearch  
n5 192.168.231.64 kibana           查询数据      kibana

安装elasticsearch

安装java环境

elasticsearch节点都需安装

[root@n3 ~]# java -version

java version "1.8.0_191"

Java(TM) SE Runtime Environment (build 1.8.0_191-b12)

Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

解压安装包

[root@n4 ~]# tar xf  elasticsearch-6.0.0.tar.gz

[root@n4 ~]# mv elasticsearch-6.0.0 /usr/local/elasticsearch

对配置文件进行备份(修改配置文件前最好进行备份)

[root@n4 ~]# cd /usr/local/elasticsearch/config/

[root@n4 config]# cp elasticsearch.yml elasticsearch.yml.bak

修改elasticsearch配置文件

[root@n4 config]# cat elasticsearch.yml | grep -v "^$" | grep -v "^#"
cluster.name: my-es-cluster  #集群名称
node.name: 192.168.231.63 #本node节点的名称
path.data: /usr/local/elasticsearch/data #设置索引数据的存储路径
path.logs: /usr/local/elasticsearch/logs #日志存放路径
network.host: 192.168.231.63  #服务监听的IP地址
node.master: true #是否可以为主节点。
node.data: true #这个属性表示节点是否存储数据。
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.231.63","192.168.231.65"]  #禁用组播,手动设置集群的节点ip 
discovery.zen.minimum_master_nodes: 1  #指定集群中master最少数量
http.cors.enabled: true   #如果启用了 HTTP 端口,那么此属性会指定是否允许跨源 REST 请求。
http.cors.allow-origin: "*"#如果 http.cors.enabled 的值为 true,那么该属性会指定允许 REST 请求来自何处。

添加如下内容,为elasticsearch做准备,可以使集群健康值连接

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

添加elasticsearch用户

[root@n4 ~]# useradd elasticsearch

[root@n4 ~]# chown -R elasticsearch.elasticsearch /usr/local/elasticsearch

[root@n4 ~]# vim /etc/sysctl.conf

vm.max_map_count = 655360 #默认操作系统限制可能太低,需要调大

[root@n4 ~]# sysctl -p /etc/sysctl.conf#使参数生效

vm.max_map_count = 655360

打开/etc/security/limits.conf文件,修改打开文件句柄数

[root@n4 ~]# vim /etc/security/limits.conf

* soft nofile 65536 #表示任何一个用户可以打开的最大的文件描述符数量(软限制)

* hard nofile 65536 #表示任何一个用户可以打开的最大的文件描述符数量(硬限制)

* soft nproc 65536 #可打开的文件描述符的最大数(软限制)

* hard nproc 65536 #可打开的文件描述符的最大数(硬限制)

# End of file

启动

可以先不加-d(),前台启动,看是否报错,使用elastic时要切换用户(如果用root启动了,需要chown -R elasticsearch.elasticsearch elastic目录)

[elasticsearch@n4 ~]$ /usr/local/elasticsearch/bin/elasticsearch -d

图片.png
9200:对外服务的http端口,默认就是9200
9300:节点间交互的tcp端口

curl测试(可以浏览器访问):

图片.png

把配置文件分发到n6节点(其它按照n4节点操作):

n6配置文件修改(只修改node.name,network.host即可):

[root@n6 config]# cat elasticsearch.yml | grep -v "^$" | grep -v "^#"
cluster.name: my-es-cluster
node.name: 192.168.231.65
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
network.host: 192.168.231.65
node.master: true
node.data: true
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.231.63","192.168.231.65"]
discovery.zen.minimum_master_nodes: 1

启动n6节点:

[elasticsearch@n6 ~]$ /usr/local/elasticsearch/bin/elasticsearch  -d

安装 ElasticSearch-Head

介绍

ElasticSearch-Head 是一个与Elastic集群(Cluster)相交互的Web前台。

ES-Head的主要作用:

  • 它展现ES集群的拓扑结构,并且可以通过它来进行索引(Index)和节点(Node)级别的操作
  • 它提供一组针对集群的查询API,并将结果以json和表格形式返回
  • 它提供一些快捷菜单,用以展现集群的各种状态

安装node.js

#由于head插件本质上还是一个nodejs的工程,因此需要安装node,使用npm来安装依赖的包。

下载并安装node

[root@n4 ~]# wget https://nodejs.org/dist/v8.10.0/node-v8.10.0-linux-x64.tar.xz

[root@n4 ~]# tar xf node-v8.10.0-linux-x64.tar.xz

[root@n4 ~]# mv node-v8.10.0-linux-x64 /usr/local/node

[root@n4 ~]# vim /etc/profile

#node

NODE_HOME=/usr/local/node

PATH=$PATH:$NODE_HOME/bin

使环境变量生效

[root@n4 ~]# source /etc/profile

node安装成功

[root@n4 ~]# node -v

v8.10.0

[root@n4 ~]# which npm

/usr/local/node/bin/npm

下载ElasticSearch-Head

[root@n4 ~]# yum install git -y

[root@n4 ~]# git clone https://github.com/mobz/elasticsearch-head.git

[root@n4 ~]# cd elasticsearch-head/

[root@n4 elasticsearch-head]# npm install -g grunt --registry=https://registry.npm.taobao.org    

[root@n4 elasticsearch-head]#   ls -d node_modules/grunt

node_modules/grunt

#如果没产生此目录需要:#cd elasticsearch-head  && npm install grunt --save

安装淘宝grunt工具(用来启动head)

[root@n4 ~]# npm install -g grunt-cli --registry=https://registry.npm.taobao.org

[root@n4 elasticsearch-head]# npm install  --registry=https://registry.npm.taobao.org 

修改head配置文件

[root@n4 ~]# mv elasticsearch-head /usr/local/head
[root@n4 ~]# chown -R elasticsearch.elasticsearch /usr/local/head/
[root@n4 ~]# vim /usr/local/head/Gruntfile.js
connect: {
                       server: {
                               options: {
                                       port: 9100,
                                       hostname: '*',#添加
                                       base: '.',
                                       keepalive: true
[root@n4 ~]# vim /usr/local/head/_site/app.js
                       this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://192.168.231.63:9200";#添加elasticsearch主机IP

重新启动n4节点:

杀死进程PID

[root@n4 ~]# ss -lntup | grep 9200

再次启动

[elasticsearch@n4 ~]$ /usr/local/elasticsearch/bin/elasticsearch  -d

启动head

[elasticsearch@n4 head]$ pwd
/usr/local/head
[elasticsearch@n4 head]$ grunt server  &

浏览器访问

图片.png

安装logstash

解压logstash压缩包

[root@n3 ~]# wget  https://artifacts.elastic.co/downloads/logstash/logstash-6.6.2.tar.gz

[root@n3 ~]# tar xf logstash-6.6.2.tar.gz

[root@n3 ~]# mv logstash-6.6.2 /usr/local/logstash

测试:

#-e:执行命令,后边是相应的语法格式,输入和输出

[root@n3 ~]# /usr/local/logstash/bin/logstash -e 'input{ stdin{} } output{ stdout{ codec => rubydebug } }'

hello world
{
   "@timestamp" => 2019-03-30T09:30:54.255Z,
     "@version" => "1",
         "host" => "n3",
      "message" => "hello world"
}

安装filebeat

Filebead介绍

   在创建Logstash管道之前,将配置Filebeat以将日志行发送到Logstash。Filebeat客户端是一个轻量级的,资源友好的工具,它从服务器上的文件中收集日志,并将这些日志转发给Logstash实例进行处理。 Filebeat专为可靠性和低延迟而设计。 Filebeat在主机上占用的资源较少,Beats输入插件最大限度地减少了Logstash实例的资源需求。       

#正常生产场景下,哪台机器需要收集日志,如web日志等会在其机器上安装Filebeat客户端

在logstash上面下载filebeat并启动,通过它来监听数据源文件的新增内容经过logstash处理后上传到es里面

[root@n3 ~]# tar xf filebeat-6.0.1-linux-x86_64.tar.gz

[root@n3 ~]# mv filebeat-6.0.1-linux-x86_64 /usr/local/filebeat

修改配置文件

[root@n3 ~]# cd /usr/local/filebeat/

[root@n3 filebeat]# cp filebeat.yml filebeat.yml.bak

[root@n3 filebeat]# cat filebeat.yml | grep -v "^$" |grep -v "^#" |grep -v "#"

filebeat.prospectors:
- type: log
 paths:
   - /var/log/messages-log#监控本地messages-log,自行创建,进行测试
output.logstash:
 hosts: ["192.168.231.62:5044"]

复制/var/log/messages下的几条数据到messages-log文件进行测试

启动filebeat,一定要切换到filebeat目录运行,否则会报错

[root@n3 ~]# cd /usr/local/filebeat/

后台运行

[root@n3 filebeat]# ./filebeat &

[1] 2750

注意filebeat没有监听端口,主要看日志和进程

图片.png

关闭filebeat可以杀死进程号PID

图片.png

注意filebeat监听的文件记录信息在/usr/local/filebeat/data/registry

filebeat配合logstash使用,需要编写配置文件

[root@n3 ~]# cd /usr/local/logstash/

[root@n3 logstash]# mkdir conf

创建一个logstash的启动指定配置文件

[root@n3 conf]# vim test.conf
input {
 beats {
   port => "5044"} #监听5044端口,filebeat获得文件发送到logstash处理
}
output {
 elasticsearch {
   hosts => "192.168.231.63:9200" #相应信息发送到elasticsearch进行存储
}
 stdout { codec => rubydebug}  #对信息进行打印,在屏幕上进行显示
}

执行命令进行测试,-e:执行

[root@n3 ~]# /usr/local/logstash/bin/logstash -e 'input{ stdin{} } output{ stdout{} }'

##过程会很漫长

输入hello,输出相关信息

图片.png

Ctrl+c结束

指定配置文件test.conf进行测试

[root@n3 ~]# cd /usr/local/logstash/conf

[root@n3 conf]# /usr/local/logstash/bin/logstash -f ./test.conf

查看5044端口和9600端口是否开启

图片.png

等一会er会输出如下信息,这也就是test.conf中定义的stdout

图片.png

配置文件中也输入到了elasticsearch中

在n4上进行查看

[elasticsearch@n4 ~]$ curl http://192.168.231.63:9200/_search?pretty
{
  "took" : 70,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 7,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "logstash-2019.03.28",
        "_type" : "doc",
        "_id" : "usXNw2kBNrAk1TCJ5ME_",
        "_score" : 1.0,
        "_source" : {
          "offset" : 126,
          "@timestamp" : "2019-03-28T10:14:33.237Z",
          "beat" : {
            "version" : "6.0.1",
            "name" : "n3",
            "hostname" : "n3"
          },
          "source" : "/var/log/messages-log",
          "prospector" : {
            "type" : "log"
          },
          "@version" : "1",
          "host" : "n3",
          "message" : "Mar 28 17:22:43 n3 systemd: Starting Session 14 of user root.",
          "tags" : [
            "beats_input_codec_plain_applied"
          ]
        }
      },

head查看索引

安装kibana

解压

[root@n5 ~]# tar xf kibana-6.0.0-linux-x86_64.tar.gz

[root@n5 ~]# mv kibana-6.0.0-linux-x86_64 /usr/local/kibana

对配置文件进行备份

[root@n5 ~]# cd /usr/local/kibana/config/

[root@n5 config]# cp kibana.yml kibana.yml.bak

修改配置文件

[root@n5 config]# cat kibana.yml| grep -v "^$" | grep -v "#" | grep -v "^#"

server.port: 5601

server.host: "192.168.231.64"

elasticsearch.url: "http://192.168.231.63:9200"

启动kibana

[root@n5 ~]# /usr/local/kibana/bin/kibana

查看端口是否存在

图片.png

浏览器访问http://192.168.231.64:5601

图片.png

创建后依次点击Discover,logstash-*

图片.png

猜你喜欢

转载自blog.csdn.net/yuanfangPOET/article/details/88918655