如何使用docker部署Elasticsearch集群

今天小編給大家分享一下如何使用docker部署Elasticsearch集群的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的吉林網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

注意,6.x版本已經(jīng)不能通過(guò) -epath.config 參數(shù)去指定配置文件的加載位置,文檔說(shuō)明:

for the archive distributions, the config directory location defaults to $es_home/config. the location of the >config directory can be changed via the es_path_conf environment variable as follows:
es_path_conf=/path/to/my/config ./bin/elasticsearch
alternatively, you can export the es_path_conf environment variable via the command line or via your shell profile.

即交給環(huán)境變量 es_path_conf 來(lái)設(shè)定了(),單機(jī)部署多個(gè)實(shí)例且不使用容器的同學(xué)多多注意。

準(zhǔn)備工作

安裝 docker & docker-compose

這里推進(jìn)使用 daocloud 做個(gè)加速安裝:

#docker
curl -ssl https://get.daocloud.io/docker | sh

#docker-compose
curl -l \
https://get.daocloud.io/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` \
> /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

#查看安裝結(jié)果
docker-compose -v

數(shù)據(jù)目錄

#創(chuàng)建數(shù)據(jù)/日志目錄 這里我們部署3個(gè)節(jié)點(diǎn)
mkdir /opt/elasticsearch/data/{node0,nod1,node2} -p
mkdir /opt/elasticsearch/logs/{node0,nod1,node2} -p
cd /opt/elasticsearch
#權(quán)限我也很懵逼啦 給了 privileged 也不行 索性0777好了
chmod 0777 data/* -r && chmod 0777 logs/* -r

#防止jvm報(bào)錯(cuò)
echo vm.max_map_count=262144 >> /etc/sysctl.conf
sysctl -p

docker-compse 編排服務(wù)

創(chuàng)建編排文件

vim docker-compose.yml

參數(shù)說(shuō)明

- cluster.name=elasticsearch-cluster

集群名稱(chēng)

- node.name=node0
- node.master=true
- node.data=true

節(jié)點(diǎn)名稱(chēng)、是否可作為主節(jié)點(diǎn)、是否存儲(chǔ)數(shù)據(jù)

- bootstrap.memory_lock=true

鎖定進(jìn)程的物理內(nèi)存地址避免交換(swapped)來(lái)提高性能

- http.cors.enabled=true
- http.cors.allow-origin=*

開(kāi)啟cors以便使用head插件

- "es_java_opts=-xms512m -xmx512m"

jvm內(nèi)存大小配置

- "discovery.zen.ping.unicast.hosts=elasticsearch_n0,elasticsearch_n1,elasticsearch_n2"
- "discovery.zen.minimum_master_nodes=2"

由于5.2.1后的版本是不支持多播的,所以需要手動(dòng)指定集群各節(jié)點(diǎn)的tcp數(shù)據(jù)交互地址,用于集群的節(jié)點(diǎn)發(fā)現(xiàn)failover,默認(rèn)缺省9300端口,如設(shè)定了其它端口需另行指定,這里我們直接借助容器通信,也可以將各節(jié)點(diǎn)的9300映射至宿主機(jī),通過(guò)網(wǎng)絡(luò)端口通信。

設(shè)定failover選取的quorum = nodes/2 + 1

當(dāng)然,也可以掛載自己的配置文件,es鏡像的配置文件是/usr/share/elasticsearch/config/elasticsearch.yml,掛載如下:

volumes:
 - path/to/local/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro

docker-compose.yml

version: '3'
services:
 elasticsearch_n0:
  image: elasticsearch:6.6.2
  container_name: elasticsearch_n0
  privileged: true
  environment:
   - cluster.name=elasticsearch-cluster
   - node.name=node0
   - node.master=true
   - node.data=true
   - bootstrap.memory_lock=true
   - http.cors.enabled=true
   - http.cors.allow-origin=*
   - "es_java_opts=-xms512m -xmx512m"
   - "discovery.zen.ping.unicast.hosts=elasticsearch_n0,elasticsearch_n1,elasticsearch_n2"
   - "discovery.zen.minimum_master_nodes=2"
  ulimits:
   memlock:
    soft: -1
    hard: -1
  volumes:
   - ./data/node0:/usr/share/elasticsearch/data
   - ./logs/node0:/usr/share/elasticsearch/logs
  ports:
   - 9200:9200
 elasticsearch_n1:
  image: elasticsearch:6.6.2
  container_name: elasticsearch_n1
  privileged: true
  environment:
   - cluster.name=elasticsearch-cluster
   - node.name=node1
   - node.master=true
   - node.data=true
   - bootstrap.memory_lock=true
   - http.cors.enabled=true
   - http.cors.allow-origin=*
   - "es_java_opts=-xms512m -xmx512m"
   - "discovery.zen.ping.unicast.hosts=elasticsearch_n0,elasticsearch_n1,elasticsearch_n2"
   - "discovery.zen.minimum_master_nodes=2"
  ulimits:
   memlock:
    soft: -1
    hard: -1
  volumes:
   - ./data/node1:/usr/share/elasticsearch/data
   - ./logs/node1:/usr/share/elasticsearch/logs
  ports:
   - 9201:9200
 elasticsearch_n2:
  image: elasticsearch:6.6.2
  container_name: elasticsearch_n2
  privileged: true
  environment:
   - cluster.name=elasticsearch-cluster
   - node.name=node2
   - node.master=true
   - node.data=true
   - bootstrap.memory_lock=true
   - http.cors.enabled=true
   - http.cors.allow-origin=*
   - "es_java_opts=-xms512m -xmx512m"
   - "discovery.zen.ping.unicast.hosts=elasticsearch_n0,elasticsearch_n1,elasticsearch_n2"
   - "discovery.zen.minimum_master_nodes=2"
  ulimits:
   memlock:
    soft: -1
    hard: -1
  volumes:
   - ./data/node2:/usr/share/elasticsearch/data
   - ./logs/node2:/usr/share/elasticsearch/logs
  ports:
   - 9202:9200

這里我們分別為node0/node1/node2開(kāi)放宿主機(jī)的9200/9201/9202作為http服務(wù)端口,各實(shí)例的tcp數(shù)據(jù)傳輸用默認(rèn)的9300通過(guò)容器管理通信。

如果需要多機(jī)部署,則將estransport.tcp.port: 9300端口映射至宿主機(jī)xxxx端口,discovery.zen.ping.unicast.hosts填寫(xiě)各主機(jī)代理的地址即可:

#比如其中一臺(tái)宿主機(jī)為192.168.1.100
  ...
  - "discovery.zen.ping.unicast.hosts=192.168.1.100:9300,192.168.1.101:9300,192.168.1.102:9300"
  ...
ports:
 ...
 - 9300:9300

創(chuàng)建并啟動(dòng)服務(wù)

[root@localhost elasticsearch]# docker-compose up -d
[root@localhost elasticsearch]# docker-compose ps
   name          command        state        ports       
--------------------------------------------------------------------------------------------
elasticsearch_n0  /usr/local/bin/docker-entr ...  up   0.0.0.0:9200->9200/tcp, 9300/tcp
elasticsearch_n1  /usr/local/bin/docker-entr ...  up   0.0.0.0:9201->9200/tcp, 9300/tcp
elasticsearch_n2  /usr/local/bin/docker-entr ...  up   0.0.0.0:9202->9200/tcp, 9300/tcp

#啟動(dòng)失敗查看錯(cuò)誤
[root@localhost elasticsearch]# docker-compose logs
#最多是一些訪問(wèn)權(quán)限/jvm vm.max_map_count 的設(shè)置問(wèn)題

查看集群狀態(tài)

192.168.20.6 是我的服務(wù)器地址

訪問(wèn)http://192.168.20.6:9200/_cat/nodes?v即可查看集群狀態(tài):

ip     heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.25.0.3      36     98 79  3.43  0.88   0.54 mdi    *   node0
172.25.0.2      48     98 79  3.43  0.88   0.54 mdi    -   node2
172.25.0.4      42     98 51  3.43  0.88   0.54 mdi    -   node1

驗(yàn)證 failover

通過(guò)集群接口查看狀態(tài)

模擬主節(jié)點(diǎn)下線,集群開(kāi)始選舉新的主節(jié)點(diǎn),并對(duì)數(shù)據(jù)進(jìn)行遷移,重新分片。

[root@localhost elasticsearch]# docker-compose stop elasticsearch_n0
stopping elasticsearch_n0 ... done

集群狀態(tài)(注意換個(gè)http端口 原主節(jié)點(diǎn)下線了),down掉的節(jié)點(diǎn)還在集群中,等待一段時(shí)間仍未恢復(fù)后就會(huì)被剔出

ip     heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.25.0.2      57     84  5  0.46  0.65   0.50 mdi    -   node2
172.25.0.4      49     84  5  0.46  0.65   0.50 mdi    *   node1
172.25.0.3                            mdi    -   node0

等待一段時(shí)間

ip     heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.25.0.2      44     84  1  0.10  0.33   0.40 mdi    -   node2
172.25.0.4      34     84  1  0.10  0.33   0.40 mdi    *   node1

恢復(fù)節(jié)點(diǎn) node0

[root@localhost elasticsearch]# docker-compose start elasticsearch_n0
starting elasticsearch_n0 ... done

等待一段時(shí)間

ip     heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.25.0.2      52     98 25  0.67  0.43   0.43 mdi    -   node2
172.25.0.4      43     98 25  0.67  0.43   0.43 mdi    *   node1
172.25.0.3      40     98 46  0.67  0.43   0.43 mdi    -   node0

配合 head 插件觀察

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start

集群狀態(tài)圖示更容易看出數(shù)據(jù)自動(dòng)遷移的過(guò)程

1、集群正常 數(shù)據(jù)安全分布在3個(gè)節(jié)點(diǎn)上

如何使用docker部署Elasticsearch集群

2、下線 node1 主節(jié)點(diǎn) 集群開(kāi)始遷移數(shù)據(jù)

遷移中

如何使用docker部署Elasticsearch集群

遷移完成

如何使用docker部署Elasticsearch集群

3、恢復(fù) node1 節(jié)點(diǎn)

如何使用docker部署Elasticsearch集群

問(wèn)題小記

elasticsearch watermark

部署完后創(chuàng)建索引發(fā)現(xiàn)有些分片處于 unsigned 狀態(tài),是由于 elasticsearch watermark:low,high,flood_stage的限定造成的,默認(rèn)硬盤(pán)使用率高于85%就會(huì)告警,開(kāi)發(fā)嘛,手動(dòng)關(guān)掉好了,數(shù)據(jù)會(huì)分片到各節(jié)點(diǎn),生產(chǎn)自行決斷。

curl -x put http://192.168.20.6:9201/_cluster/settings \
-h 'content-type':'application/json' \
-d '{"transient":{"cluster.routing.allocation.disk.threshold_enabled": false}}'

以上就是“如何使用docker部署Elasticsearch集群”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站名稱(chēng):如何使用docker部署Elasticsearch集群
當(dāng)前地址:http://muchs.cn/article4/pphpie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)域名注冊(cè)、小程序開(kāi)發(fā)商城網(wǎng)站、做網(wǎng)站服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)