设为首页 收藏本站
查看: 736|回复: 0

[经验分享] 快速搭建ELK日志分析系统

[复制链接]

尚未签到

发表于 2019-1-28 12:15:02 | 显示全部楼层 |阅读模式
  文档参考:https://www.cnblogs.com/yuhuLin/p/7018858.html

一、ELK搭建篇
  ——————————————————————————————————————————
官网地址:https://www.elastic.co/cn/
官网权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
安装指南:https://www.elastic.co/guide/en/elasticsearch/reference/5.x/rpm.html
ELK是Elasticsearch、Logstash、Kibana的简称,这三者是核心套件,但并非全部。
Elasticsearch是实时全文搜索和分析引擎,提供搜集、分析、存储数据三大功能;是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统。它构建于Apache Lucene搜索引擎库之上。
Logstash是一个用来搜集、分析、过滤日志的工具。它支持几乎任何类型的日志,包括系统日志、错误日志和自定义应用程序日志。它可以从许多来源接收日志,这些来源包括 syslog、消息传递(例如 RabbitMQ)和JMX,它能够以多种方式输出数据,包括电子邮件、websockets和Elasticsearch。
Kibana是一个基于Web的图形界面,用于搜索、分析和可视化存储在 Elasticsearch指标中的日志数据。它利用Elasticsearch的REST接口来检索数据,不仅允许用户创建他们自己的数据的定制仪表板视图,还允许他们以特殊的方式查询和过滤数据


环境
  ——————————————————————————————————————————
centos7.3两台
IP:172.20.2.207          安装:elasticsearch、logstash、Kibana、Nginx、Http、Redis
173.172.20.2.198   安装:logstash
备注:此次安装的ELK三个组件均为最新版(6.5.1)。在安装配置过程中出现各种各样的问题。不过和其他版本的安装基本一样。这里不再详细描述安装过程,将安装服务做成脚本。便于后续自动安装ELK。
ELK总的安装脚本:https://github.com/LWang22/-/blob/master/install_elk.sh
elastic安装脚本:    https://github.com/LWang22/-/blob/master/install_elastic.sh
logstash安装脚本:https://github.com/LWang22/-/blob/master/install_logstash.sh
kibana安装脚本:   https://github.com/LWang22/-/blob/master/install_kibana.sh

手动安装参考:https://www.cnblogs.com/yuhuLin/p/7018858.html
  本次配置也是参考此链接。
此处详细描述下elastic-head的安装过程,之前安装是源码安装,其实和rpm安装差不多,但就是 npm install 有问题。这里安装npm的rpm安装方式。
  使用 git 安装elasticsearch-head
yum -y install npm
git clone  git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install (安装模块到node_modules;安装的是grunt模块)
进入grunt模块,启动grunt模块。
nohup ./grunt server &
检查端口是否起来
netstat -anple  | grep 9100

浏览器访问测试是否正常


logStash的使用
  ——————————————————————————————————————————
执行logstash命令
./logstash -e 'input { stdin { } } output { stdout {} }'
运行成功后输入:
你好
stdout返回的结果:

注:
-e          执行操作
input       标准输入
{ input }   插件
output      标准输出
{ stdout }  插件
  通过rubydebug来输出下更详细的信息
#./logstash -e 'input { stdin { } } output { stdout {codec => rubydebug} }'
执行成功输入:
怎么这么慢
stdout输出的结果:

如果标准输出还有elasticsearch中都需要保留应该怎么玩,看下面
#bin/logstash -e 'input { stdin { } } output { elasticsearch { hosts => ["172.20.2.207:9200"] } stdout { codec => rubydebug }}'
运行成功以后输入:
I am elk
返回的结果(标准输出中的结果):

上图引用

logstash使用配置文件
  ——————————————————————————————————————————
官方指南:
https://www.elastic.co/guide/en/logstash/current/configuration.html
创建配置文件elk.conf
#vim /etc/logstash/conf.d/elk.conf
  文件中添加以下内容
input { stdin { } }
output {
elasticsearch { hosts => ["192.168.1.202:9200"] }
stdout { codec => rubydebug }
}
  使用配置文件运行logstash
#bin/logstash -f config/elk.conf


logstash的数据类型
  ——————————————————————————————————————————


  • Input插件
    权威指南:https://www.elastic.co/guide/en/logstash/current/input-plugins.html
  file插件的使用
