hgjgh 发表于 2019-1-28 08:35:56

ELK2.x日志搜集系统服务

一、ELK工作原理

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

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

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

ELK官方网站
  https://www.elastic.co/cn/

工作基本流程图
http://i2.运维网.com/images/blog/201809/11/a46adb17e154e999875b0bafc30565f1.png

所需环境




主机名
IP地址
所需软件




node-1
192.168.144.114
jdk8、elasticsearch2.4.6、logstash2.1.3、kibana-4.3.1-linux-x64.tar.gz


node-2
192.168.144.117
jdk8、elasticsearch2.4.6



二、Elasticsearch及Elasticsearch群集部署

节点1安装elasticsearch并打开群集功能


[*]获取官方验证密匙

  rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch



[*]配置yum安装源

  vim 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

[*]安装elasticsearch

  yum install elasticsearch -y
  yum install java -y(1.8版本)



[*]查看java版本

  java -version



[*]修改配置文件

  vim /etc/elasticsearch/elasticsearch.yml


17行 集群名称
cluster.name: yun
23行 节点名称
node.name: linux-node1
33行 工作目录
path.data: /data/es-data            //这两个目录建议更改,若采用默认格式,版本更新可能会被默认删除
path.logs: /var/log/elasticsearch/    //创建这两个目录后,注意目录属主属组权限
43行 防止交换swap分区
bootstrap.memory_lock: true
54行 监听网络
network.host: 0.0.0.0
58行 端口
http.port: 9200
69行 单播列表自动发现机制
discovery.zen.ping.unicast.hosts: ["127.0.0.1", "192.168.144.117"]    //一个本地IP,一个节点2IP
  mkdir -p /data/es-data
chown -R elasticsearch:elasticsearch /data/es-data/
systemctl start elasticsearch.service
netstat -ntap | grep 9200


注意事项
  在打开防止交换分区功能后,在es启动日志中可观察到如下信息:

  vim /var/log/elasticsearch/abner.log


...

These can be adjusted by modifying /etc/security/limits.conf, for example:
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
...

[*]针对如上情况,我们需要对系统内存进行相应优化

  vim /etc/security/limits.conf


//末尾插入
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
* soft nofile 65535      
* hard nofile 65535    //需重启生效
安装ES-head插件

  /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head



[*]查询已经安装的插件

  /usr/share/elasticsearch/bin/plugin list



[*]插件默认安装位置

  /usr/share/elasticsearch/plugins/head


页面测试


[*]未安装插件前
http://i2.运维网.com/images/blog/201809/11/05c91dfbd581e2a8bb1793bfcd814a7a.png


[*]安装head插件后
http://i2.运维网.com/images/blog/201809/11/56a8efccb15c7d004930b9b429da7397.png

部署节点2群集节点


[*]es配置方式如节点一
[*]编辑配置如下:
[*]查看配置文件
  cat /etc/elasticsearch/elasticsearch.yml | grep -v "^#" | grep -v "^$"



cluster.name: yun   //注意:统一群集下,使用的群集名称需相同
node.name: node-2
path.data: /data/es-data
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.144.117", "192.168.144.114"]
  mkdir -p /data/es-data创建工作目录
chown -R elasticsearch:elasticsearch /data/es-data/
systemctl start elasticsearch.service
netstat -ntap | grep 9200


ES群集测试


[*]访问节点一的head插件页面
http://i2.运维网.com/images/blog/201809/11/79fdd9a8e55cd326c42c3d4456429e2b.png
http://i2.运维网.com/images/blog/201809/11/529d5c5a3f9c5834a2e263816b6ff9d1.png
http://i2.运维网.com/images/blog/201809/11/385735eb861fc1df0f2c5a6f9cb9bdc9.png

监控组件插件kopf安装


[*]默认会在github.com 中查找 kopf

  /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf



[*]监控页面测试
  http://192.168.144.114:9200/_plugin/kopf/#!/cluster
http://i2.运维网.com/images/blog/201809/11/3771398c98a58e3ca8ec710b8e5092dd.png

三、部署logstash收集日志
  为了试验方便起见,本次logstash服务部署在es节点1上。并且将收集到的日志传递给es进行处理。

logstash部署


[*]获取官方验证密匙

  rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch



[*]创建软件yum源

  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

[*]安装logstash

  yum install logstash -y



[*]将logstash命令能让系统识别

  ln -s /opt/logstash/bin/logstash /usr/bin/


创建logstash启动配置文件


[*]创建logstash配置文件读取本机httpd服务的日志文件

  vim file.conf


input {
file {
path => "/var/log/httpd/access_log"
type => "accesslog"
start_position => "beginning"
}
file {
path => "/var/log/httpd/error_log"
type => "errorlog"
start_position => "beginning"
}
}
output {
if == "accesslog" {
elasticsearch {
hosts => ["192.168.144.114:9200"]      //将日志收集数据传递给es
index => "access-%{+YYYY.MM.dd}"
}
}
if == "errorlog" {
elasticsearch {
hosts => ["192.168.144.114:9200"]
index => "error-%{+YYYY.MM.dd}"
}
}
}

[*]观察head插件,可观察到日志收集情况
http://i2.运维网.com/images/blog/201809/11/3eca1ea2dd68038f2fe5c24e24a8c3f9.png
http://i2.运维网.com/images/blog/201809/11/64977575fc44819e04476e10568720aa.png

四、部署kibana展示


[*]获取kibana软件包

  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 kibana-4.3.1-linux-x64/ /usr/local/
  mv kibana-4.3.1-linux-x64/ kibana



[*]编辑kibana配置文件

  vim /usr/local/kibana/config/kibana.yml


//2行
server.port: 5601
//5行
server.host: "0.0.0.0"
//12行 ES地址
elasticsearch.url: "http://192.168.175.132:9200"
//20行
kibana.index: ".kibana"
  yum install screen -y
screen   换屏操作



[*]启动监听kibana

  /usr/local/kibana/bin/kibana

http://i2.运维网.com/images/blog/201809/11/50c7890e0de84373e4ab9091a933c8de.png


[*]进行丢入后台

  ctrl+a+d


kibana页面展示测试
  http://192.168.144.114:5601/
http://i2.运维网.com/images/blog/201809/11/4286f0fbd5899df4930e01726922b327.png



页: [1]
查看完整版本: ELK2.x日志搜集系统服务