ElasticSearch学习日志(二):构建基于Docker的ElasticSearch服务集群

在上篇文章《ElasticSearch学习日志(一):基于Dockerfile构建Docker镜像以及安装分词器插件(ik/pinyin)》中,我已经做好了包含ikpinyin的分词插件镜像my-es-image:0.0.1,本篇文章将记录基于此镜像如何搭建ElasticSearch服务集群。

由于我没有那么多的服务器,因此使用docker容器来模拟。

首先,创建文件夹cluster,里面再创建三个文件夹,分别为node1node2node3,模拟三台服务器。

然后分别在node1node2node3中创建configdata/nodeslogs文件夹。其中config文件夹中存放ElasticSearch配置文件elasticsearch.ymldata/nodes文件夹是用来挂载数据,logs文件夹用来挂载日志。

最终的文件夹结构如下图所示:

下面是elasticsearch.yml配置文件内容,不同的节点只需要修改节点名称即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 设置开启跨域 可以让es-head访问
http.cors.enabled: true
http.cors.allow-origin: "*"

# 集群名字
cluster.name: "docker-cluster"
# 节点名字 我这里就是es-node-1、2、3
node.name: es-node-1
# 指定该节点是否有资格被选举为master,默认是true
node.master: true
# 允许该节点存储数据 默认true
node.data: true
# 允许任何ip访问 生产环境需要不建议这样做
network.host: 0.0.0.0
# 通过ip列表进行节点发现。这里我填写的是docker容器的地址,生产环境应修改为实际ip地址
discovery.zen.ping.unicast.hosts: ["127.0.0.1","172.17.0.2","172.17.0.3","172.17.0.4"]
# 【分裂大脑】 一个节点需要看到的具有master节点资格的最小数量(默认是1)。官方的推荐值是(N/2)+1,其中N是具有master资格的节点的数量(我的情况是3,因此这个参数设置为2)
discovery.zen.minimum_master_nodes: 2

然后在cluster文件夹下创建docker-compose.yml文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
version: '2'
services:
es-1:
image: my-es-image:0.0.1
network_mode: "bridge"
ports:
- "9201:9200"
- "9301:9300"
volumes:
- "./node1/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml"
- "./node1/data/nodes:/usr/share/elasticsearch/data/nodes"
- "./node1/logs:/usr/share/elasticsearch/logs"
es-2:
image: my-es-image:0.0.1
network_mode: "bridge"
ports:
- "9202:9200"
- "9302:9300"
volumes:
- "./node2/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml"
- "./node2/data/nodes:/usr/share/elasticsearch/data/nodes"
- "./node2/logs:/usr/share/elasticsearch/logs"
es-3:
image: my-es-image:0.0.1
network_mode: "bridge"
ports:
- "9203:9200"
- "9303:9300"
volumes:
- "./node3/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml"
- "./node3/data/nodes:/usr/share/elasticsearch/data/nodes"
- "./node3/logs:/usr/share/elasticsearch/logs"

启动前注意项

  • 确保执行过sysctl -w vm.max_map_count=262144,否则服务会挂掉。
  • ElasticSearch Docker挂载要求相关目录及文件必须属于user(elasticsearch:1000)、group(elasticsearch:1000),如果是挂载在home文件夹下这个问题是没有的。

命令参考:

1
2
3
4
mkdir esdatadir
chmod g+rwx esdatadir
chgrp -R 1000 esdatadir
chown -R 1000 esdatadir

启动

docker-compose up -d

启动后,可以访问url检测各个节点的状态

curl localhost:9201

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name" : "es-node-1",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "HGcowtf0SbGClkOXAne-Sw",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

curl localhost:9202

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name" : "es-node-2",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "HGcowtf0SbGClkOXAne-Sw",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

curl localhost:9203

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name" : "es-node-3",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "HGcowtf0SbGClkOXAne-Sw",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

停止

docker-compose down