步骤:
1.两台以上主机,可以虚拟机等等,本文以两个节点为例
2.主机配置:vi /etc/sysctl.conf 在最后一行添加 vm.max_map_count = 262144
qw!保存退出
执行systemctl -p检查
2.两台主机分别安装docker,jdk
3.在两台主机上用docker拉取ES镜像
4.创建配置文件elasticsearch.yml
比如在本地/home/ES目录下创建elasticsearch.yml,挂载到容器默认目录/usr/share/elasticsearch/config/。在docker-compose.yml挂载就可以。主机1的elasticsearch.yml:
cluster.name: "elasticsearch-cluter"
node.name: "es-node1"
node.master: true //master节点设为true,其他为false
node.data: true
network.host: 0.0.0.0 //默认随机分配一个本机的ip给容器,并用127.0.0.1或者0.0.0.0可以访问该容器
network.publish_host: 192.168.0.1 //绑定本机的ip给容器,以此为例
transport.tcp.port: 9300
discovery.seed_hosts: ["192.168.0.1:9300","192.168.0.2:9300"] //注意如果不使用network.publish_host绑定此ip,那么跨主机的容器之间相互发现不了,因为network.host已经随机分配了一个,但和这里的不一致
cluster.initial_master_nodes: "es-node1" //需与hostname保持一致
http.cors.enabled: true
http.cors.allow-origin: "*"
主机2的elasticsearch.yml:
cluster.name: "elasticsearch-cluter"
node.name: "es-node2"
node.master: false
node.data: true
network.host: 0.0.0.0
network.publish_host: 192.168.0.2
transport.tcp.port: 9300
discovery.seed_hosts: ["192.168.0.1:9300","192.168.0.2:9300"]
cluster.initial_master_nodes: "es-node1"
http.cors.enabled: true
http.cors.allow-origin: "*"
5.主机1的docker-compose.yml:
version: '2.2'
services:
es01: //服务名
image: elasticsearch:7.4.0 //镜像名
container_name: es01 //容器名
environment:
bootstrap.memory_lock: "true" //注意true要加引号
ES_JAVA_OPTS: -Xms256m -Xmx256m
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/ES/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml //:前的目录与上文一致
ports:
- 9200:9200
- 9300:9300
hostname: es-node1
主机2的docke-compose.yml:
version: '2.2'
services:
es02: //服务名
image: elasticsearch:7.4.0 //镜像名
container_name: es02 //容器名
environment:
bootstrap.memory_lock: "true" //注意true要加引号
ES_JAVA_OPTS: -Xms256m -Xmx256m
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/ES/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml //:前的目录与上文一致
ports:
- 9200:9200
- 9300:9300
hostname: es-node2
6.分别在两主机的docker-compose.yml所在目录执行:docker-compose.yml即可
注意点:
两主机的配置除了名称以外尽量一致
注意在两主机里的容器相互访问与一主机里的容器相互访问不同,更不能用一个docker-compose.yml启动不同主机的容器