mkdir -p /usr/local/microservices/elasticSearch/{es_data01,es_data02,es_data03,es_conf/_data,es_plugins}
/usr/local/microservices/elasticSearch/es_conf/_data
目录下编写ES的yml文件vim es01.yml
文件内容:
注意:需要修改文件中 network.publish_host,discovery.zen.ping.unicast.hosts 两个配置项的IP
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 192.168.31.70 #替换为docker宿主机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: ["192.168.31.70:9300","192.168.31.70:9301","192.168.31.70:9302"] #替换为docker宿主机IP
discovery.zen.minimum_master_nodes: 2
indices.query.bool.max_clause_count: 10240
vim es02.yml
文件内容:
注意:需要修改文件中 network.publish_host,discovery.zen.ping.unicast.hosts 两个配置项的IP
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 192.168.31.70 #替换为docker宿主机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: ["192.168.31.70:9300","192.168.31.70:9301","192.168.31.70:9302"] #替换为docker宿主机IP
discovery.zen.minimum_master_nodes: 2
indices.query.bool.max_clause_count: 10240
vim es03.yml
文件内容:
注意:需要修改文件中 network.publish_host,discovery.zen.ping.unicast.hosts 两个配置项的IP
cluster.name: elasticsearch-cluster
node.name: es-node3
network.bind_host: 0.0.0.0
network.publish_host: 192.168.31.70 #替换为docker宿主机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: ["192.168.31.70:9300","192.168.31.70:9301","192.168.31.70:9302"] #替换为docker宿主机IP
discovery.zen.minimum_master_nodes: 2
indices.query.bool.max_clause_count: 10240
/usr/local/microservices/elasticSearch
目录下编写docker-compose.yml文件vim docker-compose.yml
文件内容:
version: '2.2'
services:
es01:
image: elasticsearch:7.8.0
container_name: es01
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- TAKE_FILE_OWNERSHIP=true
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es_data01:/usr/share/elasticsearch/data
- ./es_conf/_data/es01.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es_plugins:/usr/share/elasticsearch/plugins
ports:
- 9200:9200
- 9300:9300
networks:
- es-net
restart: always
es02:
image: elasticsearch:7.8.0
container_name: es02
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- TAKE_FILE_OWNERSHIP=true
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es_data02:/usr/share/elasticsearch/data
- ./es_conf/_data/es02.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es_plugins:/usr/share/elasticsearch/plugins
ports:
- 9201:9201
- 9301:9301
networks:
- es-net
restart: always
es03:
image: elasticsearch:7.8.0
container_name: es03
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- TAKE_FILE_OWNERSHIP=true
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./es_data03:/usr/share/elasticsearch/data
- ./es_conf/_data/es03.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./es_plugins:/usr/share/elasticsearch/plugins
ports:
- 9202:9202
- 9302:9302
networks:
- es-net
restart: always
networks:
es-net:
注意:此处一定要开放ES集群的内部通信端口,否则,ES集群无法互联
vi /etc/sysctl.conf
在文件末尾新增如下内容:
vm.max_map_count = 655360
刷新系统配置
sysctl -p
docker-compose up -d
docker ps
注意,此处启动后需要大概等1分钟的样子才能执行测试语句,不然会出现如下错误,具体原因本人还不清楚,知道的小伙伴,可以下方评论告诉我,谢谢
错误如下:
curl: (56) Recv failure: Connection reset by peer
curl localhost:9200
## 浏览器输入
http://docker宿主机IP:9200/_cat/nodes?pretty
1.执行curl localhost:9200
后不能出现ES信息,使用docker logs 容器ID
命令查看日志,出现如下错误:
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase
解决方案:
vi /etc/sysctl.conf
在文件末尾新增如下内容:
vm.max_map_count = 655360
刷新系统配置
sysctl -p
2.在docker宿主机上执行curl localhost:9200
后可以出现ES信息,但是外网不能连接
在浏览器通过外网进行访问
http://docker宿主机IP:9200/
http://docker宿主机IP:9201/
http://docker宿主机IP:9202/
若访问不到,可尝试如下解决办法:
2.1 检查防火墙,9200
,9201
,9202
,这三个端口是否开放
2.2 重启docker,重启ES集群
3.出现503错误
{
"error": {
"root_cause": [
{
"type": "cluster_block_exception",
"reason": "blocked by: [SERVICE_UNAVAILABLE/1/state not recovered / initialized];"
}
],
"type": "cluster_block_exception",
"reason": "blocked by: [SERVICE_UNAVAILABLE/1/state not recovered / initialized];"
},
"status": 503
}
解决方案:
将/usr/local/microservices/elasticSearch/
下的es_data01
,es_data02
,es_data03
,文件夹中的内容删除再重启
4.ES集群无法互联,单机访问没有问题,但是集群无法互联
解决方案:
检查上文中要求开放的9301
,9302
,9303
,三个内部通信端口是否开通,开通后重启ES
安装IK分词器
1.下载文件
IK分词器7.8.0下载地址
2.将下载好的elasticsearch-analysis-ik-7.8.0.zip
文件上传至/usr/local/microservices/elasticSearch/es_plugins
目录下
3.将压缩包拷贝到容器
docker cp /usr/local/microservices/elasticSearch/es_plugins/elasticsearch-analysis-ik-7.8.0.zip es01:/usr/share/elasticsearch/plugins
4.进入es01
容器
docker exec -it es01 /bin/bash
5.创建目录
mkdir /usr/share/elasticsearch/plugins/ik
6.将文件压缩包移动到ik
目录下
mv /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik-7.8.0.zip /usr/share/elasticsearch/plugins/ik
5.进入ik
目录
cd /usr/share/elasticsearch/plugins/ik
6.解压
unzip elasticsearch-analysis-ik-7.8.0.zip
7.退出容器
exit
8.重启es集群
在/usr/local/microservices/elasticSearch/
目录下执行
#停止
docker-compose stop
#启动
docker-compose up -d
至此,搭建成功