(一)Docker 单机集群部署
拉取镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.4.2
搭建集群
1. docker-compose 搭建elasticsearch 集群
-
(1). 首先安装docker-compose docker-compose
-
(2). 建立
docker-compose.yml
内容如下:
version: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data02:/usr/share/elasticsearch/data
networks:
- elastic
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data03:/usr/share/elasticsearch/data
networks:
- elastic
volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local
networks:
elastic:
driver: bridge
-
(3). 启动集群
- 启动命令:
docker-compose up
- 查看节点状态:
curl -X GET "localhost:9200/_cat/nodes?v&pretty"
- 启动命令:
-
(4). 删除集群
- 删除集群命令:
docker-compose down
删除构建的容器,但是并不会清除挂载的数据卷以及数据。docker-compose down -v
则会清除挂载的数据卷以及数据
- 删除集群命令:
2. 非docker-compose 搭建集群
现在也来建三个节点分别是: es01,es02,es03;端口分别是: 9300,9301,9302
- (1). 创建一个
elasticsearch.yml
内容如下:
#es01
cluster.name: "es-docker-cluster"
network.host: 0.0.0.0
network.publish_host: 172.16.5.1
discovery.seed_hosts: ["172.16.5.1:9301","172.16.5.1:9302"] #除本机以外的node ip:93端口( transport.tcp.port)
transport.tcp.port: 9300 #es02 9301 es03 9302
cluster.initial_master_nodes: ["172.16.5.1:9301","172.16.5.1:9302","172.16.5.1:9300"]
- (2). 启动命令
#node1
docker run -p 9200:9200 -p 9300:9300 --name="es01" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e node.name="es01" -v path/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml docker.elastic.co/elasticsearch/elasticsearch:7.4.2
#node2
docker run -p 9201:9200 -p 9301:9301 --name="es02" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e node.name="es02" -v path/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml docker.elastic.co/elasticsearch/elasticsearch:7.4.2
#node3
docker run -p 9202:9200 -p 9302:9302 --name="es03" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e node.name="es03" -v path/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml docker.elastic.co/elasticsearch/elasticsearch:7.4.2
(二)Docker 多机集群部署
镜像获取同单机部署一样,须在每一个节点都拉取docker镜像.
- 假设现有三台不同的主机 ip 分别为172.16.5.1, 172.16.5.2, 172.16.5.3。
node | ip |
---|---|
es01 | 172.16.5.1 |
es02 | 172.16.5.2 |
es03 | 172.16.5.3 |
搭建多机集群
这里就直接用 docker-compose 来搭建elasticsearch 多机集群
- 创建
elasticsearch.yml
#es01
cluster.name: "docker-cluster"
network.host: 0.0.0.0
network.publish_host: 172.16.5.1 ## 改成相应的 node ip
- 创建
docker-compose.yml
version: '2.2'
services:
es01: #相应node的服务名
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
container_name: es01
environment:
- node.name=es01 #相应的node名
- cluster.name=es-docker-cluster
- bootstrap.memory_lock=true
- "discovery.seed_hosts=172.16.5.2, 172.16.5.3" #这里配置除本机以外的node ip
- "cluster.initial_master_nodes=172.16.5.1,172.16.5.2,172.16.5.3" #所有节点ip
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/elasticsearch/data:/usr/share/elasticsearch/data:rw #把数据挂载在外面
- /config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml #挂载elasticsearch.yml
ports:
- 9200:9200
- 9300:9300
networks:
- elastic
networks:
elastic:
driver: bridge
注意问题
- 挂载数据的时候出现访问受限以至于无法启动时,可将挂载数据的目录所有者和用户组改为 elastic 前提条件得先创建用户和用户组,省事一点就直接
chown -R 1000:1000 /home/elasticsearch/data
- 在运行elasticsearch时出现
max virtual memory areas vm.max_map_count [65530]
, 请在宿主机中执行:sudo sysctl -w vm.max_map_count=262144
或者运行sudo vi /etc/sysctl.conf
在文件中追加vm.max_map_count=262144
保存后执行sudo sysctl -p
cluster.initial_master_nodes
为集群创建初期具有master选举权的节点。discovery.seed_hosts
发送信息给具有master选举权的节点,如果当前节点具有master节点的选举权,则需要向其他同样具有master节点选举权的发送消息。如果无master 节点选举权,则须把所有具有master选举权的节点host 加入。- 集群master节点须大于等于3个,因为如果当前主节点挂了之后es须从具有master 选举权的节点中重新选举出新的主节点,如果具有master 选举权的节点存活少于一半时,节点是无法完成选举的,所以必须有大于一半以上且具有master选举权的节点存在时es才能重新选举出新的主节点。不然节点无法工作。