搭建es集群,我使用的是 elasticsearch-6.7.2版本。在搭建的过程中遇到了N多坑,在参考别人的经验后,终于搞定了。
搭建docker和docker-compose环境
可以上菜鸟教程,里面有详细的步骤:
我使用的CentOS7
安装docker后,需要修改一下镜像加速器地址,使用的阿里云镜像加速器,可以到阿里云官网那里申请
搭建好环境之后,就可以开始集群搭建了。
es集群环境准备
创建es挂载目录
mkdir es
cd es
mkdir config
mkdir data1
mkdir data2
mkdir data3
将data1 data2 data3 开启777权限:
chmod 777 data1 data2 data3
开放端口
firewall-cmd --add-port=9300/tcp
firewall-cmd --add-port=9301/tcp
firewall-cmd --add-port=9302/tcp
开放端口后,还是害怕防火墙会搞怪,所以又把防火墙永久关闭了
防火墙关闭命令:systemctl stop firewalld.service
查看防火墙命令:systemctl status firewalld.service
开机禁用防火墙自启命令:systemctl disable firewalld.service
遇到的坑及解决
提前修改好,就不用担心运行compose文件会出错了~~~
jvm内存不够:
需要在运行镜像的时候使用一下命令
-e ES_JAVA_OPTS="-Xms512m -Xmx512m"
设置-e ES_JAVA_OPTS="-Xms256m -Xmx256m" 是因为/etc/elasticsearch/jvm.options 默认jvm最大最小内存是2G
JVM线程数限制数量:
vim /etc/sysctl.conf
vm.max_map_count=262144
sysctl -p
编写docker-compose.yml文件和挂载文件
docker-compose.yml:
version: '2.2'
services:
es-node1:
image: elasticsearch:6.7.2
container_name: elasticsearch1
restart: always
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- ./es/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es/data1:/usr/share/elasticsearch/data
ports:
- 9200:9200
- 9300:9300
ulimits:
memlock:
soft: -1
hard: -1
networks:
- es_network
es-node2:
image: elasticsearch:6.7.2
container_name: elasticsearch2
restart: always
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- ./es/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es/data2:/usr/share/elasticsearch/data
ports:
- 9201:9201
- 9301:9301
ulimits:
memlock:
soft: -1
hard: -1
networks:
- es_network
es-node3:
image: elasticsearch:6.7.2
container_name: elasticsearch3
restart: always
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es/data3:/usr/share/elasticsearch/data
ports:
- 9202:9202
- 9302:9302
networks:
- es_network
es-head:
image: mobz/elasticsearch-head:5
container_name: elasticsearch-head
restart: always
ports:
- 9100:9100
cerebro:
image: lmenezes/cerebro:0.8.3
container_name: cerebro
ports:
- 9000:9000
command:
- -Dhosts.0.host=http://es-node1:9200
networks:
- es_network
kibana:
image: docker.elastic.co/kibana/kibana:6.7.2
container_name: kibana
environment:
- I18N_LOCALE=zh-CN
- XPACK_GRAPH_ENABLED=true
- TIMELION_ENABLED=true
- XPACK_MONITORING_COLLECTION_ENABLED="true"
ports:
- "5601:5601"
links:
- es-node1:elasticsearch
depends_on:
- es-node1
networks:
- es_network
networks:
es_network:
driver: bridge
进到刚才创建挂载目录es下的config目录:
vi es1.yml:
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 自己虚拟机IP
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: ["es-node1:9300","es-node2:9301","es-node3:9302"]
discovery.zen.minimum_master_nodes: 1
vi es2.yml:
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 自己虚拟机IP
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: ["es-node1:9300","es-node2:9301","es-node3:9302"]
discovery.zen.minimum_master_nodes: 1
vi es3.yml:
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 自己虚拟机IP
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: ["es-node1:9300","es-node2:9301","es-node3:9302"]
discovery.zen.minimum_master_nodes: 1
我是在一台虚拟机上搭建3个节点的,所以其他环境需要视情况修改ip端口
运行docker-compose.yml文件
运行的使用要进入docker-compose.yml文件所在目录的命令行
执行compose文件
docker-compose up -d
查看运行的容器:
docker-compose ps
如果第一次在网页访问不了,可以停止容器服务后,重启虚拟机,在开启服务。
说一下使用的虚拟机网卡
使用了两个网卡,仅主机的ip地址用于节点之间的连接,桥接网卡用于连接外网