基于Docker方式实现Elasticsearch集群

文本环境:Docker + (Elasticsearch6.8.5 * 3)

1、拉取Elasticsearch

基于Elasticsearch6.8.5版本:

docker  pull  elasticsearch6.8.5

2、创建es挂载目录

创建3个文件夹用于存放es挂载地址:es01、es02、es03

[root@CentOS7 ~]# mkdir /es-cluster
[root@CentOS7 ~]# cd /es-cluster/
[root@CentOS7 es-cluster]# mkdir es01
[root@CentOS7 es-cluster]# mkdir es02
[root@CentOS7 es-cluster]# mkdir es03

3、创建配置文件及数据存放目录

我们以es01 为例,cd es01,增加es01.yml配置文件:

# es01.yml
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 10.211.55.4
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: ["10.211.55.4:9300","10.211.55.4:9301","10.211.55.4:9302"]
discovery.zen.minimum_master_nodes: 2

其他两个es配置文件类似:

# es02.yml
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 10.211.55.4
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: ["10.211.55.4:9300","10.211.55.4:9301","10.211.55.4:9302"]
discovery.zen.minimum_master_nodes: 2

# es03.yml
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 10.211.55.4
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: ["10.211.55.4:9300","10.211.55.4:9301","10.211.55.4:9302"]
discovery.zen.minimum_master_nodes: 2

由于默认es实例是1g,太吃内存了,我们修改一下jvm参数,每个es目录下放一个jvm.option文件:

-Xms128m
-Xmx128m

4、创建es容器并启动

docker create --name es01 --net host -v /es-cluster/es01/es01.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /es-cluster/es01/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /es-cluster/es01/data:/usr/share/elasticsearch/data elasticsearch:6.8.5

docker create --name es02 --net host -v /es-cluster/es02/es02.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /es-cluster/es02/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /es-cluster/es02/data:/usr/share/elasticsearch/data elasticsearch:6.8.5

docker create --name es03 --net host -v /es-cluster/es03/es03.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /es-cluster/es03/jvm.options:/usr/share/elasticsearch/config/jvm.options -v /es-cluster/es03/data:/usr/share/elasticsearch/data elasticsearch:6.8.5

创建完docker容器后,启动es容器:

docker start es01 es02 es03

通过 docker ps -a 查看启动情况如图所示,启动失败了,我们通过 docker logs es01 查看启动日志:

data目录访问权限异常,我们给一下权限:

chmod 777 es01/data/ -R 
chmod 777 es02/data/ -R
chmod 777 es03/data/ -R

重新启动一下:

docker start es01 es02 es03

Elasticsearch-head中连接一下集群:

5、Elasticsearch-head安装

elasticsearch-head 是用于监控 Elasticsearch 状态的客户端插件,包括数据可视化、执行增删改查操作等。elasticsearch-head 插件的安装在 LinuxWindows 没什么区别,安装之前确保当前系统已经安装 nodejs 即可。

github地址https://github.com/mobz/elasticsearch-head

如下是安装命令:

# git 克隆
git clone git://github.com/mobz/elasticsearch-head.git
# 进入下载目录
cd elasticsearch-head
# 安装依赖「需要node缓解」
npm install
# 运行
npm run start

浏览器访问http://127.0.0.1:9100

6、最后补充

至此,基于DockerElasticsearch简单集群就搭建完了,下一篇我们将通过创建索引实例来介绍分片和副本,以及集群的 故障转移 等知识点。

推荐阅读:

重温Elasticsearch

elasticsearch集群搭建-windows

了解一下Elasticsearch的基本概念

用Elasticsearch代替数据库存储日志方式

posted @ 2019-08-12 21:56  niceyoo  阅读(4830)  评论(0编辑  收藏  举报