当前位置: 移动技术网 > IT编程>开发语言>JavaScript > ELK技术1--收集nginx正确和错误日志

ELK技术1--收集nginx正确和错误日志

2020年07月19日  | 移动技术网IT编程  | 我要评论

一.ELK介绍

1.ELK的提出

1.一般我们需要进行日志分析场景:直接在日志文件中 grep、awk 就可以获得自己想要的信息。
2.但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档、文本搜索太慢怎么办、如何多维度查询。需要集中化的日志管理,所有服务器上的日志收集汇总。
3.常见解决思路是建立集中式日志收集系统,将所有节点上的日志统一收集,管理,访问。
4.ELK提供了一整套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用。目前主流的一种日志系统。

2.ELK简介

ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输到Elasticsearch,官方也推荐此工具。

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

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

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

二.日志收集分类

代理层:nginx,haproxy
web层:nginx,tomcat
数据库层:mysql,redis,mongo,elasticsearch
操作系统层:source,message

三.安装部署ELK

架构图
在这里插入图片描述
环境准备:

[root@m01 ~]# cat /etc/hosts
10.0.0.51 db01
10.0.0.52 db02
10.0.0.53 db03
[root@m02 ~]# cat /etc/hosts
10.0.0.51 db01
10.0.0.52 db02
[root@m03 ~]# cat /etc/hosts
10.0.0.51 db01
10.0.0.53 db03

1.安装配置elasticsearch

在db01操作:
安装部署elasticsearch:
1.安装Java
yum install -y java-1.8.0-openjdk.x86_64
2.下载安装软件
mkdir /data/soft -p
cd /data/soft/
rz -E 
#上传elasticsearch-6.6.0.rpm      
rpm -ivh elasticsearch-6.6.0.rpm
3.配置启动
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
4.检查是否安装成功(要隔一会才会启动起来)
netstat -lntup|grep 9200
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      6440/java
tcp6       0      0 ::1:9200                :::*                    LISTEN      6440/java
curl 127.0.0.1:9200
[root@m01 ~]#mkdir /data/elasticsearch
[root@m01 ~]#chown -R elasticsearch:elasticsearch /data/elasticsearch/
[root@m01 ~]# chmod a+x /data
[root@m01 ~]# chmod a+x /data/elasticsearch 
[root@m01 ~]# ll -d /data
drwxr-xr-x 5 root root 60 Jul 10 17:15 /data
[root@m01 ~]# ll -d /data/elasticsearch/
drwxr-xr-x 3 elasticsearch elasticsearch 19 Jul 10 18:57 /data/elasticsearch/
[root@m01 ~]# vim /etc/elasticsearch/elasticsearch.yml 
node.name: node-1		                            #节点名称
path.data: /data/elasticsearch                    	#数据目录
path.logs: /var/log/elasticsearch                	#日志目录
bootstrap.memory_lock: true	                        #锁定内存
network.host: 10.0.0.51		                        #绑定IP地址
http.port: 9200			                            #端口号
[root@m01 ~]#vim /etc/elasticsearch/jvm.options
#如果是1G的内存,需要修改一下这里的内存。改为512m
#如果是1G内存,则默认就可以了
-Xms1g     
-Xmx1g

内存限制:
1.不要超过32G
2.最大最小内存设置为一样
3.配置文件设置锁定内存
4.至少给服务器本身空余50%的内存

2.修改配置:
[root@m01 ~]#cd /etc/elasticsearch/
[root@m01 elasticsearch]#systemctl edit elasticsearch
# 增加如下参数
[Service]
LimitMEMLOCK=infinity

3.重新启动:
systemctl daemon-reload
systemctl restart elasticsearch
systemctl status elasticsearch

4.查看是否启动成功
[root@m01 ~]#  netstat -lntup|grep 9200
tcp6       0      0 10.0.0.51:9200          :::*                    LISTEN      8344/java
所有机子操作:
配置时间同步:
yum install ntpdate -y

2.安装配置kibana

