2025年easticsearsh+filebeat+kibana+nginx搭建日志收集系统

easticsearsh+filebeat+kibana+nginx搭建日志收集系统easticsearsh filebeat kibana nginx 搭建日志收集系统 一 简介 1 elasticsearc 是一个基于 Lucene 的搜索服务器 它提供了一个分布式多用户能力的全文搜索引擎 基于 RESTful web 接口 Elasticsearc 是用 Java 语言开发的

大家好,我是讯享网,很高兴认识大家。

easticsearsh+filebeat+kibana+nginx搭建日志收集系统

一、简介

1.elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎;
2.filebeat:轻量型日志采集器,Beats中的一员,Beats在是一个轻量级日志采集器,其实Beats家族有6个成员,早期的ELK架构中使用Logstash收集、解析日志,但是Logstash对内存、cpu、io等资源消耗比较高。相比Logstash,Beats所占系统的CPU和内存几乎可以忽略不计。Filebeat包含两个主要组件:input和harvester,harvester负责读取单个文件的内容,input负责管理harvest并查找所有可读取的资源。
3.kibana:一个与Elasticsearch协同工作的开源分析和可视化平台,Kibana 可以让你更方便地对 Elasticsearch 中数据进行操作,包括高级的数据分析以及在图表中可视化您的数据。

二、安装

elasticsearch安装

rpm -ivh elasticsearch-7.6.1.rpm 

讯享网

2.修改配置文件:vim /etc/elasticsearch/elasticsearch.yml

讯享网#修改运行日志和数据存放位置(如果默认的存储位置磁盘空间够大,也可以不用改) # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): #数据存放位置 path.data: /var/lib/elasticsearch # # Path to log files: #运行日志存放位置 path.logs: /var/lib/elasticsearch #注:如果改了存放位置,如path.logs: /data/elasticsearch7.6.1/log/elasticsearch,则需要用“chown elasticsearch 文件夹名称”命令更改elasticsearch文件夹所属用户,否则会导致无法启动 
#修改访问地址 # ---------------------------------- Network ----------------------------------- # # Set the bind address to a specific IP (IPv4 or IPv6): # network.host: 本机ip # # Set a custom port for HTTP: # http.port: 9200 
讯享网#修改节点 # ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # cluster.name: my-application # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # node.name: node-1 # --------------------------------- Discovery ---------------------------------- # # Pass an initial list of hosts to perform discovery when this node is started: # The default list of hosts is ["127.0.0.1", "[::1]"] # #discovery.seed_hosts: ["host1", "host2"] discovery.seed_hosts: ["127.0.0.1"] # # Bootstrap the cluster using an initial set of master-eligible nodes: # #cluster.initial_master_nodes: ["node-1", "node-2"] cluster.initial_master_nodes: ["node-1"] 

3.启动服务

#重新加载服务配置文件 systemctl daemon-reload #将服务设置为每次开机启动 systemctl enable elasticsearch #启动服务 systemctl start elasticsearch #查看启动状态 systemctl status elasticsearch 

4.查看是否正常:curl http://elasticsearchip:elasticsearch端口,出现以下画面说明elasticsearch正常启动
出现以下画面说明elasticsearch正常启动
讯享网
6.nginx配置(如果不需要,则不用配置)

