jy166102 发表于 2019-1-28 10:58:25

【官网最新版】ELK 6.4 实时日志分析平台部署

ELK简介:
  ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。
  Elasticsearch 是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
  Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
  Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
  Filebeat 隶属于Beats。目前Beats包含四种工具:


[*]Packetbeat(搜集网络流量数据)
[*]Topbeat(搜集系统、进程和文件系统级别的 CPU 和内存使用情况等数据)
[*]Filebeat(搜集文件数据)
[*]Winlogbeat(搜集 Windows 事件日志数据)
  官方文档:
Filebeat:
https://www.elastic.co/cn/products/beats/filebeat
  Logstash:
https://www.elastic.co/cn/products/logstash
  Kibana:
https://www.elastic.co/cn/products/kibana
  Elasticsearch:
https://www.elastic.co/cn/products/elasticsearch
  elasticsearch中文社区:
https://elasticsearch.cn/
  一般我们需要进行日志分析场景:直接在日志文件中 grep、awk 就可以获得自己想要的信息。但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档、文本搜索太慢怎么办、如何多维度查询。需要集中化的日志管理,所有服务器上的日志收集汇总。常见解决思路是建立集中式日志收集系统,将所有节点上的日志统一收集,管理,访问。
  一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块,构建一套集中式日志系统,可以提高定位问题的效率。
  一个完整的集中式日志系统,需要包含以下几个主要特点:


[*]收集-能够采集多种来源的日志数据
[*]传输-能够稳定的把日志数据传输到中央系统
[*]存储-如何存储日志数据
[*]分析-可以支持 UI 分析
[*]警告-能够提供错误报告,监控机制
  ELK提供了一整套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用。目前主流的一种日志系统。

系统环境




主机名
操作系统
IP地址
服务名




es
centos7.4
192.168.96.85
elasticsearch 6.4.0、kibana 6.4.0、rsyslog


nginx
centos7.4
192.168.96.60
elasticsearch 6.4.0、logstash-6.4.0


httpd
centos7.4
192.168.96.86
elasticsearch 6.4.0、filebeat-6.4


客户机
windows 10
192.168.96.2
网页浏览器


  以上所有服务器均关闭防火墙及SElinux功能

setenforece 0
systemctl stop firewalld
角色划分:


[*]所有机器全部安装jdk1.8,因为elasticsearch是java开发的,全部安装elasticsearch
[*]es服务器作为主节点
[*]三台服务器均为数据节点
[*]es服务器上需要安装kibana
[*]es服务器上通过rsyslog收集日志
[*]nginx服务器上通过logstash收集日志
[*]httpd服务器上通过filebeat收集日志
  这里分别使用了3种收集日志的方法,官网建议选用filebeat,因为轻量、高效

开始部署

es集群(三台服务器操作一致):

1. 编辑hosts文件

  vim /etc/hosts


192.168.96.85 es
192.168.96.86 httpd
192.168.96.60 nginx
2. 安装elasticsearch软件包

# 导入key
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 创建es仓库源
vim /etc/yum.repos.d/elasticsearch.repo

name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
# 安装es软件包
yum -y install elasticsearch
3.编辑es配置文件

  vim /etc/elasticsearch/elasticsearch.yml


17 cluster.name: es-server
23 node.name: master
24 node.master: true
25 node.data: true
58 network.host: 0.0.0.0
62 http.port: 9200
71 discovery.zen.ping.unicast.hosts: ["192.168.96.85", "192.168.96.86","192.168.96.60"]
提醒:以上三台es均安装好后再将elasticsearch.yml配置文件发送到httpd、nginx服务器

scp /etc/elasticsearch/elasticsearch.yml httpd:/etc/elasticsearch/
scp /etc/elasticsearch/elasticsearch.yml nginx:/etc/elasticsearch/
下面分别修改httpd、nginx服务器上的elasticsearch配置文件

# httpd服务器:
vim /etc/elasticsearch/elasticsearch.yml
node.name: httpd
node.master: false
# nginx服务器:
vim /etc/elasticsearch/elasticsearch.yml
node.name: nginx
node.master: false
启动elasticsearch服务,先启动master、在启动其他es服务

# es服务器:(先启动master,在启动其他es)
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
# nginx服务器:
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
# httpd服务器:
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
http://i2.运维网.com/images/blog/201809/06/c82224dc16a5ba5bccad06413f1de03b.png
http://i2.运维网.com/images/blog/201809/06/719d4b02f2630192a090c9066b4fb379.png
http://i2.运维网.com/images/blog/201809/06/75e1b4e30f870bcbdb85dc840c35d78b.png

es集群的健康检查:
  http://192.168.96.85:9200/_cluster/health?pretty
http://i2.运维网.com/images/blog/201809/06/1d1ebc8ad46e95caa5f99e48f6961139.png

es集群的状态查看:
  http://192.168.96.85:9200/_cluster/state?pretty
http://i2.运维网.com/images/blog/201809/06/da4b002ab9dbc7b375253f5a1b8e1e26.png
  至此,es集群已经部署完成了

kibana(es服务器)

1. 安装kibana

yum -y install kibana
2. 编辑kibana.yml配置文件

  vim /etc/kibana/kibana.yml


