轻量日志管理方案-[EFK]

使用FileBeat进行日志文件的数据收集,并发送到ES进行存储,最后Kibana进行查看展示;
这个应该是最简单,轻量的日志收集方案了。

最总方案为:FileBeat+ES+Kibana ; 【Kibana过于强大,感觉可以无限扩展】


尝试内容

在114上部署ES+Kibana,对113的隧道消防服务做日志收集,在100Nginx做日志收集

搭建【ES】

docker network create elastic
# 3ef299947c816f6b1eda8790a8a1b2306409ae07f0371413bd037a892685e32c
# 拉取
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.8.1
# 启动
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -e "discovery.type=single-node" --name es-node01 --net elastic -p 9200:9200 -p 9300:9300 -t docker.elastic.co/elasticsearch/elasticsearch:8.8.1

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ Elasticsearch security features have been automatically configured!
✅ Authentication is enabled and cluster connections are encrypted.

ℹ️  Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
  LQtjAfZ7ncioEEAehcxl

ℹ️  HTTP CA certificate SHA-256 fingerprint:
  d26d334c21311131ba75516d5c26a36441a1020e723848c6d19bfeb2b397becc

ℹ️  Configure Kibana to use this cluster:
• Run Kibana and click the configuration link in the terminal when Kibana starts.
• Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
  eyJ2ZXIiOiI4LjguMSIsImFkciI6WyIxNzIuMTguMC4yOjkyMDAiXSwiZmdyIjoiZDI2ZDMzNGMyMTMxMTEzMWJhNzU1MTZkNWMyNmEzNjQ0MWExMDIwZTcyMzg0OGM2ZDE5YmZlYjJiMzk3YmVjYyIsImtleSI6IkhXc2pfSWdCYkR5UzZVUFBiUTNnOlRLSk5yQkVxUjdpUUZCZE5rSU96ZHcifQ==

ℹ️ Configure other nodes to join this cluster:
• Copy the following enrollment token and start new Elasticsearch nodes with `bin/elasticsearch --enrollment-token <token>` (valid for the next 30 minutes):
  eyJ2ZXIiOiI4LjguMSIsImFkciI6WyIxNzIuMTguMC4yOjkyMDAiXSwiZmdyIjoiZDI2ZDMzNGMyMTMxMTEzMWJhNzU1MTZkNWMyNmEzNjQ0MWExMDIwZTcyMzg0OGM2ZDE5YmZlYjJiMzk3YmVjYyIsImtleSI6Ikcyc2pfSWdCYkR5UzZVUFBiUTNmOmRpRlAyb3NJUXUyZE1WWnpkMi1DbmcifQ==

  If you're running in Docker, copy the enrollment token and run:
  `docker run -e "ENROLLMENT_TOKEN=<token>" docker.elastic.co/elasticsearch/elasticsearch:8.8.1`
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 过期了用这个去es下生成/usr/share/elasticsearch/bin
bin\elasticsearch-create-enrollment-token --scope kibana  

docker cp es-node01:/usr/share/elasticsearch/config/elasticsearch.yml ./elasticsearch.yml
# 修改容器内部/usr/share/elasticsearch/config/elasticsearch.yml文件中xpack.security.enabled的值为false。
docker cp ./elasticsearch.yml es-node01:/usr/share/elasticsearch/config/elasticsearch.yml


搭建【kibana】

# 拉取
docker pull docker.elastic.co/kibana/kibana:8.8.1
# 启动
docker run --name kib-01 --net elastic -p 5601:5601  docker.elastic.co/kibana/kibana:8.8.1
# /usr/share/kibana/config  
Go to http://0.0.0.0:5601/?code=code=093966  to get started.
# 这个code用来验证使用

用户:elastic 密码:LQtjAfZ7ncioEEAehcxl

# 配置为中文汉化
 docker cp  kib-01:/usr/share/kibana/config/kibana.yml /home/kibana/config/kibana.yml
 # 修改文件 添加中文配置
 i18n.locale: "zh-CN"
 # 再次复制替换回去
 docker cp  /home/kibana/config/kibana.yml  kib-01:/usr/share/kibana/config/kibana.yml 


搭建【filebeat】

这里对单台物理服务区内docker容器很多的情况,需要自定义多个索引的,不让日志都混在一起无法区分。

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.8.1-linux-x86_64.tar.gz
tar -zxvf ./filebeat-8.8.1-linux-x86_64.tar.gz

# 修改配置并启动即可
nohup ./filebeat -e -c filebeat.yml > /data/Filebeat/log/Filebeat-log.log 2>&1 &

可用的配置:

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /data/dream/emergency/logs/dream-emergency/info.log
  fields:
    source: emergency
# ...........
# ============================== Filebeat modules ==============================
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: true

