Docker中EFK环境的搭建及使用

问题
在服务端使用docker搭建了几个server,当server出现问题只能通过docker logs命令来查看日志,或者使用json-file日志驱动得来的日志;
1. 使用docker logs命令麻烦,无法储存,开发或者实施人员查看不是很方便
2. 使用json-file日志得来的日志,以 /var/lib/docker/containers/container_id/container_id.json方式存在,最关键是容器一旦被重建,日志不复存在
3. 多日志分散,存放查看麻烦

EFK的简介
Elasticsearch,Fluentd,Kibana三个组合起来简称EFK,
Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Fluentd是一个日志收集系统,通过丰富的插件,可以收集来自于各种系统或应用的日志,然后根据用户定义将日志做分类处理。
Kibana 也是一个开源和免费的工具,Kibana可以为 Fluent 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
将Fluentd收集来的日志交给Elasticsearch检索使用,并用Kibana将其可视化

  1. 准备docker-compose.yml

    version: "3"
    services:
      elk_server:
        image: lis0x90/efk 
        container_name: efk_server
            network_mode: bridge   # 可有可无
        ports:
          - "5601:5601"
          - "9200:9200"
          - "24224:24224"
        volumes:
          - ./config:/etc/fluent  # ./config/fluent.conf 定义了fluentd服务的配置
          - ./log:/var/log  # 将fluentd收集的日志映射出来,留一份到本地
          - /etc/localtime:/etc/localtime  # 同步本地的时间(可有可无)
        logging:
          driver: "json-file"
          options:
             max-size: "200k"
             max-file: "10"
  2. 准备fluent.conf

        <source>
          @type forward
          port 24224
          bind 0.0.0.0
        </source>
    
        <match *.**>
          @type copy
          <store>
            @type elasticsearch
            host localhost
            port 9200
            logstash_format true
            logstash_prefix fluentd
            logstash_dateformat %Y%m%d
            include_tag_key true
            type_name access_log
            tag_key @log_name
            flush_interval 1s
          </store>
          <store>
            @type stdout
          </store>
        </match>
  3. 运行 docker-compose up -d,启动EFK.

  4. 测试服务:httpd_server
    docker-composer.yml

    version: '3'
    services:
      http_server:
        image: httpd
        container_name: http_server
        ports:
          - "80:80"
        logging:
          driver: "fluentd"
          options:
            fluentd-address: localhost:24224
            tag: httpd_server
  5. 运行 docker-compose up -d,启动http_server.
  6. http://your_ip:9200/_search?pretty 获取 “_index” : “fluentd-*”,
  7. http://your_ip:5601 使用kiabna

猜你喜欢

转载自blog.csdn.net/yzlll/article/details/80313595