Linux系統Centos7 基於Docker搭建ELK分佈式日誌系統

ELK 基本概述

ELK是Elasticsearch、Logstash、Kibana的簡稱,經常用於部署分佈式系統日誌服務。
  • Elasticsearch:全球實時全文搜索和分析引擎,提供蒐集、分析、存儲數據三大功能;是一套開放REST和JAVA API等結構提供高效搜索功能,可擴展的分佈式系統。它構建於Apache Lucene搜索引擎庫之上。
  • Logstash:用來蒐集、分析、過濾日誌的工具。它支持幾乎任何類型的日誌,包括系統日誌、錯誤日誌和自定義應用程序日誌。它能夠從許多來源接收日誌,這些來源包括 syslog、消息傳遞(例如 RabbitMQ)和JMX,它可以以多種方式輸出數據,包括電子郵件、websockets和Elasticsearch。
  • Kibana:基於Web的圖形可視化界面,用於搜索、分析和可視化存儲在 Elasticsearch指標中的日誌數據。它利用Elasticsearch的REST接口來檢索數據,不只容許用戶建立他們本身的數據的定製儀表板視圖,還容許他們以特殊的方式查詢和過濾數據。

基本架構圖elk-architecture]:
ce9905fa6e9ea486655e7f0ce63aead9.png
應用程序將日誌按照約定的Key寫入Redis,Logstash從Redis中讀取日誌信息寫入ElasticSearch集羣。Kibana讀取ElasticSearch中的日誌,並在Web頁面中以表格/圖表的形式展現。html

搭建部署ElasticSearch服務

Docker 部署應用服務的基本步驟:Search[查詢鏡像]->Pull[拉取鏡像]->Run[部署鏡像]

1.查詢Elasticsearch 鏡像: docker search elasticsearch
elasticsearch-search前端

ps[注意事項]:node

  1. 通常拉取鏡像資源都是從Docker官方倉庫[docker-hub]拉取,或者本身構建的Docker雲倉庫aliyun-docker
  2. 本教程選取的ELK鏡像均是基於ELK官方Docker倉庫elastic-io

2.拉取Elasticsearch 鏡像:docker pull docker.elastic.co/elasticsearch/elasticsearch:7.3.1
elasticsearch-pulllinux

ps[注意事項]:
1.本教程採用7.3.x版本,目前最新版本7.4.x[主要用7.3.x版本在阿里雲搭建過,避免入坑問題]
2.拉取的過程當中可能會出現[net/http: TLS handshake timeout]問題,多嘗試幾回,主要是網絡帶寬限制問題

3.修改鏡像名稱:docker tag docker.elastic.co/elasticsearch/elasticsearch:7.3.1  elasticsearch:latest
elasticsearch-tagnginx

ps[注意事項]:
1.名稱過長致使查看些許不便,經過docker tag source-image[來源鏡像] target-image[目標鏡像],推薦統一採用[target-image:target-version]格式定義,且不佔用空間,至關於重命名鏡像
2.對於拉取kibana[docker.elastic.co/kibana/kibana:7.3.1]和logstash[docker.elastic.co/logstash/logstash:7.3.1] 都建議修改。

4.部署鏡像服務:
部署命令:
docker run -itd -p 9200:9200 -p 9300:9300 --restart=always --privileged=true --name elasticsearch-server -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms=512m -Xms=512m" elasticsearch:latestweb

/usr/share/elasticsearch/config
/usr/share/elasticsearch/logs
查看容器列表:docker ps --format "table {{.ID}}t{{.Names}}t{{.Ports}}"
docker ps
ps[注意事項]:spring

1.須要開放端口[9200和9300]->9200做爲Http協議,主要用於外部通信,9300做爲Tcp協議,jar之間就是經過tcp協議通信,一般部署集羣就是經過9300通訊。推薦[宿主機自定義端口:9200]
2.--restart=always :配置容器重啓策略,當宿主機重啓因爲配置了開機自啓動,不用手動啓動
3.--privileged:配置容器操做權限[true-root操做權限,false-當前容器用戶操做權限]
4.對於部署網絡模式推薦默認橋接模式,也自定義能夠host模式等

5.修改配置:
進入容器:docker exec -it container-id[容器id] or container-name[容器名稱] /bin/bash
例如:docker exec -it f2d2e97da375 /bin/bash #f2d2e97da375-> container-id
docker-execdocker

修改配置文件:apache

[root@f2d2e97da375 elasticsearch]# ls 
LICENSE.txt  NOTICE.txt  README.textile  bin  config  data  jdk  lib  logs  modules  plugins
[root@f2d2e97da375 elasticsearch]# 
[root@f2d2e97da375 elasticsearch]# cd config  
[root@f2d2e97da375 config]# ls
elasticsearch.keystore  elasticsearch.yml  jvm.options  log4j2.properties  role_mapping.yml  roles.yml  users  users_roles
[root@f2d2e97da375 config]# vi elasticsearch.yml

添加跨域配置: http.cors.enabled: true && http.cors.allow-origin: "*" json

cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"

而後退出exit容器,在宿主機重啓容器:docker restart container-id[容器id] or container-name[容器名稱]
docker restart f2d2e97da375

[root@f2d2e97da375 config]# exit
exit
[root@centos-meteor ~]# docker restart f2d2e97da375
f2d2e97da375
[root@centos-meteor ~]#

ps[注意事項]:
1.進入容器方式:包括使用 docker attach 命令或 docker exec 命令,
推薦使用 docker exec 命令。緣由:

  • docker attach: 使用exit退出容器,會致使容器的中止
  • docker exec:使用exit退出容器,不會致使容器的中止
  • 參考docker進入容器的幾種方法博客-docker進入容器的幾種方法

2.若是Docker安裝了可視化界面 Portainer,推薦採用這種方式進入容器:
docker-portainer

搭建部署ElasticSearch-Head服務

ElasticSearch-Head:彈性搜索集羣的Web前端界面,是使用Nodjs構建的,主要用於查看ElasticSearch相關信息

1.拉取Elasticsearch-Head 鏡像:docker pull mobz/elasticsearch-head:5

[root@centos-amber ~]# docker pull mobz/elasticsearch-head:5
5: Pulling from mobz/elasticsearch-head
75a822cd7888: Pull complete 
57de64c72267: Pull complete 
4306be1e8943: Pull complete 
871436ab7225: Pull complete 
0110c26a367a: Pull complete 
1f04fe713f1b: Pull complete 
723bac39028e: Pull complete 
7d8cb47f1c60: Pull complete 
7328dcf65c42: Pull complete 
b451f2ccfb9a: Pull complete 
304d5c28a4cf: Pull complete 
4cf804850db1: Pull complete 
Digest: sha256:55a3c82dd4ba776e304b09308411edd85de0dc9719f9d97a2f33baa320223f34
Status: Downloaded newer image for mobz/elasticsearch-head:5
docker.io/mobz/elasticsearch-head:5
[root@centos-amber ~]#

2.修改Elasticsearch-Head 鏡像名稱:docker tag  mobz/elasticsearch-head:5   elasticsearch-head:latest

[root@centos-amber ~]# docker tag  mobz/elasticsearch-head:5        elasticsearch-head:latest
[root@centos-amber ~]# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
grafana/grafana                                 latest              05d1bcf30d16        7 days ago          207MB
nginx                                           latest              540a289bab6c        3 weeks ago         126MB
prom/prometheus                                 latest              2c8e464e47f4        3 weeks ago         129MB
moxm/sentinel-dashboard                         latest              0ccaac81584e        4 weeks ago         167MB
portainer                                       latest              4cda95efb0e4        4 weeks ago         80.6MB
portainer/portainer                             latest              4cda95efb0e4        4 weeks ago         80.6MB
apache/skywalking-ui                            latest              fa66ca9c9862        2 months ago        123MB
apache/skywalking-oap-server                    latest              376a37cdf65c        2 months ago        190MB
docker.elastic.co/kibana/kibana                 7.3.1               b54865ba6b0b        2 months ago        1.01GB
docker.elastic.co/elasticsearch/elasticsearch   7.3.1               3d3aa92f641f        2 months ago        807MB
elasticsearch                                   latest              3d3aa92f641f        2 months ago        807MB
prom/node-exporter                              latest              e5a616e4b9cf        5 months ago        22.9MB
google/cadvisor                                 latest              eb1210707573        12 months ago       69.6MB
elasticsearch-head                              latest              b19a5c98e43b        2 years ago         824MB
mobz/elasticsearch-head                         5                   b19a5c98e43b        2 years ago         824MB
tutum/influxdb                                  latest              c061e5808198        3 years ago         290MB
[root@centos-amber ~]#

3.部署Elasticsearch-Head 容器:docker run -itd --restart=always --privileged=true -p 9100:9100 --name elasticsearch-head-server elasticsearch-head:latest
查看容器服務:docker ps --format "table {{.ID}}t{{.Names}}t{{.Ports}}"
docker-elasticsearch-head
4.瀏覽器訪問:http://remote-ip:9100/
elasticsearch-head

搭建部署Kibana服務

1.拉取Kibana 鏡像:
docker pull docker.elastic.co/kibana/kibana:7.3.1
2.修改Kibana鏡像名稱:
docker tag docker.elastic.co/kibana/kibana:7.3.1  kibana:latest
3.部署Kibana鏡像容器:
docker run -itd -p 5601:5601 --restart=always --privileged=true --link
elasticsearch-server:elasticsearch --name kibana-server -e ELASTICSEARCH_URL=http://elasticsearch:9200 kibana:latest

搭建部署Logstash服務

1.拉取Logstash 鏡像:
docker pull docker.elastic.co/logstash/logstash:7.3.1
2.修改Kibana鏡像名稱:
docker tag docker.elastic.co/logstash/logstash:7.3.1  logstash:latest
3.部署Kibana鏡像容器:
docker run -itd --restart=always --privileged=true -p 5043:5043 --name logstash-server  --link elasticsearch-server:elasticsearch  logstash:latest
4.進入容器-修改配置logstash.yml:

