从0开始搭建ELK日志收集系统

Elasticsearch

  • elasticsearch是一个高度可扩展全文搜索和分析引擎,基于Apache Lucene 构建,能对大容量的数据进行接近实时的存储、搜索和分析操作,可以处理大规模日志数据,比如Nginx、Tomcat、系统日志等功能。

Logstash

  • 数据收集引擎。它支持动态的从各种数据源搜集数据,并对数据进行过滤、分析、丰富、统一格式等操作,然后存储到用户指定的位置;支持普通log、自定义json格式的日志解析。

Kibana

  • 数据分析和可视化平台。通常与 Elasticsearch 配合使用,对其中数据进行搜索、分析和以统计图表的方式展示。

1.上传软件包

  • elasticsearch-5.6.16-linux-x86_64.tar.gz
  • kibana-5.6.16-linux-x86_64.tar.gz
  • logstash-5.6.16.tar.gz

注意此三个包需要版本一致才可以

 2.安装elasticsearch

进入到你上传的目录

cd /opt

解压安装包

tar -zxvf elasticsearch-5.6.16-linux-x86_64.tar.gz

若为zip使用

unzip elasticsearch-5.6.16.zip

进入配置文件修改配置

vi  elasticsearch-5.6.16/config/elasticsearch.yml

添加如下配置 

若为集群

cluster.name: ELK-Cluster    #ELK的集群名称,名称相同即属于是同一个集群
node.name: elk-node1    #本机在集群内的节点名称
path.data: /ELK/elasticsearch/data    #数据存放目录
path.logs: /ELK/elasticsearch/data/log    #日志保存目录
bootstrap.memory_lock: true    #服务启动的时候锁定足够的内存,防止数据写入swap
network.host: 10.77.169.2    #监听的IP地址
http.port: 9200    #服务监听的端口
discovery.seed_hosts: ["10.77.169.2", "10.77.19.4"]   #单播配置一台即可
cluster.initial_master_nodes: ["elk-node1", "elk-node2"]  

若为单机

network.host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"

配置内存限制

vim jvm.options

修改内存大小如下

-Xms2g

-Xmx2g 

创建用户及其数据目录

创建elsearch用户组及elsearch用户

  • groupadd elsearch
  • useradd elsearch -g elsearch -p elasticsearch

更改elasticsearch-5.6.16文件夹及内部文件的所属用户及组为elsearch:elsearch

  • cd切换到elasticsearch-5.6.16的父路径下
  • chown -R elsearch:elsearch elasticsearch-5.6.16

切换到elsearch用户再启动

  • su elsearch
  • cd elasticsearch-5.6.16/bin
  • sh elasticsearch &

创建数据目录

  • mkdir -p /ELK/elasticsearch/{data,data/log}

 使用es-head插件查看是否成功

若启动报错,则需要修改内核配置

[root@localhost bin]# vim /etc/security/limits.conf

配置如下:

* soft nofile 65536
* hard nofile 131072
* soft memlock unlimited
* hard memlock unlimited

修改最大线程数

[root@localhost bin]# vim /etc/security/limits.d/20-nproc.conf

配置如下:

*     soft    nproc    unlimited

[root@localhost bin]# vim /etc/sysctl.conf

配置如下:

vm.max_map_count = 655360

令配置生效:

[root@localhost bin]# sysctl -p
[root@localhost bin]# reboot

3.Logstash部署

解压安装包

tar -zxvf logstash-5.6.16.tar.gz

创建配置文件

touch logstash-5.6.16/bin/config/logstash-nginx.conf

添加配置文件如下,根据nginx和ongodb的路径创建配置

input {
    file {
        path => "/usr/local/nginx/logs/*.log"
        start_position => beginning
        type => "nginx"
    }
    file {
        path => "/usr/local/mongodb3/logs/*.log"
        start_position => beginning
        type =>"mongodb"
    }
}
filter {

}
output {
    if[type] == "nginx"{
        elasticsearch {
            hosts => "10.77.169.4:9200"
            index => "nginx-%{+YYYY.MM.dd}"
        }
    }
    if[type] == "mongodb"{
         elasticsearch {
            hosts => "10.77.169.4:9200"
            index => "mongodb-%{+YYYY.MM.dd}"
        }

    }
}

 使用刚才创建的配置文件启动logstash

sh logstash -f ../config/logstash-nginx.conf &

 验证是否成功

 见上方已经创建了两个索引,成功

4.部署kibana

解压

tar -zxvf kibana-5.6.16-linux-x86_64.tar.gz

进入配置文件

vi kibana.yml  

添加如下配置

server.port: 5601    #监听端口
server.host: "10.77.169.4"    #自己的地址
elasticsearch.hosts: ["http://10.77.169.2:9200","http://10.77.169.4:9200"] #elasticsearch服务器地址
i18n.locale: "zh-CN"    #修改为中文

 若使用单机

 server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://localhost:9200"
kibana.index: ".kibana"

注意,低版本无法修改为中文,只能使用插件修改为中文

cd命令进入bin目录,

sh kibana & 

输入ip:5601查看是否成功 

 

猜你喜欢

转载自blog.csdn.net/wang20010104/article/details/127846788