#vim /etc/logstash/conf.d/elk.conf
添加如下配置
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {   
elasticsearch {
hosts => ["172.20.2.207:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
  运行logstash指定elk.conf配置文件,进行过滤匹配
#bin/logstash -f  config/elk.conf


logstash上面没有显示出来type类型,显示的_type。打开索引可查看具体的type。如图所示。
  来一发配置安全日志的并且把日志的索引按类型做存放,继续编辑elk.conf文件
  #vim /etc/logstash/conf.d/elk.conf
添加secure日志的路径
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}

file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
}
  }
  output {

if [type] == "system" {
elasticsearch {
hosts => ["172.20.2.207:9200"]
index => system-%{+YYYY.MM.dd}"
}
}
if [type] == "secure" {
elasticsearch {
hosts => ["172.20.2.207:9200"]
index => "secure-%{+YYYY.MM.dd}"
}
}
  }
  运行logstash指定elk.conf配置文件,进行过滤匹配
#bin/logstash -f  config/elk.conf

这些都没有问题,接下来安装kibana,可以在前台展示。

kibana的安装和使用
  kibana的安装较简单,因为目前仅仅是简单使用,因此调整的参数不是很多。
安装kibana环境
官方安装手册:https://www.elastic.co/guide/en/kibana/current/install.html
因为上述已经安装过kibana了,因此保证kiban.yml开启以下配置即可。

在bin目录下执行,在后台运行起来。nohup还是有缺陷,后面会改成supervisor
nohup ./kibana &   
查看5601端口监听情况(kibana监听端口:5601)

在web浏览器访问172.20.2.207:5601

这里我们不使用kibana提供的模板数据,探索我们的自己的数据。因此选择 Explore on my own

选择 Connect to your Elasticsearch index ;创建索引(创建索引和elastic上面看到的一样,这样kibana就能根据elastic存储的数据进行搜索展示了)

创建完索引后尽可以在Discover界面查看到elastic保存的数据了。


二、ELK实战篇
  ——————————————————————————————————————————
好,现在索引也可以创建了,现在可以来输出nginx、apache、message、secrue的日志到前台展示(Nginx有的话直接修改,没有自行安装)
编辑nginx配置文件,修改以下内容(在http模块下添加)

log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domian":"$host",'
'"host":"$server_addr",'
'"size":"$body_bytes_sent",'
'"responsetime":"$request_time",'
'"referer":"$http_referer",'
'"ua":"$http_user_agent"'
'}';
  修改access_log的输出格式为刚才定义的json
access_log  logs/elk.access.log  json;
  继续修改apache的配置文件

LogFormat "{ \
\"@timestamp\": \"%{%Y-%m-%dT%H:%M:%S%z}t\", \
\"@version\": \"1\", \
\"tags\":[\"apache\"], \
\"message\": \"%h %l %u %t \\\"%r\\\" %>s %b\", \
\"clientip\": \"%a\", \
\"duration\": %D, \
\"status\": %>s, \
\"request\": \"%U%q\", \
\"urlpath\": \"%U\", \
\"urlquery\": \"%q\", \
\"bytes\": %B, \
\"method\": \"%m\", \
\"site\": \"%{Host}i\", \
\"referer\": \"%{Referer}i\", \
\"useragent\": \"%{User-agent}i\" \
}" ls_apache_json
  一样修改输出格式为上面定义的json格式
CustomLog logs/access_log ls_apache_json
  编辑logstash配置文件,进行日志收集
vim /etc/logstash/conf.d/full.conf

input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}   
file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
}   
file {
path => "/etc/httpd/logs/access_log"
type => "http"
start_position => "beginning"
}   
file {
path => "/var/log/nginx/access.log"
type => "nginx"
start_position => "beginning"
}   
  }
  output {

if [type] == "system" {
elasticsearch {
hosts => ["172.20.2.207:9200"]
index => "system-%{+YYYY.MM.dd}"
}      
}   
if [type] == "secure" {
elasticsearch {
hosts => ["172.20.2.207:9200"]
index => "secure-%{+YYYY.MM.dd}"
}
}
if [type] == "http" {
elasticsearch {
hosts => ["172.2.20.207:9200"]
index => "http-%{+YYYY.MM.dd}"
}
}
if [type] == "nginx" {
elasticsearch {
hosts => ["172.20.2.207:9200"]
index => "nginx-%{+YYYY.MM.dd}"
}
}
  }
运行看看效果如何
bin/logstash -f config/elk.conf
其实前面已经展示了。这里不再重复展示。




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-668694-1-1.html 上篇帖子: ELK详细安装配置 下篇帖子: centos6安装部署ELK以及后续操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表