【英文】通过Docker部署Elasticsearch集群

Preface

Deploying an Elasticsearch cluster using Docker

Create a network

es-network: Define a network name

1
docker network create es-network

Pulling the Image

  • This example is based on version 7.12.1
1
docker pull elasticsearch:7.12.1

Create a docker-compose File

image: Specify the image to use
container_name: Specify the name of the image
environment: Specify the environment configuration

cluster.name=: Specify the cluster name. When the cluster name is the same, it will form a cluster
discovery.seed_hosts=: IP addresses or domain names of other nodes in the cluster
cluster.initial_master_nodes=: Specify the master nodes in the cluster. If multiple nodes are specified, the master node will be automatically elected.
ES_JAVA_OPTS=-Xms1g -Xmx1g: Configure heap memory size. The default is 1G. It is recommended to set the minimum heap memory and maximum heap memory to the same size.

volumes: Specify the data volume mapping
ports: Specify the port mapping
networks: Specify the network configuration

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
version: "2.2"
services:
es01:
image: elasticsearch:7.12.1
container_name: es01
environment:
- node.name=es01
- cluster.name=docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- ES_JAVA_OPTS=-Xms512m -Xmx512m
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9201:9200
networks:
- es-network
es02:
image: elasticsearch:7.12.1
container_name: es02
environment:
- node.name=es02
- cluster.name=docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- ES_JAVA_OPTS=-Xms512m -Xmx512m
volumes:
- data02:/usr/share/elasticsearch/data
ports:
- 9202:9200
networks:
- es-network
es03:
image: elasticsearch:7.12.1
container_name: es03
environment:
- node.name=es03
- cluster.name=docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- ES_JAVA_OPTS=-Xms512m -Xmx512m
volumes:
- data03:/usr/share/elasticsearch/data
ports:
- 9203:9200
networks:
- es-network
volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local
networks:
es-network:
driver: bridge

Start the Containers

1
docker-compose up -d

Error

  • Failed to start with the error: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

Cause

  • The Elasticsearch user in the operating system has insufficient memory permissions, requiring at least 262144.

Resolve the Issue (Linux)

1
2
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p

Resolve the Issue (MacOS)

Using DockerDesktop as the Docker runtime

1
2
screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
sysctl -w vm.max_map_count=262144

Using Lima as the Docker runtime

1
2
3
limactl shell docker
sudo sysctl -w vm.max_map_count=262144
exit

Done

References

Bilibili-Heima Programmer
Blog-Cnblogs
CSDN-whatday
Elasticsearch Documentation
CSDN-jingyu_z