xuke123 发表于 2019-1-29 07:40:55

Logstash+Kibana+多ElasticSearch集群部署

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



[*]Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
[*]Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
[*]Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。

   




主机
IP
主要服务




linux-node1
192.168.200.129
jdk、logstash、elasticsearch、kibana


linux-node2
192.168.200.130
jdk、elasticsearch


   

操作步骤
   


[*]安装elasticsearch、jdk
[*]  Logstash需要Java 8.不支持Java 9。

# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch//导入密钥
# vim /etc/yum.repos.d/elasticsearch.repo       //配置源

name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1
# yum install elasticsearch -y   //安装elasticsearch
# yum install java -y             //安装javajdk
# java -version   //查看java版本 (1.8版本)
[*]  配置elasticsearch

-------------------linux-node1服务器配置 -------------------------
# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: abner   //17行 集群名称
node.name: linux-node1   //23行 节点名称
path.data: /data/es-data   //33行37行 工作目录
path.logs: /var/log/elasticsearch/
bootstrap.memory_lock: true   //43行 防止交换swap分区
network.host: 0.0.0.0    //54行 监听网络
http.port: 9200   //58行 端口
discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.200.130"]   
//68行 单播列表自动发现机制
-------------------linux-node2服务器配置(其他同上)---------------------
node.name: linux-node2   // 23行 节点名称
discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.200.129"]   
//68行 单播列表自动发现机制
# mkdir -p /data/es-data
# chown -R elasticsearch:elasticsearch /data/es-data/
# systemctl start elasticsearch.service
# netstat -ntap | grep 9200
http://i2.运维网.com/images/blog/201808/21/164c24ccfed99aac8e14f7295c247268.png


[*]测试
  http://192.168.175.132:9200


   
http://i2.运维网.com/images/blog/201808/21/3f9f3c2ea930fc42fa756ca5a0f348e0.png
   



[*]  RESTful API (通过json格式交互)

# curl -i -XGET 'http://192.168.200.129:9200/_count?pretty' -d '{"query": { "match_all": {} }}'
http://i2.运维网.com/images/blog/201808/21/4a728dc218b82f7ac7fc339c8bed6a46.png

   



[*]
内存解锁和文件限制
  生产环境中必须要修改(注意)

# vim /etc/security/limits.conf
//末尾插入
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
* soft nofile 65535      
* hard nofile 65535
# systemctl stop elasticsearch.service
# systemctl start elasticsearch.service
   



[*]
安装elasticsearch-head插件
[*]安装完成后打开浏览器输入:
[*]  http://192.168.200.129:9200/_plugin/head/

# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
http://i2.运维网.com/images/blog/201808/21/3fd330a5288fc6118ec79b287659c6e6.pnghttp://i2.运维网.com/images/blog/201808/21/74d580fd58072b4782891a400f4e09fc.pnghttp://i2.运维网.com/images/blog/201808/21/f8510b93a850950c5a92bb866eba18c6.pnghttp://i2.运维网.com/images/blog/201808/21/fe75d6144f8284f97143bc7a46fa9f29.pnghttp://i2.运维网.com/images/blog/201808/21/cdff75369885cdbdd5a4a98a8f6e175c.png
   



[*]
安装elasticsearch-kopf插件
[*]安装完成后打开浏览器输入:
[*]  http://192.168.200.129:9200/_plugin/kopf/#!/cluster

# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
http://i2.运维网.com/images/blog/201808/21/b84d0a001694bad908c33382a0816e7e.png
http://i2.运维网.com/images/blog/201808/21/2f2f8fc8f4960bc872b2f0c797ecb76c.png

   


logstash部署


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

[*]  安装logstash

# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
# vim /etc/yum.repos.d/logstash.repo

name=Logstash repository for 2.1.x packages
baseurl=http://packages.elastic.co/logstash/2.1/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1
# yum install logstash -y
[*]  定义输入和输出流,类似管道

# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'
[*]  详细格式显示

# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } }'
http://i2.运维网.com/images/blog/201808/21/ffa455ffc3438fa3e0f4587173840135.png

   


[*]  写入到elasticsearch中

# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.200.129:9200"] } }'
http://i2.运维网.com/images/blog/201808/21/8864ac5c238efb40e705c8bf0243b074.pnghttp://i2.运维网.com/images/blog/201808/21/ec1cd9e5a4a0c1d04c36a16043827ee5.png
   


[*]  写入ES和同时详细格式显示

# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.200.129:9200"] } stdout { codec => rubydebug } }'
http://i2.运维网.com/images/blog/201808/21/a0d254bf6fd452d87e6fc7fb66f92836.png
   


[*]  创建 logstash配置文件写入

# vim /etc/logstash/conf.d/01-logstash.conf
input { stdin { } }
output {
elasticsearch { hosts => ["192.168.200.129:9200"] }
stdout { codec => rubydebug }
}
# /opt/logstash/bin/logstash -f /etc/logstash/conf.d/01-logstash.conf
   


[*]使用logstash配置文件:
[*]收集系统日志
[*]收集java异常日志
[*]  事件优化处理

# vim /root/file.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}   
file {
path => "/var/log/elasticsearch/abner.log"
type => "es-error"
start_position => "beginning"   
codec => multiline {
pattern => "^\["
negate => true
what => "previous"
}
}
}
output {
if == "system" {
elasticsearch {
hosts => ["192.168.200.129:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if == "es-error" {
elasticsearch {
hosts => ["192.168.200.129:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
}
# logstash -f /root/file.conf
http://i2.运维网.com/images/blog/201808/21/228b6f78c6763b217209a251913ba624.pnghttp://i2.运维网.com/images/blog/201808/21/f0899bb19df8527e066edd49c34fddbb.pnghttp://i2.运维网.com/images/blog/201808/21/ef010b5b65ce4d8762dca4506d508b5f.png
   


kibana部署


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

# wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz
# tar zxvf kibana-4.3.1-linux-x64.tar.gz -C /opt/
# mv /opt/kibana-4.3.1-linux-x64/ /usr/local/kibana
# vim /usr/local/kibana/config/kibana.yml
server.port: 5601         //2行
server.host: "0.0.0.0"   //5行
elasticsearch.url: "http://192.168.200.129:9200"    //12行 ES地址
elasticsearch.url: "http://192.168.200.130:9200"    //节点2的 ES地址
kibana.index: ".kibana"    //20行
[*]  Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。GNU Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。

# yum install screen -y
#screen   //开启
# /usr/local/kibana/bin/kibana   //启动监听
  按下组合键 ctrl+a+d进行丢入后台
打开浏览器:http://192.168.200.129:5601/


http://i2.运维网.com/images/blog/201808/21/d1d574413850a10cda3479b4eadd0cf1.png
http://i2.运维网.com/images/blog/201808/21/4488fcd8b84c0eb9e61695812f008003.png
http://i2.运维网.com/images/blog/201808/21/064b0a9c142128c58420bdfc1f4ae9a8.pnghttp://i2.运维网.com/images/blog/201808/21/be806c6db2d4e1e40ade17ed6e38aa29.png



页: [1]
查看完整版本: Logstash+Kibana+多ElasticSearch集群部署