使用docker-compose构建elasticsearch集群

环境说明

  • linux:centos7
  • elasticsearch:5.6.4
  • elasticsearch-head:5

目录结构

项目源码下载

1
2
3
4
5
6
7
8
9
10
elasticsearch/
├── docker-compose.yml
├── head
└── node
├── es1
│   ├── data
│   └── elasticsearch.yml
└── es2
├── data
└── elasticsearch.yml

主节点elasticsearch.yml配置文件

elasticsearch/node/es1/elasticsearch.yml

1
2
3
4
5
6
7
8
9
10
network.bind_host: 0.0.0.0
cluster.name: elasticsearch_cluster
cluster.routing.allocation.disk.threshold_enabled: false
node.name: master
node.master: true
node.data: true
http.cors.enabled: true
http.cors.allow-origin: "*"
network.host: 0.0.0.0
discovery.zen.minimum_master_nodes: 1

从节点elasticsearch.yml配置文件

elasticsearch/node/es2/elasticsearch.yml

1
2
3
4
5
6
7
8
9
10
11
network.bind_host: 0.0.0.0
cluster.name: elasticsearch_cluster
cluster.routing.allocation.disk.threshold_enabled: false
node.name: node2
node.master: false
node.data: true
http.cors.enabled: true
http.cors.allow-origin: "*"
network.host: 0.0.0.0
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: es1

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
33
34
35
36
37
38
39
40
41
version: '2.0'
services:
elasticsearch-central:
image: elasticsearch:5.6.4
container_name: es1
volumes:
- /opt/modules/elasticsearch/node/es1/data:/usr/share/elasticsearch/data
- /opt/modules/elasticsearch/node/es1/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- ES_CLUSTERNAME=elasticsearch
command: elasticsearch
ports:
- "9200:9200"
- "9300:9300"
elasticsearch-data:
image: elasticsearch:5.6.4
container_name: es2
volumes:
- /opt/modules/elasticsearch/node/es2/data:/usr/share/elasticsearch/data
- /opt/modules/elasticsearch/node/es2/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
environment:
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- ES_CLUSTERNAME=elasticsearch
command: elasticsearch
ports:
- "9201:9200"
- "9301:9300"
links:
- elasticsearch-central:elasticsearch
elasticsearch-head:
image: mobz/elasticsearch-head:5
container_name: head
volumes:
- /opt/modules/elasticsearch/head/Gruntfile.js:/usr/src/app/Gruntfile.js
- /opt/modules/elasticsearch/head/_site/app.js:/usr/src/app/_site/app.js
ports:
- "9100:9100"
links:
- elasticsearch-central:elasticsearch

配置head

  • 下载elasticsearch-head
    1
    2
    3
    cd elasticsearch
    git clone git://github.com/mobz/elasticsearch-head.git
    mv elasticsearch-head head

下载下来的代码结构如下:
elasticsearch-head目录

  • elasticsearch\head\Gruntfile.js修改以下片段

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    connect: {
    server: {
    options: {
    /* 默认监控:127.0.0.1,修改为:0.0.0.0 */
    hostname: '0.0.0.0',
    port: 9100,
    base: '.',
    keepalive: true
    }
    }
    }
  • elasticsearch\head\_site\app.js修改以下代码片段

1
2
* 修改localhost为elasticsearch集群地址,Docker部署中,一般是elasticsearch宿主机地址 */
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";

启动

  • 运行elasticsearch需要vm.max_map_count至少需要262144内存
1
2
3
4
5
6
切换到root用户修改配置sysctl.conf
vi /etc/sysctl.conf
在尾行添加以下内容
vm.max_map_count=262144
并执行命令
sysctl -p

elk启动的时候可能会提示如下错误:
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

  • 启动es
    1
    2
    docker-compose up 启动
    docker-compose down 关闭

测试

elasticsearch-head可视化页面:http://es所在机器IP:9100
elasticsearch-head可视化页面

参考链接:
https://www.jianshu.com/p/a26c8c7226d7
https://blog.csdn.net/sinat_31908303/article/details/80496349
https://blog.csdn.net/ggwxk1990/article/details/78698648