

使用Filebeat和Elasticsearch分析Nginx日志
source link: https://easeapi.com/blog/blog/145-elasticsearch-nginx.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本文实战记录使用Filebeat、Kibana、Elasticsearch等软件搭建搜集分析Nginx日志的系统。Filebeat主要负责将Nginx日志数据作为数据源输送至Elasticsearch。

作为入门介绍文章,首先说明下几个软件之间的关系:Elasticsearch是基于RESTful风格的分布式全文检索和数据分析引擎;Kibana是对Elasticsearch数据进行可视化展示的Web系统;而Filebeat负责将Nginx日志数据作为数据源输送至Elasticsearch。
本文服务器环境:Ubuntu 20.04,仅配置一个服务器节点。
环境准备:JAVA
需要安装Java Runtime Environment (JRE)和Java Development Kit (JDK)。
sudo apt update
apt install default-jre
java -version
sudo apt install default-jdk
javac -version
参考:How To Install Java with Apt on Ubuntu 20.04
Elasticsearch
安装Elasticsearch
#将Elasticsearch公共GPG密钥导入APT:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
#添加Elastic源(笔者试验时elasticsearch最新是7.xx版本):
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee –a /etc/apt/sources.list.d/elastic-7.x.list
#安装elasticsearch
sudo apt update
sudo apt install elasticsearch
配置Elasticsearch
Elasticsearch的配置文件位于:
sudo vim /etc/elasticsearch/elasticsearch.yml
#重要配置项:
#集群名称,相同网络下相同的集群名的节点为一个集群。
cluster.name
#存放数据的目录
path.data
#主机地址
network.host: localhost
#服务端口号
http.port: 9200
管理Elasticsearch:
#启动/停止/重启
sudo systemctl start/stop/restart elasticsearch.service
#注册开机自启动
sudo systemctl enable elasticsearch.service
验证Elasticsearch服务是否启动
root@easeapi:~# curl -X GET localhost:9200
{
"name" : "easeapi",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "easeapi-wDpaWmxXHJ8yu7a",
"version" : {
"number" : "7.11.2",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "c4ca4238a0b923820dcc509a6f75849b",
"build_date" : "2021-03-01T08:54:38.141101Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
修改Elasticsearch占用内存大小
在小内存服务器上可以适当调小。Elasticsearch 7.x的配置方式较之前有不同,需要在/etc/elasticsearch/jvm.options.d目录新建以.options为后缀的文件。修改内存用量的配置如下:
vim jvm.options.d/jvm.options
-Xms1g
-Xmx1g
Kibana
上一步已添加源,直接安装即可。因为版本依赖的关系,先安装Elasticsearch再安装Kibana。
sudo apt install kibana
Elasticsearch的配置文件位于:
vim /etc/kibana/kibana.yml
重要配置项:
server.port: 5601
server.host: "localhost"
server.basePath: "/kibana"//保持和nginx的访问目录配置一致
server.rewriteBasePath: true
elasticsearch.hosts: ["http://localhost:9200"]
#配置为中文
i18n.locale: "zh-CN"
管理Kibana
#启动/停止/重新启动
sudo systemctl start/stop/restart kibana
#加入自启动
sudo systemctl enable kibana
Filebeat
Filebeat是轻量级的数据收集处理工具,具有占用资源少的优点。
sudo apt install filebeat
#配置文件
sudo vim /etc/filebeat/filebeat.yml
#主要配置项
setup.kibana:
host: "localhost:5601"
output.elasticsearch:
hosts: ["localhost:9200"]
管理Filebeat
#启动/停止/重新启动
sudo systemctl start/stop/restart filebeat
#加入自启动
sudo systemctl enable filebeat
#测试连通性
filebeat test output
Filebeat通过Module进行扩展以支持不同数据格式的文件。Module相关的操作命令:
#查看模块状态
sudo filebeat modules list
#启动指定Module
sudo filebeat modules enable nginx
以Nginx为例,在启动Nginx的Module后,可以看到/etc/filebeat/modules.d目录的文件nginx.yml..disabled变成了nginx.yml。
vim /etc/filebeat/modules.d/nginx.yml
#配置nginx的日志路径。
- module: nginx
access:
enabled: true
var.paths: ["/var/log/nginx/access.log"]
error:
enabled: true
ar.paths: ["/var/log/nginx/error.log"]
配置完成后,使用filebeat setup
写入配置,然后启动filebeat即可。
去除冗余字段
Filebeat默认输出的字段有些情况下可能无用,造成内容冗余。可以将冗余字段去除:
sudo vim /etc/filebeat/filebeat.yml
processors:
#- add_host_metadata:
# when.not.contains.tags: forwarded
#- add_cloud_metadata: ~
#- add_docker_metadata: ~
#- add_kubernetes_metadata: ~
- drop_fields:
fields: ["agent", "nginx", "log.offset", "fileset", "input"]
根据需要补充需要去除的字段。
依次启动三个组件:
sudo service elasticsearch start
sudo service kibana start
sudo service filebeat start
Nginx站点配置kibana访问
以Nginx站点(https://easeapi.com)的二级目录作为访问路径为例:
server {
...
location /kibana/ {
proxy_pass http://127.0.0.1:5601;
rewrite ^/kibabna/(.*)$ /$1 break;
}
}
通过https://easeapi.com/kibana 即可访问到kibana。
为kibana配置密码
apt install apache2-utils
htpasswd -c /home/htpasswd <username>
将生成一个基于HTTP Basic Authentication认证的密码文件。在Nginx站点中增加配置:
server {
auth_basic 'easeapi center';
auth_basic_user_file /home/htpasswd;
}
当访问对应页面时,会提示输入密码验证。
打开kibana页面后,点击「添加数据」,选择「Nginx日志」(会有Filebeat模块解析Nginx日志的操作指南),按指引操作完成。
How to Install ELK on Ubuntu 20.04
使用CDN提升网站访问速度
优化Nginx配置提升网站性能
Let’s Encrypt acme.sh 泛域名证书
utf8mb4:MYSQL中使用Emoji
Recommend
-
164
Elasticsearch分布式搜索和分析引擎。具有高可伸缩、高可靠和易管理等特点。基于 Apache Lucene 构建,能对大容量的数据进行接近实时的存储、搜索和分析操作。 Logstash日志收集器。搜集各种数据源,并对数据进行过滤、分析、格式化等操作,然后存储到 Elasticsear...
-
70
海量日志下的日志架构优化:filebeat+logstash+kafka+ELK
-
17
前面我们了解了elk集群中的logstash的用法,使用logstash处理日志挺好的,但是有一个缺陷,就是太慢了;当然logstash慢的原因是它依赖jruby虚拟机,jruby虚拟机就是用java语言开发的ruby虚拟机,本身java程序运行在jvm上就已经很慢了,而lo...
-
8
Docker Compose 部署 ELK 日志管理系统 Elasticsearch + Logstash + Kibana + Filebeat ELK日志管理系统是Elasticsearch + Logstash + Kibana的简称,加上Filebeat组成一...
-
13
How we use ElasticSearch, Kibana and Filebeat to handle our logs Monday 9th of August 2021 Flare runs on a few different servers and each one of t...
-
7
Kubernetes使用filebeat Multiline自定义收集日志 2021-11-11 2 分钟阅读 我们介绍了如何在 kubernetes 环境中使用 filebeat sidecar 方式收集日志 使用的是 filebeat 的 moudle 模块,但凡是常用的软件,基本都有对...
-
8
-
9
filebeat使用modules收集nginx日志 1.为什么要使用modules收集日志 modules只是filebeat的一个小功能,由于像mysql、redis等这种日志无发输出成json格式,filebeat无法将收集来的普通日志转换为json格式,从而进行细致的统计...
-
9
elasticsearch filebeat (status=403)/blocked by: [FORBIDDEN/12/index read-only / allow delete () 精选 原创
-
9
K8s部署EFK(elasticsearch + filebeat + kibana)日志收集 一.准备镜像 # 在本机拉取镜像 docker pull docker.elastic.co/elastics...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK