docker撰写elasticsearch和golang连接被拒绝

uubf1zoe  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(610)

我使用docker compose在本地机器上运行golang应用程序和单节点ElasticSearch,并使用https://github.com/vishnubob/wait-for-it 订购开始。在连接我的golang应用程序和elastic时,我面临着连接被拒绝的问题。我想我把正确的ip从我的golang容器指向我的es容器。我想我没有正确设置es容器
错误日志:

web_1      | wait-for-it.sh: waiting 15 seconds for elastic:9200
web_1      | wait-for-it.sh: timeout occurred after waiting 15 seconds for elastic:9200
web_1      | 2020/11/16 04:05:12 Setup ElasticSearch: failed cause, dial tcp 172.28.0.3:9200: connect: connection refused

这是我的配置:
docker-compose.yml公司

version: "3.8"
services: 
  web:
    build: .
    command: ["./wait-for-it.sh","mongo:27017","redis:6379","elastic:9200","--","./server"]
    ports:
      - "8080:8080"
    networks:
      - s2l_network
    depends_on:
      - mongo
      - redis
      - elastic

  mongo:
    container_name: mongo
    image: mongo
    restart: always
    ports:
      - "27017:27017"
    #volumes:
    #  - "s2l_mongo:/data/db"
    networks:
      - s2l_network

  redis:
    container_name: redis
    image: redis
    ports:
      - "6379:6379"
    networks:
      - s2l_network

  elastic:
    container_name: elastic
    image: docker.elastic.co/elasticsearch/elasticsearch:7.9.3
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      - discovery.type=single-node
      - network.host=0.0.0.0
    networks:
      - s2l_network  

networks:
  s2l_network:

dockerfile文件

FROM golang:1.14 

ENV GO111MODULE=on

WORKDIR /app
COPY go.mod .
COPY go.sum .

RUN go mod download
COPY . .

RUN chmod +x ./wait-for-it.sh

RUN go build

连接.go

cfg := elasticsearch.Config{
        MaxRetries: 10,
        Transport: &http.Transport{
            ResponseHeaderTimeout: 10 * time.Second,
        },
        Addresses: []string{
            "http://elastic:9200",
        },
    }

    es, err := elasticsearch.NewClient(cfg)
    if err != nil {
        log.Printf("Setup ElasticSearch: failed cause, %s", err)
        return nil, err
    }

    _, err = es.Info()
    if err != nil {
        log.Printf("Setup ElasticSearch: failed cause, %s", err)
        return nil, err
    }

弹性容器检查:

...
 "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "69cd26188f8e9c1b82df2ad6991b25c8e0bd27da965c2cfcd22a83d60127fd62",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "9200/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "9200"
                    }
                ],
                "9300/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "9300"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/69cd26188f8e",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "market-place_s2l_network": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "0060c27403e7",
                        "elastic"
                    ],
                    "NetworkID": "c9181ea9396ed3fedb26852f130b0cec1f567951a433c5261eb9b4d0564a86d4",
                    "EndpointID": "f1adce667b74abf83967ba0115afe39f873b3611c25951b488c6be90b3563ec8",
                    "Gateway": "172.28.0.1",
                    "IPAddress": "172.28.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:1c:00:03",
                    "DriverOpts": null
                }
            }
        }
    }

我在想我的问题:因为我在两个独立的容器中运行我的golang应用程序和elastic,所以我必须用elastic连接我的golang应用程序,而不是通过localhost网络。所以myconnection字符串uri应该是“http://elastic:9200".
弹性文件:
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html.

Elasticsearch binds to localhost only by default

我对该语句的解释是,如果不设置任何其他附加配置,我们只能通过127.0.0.1:9200进行连接。所以我们需要这个额外的配置设置来允许我们从本地主机外部连接。

network.host=0.0.0.0

我希望有人能解释我,如果我的基本理解正确或错误
谢谢

7gyucuyw

7gyucuyw1#

问题来自于定时启动,甚至我们已经订购了启动容器。es容器的准备时间超过15秒,在默认配置中等待超时15秒。所以我们需要配置重启:在我们的golang应用程序容器中出现故障时。

web:
  restart: on-failure
  ...

相关问题