http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.url: http://host-ip:9200
xpack.monitoring.elasticsearch.username: elastic
xpack.monitoring.elasticsearch.password: changme

ps[注意事項]:
1.host-ip是本機ip地址
5.進入容器-修改pipeline下的logstash.conf文件:

#默認配置
#========================================
#input {
#  beats {
#    port => 5044
#  }
#}

#output {
#  stdout {
#    codec => rubydebug
#  }
#}
#========================================
#添加配置
input {
        file {
            codec=> json
                path => "/usr/local/*.json"
        }
}
filter {
  #定義數據的格式
  grok {
    match => { "message" => "%{DATA:timestamp}\|%{IP:serverIp}\|%{IP:clientIp}\|%{DATA:logSource}\|%{DATA:userId}\|%{DATA:reqUrl}\|%{DATA:reqUri}\|%{DATA:refer}\|%{DATA:device}\|%{DATA:textDuring}\|%{DATA:duringTime:int}\|\|"}
  }
}
output {
   elasticsearch{
     hosts=> "http://host-ip:9200"
   }
}

6.退出容器在宿主機重啓elk相關的容器:docker restart elk相關容器服務

ps[注意事項]:若是Docker安裝了可視化界面 Portainer,能夠在界面操做:
portainer-command

7.訪問地址:http://remote-ip:5601/,而後可就額操做kibana面板

搭建部署Apm-server服務和Filebeat服務

步驟基本和上述操做差很少,只是配置文件和端口可能不一致:
拉取鏡像:
docker pull docker.elastic.co/beats/filebeat:7.3.1
docker pull docker.elastic.co/apm/apm-server:7.3.1

修改鏡像名稱:
docker tag docker.elastic.co/beats/filebeat:7.3.1     filebeat:latest
docker tag docker.elastic.co/apm/apm-server:7.3.1     apm-server:latest

部署容器:
docker run -itd --restart=always --privileged=true  -p 5044:5044 --name filebeat-server --link logstash-server:logstash  filebeat:latest

docker run -itd --restart=always --privileged=true -p 8200:8200  --name apm-server  --link elasticsearch-server:elasticsearch apm-server:latest --strict.perms=false -e  -E output.elasticsearch.hosts=["elasticsearch:9200"]

最後修改配置文件整合相關資源,重啓容器服務

ps[注意事項]: 能夠參考官方文檔:
elasticsearch:https://www.elastic.co/guide/...
kibana:https://www.elastic.co/guide/...
logstash:https://www.elastic.co/guide/...
filebeat:https://www.elastic.co/guide/...
apm-server:https://www.elastic.co/guide/...

開發SpringBoot+Elasticsearch集成實戰

[1] 集成Maven配置方式:

Java與ElasticSearch鏈接的兩種方式:
(1)使用Transport與ElasticSearch創建鏈接

<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
       <groupId>org.elasticsearch</groupId>
       <artifactId>elasticsearch</artifactId>
       <version>6.4.2</version>
 </dependency>
   <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->      <dependency>
   <groupId>org.elasticsearch.client</groupId>
   <artifactId>transport</artifactId>
   <version>6.4.2</version>
   <exclusions>
       <exclusion>
       <groupId>org.elasticsearch</groupId>
       <artifactId>elasticsearch</artifactId>
       </exclusion>
   </exclusions>
 </dependency>

                                                             
(2)使用SpringDataElasticSearch創建鏈接

<dependency>    
<groupId>org.springframework.boot</groupId>    
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

或者:

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    // 使用SpringDataElasticSearch只須要添加一處依賴即用
    compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')
}

兩種方式的優缺點:
(1)優勢:脫離框架,集成過程當中不須要考慮與Spring的版本兼容問題,容易集成

缺點:使用原生API操做ES,代碼量大,撰寫困難

(2)優勢:將原生API進行封裝,提供了ElasticsearchRepository,操做ES很是簡單,與JPA同理

 缺點:出生於Spring家族,與SpringBoot,SpringData版本容易衝突

[2] 參數鏈接配置方式:

# Elasticsearch# 9200端口是用來讓HTTP REST API來訪問ElasticSearch,而9300端口是傳輸層監聽的默認端口
elasticsearch.ip=192.168.30.128
elasticsearch.port=9300
elasticsearch.pool=5
elasticsearch.cluster.name=my-application

node.name: "elasticsearch-server"network.host: 0.0.0.0network.bind_host: 0.0.0.0network.publish_host: 0.0.0.0http.cors.enabled: truehttp.cors.allow-origin: "*"bootstrap.memory_lock: truetransport.tcp.port: 9300transport.tcp.compress: truehttp.max_content_length: 128mb版權聲明:本文爲博主原創文章,遵循相關版權協議,如若轉載或者分享請附上原文出處連接和連接來源。

相關文章
相關標籤/搜索