Docker Compose下Elasticsearch与OpenSearch启动失败求助
问题分析与修复方案
核心问题
你的Elasticsearch容器启动失败,主要原因是启动命令的语法错误,导致初始化脚本无法正确执行,同时端口配置和集群参数的细节也需要调整。
具体修复步骤
1. 修正启动脚本的引号冲突
原启动命令中,curl的-d参数后引号处理错误,JSON内容被截断,引发执行失败。改用Here Document(<<EOF)传递JSON数据,彻底避免引号冲突。
2. 调整Elasticsearch副本数
单节点Elasticsearch集群中,number_of_replicas设为0(原配置为1),否则集群会因副本无法分配一直处于yellow状态,影响后续操作。
3. 打通Logstash与Elasticsearch的网络
如果后续需要用Logstash迁移数据,需将Logstash加入elastic网络,否则两个独立网络无法互通。
4. 简化冗余配置
移除不必要的expose配置(端口映射已包含暴露逻辑),同时确保transport端口与映射一致。
修正后的完整docker-compose.yml
services: elasticsearch: image: elasticsearch:7.10.1 container_name: elasticsearch environment: - discovery.type=single-node - http.host=0.0.0.0 - http.port=9201 - transport.port=9300 - ES_JAVA_OPTS=-Xms512m -Xmx512m - xpack.security.enabled=false ports: - "9201:9201" - "9300:9300" healthcheck: test: ["CMD-SHELL", "curl --silent --fail localhost:9201/_cluster/health || exit 1"] interval: 10s timeout: 10s retries: 3 networks: - elastic command: > bash -c ' until curl -sS "http://localhost:9201/_cat/health?h=status" | grep -q "green\|yellow"; do sleep 2 done curl -X PUT "http://localhost:9201/products" -H "Content-Type: application/json" -d <<EOF { "settings": { "number_of_shards": 1, "number_of_replicas": 0, "analysis": { "analyzer": "standard" } }, "mappings": { "properties": { "title": { "type": "text", "analyzer": "standard" }, "author": { "type": "text", "analyzer": "standard" }, "description": { "type": "text", "analyzer": "standard" }, "published_date": { "type": "date" }, "purchase_url": { "type": "keyword" } } } } EOF ' deploy: resources: limits: memory: 2GB opensearch-node1A: image: opensearchproject/opensearch:1.2.3 container_name: opensearch-node1A environment: - cluster.name=opensearch-cluster - node.name=opensearch-node1A - discovery.seed_hosts=opensearch-node1A,opensearch-node2A - cluster.initial_master_nodes=opensearch-node1A,opensearch-node2A - bootstrap.memory_lock=true - "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 volumes: - opensearch-data1:/usr/share/opensearch/data ports: - 9200:9200 - 9600:9600 networks: - opensearch-net opensearch-node2A: image: opensearchproject/opensearch:1.2.3 container_name: opensearch-node2A environment: - cluster.name=opensearch-cluster - node.name=opensearch-node2A - discovery.seed_hosts=opensearch-node1A,opensearch-node2A - cluster.initial_master_nodes=opensearch-node1A,opensearch-node2A - bootstrap.memory_lock=true - "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m" ulimits: memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 volumes: - opensearch-data2:/usr/share/opensearch/data networks: - opensearch-net opensearch-dashboardsA: image: opensearchproject/opensearch-dashboards:1.1.0 container_name: opensearch-dashboardsA ports: - 5601:5601 environment: OPENSEARCH_HOSTS: '["https://opensearch-node1A:9200","https://opensearch-node2A:9200"]' networks: - opensearch-net logstash-with-plugin: image: opensearchproject/logstash-oss-with-opensearch-output-plugin:7.16.2 container_name: logstash-with-plugin networks: - opensearch-net - elastic volumes: opensearch-data1: opensearch-data2: networks: opensearch-net: driver: bridge elastic: driver: bridge
启动前准备
执行命令清理旧容器和卷,避免残留数据干扰:
docker-compose down -v
内容的提问来源于stack exchange,提问作者cybertextron
相关产品推荐
相关产品推荐

