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]:
應用程式將日誌按照約定的Key寫入Redis,Logstash從Redis中讀取日誌資訊寫入ElasticSearch叢集。Kibana讀取ElasticSearch中的日誌,並在Web頁面中以表格/圖表的形式展示。
搭建部署ElasticSearch服務
Docker 部署應用服務的基本步驟:Search[查詢映象]->Pull[拉取映象]->Run[部署映象]
1.查詢Elasticsearch 映象: docker search elasticsearch
ps[注意事項]:
- 一般拉取映象資源都是從Docker官方倉庫[docker-hub]拉取,或者自己構建的Docker雲倉庫aliyun-docker 等
- 本教程選取的ELK映象均是基於ELK官方Docker倉庫elastic-io
2.拉取Elasticsearch 映象:docker pull docker.elastic.co/elasticsearch/elasticsearch:7.3.1
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
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:latest
/usr/share/elasticsearch/config
/usr/share/elasticsearch/logs
檢視容器列表:docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"
ps[注意事項]:
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
修改配置檔案:
[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: "*"
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,推薦採用這種方式進入容器:
搭建部署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}}"
4.瀏覽器訪問:http://remote-ip:9100/
搭建部署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,可以在介面操作:
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/en/elasticsearch/reference/7.4/docker.html
kibana:https://www.elastic.co/guide/en/kibana/7.3/docker.html
logstash:https://www.elastic.co/guide/en/logstash/7.3/docker.html
filebeat:https://www.elastic.co/guide/en/beats/filebeat/7.3/running-on-docker.html
apm-server:https://www.elastic.co/guide/en/apm/server/7.3/running-on-docker.html
開發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.0
network.bind_host: 0.0.0.0
network.publish_host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
bootstrap.memory_lock: true
transport.tcp.port: 9300
transport.tcp.compress: true
http.max_content_length: 128mb
版權宣告:本文為博主原創文章,遵循相關版權協議,如若轉載或者分享請附上原文出處連結和連結來源。