一. ELK Stack简介   ELK 是 Elasticsearch、Logstrash 和 Kibana 的缩写,它们代表的是一套成熟的日志管理系统,ELK Stack 已经成为目前最流行的集中式日志解决管理方案。
Elasticsearch:分布式搜索和分析引擎,具有高可伸缩、高可靠和易管理等特点。基于 Apache Lucene 构建,能对大容量的数据进行接近实时的存储、搜索和分析操作。通常被用作某些应用的基础搜索引擎,使其具有复杂的搜索功能; Logstash:数据收集引擎。它支持动态的从各种数据源搜集数据,并对数据进行过滤、分析、丰富、统一格式等操作,然后存储到用户指定的位置; Kibana:数据分析和可视化平台。通常与 Elasticsearch 配合使用,对其中数据进行搜索、分析和以统计图表的方式展示; Filebeat:ELK 协议栈的新成员,一个轻量级开源日志文件数据搜集器,基于 Logstash-Forwarder 源代码开发是对它的替代。在需要采集日志数据的服务上安装 Filebeat,并指定日志目录或日志文件后,Filebeat就能读取日志文件数据,迅速发送到 Logstash进行解析,或直接发送到 Elasticsearch进行集中式存储和分析。
二. 基于Filebeat分布式集群架构部署方案   前面提到 Filebeat已经完全替代了 Logstash-Forwarder成为新一代的日志采集器,同时鉴于它轻量、安全等特点,越来越多人开始使用它。具体基于Filebeat的ELK分布式集中日志解决方案架构如图所示:
三. 软件版本 1 2 3 4 5 Kibana:7.1.1 Filebeat:7.1.1 Logstash:7.1.1 Elasticsearch:7.1.1 Elasticsearch Head:5 
 