setup.template.settings:
  index.number_of_shards: 1
  #index.codec: best_compression
  #_source.enabled: false

# 允许自动生成index模板
setup.template.enabled: true
# # 生成index模板时字段配置文件
setup.template.fields: fields.yml
# # 如果存在模块则覆盖
setup.template.overwrite: true
# # 生成index模板的名称
setup.template.name: "app_log" 
# # 生成index模板匹配的index格式       
setup.template.pattern: "app*" 
setup.ilm.enabled: auto
# 这里一定要注意 会在alias后面自动添加-*
setup.ilm.rollover_alias: "park-ssm"
setup.ilm.pattern: "{now/d}"
# # 生成kibana中的index pattern,便于检索日志
#setup.dashboards.index: myfilebeat-7.0.0-*
#filebeat默认值为auto,创建的elasticsearch索引生命周期为50GB+30天。如果不改,可以不用设置
setup.ilm.enabled: false
# =================================== Kibana ===================================
# setup.kibana:
#   hosts: ["192.168.xxx.114:9200"]
# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
  hosts: ["192.168.xxx.114:9200"]
  index: "app-%{[fields.source]}*"
  indices:
    #- index: "app-emergency-%{+yyyy.MM.dd}"
    - index: "app-emergency"
      when.equals:
        fields:
          source: "emergency"
    # ........

  username: "elastic"
  password: "LQtjAfZ7ncioEEAehcxl"
# ------------------------- Processors ----------------------------------
processors:
  - add_host_metadata:
      when.not.contains.tags: forwarded
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~

使用【kibana】

官方使用文档:https://www.elastic.co/guide/cn/kibana/current/index.html

日志查看

使用Discover进入 Kibana 的数据探索功能,对日志索引的数据进行查询,使用的ES的查询语法。

数据统计

使用Dashboard展示保存的可视化结果集合。这个就需要使用查询语句去进行数据的处理了。

这里推荐还是使用现有的集成控件,直接使用Beat系列现成收集模型,进行收集与仪表盘的展示。

这里还需要一个栗子:比如Nginx的日志。【待研究处理】


Nginx日志栗子

# 集成搜索nginx  -> Nginx 日志 
# 选用gz的包:macOS  和 Linux RPM 都行 
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.8.1-linux-x86_64.tar.gz
tar -zxvf ./filebeat-8.8.1-linux-x86_64.tar.gz
# 修改 /etc/filebeat/filebeat.yml
---------- 修改内容 -----------------
output.elasticsearch:
  hosts: ["<es_url>"]
  username: "elastic"
  password: "<password>"
setup.kibana:
  host: "<kibana_url>"
----------------------------------
./filebeat modules enable nginx
# 配置nginx 日志的内容 ,位置:/home/filebeat/filebeat-8.8.1-linux-x86_64/modules.d
# 修改 nginx.yml
----------- 修改内容 ------------------
- module: nginx
  access:
    enabled: true
    var.paths: ["/usr/local/nginx/logs/access*.log","/usr/local/nginx/logs/access.log","/usr/local/nginx/logs/gisaccess.log"]
  error:
    enabled: true
    var.paths: ["/usr/local/nginx/logs/error.log"]
  ingress_controller:
    enabled: false
----------------------------------
# 这个命令会连接kibana,去创建所有filebeat相关的仪表盘等等内容
./filebeat setup 
# 启动
./filebeat -e
nohup ./filebeat -e > /data/Filebeat/log/Filebeat-log.log 2>&1 &

这里建议Nginx的日志格式,还是按照默认的即可,越改越错 ε=(´ο`*)))唉

# nginx 的日志模板配置 【默认的】
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

最后查看效果,在Dashboard的列表中搜索Nginx即可查到相关的仪表盘,点开即可;
请添加图片描述

扩展

Kibana的收集器,是在Beats 系列里面的,这里用的还只是filebeat对文件进行收集,还有Metricbeat对系统进行收集,Packetbeat网络收集等等的东东;

用法差不多的,开箱即用。这里配合Kibana的集成里面的教程,直接就可以随便扩展监控内容了。

这里面可监控的内容很多,需要安装各种Beat收集数据,在这里创建可视化以及其他操作。


参考各种的东东

  • https://blog.51cto.com/ghostwritten/5345646#3_Filebeat__19
  • https://www.elastic.co/cn/ 官网
  • https://www.elastic.co/cn/beats/filebeat 官网
  • https://blog.csdn.net/weixin_48114253/article/details/116991334
  • https://www.elastic.co/guide/en/kibana/current/docker.html 官方安装kibana教程
  • https://blog.csdn.net/weixin_44953658/article/details/117320403 整体教程
  • 官方使用文档:https://www.elastic.co/guide/cn/kibana/current/index.html

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小_杭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值