2 server.port: 5601
7 server.host: "192.168.96.85"
28 elasticsearch.url: "http://192.168.96.85:9200"
96 logging.dest: /var/log/kibana.log
3. 创建kibana日志文件

touch /var/log/kibana.log
chmod 777 /var/log/kibana.log
4. 启动kibana服务

systemctl enable kibana
systemctl start kibana
5. 查询kibana服务

# netstat -tunlp | grep 5601
tcp      0      0 192.168.96.85:5601      0.0.0.0:*               LISTEN      2597/node
http://i2.运维网.com/images/blog/201809/06/972a03c22cfa236434e17633dd4d3d9d.png

客户机访问kibana(http://192.168.96.85:5601)测试
http://i2.运维网.com/images/blog/201809/06/de6c89f59dbde887501b161f25a3916e.png

收集系统日志

1. 安装logstach(暂不支持jdk1.9版本,这里安装的是jdk1.8)

yum install logstash -y
http://i2.运维网.com/images/blog/201809/06/7320a2ae75a04523f43f4041a6fd1568.png

2. 配置logstash收集syslog系统日志

  vim /etc/rsyslog.conf


第91行
*.* @@192.168.96.85:10514
3. 重启rsyslog服务

systemctl restart rsyslog
4.收集系统日志(rsyslog收集日志)

  vim /etc/logstash/conf.d/syslog.conf


input {
syslog {
type => "system-syslog"
port => 10514
}
}
output {
elasticsearch {
hosts => ["192.168.96.85:9200"]            //es服务器ip地址
index => "system-syslog-%{+YYYY.MM}"         //索引
}
}
5.启动logstash收集syslog日志

./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/syslog.conf
http://i2.运维网.com/images/blog/201809/06/6292e59142f0c029ecb63608d1b29c17.png

6.通过客户端浏览kibana(192.168.96.85:5601),页面提醒设置索引
http://i2.运维网.com/images/blog/201809/06/f21cddb38feed3edcc57761c806068a1.png
http://i2.运维网.com/images/blog/201809/06/c391d0b080269c43914dd2fe059b443a.png
http://i2.运维网.com/images/blog/201809/06/74e8a5e8e3d9ba1c47c187cdef14c5c2.png

httpd服务器:(filebeat收集日志)

1. 下载并安装filebeat软件包

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.4.0-x86_64.rpm
rpm -ivh filebeat-6.4.0-x86_64.rpm
2. 编辑filebeat配置文件

  vim /etc/filebeat/filebeat.yml


#注释掉下一行
#enabled: false
paths:
- /var/log/messages      //指定日志路径
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["192.168.96.85:9200"]   //指向es服务器
3. 启动filebeat服务

systemctl enable filebeat
systemctl start filebeat
4. 查看filebeat进行信息

ps aux | grep filebeat
http://i2.运维网.com/images/blog/201809/06/32ede10fe99f9ce1b2c3c04d92b1cdc1.png

5. 查看索引信息

curl '192.168.96.85:9200/_cat/indices?v'
http://i2.运维网.com/images/blog/201809/06/4c308bfd5f058c74c14debb9b95b04ad.png

nginx服务器:(logstash收集日志)

1.创建收集nginx日志配置文件

  vim /etc/logstash/conf.d/nginx.conf


input {
file {
path => "/var/log/logstash/elk_access.log"
start_position => "beginning"
type => "nginx"
}
}
filter {
grok {
match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"}
}
geoip {
source => "clientip"
}
}
output {
stdout { codec => rubydebug }
elasticsearch {
hosts => ["192.168.96.85:9200"]   //es服务器ip
index => "nginx-test-%{+YYYY.MM.dd}"
}
}
2. 检查配置文件是否有错误

/usr/share/logstash/bin/logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf --config.test_and_exit
http://i2.运维网.com/images/blog/201809/06/add7b1d378ee8cacedce3c44b1d980d2.png

3. 下面新增nginx虚拟主机配置指向kibana

  vim /etc/nginx/conf.d/elk.conf


server {
listen 80;
server_name www.test.com;
location / {
proxy_pass      http://192.168.96.85:5601;
proxy_set_header Host   $host;
proxy_set_header X-Real-IP      $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log/var/log/logstash/elk_access.log main2;
}
4. 在nginx主配置文件中设置日志格式,如下

log_format main2 '$http_host $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$upstream_addr" $request_time';
http://i2.运维网.com/images/blog/201809/06/fecee4307959799be14cfefdc41ac453.png

5. 启动logstash服务

systemctl enable logstash
systemctl start logstash
  重启nginx服务

systemctl restart nginx
  开启logstash收集nginx日志

/usr/share/logstash/bin/logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf
客户端访问浏览kibana(192.168.96.85:5601)
http://i2.运维网.com/images/blog/201809/06/3b7ad0c77cd5fdf5e9094e2a7d176386.png
http://i2.运维网.com/images/blog/201809/06/2615cc40223f03c7a13e353b9765cd0e.png
http://i2.运维网.com/images/blog/201809/06/5a3c1ec400f665e8f3301c1bc5a2d5fd.png
http://i2.运维网.com/images/blog/201809/06/20d460698bdff1bd56756dc3543ced34.png



页: [1]
查看完整版本: 【官网最新版】ELK 6.4 实时日志分析平台部署