在db01操作:
[root@m01 ~]# mkdir /data/soft -p
[root@m01 ~]# cd /data/soft
[root@m01 soft]# ls
elasticsearch-6.6.0.rpm
filebeat-6.6.0-x86_64.rpm
kibana-6.6.0-x86_64.rpm
安装配置kibana:
[root@m01 soft]# rpm -ivh kibana-6.6.0-x86_64.rpm
[root@m01 soft]#vim /etc/kibana/kibana.yml
server.port: 5601
server.host: "10.0.0.51"
server.name: "m01"
elasticsearch.hosts: ["http://10.0.0.51:9200"]
kibana.index: ".kibana" 

systemctl start kibana
systemctl status kibana
[root@m01 soft]# netstat -lntup|grep 5601
tcp        0      0 10.0.0.51:5601          0.0.0.0:*               LISTEN      6250/node

查看:
在这里插入图片描述
http://10.0.0.51:5601/app/kibana#/home?_g=()
在这里插入图片描述

四.使用filebeat配置nginx日志收集

1.filebeat收集nginx日志信息

在db01操作:
收集nginx日志
1.安装nginx和ab工具
[root@m01 soft]# yum install nginx httpd-tools -y
2.启动测试
[root@m01 soft]#systemctl start nginx
[root@m01 soft]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1888/nginx: master  
tcp6       0      0 :::80                   :::*                    LISTEN      1888/nginx: master
[root@m01 soft]# ab -c 10 -n 100 10.0.0.51/
[root@m01 soft]# ab -c 10 -n 100 10.0.0.51/test.html
3.查看日志
[root@m01 soft]# tail -f /var/log/nginx/access.log 
[root@m01 soft]# rpm -ivh filebeat-6.6.0-x86_64.rpm
[root@m01 soft]# cp /etc/filebeat/filebeat.yml /root
[root@m01 soft]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
[root@m01 filebeat]# systemctl restart filebeat

查看:
在这里插入图片描述
在这里插入图片描述
用Kibana 分析日志:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.filebeat收集nginx的JSON日志信息

在这里插入图片描述

在db01操作:
[root@m01 filebeat]# vim /etc/nginx/nginx.conf
#找到配置文件中log_format json 这部分,然后在其部分结束后面添加这一部分
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;
access_log  /var/log/nginx/access.log  ;#删除这一行
[root@m01 filebeat]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@m01 filebeat]# >/var/log/nginx/access.log
[root@m01 filebeat]# systemctl restart nginx
[root@m01 ~]#  ab -c 10 -n 100 10.0.0.51/

因为跟之前的格式不一样了,所以删除之前的filebeat和kibana的项目,然后进行操作
删除filebeat项目:
在这里插入图片描述
删除kibana项目
在这里插入图片描述
问题及反思:

第一种情况:不断重启filebeat时,写入重复数据

[root@m01 filebeat]# systemctl restart filebeat
在这里插入图片描述
第二种情况:停掉filebeat时nginx写入数据
[root@m01 ~]# >/var/log/nginx/access.log
[root@m01 ~]# ab -c 10 -n 100 10.0.0.51/
[root@m01 ~]# systemctl stop filebeat
[root@m01 ~]# ab -c 10 -n 100 10.0.0.51/
[root@m01 ~]# systemctl start filebeat
[root@m01 ~]# ab -c 20 -n 20 10.0.0.51/
在这里插入图片描述
此时nginx改为JSON格式后,filebeat并没有识别,因此需要修改下面配置文件

在db01操作:
[root@m01 soft]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
#添加这2行(注意这2行代码的位置,不然会实现不了效果):
   json.keys_under_root: true//这2行把整串字符变成键值处理,可以针对某个键值处理)
   json.overwrite_keys: true

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  删除filebeat和kinaba的项目,然后执行下面命令
[root@m01 filebeat]# systemctl restart filebeat
制造日志:
[root@m01 ~]# >/var/log/nginx/access.log
[root@m01 ~]#  ab -c 100 -n 100 http://10.0.0.51/abcx200s404.html

查看
在这里插入图片描述

3.filebeat收集nginx的自定义索引日志信息

3.1 filebeat收集单台nginx的自定义索引日志信息

