Filebeat + es +kibana

背景


	分析--->报警	Kibana
	展示			Kibana
	存储			ElasticSerach
	过滤			Logstash
	收集			Filebeat, Logstash

logstash:因为是由java写的,所以在它收集日志时非常占用业务系统的资源,从而影响线上业务,所以我们将其替换为filebeat.
filebeat:为轻量的日志收集组件,会让业务系统的运行更加的稳定.


由于痛点:
	1.出现故障,要排查的日志非常的多,没有办法很快定位;
	2.统计TOP,PV,UV,IP信息; awk无法满足需求;  | elkstack | [ 在线活跃用户, 订单交易金额; ]
所以出现了日志收集: logstash 或者 filebeat

收集日志到es(elasticsearch)存储上有两种方法:
	1.直接用filebeat将日志格式转为json格式
	2.上面方法不可用,用filebeat收集日志,接入logstash进行处理,再收集到es存储


PS:
	app日志中,若是出现了如下类型的,我们的filebeat不能够直接修改日志格式将其修改为json格式,下面会解决.
	例如:
		[商品名] [用户] [订单数] [交易金额] 

Nginx篇


虚拟主机收集+Nginx错误日志收集
#nginx主配置文件
[root@web01 ~]# cat /etc/nginx/nginx.conf 
user  www;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  10000;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

log_format json '{ "time_local": "$time_local", '
						  '"remote_addr": "$remote_addr", '
						  '"referer": "$http_referer", '
						  '"request": "$request", '
						  '"status": $status, '
						  '"bytes": $body_bytes_sent, '
						  '"agent": "$http_user_agent", '
						  '"x_forwarded": "$http_x_forwarded_for", '
						  '"up_addr": "$upstream_addr",'
						  '"up_host": "$upstream_http_host",'
						  '"upstream_time": "$upstream_response_time",'
						  '"request_time": "$request_time"'
	'}';

    access_log  /var/log/nginx/access.log  json;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}


#网站配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/elk.qian.com.conf 
server {
	listen 80;
	server_name elk.qian.com;
	root /data;
	access_log /var/log/nginx/elk.log json;

	location / {
		index index.html;
	}

}


[root@web01 ~]# cat /etc/nginx/conf.d/blog.ming.com.conf 
server {
	listen 80;
	server_name blog.ming.com;
	root /code;
	access_log /var/log/nginx/blog.log json;

	location / {
		index index.html;
	}

}



#filebeat配置文件
[root@web01 ~]# cat /etc/filebeat/nginx.yml 
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/elk.log
  json.keys_under_root: true  #Flase会将json解析的格式存储至messages,改为true则不存储至message
  json.overwrite_keys: true   #覆盖默认message字段,使用自定义json格式的key
  tags: nginx_elk

- type: log
  enabled: true
  paths:
    - /var/log/nginx/blog.log
  json.keys_under_root: true  #Flase会将json解析的格式存储至messages,改为true则不存储至message
  json.overwrite_keys: true   #覆盖默认message字段,使用自定义json格式的key
  tags: nginx_blog

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  json.keys_under_root: true  #Flase会将json解析的格式存储至messages,改为true则不存储至message
  json.overwrite_keys: true   #覆盖默认message字段,使用自定义json格式的key
  tags: nginx_error


output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  indices:
    - index: "nginx-elk-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx_elk"
    - index: "nginx-blog-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx_blog"
    - index: "nginx-error-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "nginx_error"



setup.ilm.enabled: false
setup.template.name: "nginx"       #定义模板名称
setup.template.pattern: "nginx-*"  #定义模板的匹配索引名称

#setup.template.settings:		#开启自定义分片数量,不建议开启
#  index.number_of_shards: 3	#主分片数为:3
#  index.number_of_replicas: 1	#副本数为:1

Tomcat篇+错误日志收集


#Tomcat主配置文件
[root@web01 ~]# cat /soft/tomcat/conf/server.xml 
........此处省略配置信息
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

      <Host name="tomcat.blog.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="blog_tomcat_access_log" suffix=".txt"
               pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}" />

      </Host>

    </Engine>
  </Service>
</Server>



Filebeat配置文件
[root@web01 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /soft/tomcat/logs/blog_tomcat_access_log.*.txt
  json.keys_under_root: true  #Flase会将json解析的格式存储至messages,改为true则不存储至message
  json.overwrite_keys: true   #覆盖默认message字段,使用自定义json格式的key
  tags: tomcat_blog

- type: log
  enabled: true
  paths:
    - /soft/tomcat/logs/catalina.out
  tags: tomcat_error
  multiline.pattern: '^\d{2}'		#此处应注意,因为Tomcat的错误日志不同于网站日志,不能将其改为json格式,可用此配置,具体可以 https://www.elastic.co/guide/en/beats/filebeat/current/multiline-examples.html 查看官网
  multiline.negate: true
  multiline.match: after
  multiline.max_lines: 1000


output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
  indices:
    - index: "tomcat_blog-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "tomcat_blog"
    - index: "tomcat_error-%{[agent.version]}-%{+yyyy.MM.dd}"
      when.contains:
        tags: "tomcat_error"




setup.ilm.enabled: false
setup.template.name: "tomcat"       #定义模板名称
setup.template.pattern: "tomcat-*"  #定义模板的匹配索引名称


系统篇

#用rsyslog工具将系统配置集中到一个日志中去,由filebeat收集

[root@web01 ~]# yum install rsyslog -y
[root@web01 ~]# vim /etc/rsyslog.conf		#修改rsyslog配置文件
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
*.*     /var/log/ming.log			#添加
[root@web01 ~]# systemctl start rsyslog




#filebeat配置文件
#基本配置
[root@web01 ~]# cat  /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/text.log

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]


#增强版配置
[root@web01 ~]# cat  /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/text.log

  include_lines: ['^ERR', '^WARN', 'sshd']		  # 仅包含,错误信息,警告信息,sshd的相关配置,其他的都会过滤掉

output.elasticsearch:
  hosts: ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]

续:请看下面关于ELK的文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

登高·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值