文本環境:Docker + (Elasticsearch6.8.5 * 3)
1、拉取Elasticsearch
基於Elasticsearch6.8.5版本:
docker pull elasticsearch6.8.5
2、創建es掛載目錄
創建3個文件夾用於存放es掛載地址:es01、es02、es03
[root@CentOS7 ~]# mkdir /es-cluster
[root@CentOS7 ~]# cd /es-cluster/
[root@CentOS7 es-cluster]# mkdir es01
[root@CentOS7 es-cluster]# mkdir es02
[root@CentOS7 es-cluster]# mkdir es03
3、創建配置文件及數據存放目錄
我們以es01
為例,cd es01
,增加es01.yml
配置文件:
# es01.yml
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 10.211.55.4
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["10.211.55.4:9300","10.211.55.4:9301","10.211.55.4:9302"]
discovery.zen.minimum_master_nodes: 2
其他兩個es配置文件類似:
# es02.yml
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 10.211.55.4
http.port: 9201
transport.tcp.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["10.211.55.4:9300","10.211.55.4:9301","10.211.55.4:9302"]
discovery.zen.minimum_master_nodes: 2
# es03.yml
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 10.211.55.4
http.port: 9202
transport.tcp.port: 9302
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: ["10.211.55.4:9300","10.211.55.4:9301","10.211.55.4:9302"]
discovery.zen.minimum_master_nodes: 2
由於默認es實例是1g,太吃內存了,我們修改一下jvm參數,每個es目錄下放一個jvm.option
文件:
-Xms128m
-Xmx128m
4、創建es容器並啟動
docker create --name es01 --net host -v /es-cluster/es01/es01.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /es-cluster/es01/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /es-cluster/es01/data:/usr/share/elasticsearch/data elasticsearch:6.8.5
docker create --name es02 --net host -v /es-cluster/es02/es02.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /es-cluster/es02/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /es-cluster/es02/data:/usr/share/elasticsearch/data elasticsearch:6.8.5
docker create --name es03 --net host -v /es-cluster/es03/es03.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /es-cluster/es03/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /es-cluster/es03/data:/usr/share/elasticsearch/data elasticsearch:6.8.5
創建完docker容器后,啟動es容器:
docker start es01 es02 es03
通過 docker ps -a
查看啟動情況如圖所示,啟動失敗了,我們通過 docker logs es01
查看啟動日志:
data目錄訪問權限異常,我們給一下權限:
chmod 777 es01/data/ -R
chmod 777 es02/data/ -R
chmod 777 es03/data/ -R
重新啟動一下:
docker start es01 es02 es03
在Elasticsearch-head
中連接一下集群:
5、Elasticsearch-head安裝
elasticsearch-head 是用於監控 Elasticsearch 狀態的客戶端插件,包括數據可視化、執行增刪改查操作等。elasticsearch-head 插件的安裝在 Linux 和 Windows 沒什么區別,安裝之前確保當前系統已經安裝 nodejs 即可。
github地址:https://github.com/mobz/elasticsearch-head
如下是安裝命令:
# git 克隆
git clone git://github.com/mobz/elasticsearch-head.git
# 進入下載目錄
cd elasticsearch-head
# 安裝依賴「需要node緩解」
npm install
# 運行
npm run start
瀏覽器訪問:http://127.0.0.1:9100
6、最后補充
至此,基於Docker的Elasticsearch簡單集群就搭建完了,下一篇我們將通過創建索引實例來介紹分片和副本
,以及集群的 故障轉移
等知識點。
推薦閱讀: