Docker部署Elasticsearch集群

一、拉取镜像

有网的直接docker pull elasticsearch:7.7.1
离线部署的找到一台联网的机器,把对应版本的镜像拉取下来docker pull elasticsearch:7.7.1
将镜像保存为文件docker save elasticsearch:7.7.1 -o elasticsearch.tar
将tar镜像文件上传到要部署的机器上,安装镜像docker load -i elasticsearch.tar

二、配置yml配置文件

3个节点都需要添加

cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: vkgcom01.sdc01.scf
http.port: 9200
transport.tcp.port: 9300
# 加入跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
# 是不是有资格竞选主节点
node.master: true 
# 是否存储数据
node.data: true  
# es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点,也就是说把所有的节点都写上
discovery.seed_hosts: ["vkgcom01.sdc01.scf:9300", "vkgcom02.sdc01.scf:9300", "vkgcom03.sdc01.scf:9300"]
# 如果你不设置呢,elasticsearch就会自己选举,这里我们把三个节点都写上
cluster.initial_master_nodes: ["es-node1""es-node2""es-node3"]
三、调整配置

编辑sysctl.conf vim /etc/sysctl.conf
增加如下内容 vm.max_map_count = 262144
启用配置 sysctl -p

四、开启端口

firewall-cmd --add-port=9300/tcp
firewall-cmd --add-port=9300/tcp
firewall-cmd --add-port=9300/tcp

五、启动ES
docker run --restart=always -e ES_JAVA_OPTS="-Xms256m -Xmx2048m" -d -p 9200:9200 -p 9300:9300 -e discovery.type="zen" -v /opt/scf/es/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /opt/scf/es/data:/usr/share/elasticsearch/data --name es01 elasticsearch:7.7.1
docker run --restart=always -e ES_JAVA_OPTS="-Xms256m -Xmx2048m" -d -p 9200:9200 -p 9300:9300 -e discovery.type="zen" -v /opt/scf/es/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /opt/scf/es/data:/usr/share/elasticsearch/data --name es02 elasticsearch:7.7.1
docker run --restart=always -e ES_JAVA_OPTS="-Xms256m -Xmx2048m" -d -p 9200:9200 -p 9300:9300 -e discovery.type="zen" -v /opt/scf/es/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /opt/scf/es/data:/usr/share/elasticsearch/data --name es03 elasticsearch:7.7.1

–network host跳过防火墙

六、验证
curl  -XGET '127.0.0.1:9200/_cluster/health?pretty'
curl http://127.0.0.1:9200
curl http://127.0.0.1:9200/_cat/nodes
curl -H 'Content-Type: application/json'  -XGET '127.0.0.1:9200/_analyze?pretty' -d '{"text":"PHP是世界上最好的语言"}'
遇到的问题
问题1:
docker: Error response from daemon: driver failed programming external connectivity on endpoint ComSrves01 (215c5bc80810a7132ba729447b09a7f0ddf259ca20f684737a99c546e2831771):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 9300 -j DNAT --to-destination 172.17.0.7:9300 ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1)).

解决:
重启docker服务后再启动容器

systemctl restart docker
问题2

启动报错

uncaught exception in thread [main]
ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
	at java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:389)
	at java.base/java.nio.file.Files.createDirectory(Files.java:694)
	at java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:801)
	at java.base/java.nio.file.Files.createDirectories(Files.java:787)
	at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment.java:274)
	at org.elasticsearch.env.NodeEnvironment$NodeLock.<init>(NodeEnvironment.java:211)
	at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:271)
	at org.elasticsearch.node.Node.<init>(Node.java:284)
	at org.elasticsearch.node.Node.<init>(Node.java:264)
	at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:227)
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227)
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393)
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
	at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
	at org.elasticsearch.cli.Command.main(Command.java:90)
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /usr/share/elasticsearch/logs/elasticsearch-cluster.log

解决:
在执行路径下,赋予权限,如 chmod -R 777 es

Alt

你可能感兴趣的:(中间件,docker,docker,elasticsearch,容器)