本文主要介绍如何使用docker、docker-compose安装和运行一个三节点elasticsearch集群。
一、安装docker
docker安装和运行请参考我之前写的一篇博客:
【DockerCE】Docker-CE 20.10.12版本发布
二、下载elasticsearch镜像
# docker pull docker.elastic.co/elasticsearch/elasticsearch:7.16.2
7.16.2: Pulling from elasticsearch/elasticsearch
da847062c6f6: Pull complete
f9947111a3a4: Pull complete
5f47506629dc: Pull complete
6728f6016cfb: Pull complete
3ee4bcac6dc4: Pull complete
cbb4caf74f49: Pull complete
60e3e554a3bf: Pull complete
64906e427669: Pull complete
96b7ea4c4a98: Pull complete
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.elastic.co/elasticsearch/elasticsearch 7.16.2 66c29cde15ce 3 weeks ago 646MB
三、创建数据持久化目录
# useradd -d /data -g root elasticsearch
# mkdir -p /data/elasticsearch/config/{es01,es02,es03}
# mkdir -p /data/elasticsearch/data/{es01,es02,es03}
# mkdir -p /data/elasticsearch/logs/{es01,es02,es03}
# chown -R elasticsearch:root /data
# vi /data/elasticsearch/data/es01/jvm.options
-Xms512m
-Xmx512m
# vi /data/elasticsearch/data/es02/jvm.options
-Xms512m
-Xmx512m
# vi /data/elasticsearch/data/es03/jvm.options
-Xms512m
-Xmx512m
四、编写剧本
version: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65535
hard: 65535
volumes:
- /data/elasticsearch/data/es01:/usr/share/elasticsearch/data
- /data/elasticsearch/config/es01/jvm.options:/usr/share/elasticsearch/config/jvm.options
- /data/elasticsearch/logs/es01:/usr/share/elasticsearch/logs
ports:
- 9200:9200
- 9300:9300
networks:
- elastic
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65535
hard: 65535
volumes:
- /data/elasticsearch/data/es02:/usr/share/elasticsearch/data
- /data/elasticsearch/config/es02/jvm.options:/usr/share/elasticsearch/config/jvm.options
- /data/elasticsearch/logs/es02:/usr/share/elasticsearch/logs
networks:
- elastic
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
ulimits:
memlock:
soft: -1
hard: -1
nofile:
soft: 65535
hard: 65535
volumes:
- /data/elasticsearch/data/es03:/usr/share/elasticsearch/data
- /data/elasticsearch/config/es03/jvm.options:/usr/share/elasticsearch/config/jvm.options
- /data/elasticsearch/logs/es03:/usr/share/elasticsearch/logs
networks:
- elastic
networks:
elastic:
driver: bridge
五、运行
# mv docker-compose /usr/local/bin/
# chmod u+x /usr/local/bin/docker-compose
---在.bash_profile中的path字段中增加/usr/local/bin/:配置
# source .bash_profile
# docker-compose up -d
[+] Running 4/4
⠿ Network elasticsearch_elastic Created 0.0s
⠿ Container es03 Started 1.4s
⠿ Container es01 Started 1.4s
⠿ Container es02 Started
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
867c9f1580b6 docker.elastic.co/elasticsearch/elasticsearch:7.16.2 "/bin/tini -- /usr/l…" 15 seconds ago Up 13 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp es01
24a62972a736 docker.elastic.co/elasticsearch/elasticsearch:7.16.2 "/bin/tini -- /usr/l…" 15 seconds ago Up 13 seconds 9200/tcp, 9300/tcp es02
34eef8ccd068 docker.elastic.co/elasticsearch/elasticsearch:7.16.2 "/bin/tini -- /usr/l…" 15 seconds ago Up 13 seconds 9200/tcp, 9300/tcp es03
---查看集群的节点信息
# curl -X GET "localhost:9200/_cat/nodes?v=true&pretty"
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.0.2 21 95 99 4.02 1.56 0.73 cdfhilmrstw - es02
192.168.0.4 25 95 99 4.02 1.56 0.73 cdfhilmrstw * es03
192.168.0.3 55 95 99 4.02 1.56 0.73 cdfhilmrstw - es01
六、停集群容器
# docker-compose down
[+] Running 4/3
⠿ Container es01 Removed 0.8s
⠿ Container es03 Removed 0.8s
⠿ Container es02 Removed 0.8s
⠿ Network elasticsearch_elastic Removed
参考:
Install Elasticsearch with Docker | Elasticsearch Guide [7.16] | Elastic