要在 Docker 中运行 Elasticsearch 集群,您需要使用 Docker Compose。以下是步骤:
首先,创建一个新的目录,例如 “elasticsearch”,并在该目录中创建一个新文件 docker-compose.yml。
在 docker-compose.yml 文件中添加以下内容:
version: '3'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
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
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- datavolume:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- esnet
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
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
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- datavolume:/usr/share/elasticsearch/data
networks:
- esnet
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
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
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- datavolume:/usr/share/elasticsearch/data
networks:
- esnet
volumes:
datavolume:
networks:
esnet:
- 运行以下命令启动 Elasticsearch 集群:
docker-compose up
这将启动三个 Elasticsearch 节点并创建一个名为 “es-docker-cluster” 的集群。
- 现在,您可以使用 curl 或其他工具测试 Elasticsearch 是否正在运行,并连接到 http://localhost:9200。