Docker 搭建 elasticsearch 集群

前置工作

1.虚拟机内存设置起码2个g以上,不然巨卡

2.需要修改linux的进程数限制

vi /etc/sysctl.conf
vm.max_map_count=655360
sysctl -p

创建es集群

1.下载es镜像

docker pull elasticsearch:5.6.11

2.添加挂载的配置文件与文件夹

mkdir -p /mydata/elasticsearch/conf/
mkdir -p /mydata/elasticsearch2/conf/
mkdir -p /mydata/elasticsearch3/conf/

创建配置文件es1
# 开启跨域,为了让es-head可以访问
http.cors.enabled: true
http.cors.allow-origin: "*"

# 集群的名称
cluster.name: elasticsearch
# 节点的名称
node.name: es1
# 指定该节点是否有资格被选举成为master节点,默认是true,es是默认集群中的第一台机器为master,如果这台机挂了就会重新选举master
node.master: true
# 允许该节点存储数据(默认开启)
node.data: true
# 允许任何ip访问
network.host: 0.0.0.0
# 通过这个ip列表进行节点发现,我这里配置的是各个容器的ip
discovery.zen.ping.unicast.hosts: ["192.168.31.128:9300","192.168.31.128:9301","192.168.31.128:9302"]
#如果没有这种设置,遭受网络故障的集群就有可能将集群分成两个独立的集群 – 导致脑裂 - 这将导致数据丢失
discovery.zen.minimum_master_nodes: 2

创建配置文件es2
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: elasticsearch
node.name: es2

network.host: 0.0.0.0
node.master: true
node.data: true

discovery.zen.ping.unicast.hosts: ["192.168.31.128:9300","192.168.31.128:9301","192.168.31.128:9302"]
discovery.zen.minimum_master_nodes: 2

创建配置文件es3
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: elasticsearch
node.name: es3

network.host: 0.0.0.0
node.master: true
node.data: true

discovery.zen.ping.unicast.hosts: ["192.168.31.128:9300","192.168.31.128:9301","192.168.31.128:9302"]
discovery.zen.minimum_master_nodes: 2

3.集群化实例启动

实例一

docker run --name es1 -p 9200:9200 -p 9300:9300 
-e ES_JAVA_OPTS="-Xms256m -Xmx256m" 
-v /mydata/elasticsearch/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml 
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data -d elasticsearch:5.6.11

实例二

docker run --name es2 -p 9201:9200 -p 9301:9300 
-e ES_JAVA_OPTS="-Xms256m -Xmx256m" 
-v /mydata/elasticsearch2/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml 
-v /mydata/elasticsearch2/data:/usr/share/elasticsearch/data -d elasticsearch:5.6.11

实例三

docker run --name es3 -p 9202:9200 -p 9302:9300 
-e ES_JAVA_OPTS="-Xms256m -Xmx256m" 
-v /mydata/elasticsearch3/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml 
-v /mydata/elasticsearch3/data:/usr/share/elasticsearch/data -d elasticsearch:5.6.11