ELFK 分布式日志收集系统

ELFK的组成:

    • Elasticsearch: 它是一个分布式的搜索和分析引擎,它可以用来存储和索引大量的日志数据,并提供强大的搜索和分析功能。 (java语言开发,)
    • logstash: 是一个用于日志收集,处理和传输的工具,它可以从各种数据源收集日志数据,对数据进行处理和过滤,将数据发送到Elasticsearch。 java
    • kibana: 是一个用于数据可视化和分析的工具,它可以与Elasticsearch集成,帮助用户通过图表、仪表盘等方式直观地展示和分析日志数据。 java
    • filebeat: 轻量级日志收集工具,一般安装在客户端服务器上负责收集日志,传输到ES或logstash go

本章实验环境拓扑图:

  • 版本介绍:

    • Elasticsearch:6.6.0
    • kibana:6.6.0
    • filebeat:6.6.0
    • nginx:1.18.0
    • Redis:5.0.7
    • logstash:6.6.0
  • 开始部署:

    • 部署8.8服务器的es和Kibna:a

      • 复制软件包至服务器下安装:
        • rpm -ivh elasticsearch-6.6.0.rpm

        • 修改配置文件:

          • vim /etc/elasticsearch/elasticsearch.yml
            *

            bash 复制代码
            node.name: es1						
            path.data: /data/elasticsearch			
            path.logs: /var/log/elasticsearch		
            bootstrap.memory_lock: true				
            network.host: 192.168.8.8,127.0.0.1	
            http.port: 9200			
        • 创建数据目录,并修改权限
          *

          bash 复制代码
          mkdir -p /data/elasticsearch 
          chown -R elasticsearch.elasticsearch /data/elasticsearch/
        • 启动es:systemctl start elasticsearch

      • 部署安装kibana:
        • 安装kibana:rpm -ivh kibana-6.6.0-x86_64.rpm

        • 修改配置文件:

          • 修改项:
            *

            bash 复制代码
            server.port: 5601
            server.host: "192.168.8.8"
            server.name: "db01" 	#自己所在主机的主机名
            elasticsearch.hosts: ["http://192.168.8.8:9200"]   #es服务器的ip,便于接收日志数据
            保存退出
        • 启动kibana:systemctl start kibana

        • 查看两个服务的端口是否存在:

          • netstat -anpt | grep 5601
          • netstat -anpt | grep 9200
    • 部署8.9服务器山的nginx和filebeat:

      • 安装filebeat:

        • rpm -ivh filebeat-6.6.0-x86_64.rpm
      • 修改配置文件:

        • vim /etc/filebeat/filebeat.yml (清空源内容,直接覆盖)
          *

          bash 复制代码
          filebeat.inputs:		(日志来源)
          - type: log			(日志格式)
            enabled: true		(开机自启)
            paths:			(日志路径)
              - /var/log/nginx/access.log
          
          output.elasticsearch:		(日志传送到那)
            hosts: ["192.168.8.8:9200"]
        • 启动filebeat服务:

          • systemctl start filebeat
      • 安装nginx:

        • yum -y install nginx

        • 启动nginx:nginx

    • 在8.8服务器上安装网站压力测试工具:

      • yum -y install httpd-tools
    • 2.使用ab压力测试工具测试访问

  • 使用浏览器扩展程序登录es查看索引是否有访问数:

  • 修改nginx的日志个数为json:

    • vim /etc/nginx/nginx.conf

      • 添加在http{}内:

      bash 复制代码
      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 restart nginx

  • 修改filebeat.yml文件,区分nginx的访问日志和错误日志

    • vim /etc/filebeat/filebeat.yml

      bash 复制代码
      修改为:
      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"]
      
      output.elasticsearch:
        hosts: ["192.168.8.8:9200"]
        indices:
          - index: "nginx-access-%{+yyyy.MM.dd}"
            when.contains:
              tags: "access"
          - index: "nginx-error-%{+yyyy.MM.dd}"
            when.contains:
              tags: "error"
      
      setup.template.name: "nginx"
      setup.template.patten: "nginx-*"
      setup.template.enabled: false
      setup.template.overwrite: true
    • 重启服务:systemctl restart filebeat

  • 使用ab工具压力测试一下网站:

  • 使用kibana图形化展示日志访问数据:

  • 虽然以上环境也可以进行日志收集,但只适用于中小型公司,以下再多增加一台服务器,安装redis实现消息队列,和logstash日志采集,增加吞吐量。

  • 在8.10服务器上部署redis和logstash:

    • 准备安装目录和数据目录:

      bash 复制代码
      mkdir -p /data/soft
      mkdir -p /opt/redis_cluster/redis_6379/{conf,logs,pid}
    • 下载redis安装包:

      bash 复制代码
      cd /data/soft
      wget http://download.redis.io/releases/redis-5.0.7.tar.gz
    • 将软件包解压到/opt/redis_cluster文件夹中:

      bash 复制代码
      tar xf redis-5.0.7.tar.gz -C /opt/redis_cluster/
      ln -s /opt/redis_cluster/redis-5.0.7  /opt/redis_cluster/redis
    • 切换目录编译安装redis:

      bash 复制代码
      cd /opt/redis_cluster/redis
      make && make install 
    • 编写redis配置文件:

      • vim /opt/redis_cluster/redis_6379/conf/6379.conf

        bash 复制代码
        bind 127.0.0.1 192.168.8.10
        port 6379
        daemonize yes
        pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
        logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
        databases 16
        dbfilename redis.rdb
        dir /opt/redis_cluster/redis_6379
      • 启动redis服务:redis-server /opt/redis_cluster/redis_6379/conf/6379.conf

    • 修改8.9的filebeat文件(将filebeat收集的日志转发给redis):

      • vim /etc/filebeat/filebeat.yml

        bash 复制代码
        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.template.settings:
          index.number_of_shards: 3
        
        setup.kibana:
        
        output.redis:
          hosts: ["192.168.8.10"]
          key: "filebeat"
          db: 0
          timeout: 5
      • 重启服务:systemctl restart filebeat

      • 再次在8.8上使用压力测试工具访问网站:ab -c 1000 -n 20000 http://192.168.8.9/

      • 登录redis数据库:redis-cli

        • 查看是否有以filebeat命名的键:
        • filebeat与redis关联成功!
  • 继续在8.10服务器上部署logstash:

    • rpm -ivh logstash-6.6.0.rpm

    • 修改logstash配置文件,实现access和error日志分离

      • vim /etc/logstash/conf.d/redis.conf
        *

        bash 复制代码
        input {
          redis {
            host => "192.168.8.10"
            port => "6379"
            db => "0"
            key => "filebeat"
            data_type => "list"
          }
        }
        
        filter {
          mutate {
            convert => ["upstream_time","float"]
            convert => ["request_time","float"]
          }
        }
        
        output {
          stdout {}
           if "access" in [tags] {
            elasticsearch {
              hosts => ["http://192.168.8.8:9200"]
              index => "nginx_access-%{+YYYY.MM.dd}"
              manage_template => false
            }
           }
           if "error" in [tags] {
            elasticsearch {
              hosts => ["http://192.168.8.8:9200"]
              index => "nginx_error-%{+YYYY.MM.dd}"
              manage_template => false
            }
           }
        }
      • 最后重启logstash:
        *

        bash 复制代码
        /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis.conf
      • 过程需等待,启动较慢(大约2-3分钟)

    • 最后通过kibana图形化界面,可以看到nginx的access日志和error错误日志即可,最终效果和仅部署elk效果一致,只不过添加了redis数据库和filebeat日志收集工具,有了redis可以实现了消息队列为es服务器减轻了压力。

相关推荐
狐心kitsune2 分钟前
erlang学习:Linux常用命令1
linux·学习·erlang
Hqst_Kevin39 分钟前
Hqst 品牌 H81801D 千兆 DIP 网络变压器在光猫收发器机顶盒中的应用
运维·服务器·网络·5g·网络安全·信息与通信·信号处理
DREAM依旧1 小时前
《深入了解 Linux 操作系统》
linux
cooldream20091 小时前
828华为云征文 | 在华为云X实例上部署微服务架构的文物大数据管理平台的实践
微服务·架构·华为云·文物大数据平台
阿赭ochre1 小时前
Linux环境变量&&进程地址空间
linux·服务器
honey ball1 小时前
仪表放大器AD620
运维·单片机·嵌入式硬件·物联网·学习
Iceberg_wWzZ1 小时前
数据结构(Day14)
linux·c语言·数据结构·算法
秋已杰爱1 小时前
进程间关系与进程守护
运维·服务器
可儿·四系桜1 小时前
如何在多台Linux虚拟机上安装和配置Zookeeper集群
linux·服务器·zookeeper
Flying_Fish_roe2 小时前
linux-软件包管理-包管理工具(Debian 系)
linux·运维·debian