四. 环境构建   由于个人不怎么喜欢在windows宿主机上安装各种服务,故在虚拟机里面安装Linux系统进行部署;个人觉得虚拟机安装服务的好处有如下几点:   1> 娱乐时提升电脑性能,如果服务都装在宿主机上,当你不做开发时这些服务也会随系统启动而启动,同时占用电脑部分性能问题;   2> 提升效率,把服务装在虚拟机Linux系统中,就算我们电脑系统重装了,我们只用安装虚拟机然后直接启动已经装好服务的Linux系统即可开干,同时还可以复制给他人或者其他电脑使用等;
1. 虚拟机安装Linux 各位自行百度,这里就不教大家如何安装[大家如有需要虚拟机安装Linux系统配置教程可以留言],本文主要目的是Docker Compose部署ELK 7.1.1分布式集群   虚拟机 : Oracle VM VirtualBox(相对于VMware轻得多,推荐使用,当然大家可以随意,重点不在虚拟机工具)   Linux系统 : CentOS-7-x86_64-DVD-1810(建议虚拟机磁盘内存: 30G, 内存分配: 8G)
2. 安装Docker   Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。   安装教程 : Centos7 安装 Docker CE 
3. 安装Docker Compose   使用 Docker Compose可以轻松、高效的管理容器,它是一个用于定义和运行多容器Docker的应用程序工具。   官网安装教程 : 安装 Docker Compose 
4. 配置镜像加速器   阿里云地址 : [容器镜像服务](https://cr.console.aliyun.com/?spm=5176.12818093.recent.dcr.568016d0d359YZ  **官网安装教程 **: 安装 Docker Compose    针对Docker客户端版本大于 1.10.0 的用户,可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
1 2 3 4 5 6 7 8 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' {   "registry-mirrors": ["https://阿里云容器镜像服务获取地址.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker 
 
五. 注意事项 1. 启动报错 [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144](elasticsearch用户拥有的内存权限太小,至少需要262144) 解决办法:  1 2 3 4 5 6 7 #  修改配置sysctl.conf [root@localhost ~]# vi /etc/sysctl.conf #  添加下面配置: vm.max_map_count=262144 #  重新加载: [root@localhost ~]# sysctl -p #  最后重新启动elasticsearch,即可启动成功。 
2. Docker 命令自动补全 1 2 3 4 #  安装依赖工具bash-complete [root@localhost ~]# yum install -y bash-completion [root@localhost ~]# source /usr/share/bash-completion/completions/docker [root@localhost ~]# source /usr/share/bash-completion/bash_completion 
 
3. 虚拟机磁盘不足 high disk watermark [90%] exceeded on [Hr7ZULQGSGCu9WDsYgLhsA][es-slave1][/usr/share/elasticsearch/data/nodes/0] free: 631.1mb[9.9%], shards will be relocated away from this node※ 虚拟机安装Linux系统时可以避免此问题,磁盘空间设为30G  解决方法: 1> Linux系统磁盘扩容; 2> 重装虚拟机,并把磁盘存储设大一点[建议: 30G];
六. 核心配置文件   按照上面的步骤来,特别是注意事项的几个点处理好,基本上直接部署服务问题不大(经过多次顽强测试),基本上坑已踩完,相关配置文件也会贴出来,供大家参考。不会Docker基本操作的建议大家去了解了解!!!
1. docker-compose.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 version:  "3" services:   es-master:      container_name:  es-master      hostname:  es-master      image:  elasticsearch:7.1.1      restart:  always      ports:        -  9200 :9200        -  9300 :9300      volumes:        -  ./elasticsearch/master/conf/es-master.yml:/usr/share/elasticsearch/config/elasticsearch.yml        -  ./elasticsearch/master/data:/usr/share/elasticsearch/data        -  ./elasticsearch/master/logs:/usr/share/elasticsearch/logs      environment:        -  "ES_JAVA_OPTS=-Xms512m -Xmx512m"    es-slave1:      container_name:  es-slave1      image:  elasticsearch:7.1.1      restart:  always      ports:        -  9201 :9200        -  9301 :9300      volumes:        -  ./elasticsearch/slave1/conf/es-slave1.yml:/usr/share/elasticsearch/config/elasticsearch.yml        -  ./elasticsearch/slave1/data:/usr/share/elasticsearch/data        -  ./elasticsearch/slave1/logs:/usr/share/elasticsearch/logs      environment:        -  "ES_JAVA_OPTS=-Xms512m -Xmx512m"    es-slave2:      container_name:  es-slave2      image:  elasticsearch:7.1.1      restart:  always      ports:        -  9202 :9200        -  9302 :9300      volumes:        -  ./elasticsearch/slave2/conf/es-slave2.yml:/usr/share/elasticsearch/config/elasticsearch.yml        -  ./elasticsearch/slave2/data:/usr/share/elasticsearch/data        -  ./elasticsearch/slave2/logs:/usr/share/elasticsearch/logs      environment:        -  "ES_JAVA_OPTS=-Xms512m -Xmx512m"    es-head:      container_name:  es-head      image:  mobz/elasticsearch-head:5      restart:  always      ports:        -  9100 :9100      depends_on:        -  es-master        -  es-slave1        -  es-slave2    kibana:      container_name:  kibana      hostname:  kibana      image:  kibana:7.1.1      restart:  always      ports:        -  5601 :5601      volumes:        -  ./kibana/conf/kibana.yml:/usr/share/kibana/config/kibana.yml      environment:        -  elasticsearch.hosts=http://es-master:9200      depends_on:        -  es-master        -  es-slave1        -  es-slave2    filebeat:           container_name:  filebeat           hostname:  filebeat           image:  docker.elastic.co/beats/filebeat:7.1.1           restart:  always           volumes:        -  ./filebeat/conf/filebeat.yml:/usr/share/filebeat/filebeat.yml               -  ./logs:/home/project/spring-boot-elasticsearch/logs        -  ./filebeat/logs:/usr/share/filebeat/logs        -  ./filebeat/data:/usr/share/filebeat/data           links:        -  logstash           depends_on:        -  es-master        -  es-slave1        -  es-slave2    logstash:      container_name:  logstash      hostname:  logstash      image:  logstash:7.1.1      command:  logstash  -f  ./conf/logstash-filebeat.conf      restart:  always      volumes:               -  ./logstash/conf/logstash-filebeat.conf:/usr/share/logstash/conf/logstash-filebeat.conf      environment:        -  elasticsearch.hosts=http://es-master:9200               -  xpack.monitoring.elasticsearch.hosts=http://es-master:9200      ports:        -  5044 :5044      depends_on:        -  es-master        -  es-slave1        -  es-slave2  
 
2. es-master.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 cluster.name:  es-cluster node.name:  es-master node.master:  true node.data:  false network.host:  0.0 .0 .0 http.port:  9200 transport.port:  9300 discovery.seed_hosts:   -  es-master    -  es-slave1    -  es-slave2  cluster.initial_master_nodes:   -  es-master  http.cors.enabled:  true http.cors.allow-origin:  "*" xpack.security.enabled:  false 
 
3. es-slave1.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 cluster.name:  es-cluster node.name:  es-slave1 node.master:  true node.data:  true network.host:  0.0 .0 .0 http.port:  9201 discovery.seed_hosts:   -  es-master    -  es-slave1    -  es-slave2  cluster.initial_master_nodes:   -  es-master  http.cors.enabled:  true http.cors.allow-origin:  "*" xpack.security.enabled:  false 
 
4. es-slave2.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 cluster.name:  es-cluster node.name:  es-slave2 node.master:  true node.data:  true network.host:  0.0 .0 .0 http.port:  9202 discovery.seed_hosts:   -  es-master    -  es-slave1    -  es-slave2  cluster.initial_master_nodes:   -  es-master  http.cors.enabled:  true http.cors.allow-origin:  "*" xpack.security.enabled:  false 
 
5. filebeat.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 filebeat.inputs: -  type:  log   enabled:  true    paths:           -  /home/project/spring-boot-elasticsearch/logs/*.log    multiline.pattern:  ^\[    multiline.negate:  true    multiline.match:  after  filebeat.config.modules:   path:  ${path.config}/modules.d/*.yml    reload.enabled:  false  setup.template.settings:   index.number_of_shards:  1  setup.dashboards.enabled:  false setup.kibana:   host:  "http://kibana:5601"  output.logstash:   hosts:  ["logstash:5044" ] processors:   -  add_host_metadata:  ~    -  add_cloud_metadata:  ~  
 
6. logstash-filebeat.conf   Logstash日志数据收集、过滤、存储.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 input {     # 来源beats     beats {         # 端口         port => "5044"     } } # 分析、过滤插件,可以多个 filter {     grok {         match => { "message" => "%{COMBINEDAPACHELOG}"}     }     geoip {         source => "clientip"     } } output {     # 选择elasticsearch     elasticsearch {         hosts => ["http://es-master:9200"]         index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"     } } 
 
7. kibana.yml 1 2 3 4 5 6 7 8 server.port:  5601 server.host:  "0.0.0.0" elasticsearch.hosts:  ["http://es-master:9200" ]i18n.locale:  "zh-CN" 
 
8. init.sh(部署Shell脚本)   脚本可以取代繁琐重复工作!当然linux系统中创建脚本时别忘了chmod给予执行权限哦~~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 # ./bin/bash #  定义颜色 BLUE_COLOR="\033[36m" RED_COLOR="\033[31m" GREEN_COLOR="\033[32m" VIOLET_COLOR="\033[35m" RES="\033[0m" echo -e "${BLUE_COLOR}# ######################################################################${RES}" echo -e "${BLUE_COLOR}#                       Docker ELK Shell Script                        #${RES}" echo -e "${BLUE_COLOR}#                       Blog: www.lmaye.com                            #${RES}" echo -e "${BLUE_COLOR}#                       Email: lmaye@lmaye.com                         #${RES}" echo -e "${BLUE_COLOR}# ######################################################################${RES}" #  创建目录 echo -e "${BLUE_COLOR}---> create [elasticsearch]directory start.${RES}" if [ ! -d "./elasticsearch/" ]; then mkdir -p ./elasticsearch/master/conf ./elasticsearch/master/data ./elasticsearch/master/logs \     ./elasticsearch/slave1/conf ./elasticsearch/slave1/data ./elasticsearch/slave1/logs \     ./elasticsearch/slave2/conf ./elasticsearch/slave2/data ./elasticsearch/slave2/logs fi echo -e "${RED_COLOR}---> create [kibana]directory start.${RES}" if [ ! -d "./kibana/" ]; then mkdir -p ./kibana/conf ./kibana/logs fi  echo -e "${GREEN_COLOR}---> create [logstash]directory start.${RES}"  if [ ! -d "./logstash/" ]; then  mkdir -p ./logstash/conf ./logstash/logs  fi echo -e "${GREEN_COLOR}---> create [filebeat]directory start.${RES}" if [ ! -d "./filebeat/" ]; then mkdir -p ./filebeat/conf ./filebeat/logs ./filebeat/data fi echo -e "${VIOLET_COLOR}---> create [nginx]directory start.${RES}" if [ ! -d "./nginx/" ]; then mkdir -p ./nginx/conf ./nginx/logs ./nginx/www fi echo -e "${BLUE_COLOR}===> create directory success.${RES}" #  目录授权(data/logs 都要授读/写权限) echo -e "${BLUE_COLOR}---> directory authorize start.${RES}" if [ -d "./elasticsearch/" ]; then chmod 777 ./elasticsearch/master/data/ ./elasticsearch/master/logs/ \     ./elasticsearch/slave1/data/ ./elasticsearch/slave1/logs/ \     ./elasticsearch/slave2/data/ ./elasticsearch/slave2/logs fi if [ -d "./filebeat/" ]; then chmod 777 ./filebeat/data/ ./filebeat/logs/ fi echo -e "${BLUE_COLOR}===> directory authorize success.${RES}" #  移动配置文件 echo -e "${BLUE_COLOR}---> move [elasticsearch]config file start.${RES}" if [ -f "./es-master.yml" ] && [ -f "./es-slave1.yml" ] && [ -f "./es-slave2.yml" ]; then mv ./es-master.yml ./elasticsearch/master/conf mv ./es-slave1.yml ./elasticsearch/slave1/conf mv ./es-slave2.yml ./elasticsearch/slave2/conf fi echo -e "${RED_COLOR}---> move [kibana]config file start.${RES}" if [ -f "./kibana.yml" ]; then mv ./kibana.yml ./kibana/conf fi  echo -e "${GREEN_COLOR}---> move [logstash]config file start.${RES}"  if [ -f "./logstash-filebeat.conf" ]; then  mv ./logstash-filebeat.conf ./logstash/conf  fi echo -e "${GREEN_COLOR}---> move [filebeat]config file start.${RES}" if [ -f "./filebeat.yml" ]; then mv ./filebeat.yml ./filebeat/conf fi echo -e "${VIOLET_COLOR}---> move [nginx]config file start.${RES}" if [ -f "./nginx.conf" ]; then mv ./nginx.conf ./nginx/conf fi echo -e "${BLUE_COLOR}===> move config files success.${RES}" echo -e "${GREEN_COLOR}>>>>>>>>>>>>>>>>>> The End <<<<<<<<<<<<<<<<<<${RES}" #  部署项目 echo -e "${BLUE_COLOR}==================> Docker deploy Start <==================${RES}" docker-compose up --build -d 
 
七. 整理归类,蓄势待发   请大家注意一下目录结构、IP、端口哦,按自己的来调整,我的仅供参考!!!(我的IP不遮遮掩掩随意观看,哈哈哈)所有配置文件放在一个目录下,脚本会自动创建和移动配置文件(智能的)~~
执行init.sh脚本结果,如下:
 
脚本执行后的目录结构,如下: 为了测试我附带了一个ES项目,暂时忽视nginx、Dockerfile和spring boot项目
 
 
八. 效果预览 注:  filebeat日志索引没有创建时,请检查日志映射目录是否存在日志。(eg: /home/project/spring-boot-elasticsearch/logs)
ES Head查看ES集群状态和其他的操作,具体自己去玩吧
 
Kibana创建索引模式,其他功能自己去摸索 创建索引模式步骤,基本上很简单,按操作一步步来就好了:
 
 
终极效果,数据分析和可视化平台:
八. 源码地址   spring-boot-elasticsearch项目中包含所有配置文件和Spring Boot整合ES 7.1.1案例,如有需要可以参考!如果有更好的idea也欢迎互相交流,联系方式博客菜单about  ~ ~源码 
九. 总结   文章很长,且行且珍惜!一向追求高版本的我,必将迎难而上。反而越少高版本资料越少,查阅的资料中问题也非常多,为了这次整合,踩了无数次坑,无数次试验!半个月前7.1.1还是官方最高版本,如今都快7.3.x了!不禁感叹,技术更新是如此之快。同时也非常感谢身后的技术大佬提供支持,帮我解决和分析问题,才能让我最终完成此作!(后续更新多机部署ELK和Spring Boot整合ES 7.1.1,敬请关注~)