在使用 Docker Compose 启动 Elasticsearch 时,您可以通过配置 docker-compose.yml 文件来设置 Elasticsearch 集群。
以下是一个示例的 docker-compose.yml 配置文件,用于启动一个 Elasticsearch 集群:
version: '3'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:${ELK_VERSION}
container_name: elasticsearch
environment:
- node.name=node1
- cluster.name=mycluster
- discovery.seed_hosts=elasticsearch2,elasticsearch3
- cluster.initial_master_nodes=elasticsearch
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata1:/usr/share/elasticsearch/data
ports:
- 9200:9200
- 9300:9300
networks:
- esnet
elasticsearch2:
image: docker.elastic.co/elasticsearch/elasticsearch:${ELK_VERSION}
container_name: elasticsearch2
environment:
- node.name=node2
- cluster.name=mycluster
- discovery.seed_hosts=elasticsearch,elasticsearch3
- cluster.initial_master_nodes=elasticsearch
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata2:/usr/share/elasticsearch/data
networks:
- esnet
elasticsearch3:
image: docker.elastic.co/elasticsearch/elasticsearch:${ELK_VERSION}
container_name: elasticsearch3
environment:
- node.name=node3
- cluster.name=mycluster
- discovery.seed_hosts=elasticsearch,elasticsearch2
- cluster.initial_master_nodes=elasticsearch
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata3:/usr/share/elasticsearch/data
networks:
- esnet
volumes:
esdata1:
esdata2:
esdata3:
networks:
esnet:
在该示例中,我们使用了 3 个 Elasticsearch 节点,每个节点都配置了以下环境变量:
node.name
:指定节点的名称。cluster.name
:指定集群的名称。discovery.seed_hosts
:指定节点的发现机制所使用的主机列表。cluster.initial_master_nodes
:指定初始的主节点列表。
这些配置可根据您的实际需求进行调整和扩展。
同时,每个节点的数据都通过 Docker 卷(volumes)进行持久化,并通过定义的网络(networks)进行连接。
请注意,${ELK_VERSION}
是一个变量,您需要根据您使用的 Elasticsearch 版本来进行替换。
启动集群只需在包含 docker-compose.yml 的目录中运行以下命令:
docker-compose up -d
这将启动 Elasticsearch 集群。