按照需求自定义索引,使得可以不出现那么多干扰的搜索功能

先删除filebeat和kibana的项目,然后进行操作
在db01操作:
[root@m01 filebeat]# >/etc/filebeat/filebeat.yml
[root@m01 filebeat]#  vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true

setup.kibana:
  host: "10.0.0.51:5601"

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  index: "nginx-%{[beat.version]}-%{+yyyy.MM}"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true

~
~
"filebeat.yml" 19L, 409C written
[root@m01 filebeat]# systemctl restart filebeat
[root@m01 filebeat]# ab -c 100 -n 100 10.0.0.51/

查看:
在这里插入图片描述
在这里插入图片描述

3.2 filebeat收集多台nginx的自定义索引日志信息

部署3台机子:
先删除filebeat和kibana的项目,然后进行操作

在db02和db03操作:
mkdir /data/soft -p

在db01操作:
cd /data/soft
上传filebeat-6.6.0-x86_64.rpm 
scp filebeat-6.6.0-x86_64.rpm db02:/data/soft/
scp filebeat-6.6.0-x86_64.rpm db03:/data/soft/
scp /etc/nginx/nginx.conf db02:/etc/nginx
scp /etc/nginx/nginx.conf db03:/etc/nginx
scp /etc/filebeat/filebeat.yml db02:/etc/filebeat/filebeat.yml
scp /etc/filebeat/filebeat.yml db03:/etc/filebeat/filebeat.yml
 >/var/log/nginx/access.log
 ab -c 100 -n 100 10.0.0.51/
 ab -n 100 -c 100 http://10.0.0.52/
 ab -n 100 -c 100 http://10.0.0.53/
ab -n 100 -c 100 http://10.0.0.52/db02.html
ab -n 100 -c 100 http://10.0.0.53/db03.html

在db02和db03操作:
cd /data/soft/
rpm -ivh filebeat-6.6.0-x86_64.rpm
yum makecache fast
yum install -y nginx
nginx -t
systemctl start nginx
systemctl start filebeat
tail -f /var/log/nginx/access.log
cat /etc/filebeat/filebeat.yml

访问:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.filebeat收集nginx的正确和错误日志信息

4.1 filebeat收集单台nginx的正确和错误日志信息

先删除filebeat和kibana的项目,然后进行操作
在db01操作:
[root@m01 soft]# vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true
  tags: ["access"]

- type: log
  enabled: true
  paths:
    - /var/log/nginx/error.log
  tags: ["error"]

setup.kibana:
  host: "10.0.0.51:5601"

output.elasticsearch:
  hosts: ["10.0.0.51:9200"]
  #index: "nginx-%{[beat.version]}-%{+yyyy.MM}"
  indices:
    - index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "access"
    - index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
      when.contains:
        tags: "error"
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.enabled: false
setup.template.overwrite: true

[root@m01 soft]# systemctl restart filebeat
[root@m01 soft]# ab -n 100 -c 100 http://10.0.0.51/db01.html

查看:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.2 filebeat收集多台nginx的正确和错误日志信息

上面的操作只能查一台,下面让其可以查多台分类日志:

在db01操作:
[root@m01 soft]# scp /etc/filebeat/filebeat.yml db02:/etc/filebeat/filebeat.yml
root@db02's password:
filebeat.yml                                              100%  729   584.9KB/s   00:00
[root@m01 soft]# scp /etc/filebeat/filebeat.yml db03:/etc/filebeat/filebeat.yml
root@db03's password:
filebeat.yml                                              100%  729   715.8KB/s   00:00
在db02和db03操作:
[root@m02 soft]# systemctl restart filebeat
[root@m03 soft]# systemctl restart filebeat
在db01操作:
[root@m01 soft]# ab -n 100 -c 100 http://10.0.0.51/db01.html
[root@m01 soft]# ab -n 100 -c 100 http://10.0.0.52/db02.html
[root@m01 soft]# ab -n 100 -c 100 http://10.0.0.53/db03.html

在这里插入图片描述
在这里插入图片描述

本文地址:https://blog.csdn.net/weixin_44736359/article/details/107288965

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网