美文网首页
ELK搭建 轻量级解决方案 基于docker (elastics

ELK搭建 轻量级解决方案 基于docker (elastics

作者: 谁有羊毛 | 来源:发表于2020-02-11 10:00 被阅读0次
    一,构建一个 简易版 轻量级 的elk(实际上没有logstash,叫efk?)
    • 为何不使用logstash?
    1. logstash太占内存了,用起来不舒服;
    2. 如果仅仅是日志量不大的情况下,这套efk也完全足以
    3. logstash的日志格式化功能 在filebeat中也能简单处理
    二,首先开一台linux机器,这里使用centos7.x的。
    1. 我的是window,直接vmware 弄了一台虚拟机,然后安装centos7.x的iso镜像

    2. 安装docker并启动

    sudo yum update
    yum install docker
    systemctl start docker
    
    1. 配置docker阿里云的加速镜像
      https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

    2. 启动elasticsearch (单节点),多节点集群参考 官方介绍文档

    docker run -d -p 0.0.0.0:9200:9200 -p 9300:9300 --name elasticsearch -e "discovery.type=single-node" elasticsearch:7.5.2
    
    • 浏览器访问http://ip:9200/ 看见类似如下即说明启动成功了
    {
      "name" : "b79d7d5cada9",
      "cluster_name" : "docker-cluster",
      "cluster_uuid" : "8sgM5FFzTzCdQuvGu7fuig",
      "version" : {
        "number" : "7.5.2",
        "build_flavor" : "default",
        "build_type" : "docker",
        "build_hash" : "8bec50e1e0ad29dad5653712cf3bb580cd1afcdf",
        "build_date" : "2020-01-15T12:11:52.313576Z",
        "build_snapshot" : false,
        "lucene_version" : "8.3.0",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    
    1. 启动kibana 官方介绍文档
    docker run -d -p 5601:5601 --link elasticsearch -e ELASTICSEARCH_URL=http://elasticsearch:9200 kibana:7.5.2
    
    • 等待一会儿 访问http://ip:5601 , 应该可以访问kibana的web界面了
    1. filebeat 官方文档

    filebeat是一个日志(文件)收集器,特点就是轻量,可以定义多种input,output,比如es、logstash、kafka.....

    (1) filebeat依赖filebeat.xml,最简单的filebeat.xml应该是

    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /home/logs/*.log #匹配这个路径下的日志
    
    # 选择了es作为输出的地方
    output.elasticsearch: 
      hosts: ["elasticsearch:9200"]
    

    (2)使用该配置启动, 启动后应该就可以在看见filebeat收集日志,在kibana可以中可以看见 filebeat-xxx类似的index

    # ~/elk/logs/ 是我的实际的日志宿主机的路径
    docker run --rm -it --name filebeat --link elasticsearch -v ~/elk/filebeat.yml:/usr/share/filebeat/filebeat.yml -v ~/elk/logs/:/home/logs/ docker.elastic.co/beats/filebeat:7.5.2
    

    (3)在实际使用中,可以方便的收集json格式的日志(主要是json方便处理,有自带的decode_json_fields处理器),而配置如下

    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /home/logs/*.log
      json.keys_under_root: true #关键的json配置
    
    setup.ilm.enabled: false # 由于7.5.2有bug,要使用自定义index得使用这个
    setup.template.name: "filebeat"
    setup.template.pattern: "filebeat-*"
    
    # 处理器 可以添加字段 也可以对某些字段单独处理
    #processors:
    #  - add_fields:
    #      target: ""
    #      fields:
    #        name: 'Nothing'
    #  - decode_json_fields:
    #      fields: ["msg"]
    #      target: "msg"
    #      overwrite_keys: true
    
    output.elasticsearch:
      hosts: ["elasticsearch:9200"]
      index: "filebeat-node-%{+yyyy.MM.dd}" # 自定义的index格式
    
    
    • 然后再重启filebeat的docker
    # 强制删除
    docker container rm -f filebeat
    # 启动命令
    docker run --rm -it --name filebeat --link elasticsearch -v ~/elk/filebeat.yml:/usr/share/filebeat/filebeat.yml \
    -v ~/elk/logs/:/home/logs/ docker.elastic.co/beats/filebeat:7.5.2
    

    附:

    elastic文档

    相关文章

      网友评论

          本文标题:ELK搭建 轻量级解决方案 基于docker (elastics

          本文链接:https://www.haomeiwen.com/subject/fnzsxhtx.html