• 搜索
  • 夜间模式
    ©2026  依刻学习 Theme by OneBlog

    依刻学习博客

    搜索
    标签
  • 首页>
  • 学习的一天>
  • 正文
  • EFLK集群的搭建与使用

    2025年03月10日 24 阅读 0 评论 8638 字

    前言

    这两天一直在学ELK,ELK本身难度不高,但是组件有点多,这导致使用起来比较凌乱,今天就来练习一下

    环境准备
    |IP|角色|
    |-|-|
    |192.168.179.111|1号|
    |192.168.179.112|2号|
    |192.168.179.113|3号|

    目标架构图

    tomcat与nginx需要使用json格式日志,方便观察

    1.搭建ES集群

    • 安装elasticsearch集群(1+2+3号都要部署)

      #下载安装
      wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.13-x86_64.rpm
      yum localinstall -y elasticsearch-7.17.13-x86_64.rpm
      #修改配置文件
      #位于/etc/elasticsearch/elasticsearch.yml
      #集群名称
      cluster.name: <NAME>    
      #存储路径
      #path.data: <PATH>        
      #日志路径
      path.logs: <PATH>        
      #节点名称,每个节点不同(必改)
      node.name: <NAME>
      #绑定IP(谁可以访问),默认本机,可以改为0.0.0.0
      network.host: <IP>        
      #自动发现集群节点,ES集群配置,可以写IP(必改)
      #hosts解析名会报错(要写所有节点,本节点也要写入)
      discovery.seed_hosts: ["<IP>","<IP>"]    
      #初始化master节点配置,自动推举(必改)
      cluster.initial_master_nodes: ["<IP>","<IP>"]    #要写所有节点
      
      #------------2号节点示例------------
      vim /etc/elasticsearch/elasticsearch.yml
      
      cluster.name: wym-ES
      node.name: es2
      path.data: /var/lib/elasticsearch
      path.logs: /var/log/elasticsearch
      network.host: 0.0.0.0
      discovery.seed_hosts: ["192.168.179.111", "192.168.179.112","192.168.179.113"]
      cluster.initial_master_nodes: ["192.168.179.111", "192.168.179.112","192.168.179.113"]
      
      #检查
      grep -v -E '^*#|^$' /etc/elasticsearch/elasticsearch.yml
      #启动
      #!!!!!!!!!!!!!注意!!!!!!!!!!!!!!!!!!
      #三个节点最好同时启动,如果没有在'超时时间'内启动所有节点,就会导致集群启动失败
      systemctl enable --now elasticsearch
      
      #测试
      curl 192.168.179.111:9200
      curl 192.168.179.112:9200
      curl 192.168.179.113:9200

      请关注cluster_uuid,如果集群成功,那么所有节点的cluster_uuid不是null而是相同的编号

    • 安装 kibana(3号节点部署)

      #下载安装
      wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.13-x86_64.rpm
      yum localinstall kibana-7.17.13-x86_64.rpm -y
      #修改配置文件
      位于/etc/kibana/kibana.yml
      #绑定IP,允许谁访问,默认本机
      server.host: "<IP>"
      #server名称
      server.name: "<NAME>"
      #es配置集群IP
      elasticsearch.hosts: ["http://<IP1>:9200","http://<IP2>:9200"]
      #i18n是"国际化"的意思,可以理解为语言
      i18n.locale: "zh-CN"
      
      #------------示例---------
      vim /etc/kibana/kibana.yml
      
      server.host: "0.0.0.0"
      server.name: "wym-kibaba"
      elasticsearch.hosts: ["http://192.168.179.111:9200","http://192.168.179.112:9200","http://192.168.179.113:9200"]
      i18n.locale: "zh-CN"
      #检查
      cat /etc/kibana/kibana.yml |grep "^*#|^$" -v -E
      #执行
      systemctl enable --now  kibana
      systemctl status kibana
      #------------注意---------
      #如果es集群启动失败,kibana会一直处于load状态无法启动,当然如果是配置文件错误,会直接failed
      #测试
      http://192.168.179.113:5601/
      #成功会进入kibana
      #如果直接访问错误,显示404就是es集群没有启动或者配置错误
      #如果显示没有准备好(es is not  ready),就是启动时间较长,需要等一会

    检查es集群与kibana的通信
    左边边栏-->堆栈检测-->使用设置(灰色的字体)-->nodes


    • 安装filebeat(1+2号节点)

      wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.13-x86_64.rpm
      yum localinstall -y filebeat-7.17.13-x86_64.rpm
      systemctl enable --now filebeat
    • 安装logstash(1+2号节点)

      wget https://artifacts.elastic.co/downloads/logstash/logstash-7.17.13-x86_64.rpm
      yum localinstall -y logstash-7.17.13-x86_64.rpm
      #默认情况下必须绝对路径使用logstash
      ln -s /usr/share/logstash/bin/logstash /usr/local/bin/

    至此ELFK集群在此次架构中已经搭建完毕

    1. 准备nginx+redis+tomcat
    2. 安装nginx(2号节点)

      #安装
      yum install -y nginx
      #配置nginx日志
      vim /etc/nginx/nginx.conf
      
      #!!!!!!!!!!!!!!先把原来的注释掉!!!!!!!!!!!!!
       # json日志格式
       log_format log_json '{"@timestamp": "$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",'
                           '"up_resp_time": "$upstream_response_time",'
                           '"request_time": "$request_time"'
                           ' }';
       
       access_log  /var/log/nginx/access.log log_json; 
      #启动
      systemctl start nginx
    3. 安装tomcat()

      #由于jdk安装不属于本博客,所以直接按照openjdk
      yum install -y java-1.8.0-openjdk.x86_64
      #tomcat需要jdk环境才能运行
      yum install -y tomcat
      #修改配置文件
      vim /etc/tomcat/server.xml
      #替换<Value>(在文件末尾)
      
       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                  prefix="localhost_access_log" suffix=".log"
                  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;}"/>
      systemctl restart tomcat
    4. 安装redis

      vim /etc/redis.conf
      
      #修改访问密码与绑定IP
      requirepass 123
      bind 0.0.0.0

    至此,所有环境准备完成

    3.书写配置文件

    • redis-logstash-es.conf(2号节点)

      #输入端为redis
      input {
      redis {
        key => "test-tomcat-log"
        data_type => "list"
        db => 10
        host => "192.168.179.113"
        port => 6379
        password => "123"
      }
      }
      
      #输出端为es
      output {
      elasticsearch {
        hosts => ["192.168.179.111:9200","192.168.179.112:9200","192.168.179.113:9200"]
        index => "nginx-%{+YYYY.MM.dd}"
      }
      }
    • beat-logstash-es.conf(1号节点)

      #输入端为filebeat
      input {
      beats {
        port => 8888
      }
      }
      
      #输出端为es
      output {
      elasticsearch {
        hosts => ["192.168.179.111:9200","192.168.179.112:9200","192.168.179.113:9200"]
        index => "tomcat-%{+YYYY.MM.dd}"
      }
      }
    • tomcat-filebeat-logstash.yaml(1号节点)

      #输入端为tomcat日志文件(文件流)
      filebeat.inputs:
       - type: filestream
      paths:
        - /var/log/tomcat/localhost_access_log*.log
      parsers:
      - ndjson:
        keys_under_root: true
      #输出端为logstash
      output.logstash:
      hosts: ["192.168.179.111:8888"]
    • nginx-filebeat-redis.yaml(2号节点)

      #输入端为nginx日志文件(文件流)
      filebeat.inputs:
       - type: filestream
      
      paths:
        - /var/log/nginx/*.log
      parsers:
      - ndjson:
        keys_under_root: true
        
      #输出端为redis
      output.redis:
      enabled: true
      hosts:
      - 192.168.179.113:6379
       #password必须保证与redis配置文件的相同,其他可自定义
      password: "123"
      key: "test-tomcat-log"
      db: 10
      timeout: 5 
    • 测试
      需要先在kibana创建索引模板nginx与tomcat,因为logstash中没有写模板,如果kibana中没有索引模板,那么kogstash中的数据无法提交到kibana
      左列边栏-->Stack Management-->索引模板-->添加索引模板,索引模式分别为tomcat*与nginx*,必须匹配logstash中index
    #一共开4个终端,方便观察
    #1号
    filebeat -e -c /root/tomcat-filebeat-logstash.yaml
    #2号
    filebeat -e -c /root/nginx-filebeat-redis.yaml
    #1号 
    logstash -tf beat-logstash-es.conf
    #2号
    logstash -tf redis-logstash-es.conf
    
    !!!!!!!!!!删除记录!!!!!危险操作!!!!!
    rm -rf /var/lib/filebeat/*
    #如果不删除,测试时提交的内容不会再提交,这部分内容就'凭空消失'
    #更重要的是,如果filebeat直连kibana,配置中定义自定义模板在有日志的情况下不会提交(认为已经提交过了)
    #如果kibana内删除了这些模板,会导致kibana根本不会接受提交内容
    #但是,这仍是危险操作,删除记录后没有偏移量会全部提交,如果日志太庞大,可能导致服务问题 
    
    #添加测试数据(日志)
    curl 192.168.179.112:80
    curl 192.168.179.111:8080
    #查看redis是否有内容
    #注意-a指定密码,-n指定数据库编号,必须与filebeat相同,否则查不到
    redis-cli -a 123 -h 192.168.179.113 -n 10
    #遍历
    > LRANGE test-tomcat-log 0 -1

    5.最终结果

    • 检查索引
      左列边栏-->Stack Management-->索引模板

      哪个没有,就是哪条提交路径有问题,参考架构图
    • 创建索引模式(不是索引模板)
      创建tomcat与nginx索引模式,按要求输入就行了,使用文件名拓展(通配符)进行匹配
    • 检查内容
      左列边栏-->Analytics-->Discover-->选择对应的索引模式
      !!!!!!!!注意!!!!!!!
      如果明明显示了索引却没有看见,可能是因为时间段错误,修改右上角的时间(这种错误很可能是因为之前点击了柱状图导致锁定了该时间段)

    总结

    ELFK架构提供了很多了功能,但是我都时间没有好好的研究,左列边栏有大量的选项都没有使用过,这些将到未来的实际中学习使用

    本文著作权归作者 [ wymm ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    取消回复

    发表留言
    回复

    Copyright©2026  All Rights Reserved.  Load:0.036 s
    Theme by OneBlog V3.6.5
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。