关于集群的动态连接方式官方的文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-update-settings.html
关于参数的官网说明: https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-discovery-settings.html
根据官网的dockerFile文件中的信息 WORKDIR /usr/share/elasticsearch可以得知 elasticsearch 的安装位置在/usr/share/elasticsearch目录下,为了方便后面的操作,创建数据卷将elasticsearch产生的数据映射到主机中,防止es宕机数据无法恢复。
分别存放数据、配置、插件 创建3个yml配置文件
意味着只要启动ElasticSearch然后通过ES自带的RestFul风格的操作既可以完成集群的搭建 dockerhub中官方的启动方式是单节点的启动 先单独启动3个ElasticSearch节点
通过PUT方式 通过linux的curl命令操作即可 在es02和es03上通过
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d' { "persistent" : { "cluster" : { "remote" : { "leader" : { "seeds" : [ "127.0.0.1:9300" ] } } } } } '更新persistent
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d' { "persistent" : { "indices.recovery.max_bytes_per_sec" : "50mb" } } '更新transient
curl -X PUT "localhost:9200/_cluster/settings?flat_settings=true&pretty" -H 'Content-Type: application/json' -d' { "transient" : { "indices.recovery.max_bytes_per_sec" : "20mb" } } '删除transient配置内容
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d' { "transient" : { "indices.recovery.max_bytes_per_sec" : null } } '删除所有transient设置
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d' { "transient" : { "indices.recovery.*" : null } } '失败的原因应该是启动容器的时候应该少了参数。 官网文档 上说将来的版本不再使用discovery.zen.ping.unicast.hosts也就变成discovery.seed_hosts
请修改注释行宿主机ip,以及es集群通信的端口地址
cluster.name: elasticsearch-cluster node.name: es-node1 network.bind_host: 0.0.0.0 network.publish_host: 192.168.117.231 #修改为docker的宿主机ip http.port: 9200 #这个是容器内部的,所以不用改 transport.tcp.port: 9300 #这个是容器内部的,所以不用改 http.cors.enabled: true http.cors.allow-origin: "*" node.master: true node.data: true discovery.seed_hosts: ["192.168.117.231:9300","192.168.117.231:9301","192.168.117.232:9302"] discovery.zen.minimum_master_nodes: 2 indices.query.bool.max_clause_count: 10240 #使用一台主机的不同端口搭建,在启动docker容器的时候通过-p指定映射端口通过上述配置文件中可知需要映射端口分别是9300、9301、9302,这些端口是用来进行集群通信的 也就是三个节点绑定端口分别是-p 9300:9300,-p 9301:9300,-p 9302:9300, 同时我们可以推出使用-p 9200:9200,-p 9201:9200,-p 9202:9200
也就是说宿主机(192.168.117.231)的9200-9202,9300-9302端口都会被es集群使用
与es-node1不同的地方就是节点名改一下
cluster.name: elasticsearch-cluster node.name: es-node2 network.bind_host: 0.0.0.0 network.publish_host: 192.168.117.231 #修改为docker的宿主机ip http.port: 9200 #这个是容器内部的,所以不用改 transport.tcp.port: 9300 #这个是容器内部的,所以不用改 http.cors.enabled: true http.cors.allow-origin: "*" node.master: true node.data: true discovery.seed_hosts: ["192.168.117.231:9300","192.168.117.231:9301","192.168.117.232:9302"] discovery.zen.minimum_master_nodes: 2 indices.query.bool.max_clause_count: 10240 #使用一台主机的不同端口搭建,在启动docker容器的时候通过-p指定映射端口与es-node1不同的地方就是节点名改一下
cluster.name: elasticsearch-cluster node.name: es-node3 network.bind_host: 0.0.0.0 network.publish_host: 192.168.117.231 #修改为docker的宿主机ip http.port: 9200 #这个是容器内部的,所以不用改 transport.tcp.port: 9300 #这个是容器内部的,所以不用改 http.cors.enabled: true http.cors.allow-origin: "*" node.master: true node.data: true discovery.seed_hosts: ["192.168.117.231:9300","192.168.117.231:9301","192.168.117.232:9302"] discovery.zen.minimum_master_nodes: 2 indices.query.bool.max_clause_count: 10240 #使用一台主机的不同端口搭建,在启动docker容器的时候通过-p指定映射端口使用的网络是上面创建的网卡es_net,指定节点的内部网络ip 172.18.0.100需要和es_net同网段
docker run -it -d --restart always -p 9200:9200 -p 9300:9300 \ --name es01 --network=es_net --ip=172.18.0.100 \ -v es_data01:/usr/share/elasticsearch/data \ -v /var/lib/docker/volumes/es_conf/_data/es01.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ -v es_plugins:/usr/share/elasticsearch/plugins \ -e ES_JAVA_OPTS="-Xms64m -Xmx128m" elasticsearch:7.8.0ES_JAVA_OPTS指定使用的es的内存,防止启动es占用太多内存空间 --restart always开机自启 数据卷映射略,唯一需要注意的是配置文件es01.yml映射到了内部的es启动时,引使用的配置文件。