讯享网location /elasticsearch/{ proxy_pass "http://ip:9200/"; #用于解决302重定向问题,这里 proxy_set_header Host $http_host; proxy_read_timeout 300s; proxy_set_header X-Forwarded-For $http_x_forwarded_for; } 

filebeat安装

1.安装

tar -xzf filebeat-7.6.1-linux-x86_64.tar.gz 

2.配置环境变量

讯享网#编辑profie文件 vim /etc/profie #添加文件路径后保存 export PATH=$PATH:/usr/local/filebeat-7.6.1 #执行(只在当前终端生效) source /etc/profie 永久生效方法(可不使用该方法) a.vim ~/.bashrc b.在末尾添加如下代码并保存 if [ -f /etc/profile ]; then . /etc/profile fi 

3.修改配置文件:vim filebeat.yml

filebeat.inputs: # Each - is an input. Most options can be set at the input level, so # you can use different inputs for various configurations. # Below are the input specific configurations. - type: log # Change to true to enable this input configuration. enabled: true # Paths that should be crawled and fetched. Glob based paths. paths: #日志路径 - /data/log/miniapp/info.log fields: #相当于一个标签 log_topics: "miniapp-log" fields_under_root: true #==================== Elasticsearch template setting ========================== setup.template.settings: index.number_of_shards: 1 #index.codec: best_compression #_source.enabled: false setup.template.name: "filebeat" setup.template.pattern: "filebeat-*" setup.ilm.enabled: false #-------------------------- Elasticsearch output ------------------------------ output.elasticsearch: # Array of hosts to connect to. hosts: ["172.51.67.25:9200"] #没有匹配到以下配置的索引时,存放到other-log索引 index: "other-log" indices: #指定存储索引(elasticsearch不存在该名称的索引时,会自动创建该索引) - index: "miniapp-log-%{+yyyy-MM-dd}" when.contains: #匹配上面日志配置的log_topics,匹配上日志就会发送到该索引上去 log_topics: "miniapp-log" 

4.启动filebeat

讯享网nohup filebeat -e -c filebeat文件夹路径/filebeat.yml >filebeat文件夹路径/tmp/filebeat.log 2>&1 & 

5.查看运行日志

tail -f filebeat文件夹路径/tmp/filebeat.log 

在这里插入图片描述
注:运行日志中harvester里可以看到读取到几个文件,重启filebeat后,期间新增的日志会在filebeat启动后发送到elasticsearch
6.编写启动脚本(方便直接运行脚本就启动)

讯享网#!/bin/bash filebeat_path=/usr/local/filebeat-7.6.1 filebeat_file=$filebeat_path/filebeat.yml #运行日志 filebeat_log_file=$filebeat_path/tmp/filebeat.log #日志id,同一次部署打印的日志id是一样的 logId=`date +%y%m%d%H%M%S%N` log_file=$filebeat_path/startApp/startApp.log log(){ if [ -n "$1" ]; then echo "$logId `date +%F" "%T" "`${FUNCNAME[1]} \"$1\"" >> $log_file echo "$logId `date +%F" "%T" "`${FUNCNAME[1]} \"$1\"" fi return 0 } #获取filebeat进程id getPID(){ PID=$(ps -ef | grep -v grep | grep -i filebeat | awk 'BEGIN{ORS=" "}{print $2}') if [ -n "$PID" ]; then return 0 else return 1 fi } startApp(){ getPID if [ $? -eq 0 ]; then log "filebeat正在运行(pid=${PID})" kill $PID > /dev/null 2>&1 sleep 3 log "已停止${PID}进程" else log "filebeat未运行" fi log "即将启动进程" nohup filebeat -e -c $filebeat_file >$filebeat_log_file 2>&1 & sleep 3 getPID if [ $? -eq 0 ]; then log "filebeat启动成功(pid=$PID)" else log "filebeat启动失败" fi } startApp 

kibana安装

1.安装

rpm -ivh kibana-7.6.1-x86_64.rpm 

2.修改配置文件vim /etc/kibana/kibana.yml

讯享网#访问端口 server.port: 5601 #本机地址 server.host: 0.0.0.0 #代理路径(用nginx代理时该配置要跟随nginx配置的路径),配置时kibana所有访问路径会自动加上该路径 #如加载http://ip:端口/bundles/56.bundle.js会变成http://ip:端口/kibana/bundles/56.bundle.js server.basePath:"/kibana" #elasticsearch访问地址 elasticsearch.hosts: ["http://elasticsearch配置的ip:端口"] #设置中文界面 i18n.locale: "zh-CN" 

3.启动服务

#重新加载服务配置文件 systemctl daemon-reload #将服务设置为每次开机启动 systemctl enable kibana #启动服务 systemctl start kibana #查看启动状态 systemctl status kibana 

4.访问测试:curl http://ip:端口

讯享网#查看输出日志 systemctl status kibana.service 

出现302状态码说明正常启动

5.nginx配置

location /kibana/{ proxy_pass "http://kibana访问ip:端口/"; #用于解决302重定向问题,这里 proxy_set_header Host $http_host; proxy_read_timeout 300s; proxy_set_header X-Forwarded-For $http_x_forwarded_for; } 

6.索引模式配置:访问http://ip:端口/kibana/app/kibana

  • 查看从elasticsearch获取到的索引(索引:相当于数据库)

在这里插入图片描述

  • 配置索引模式(索引模式:告诉kibana你需要用到哪个索引或者哪些索引组合,比如filebeat按照时间传输日志到不同的elasticsearch索引中,此时可以用通配符来在几个索引中搜索你想要的日志)
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    7.设置不显示不想要的字段,默认显示的字段众多,这会导致查询日志时,显示的内容过于繁杂
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    8.日志搜索:搜索框内,不加引号为模糊搜索,加引号为精准搜索,搜索内容带引号要加转义符,与用and,或用or
    在这里插入图片描述
    9.更改日志显示方式:原本日志显示的内容非常繁杂,除了message中的日志外,会额外显示id这些,日志搜索时就不能直观地看到日志内容,所以这里将_source直接改成message
    在这里插入图片描述
    在这里插入图片描述
    10.更改日志排序:kibana查询日志时,默认规则是按照filebeat采集时间排序,而采集到的数据时间相差时间是毫秒级的,会导致显示出来的日志不按照本来的顺序排序,这里可以用日志偏移量来进行辅助排序,确保查询到的日志顺序是正常的,另外默认的排序顺序也要更改,否则看到的日志顺序是倒着打印的
    在这里插入图片描述
    在这里插入图片描述

三、设置定时任务

讯享网#/bin/bash #日志id,同一次部署打印的日志id是一样的 logId=`date +%y%m%d%H%M%S%N` #指定日期(7天前) old_date=`date -d "1 week ago" +%Y-%m-%d` log_file=/usr/local/filebeat-7.6.1/delIndex.log log(){ if [ -n "$1" ]; then echo "$logId `date +%F" "%T" "`${FUNCNAME[1]} \"$1\"" >> $log_file echo "$logId `date +%F" "%T" "`${FUNCNAME[1]} \"$1\"" fi return 0 } delIndex(){ log "开始清理 $old_date 索引" #查询是否有需要删除的索引 curl -XGET "http://elasticsearch的ip:端口/_cat/indices/?v"|grep $old_date if [ $? == 0 ];then #删除7天前的日志(*-${old_date}为索引名称的时间格式,否则匹配不到索引) curl -XDELETE "http://elasticsearch的ip:端口/*-${old_date}" log "清理 $old_date 索引!" else log "没有索引需要删除" fi } delIndex 

2.设置定时任务(利用linux自带的crontab):crontab -e

0 0 * * 1 sh /usr/local/filebeat-7.6.1/delIndex.sh 
小讯
上一篇 2025-03-04 09:52
下一篇 2025-01-25 13:53

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/110917.html