学习ELK日志平台(一)
一、需求及基础:
场景:
1、开发人员不能登录线上服务器查看详细日志
2、各个系统都有日志,日志数据分散难以查找
3、日志数据量大,查询速度慢,或者数据不够实时
4、一个调用会涉及到多个系统,难以在这些协调中快速定位数据
二、ELS的概念:
elasticsearch:搜索引擎,提供索引,搜索功能
Logstash:接收,处理,转发日志
Kibana:独立的、美观的图形数据web界面
三、安装及配置
1、 elasticsearch安装
安装java环境,1.8.20或以上的版本
- 1、安装Java环境
- tar xf jdk-8u201-linux-x64.tar.gz
- vim /etc/profile
- export JAVA_HOME=/root/jdk
- export PATH=$JAVA_HOME/bin:$PATH
- source /etc/profile
- 2、安装elasticsearch
- wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.0.tar.gz
- tar xf elasticsearch-1.7.0.tar.gz
- ln -s elasticsearch-1.7.0 /usr/local/elasticsearch
- 3、修改配置文件
- grep -n '^[a-z]' /usr/local/elasticsearch/config/elasticsearch.yml
- 32:cluster.name: elasticsearch #必须修改
- 40:node.name: "stu02" #必须修改
- 47:node.master: true
- 51:node.data: true
- 107:index.number_of_shards: 5
- 111:index.number_of_replicas: 1
- 145:path.conf: /usr/local/elasticsearch/config
- 149:path.data: /usr/local/elasticsearch/data
- 159:path.work: /usr/local/elasticsearch/work
- 163:path.logs: /usr/local/elasticsearch/logs
- 167:path.plugins: /usr/local/elasticsearch/plugins
- 184:bootstrap.mlockall: true
- 4、启动
- /usr/local/elasticsearch/bin/elasticsearch -d #(可加入参数,-Xms512m -Xmx512m)
- netstat -lnt | egrep '9200|9300'
- jps -lvm
测试:
- curl 127.0.0.1:9200
- curl -i -XGET http://192.168.4.16:9200
安装ELS监控管理插件
- 1、离线安装插件
- wget http://download.elasticsearch.org/elasticsearch/marvel/marvel-latest.zip
- /usr/local/elasticsearch/bin/plugin -i marvel -u file:///tmp/marvel-latest.zip
- 2、在线安装插件
- /usr/local/elasticsearch/bin/plugin -i elasticsearch/marvel/latest/
- /usr/local/elasticsearch/bin/plugin install mobz/elasticsearch-head
- 3、卸载插件
- /usr/local/elasticsearch/bin/plugin -r marvel
生产可能需要配置已下:
- max_file_descriptors: 64000
- /etc/sysctl.conf
- sysctl -w vm_max_count=262144
2、 logstash安装
- 1、安装Java环境
- tar xf jdk-8u201-linux-x64.tar.gz
- vim /etc/profile
- export JAVA_HOME=/root/jdk
- export PATH=$JAVA_HOME/bin:$PATH
- source /etc/profile
- 2、安装logstash
- wget https://download.elastic.co/logstash/logstash/logstash-1.5.3.tar.gz
- tar xf logstash-1.5.3.tar.gz
- ln -s /root/logstash-1.5.3/ /usr/local/logstash
标准输入和标准输出
- /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'
使用ruby进行更详细的输出
- /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { stdout{codec => rubydebug}}'
输出到elasticsearch
- /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch
{ host => "192.168.4.16" protocol => "http"} }'
读取日志并输出到/tmp下
- vim /etc/logstash/conf.d/logstash.conf
input {- file {
- path => "/tmp/messages"
- }
- }
- output {
- file {
- path => "/tmp/log-%{+YYYY-MM-dd}messages.gz"
- gzip => true
- }
- }
测试配置文件
- /usr/local/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf -t
启动服务
- /usr/local/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf
测试
- cat /var/log/messages >> /tmp/messages
ll /tmp/log-2019-02-11messages.gz
把输出直接传输到elasticsearch
- vim /etc/logstash/conf.d/logstash.conf
- input {
- file {
- path => "/tmp/messages"
- }
- }
- output {
- file {
- path => "/tmp/log-%{+YYYY-MM-dd}.messages.gz"
- gzip => true
- }
- elasticsearch {
- host => ["192.168.4.16"]
- protocol => "http"
- index => "system-message-%{+YYYY.MM.dd}"
- }
- }
在集群管理平台查看结果
打开浏览器输入:http://192.168.4.16:9200/_plugin/head/
将logstash输出给redis
一台logstash的配置文件
- input {
- file {
- path =>"/tmp/messages"
- }
- }
- output {
- redis {
- data_type => "list"
- key => "system-message"
- host => "192.168.4.17"
- port => "6379"
- db => "0"
- }
- }
另一台logstash的配置文件
- input {
- redis {
- data_type => "list"
- key => "system-message"
- host => "192.168.4.17"
- port => "6379"
- db => "0"
- }
- }
- output {
- elasticsearch {
- host => ["192.168.4.16"]
- protocol => "http"
- index => "redis-message-%{+YYYY.MM.dd}"
- }
- }
logstash日志json格式
安装nginx,将nginx的日志设置为json模式
- log_format logstash_json '{"@timestamp":"$time_iso8601",' #定义日志格式logstash_json
- '"host":"$server_addr",'
- '"clientip":"$remote_addr",'
- '"size":$body_bytes_sent,'
- '"responsetime":$request_time,'
- '"upstreamtime":"$upstream_response_time",'
- '"upstreamhost":"$upstream_addr",'
- '"http_host":"$host",'
- '"url":"$uri",'
- '"domain":"$host",'
- '"xff":"$http_x_forwarded_for",'
- '"referer":"$http_referer",'
- '"agent":"$http_user_agent",'
- '"status":"$status"}';
- access_log /var/log/nginx/json.access.log logstash_json; #日志文件保存路径及使用上面定义的日志格式logstash_json
配置logstash将nginx的json日志并写入到redis
一台配置
- input {
- file {
- path => "/var/log/nginx/json.access.log" #读取指定的json格式的日志
- codec => "json" #指定json格式
- }
- }
- output {
- redis {
- data_type => "list"
- key => "nginx-json-log" #nginx的json格式日志的key名称
- host => "192.168.4.17"
- port => "6379"
- db => "1"
- }
- }
另一台
- input {
- redis {
- data_type => "list"
- key => "nginx-json-log"
- host => "192.168.4.17"
- port => "6379"
- db => "1"
- }
- }
- output {
- elasticsearch {
- host => ["192.168.4.16"]
- protocol => "http"
- index => "nginx-json-log-%{+YYYY.MM.dd}"
- }
- }
使用ab访问nginx测试日志
- ab -n1000 -c10 http://192.168.4.16:81 #一共1000个请求,每次并发10个,即100次请求完成
打开浏览器输入:http://192.168.4.16:9200/_plugin/head/
3、kibana安装
- 1、安装
- wget https://download.elastic.co/kibana/kibana/kibana-4.1.1-linux-x64.tar.gz
- tar xf kibana-4.1.1-linux-x64.tar.gz
- ln -s /root/kibana-4.1.1-linux-x64/ /usr/local/kibana
- 2、修改配置
- vim /usr/local/kibana/config/kibana.yml
- elasticsearch_url: "http://192.168.4.16:9200"
- pid_file: /var/run/kibana.pid
- log_file: /usr/local/kibana/kibana.log
- 3、启动
- /usr/local/kibana/bin/kibana #直接启动
- nohup /usr/local/kibana/bin/kibana & #后台启动
访问测试:默认监听端口5601
打开浏览器输入:http://192.168.4.16:5601
配置索引:索引的名称要和logstash的output生成的索引能进行匹配才可以
查看数据:默认显示最新的500个文档
数据精确搜索:
搜索高级语法:
- status:404 OR status:500 #搜索状态是404或者是500之一的
- status:301 AND status:200 #搜索即是301和200同时匹配的
- status:[200 TO 300] #搜索指定范围的
其他的常用模块:
1、系统日志收集---> syslog:配置syslog结果写入到elasticsearch,指定端口514,主机就是要收集日志的服务器IP地址,即可使用
2、访问日志:nginx转换成json格式
3、错误日志:使用codec插件:
- https://www.elastic.co/guide/en/logstash/1.5/codec-plugins.html
- input {
- stdin {
- codec => multiline { #多行日志,比如java的日志
- pattern => "^\s" #pattern => ".*\t.*" #找到换行符,会把多行认为是一行,即会把当前行和上一行合成一行,直到有换行符结束
- what => "previous"
- }
- }
- }
4、运行日志 codec => json,如果不是json要使用grok进行匹配,相对比较麻烦,如果丢日志就看logstash.log,另外检查日志是否有效的json格式:
json效验地址:http://www.bejson.com/
5、kibana的时区和时间问题:kibana会自动根据浏览器将时间加8小时,通过logstash写入会自动解决,如果通过python脚本等写入会产生时间问题
6、在地图显示IP具体来源地址:
- https://www.elastic.co/guide/en/logstash/1.5/filter-plugins.html
7、条件判断:
- input {
- file {
- type => "apache"
- path => "/var/log/apache.log"
- }
- file {
- type => "tomcat"
- path => "/var/log/tomcat.log"
- }
- }
- filter {
- if [type] == "apache" { #假如索引为apache,就执行以下操作
- redis {
- data_type => "list"
- key => "system-message-jack"
- host => "192.168.10.205"
- port => "6379"
- db => "0"
- }
- if [type] == "tomcat" { #假如索引为tomcat,就执行一次操作
- redis {
- data_type => "list"
- key => "system-message-tomcat"
- host => "192.168.10.205"
- port => "6379"
- db => "1" #写不同的数据库
- }
- }
测试logstash配置文件语法是否正确
- /etc/init.d/logstash configtest
8、将tomcat日志的格式定义为json的格式:
- directory="logs" prefix="localhost_access_log." suffix=".log"
- pattern="{"client":"%h", "client user":"%l",
"authenticated":"%u", "access time":"%t",
"method":"%r", "status":"%s",
"send bytes":"%b", "Query?string":"%q",
"partner":"%{Referer}i", "Agent version":"%{User-Agent}i"}"/>
取到的日志结果为:
- {"client":"180.95.129.206", "client user":"-", "authenticated":"-", "access time":"[20/Apr/2016:03:47:40 +0000]", "method":"GET /image/android_logo.png HTTP/1.1", "status":"200", "send bytes":"1915", "Query string":"", "partner":"http://mobile.weathercn.com/index.do?id=101160101&partner=1000001003", "Agent version":"Mozilla/5.0 (Linux; U; Android 5.1.1; zh-cn; NX510J Build/LMY47V) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/6.6 Mobile Safari/537.36"}
学习ELK日志平台(一)的更多相关文章
- 学习ELK日志平台(二)
一.ELK介绍 1.1 elasticsearch 1.1.1 elasticsearch介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索 ...
- 学习ELK日志平台(四)
一:需求及基础: 场景: 1.开发人员不能登录线上服务器查看详细日志 2.各个系统都有日志,日志数据分散难以查找 3.日志数据量大,查询速度慢,或者数据不够实时 4.一个调用会涉及到多个系统,难以在这 ...
- 学习ELK日志平台(三)
ELK(elasticsearch.logstash.kibana) Elastic Stack是原ELK Stack在5.0版本加入Beats套件后的新称呼 解决痛点: 开发人员不能登录线上serv ...
- 学习ELK日志平台(五)
ELK Stack 通常情况下: 1,开发人员是不能登录线上服务器查看日志信息 2,各个系统的日志繁多,日志数据分散难以查找 3,日志数据量较大,查询速度慢,数据不够实时性 4,一个调用会涉及到多个系 ...
- ELK 日志平台构建
elastic中文社区 https://elasticsearch.cn/ 完整参考 ELK实时日志分析平台环境部署--完整记录 https://www.cnblogs.com/kevingrace/ ...
- elk日志平台搭建小记
最近抽出点时间,搭建了新版本的elk日志平台 elastaicsearch 和logstash,kibana和filebeat都是5.6版本的 中间使用redis做缓存,版本为3.2 使用的系统为ce ...
- Springboot项目使用aop切面保存详细日志到ELK日志平台
上一篇讲过了将Springboot项目中logback日志插入到ELK日志平台,它只是个示例.这一篇来看一下实际使用中,我们应该怎样通过aop切面,拦截所有请求日志插入到ELK日志系统.同时,由于往往 ...
- Springboot项目搭配ELK日志平台
上一篇讲过了elasticsearch和kibana的可视化组合查询,这一篇就来看看大名鼎鼎的ELK日志平台是如何搞定的. elasticsearch负责数据的存储和检索,kibana提供图形界面便于 ...
- 亿级 ELK 日志平台构建部署实践
本篇主要讲工作中的真实经历,我们怎么打造亿级日志平台,同时手把手教大家建立起这样一套亿级 ELK 系统.日志平台具体发展历程可以参考上篇 「从 ELK 到 EFK 演进」 废话不多说,老司机们座好了, ...
随机推荐
- 2021年国内外五大BI厂商_商业智能工具推荐
每家公司对于BI工具的使用都有自己的特点和期望,所以当面对国内外那么多的BI厂商时,大家可能会觉得难以选择.今天我将会为大家介绍国内外五大BI厂商,对于它们做出一些分析,让大家更了解这些BI厂商. ...
- Hadoop2.7.2源码编译过程
目录 准备工作 jar包安装 源码编译 准备工作 CentOS可以联网,验证:ping www.baidu.com 是畅通的 jar 包准备(hadoop 源码.JDK8.maven.ant .pro ...
- docker安装以及常用命令
yum-config-manager 对/etc/yum.repos.d/下的.repo文件进行增删改查 yum install -y yum-utils 添加docker源,由于yum源的docke ...
- 浏览器无插件播放rtsp流解决方案
1. 安装 FFmpeg 参考 CentOS下安装FFmpeg,特别详细. 我遇到的错误和解决办法: 缺少lame ffmpeg+libmp3lame库源码安装教程(CentOS) make ffmp ...
- selenium+python安装
整理了下selenium+python环境搭建,搭建了很多次但每次都还是手忙脚乱,今天用心整理下 selenium 是用于测试 Web 应用程序用户界面 (UI) 的常用框架,并且 Selenium ...
- Caffe2源码解析
写在前面 上一篇文章对Caffe2中的core模块进行了简单拆解Caffe2源码解析之core,本篇给出其它模块的拆解,目的是大致了解每个模块的内容和目标,进一步理解Caffe2的整体框架.内容不多, ...
- 初探 Elasticsearch,学习笔记第一讲
1. ES 基础 1.1 ES定义 ES=elaticsearch简写, Elasticsearch是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储.检索数据:本身扩展 ...
- Spring和MyBatis框架整合
Spring集成MyBatis 使用MyBatis,需要创建MyBatis框架中的某些对象,使用这些对象,就能使用mybatis提供的功能了. 需要有Dao接口的代理对象,例如StudentDao接口 ...
- ActiveMQ 笔记—01
- 一致性 hash 环
一致性 hash 环 最近做项目 做了一个分发器 ,需要 根据请求携带的参数 把请求分发到 不同的服务器上面,最终我选择使用 一致性hash 环 来实现 ,本篇 就主要讲解一下 一致性hash环 它的 ...