docker-compose的gliderlabs / registrator有时会使用错误的IP注册服务

时间:2018-07-10 14:05:35

标签: docker docker-compose consul

我得到了包含8个容器的docker-compose文件。有时它会正确地注册容器,有时会错误地使用错误的IP地址和端口来注册容器。

我打印容器名称以及它们的IP和暴露的端口:

$ docker ps|awk '{print $1}'|xargs docker inspect --format='{{.NetworkSettings.IPAddress}} {{.Name}} {{.NetworkSettings.Ports}}'
172.17.0.9 /docker_rabbitmq_1 map[5672/tcp:[{0.0.0.0 5672}] 15671/tcp:[] 15672/tcp:[{0.0.0.0 15672}] 25672/tcp:[] 4369/tcp:[] 5671/tcp:[]]
172.17.0.8 /docker_worker_1 map[]
172.17.0.6 /docker_gateway_1 map[3003/tcp:[{0.0.0.0 3003}]]
172.17.0.7 /docker_mariadb_1 map[3306/tcp:[{0.0.0.0 3306}]]
172.17.0.4 /docker_registrator_1 map[]
172.17.0.5 /docker_fabio_1 map[5000/tcp:[{0.0.0.0 5000}] 5001/tcp:[{0.0.0.0 5001}] 9998/tcp:[] 9999/tcp:[]]
172.17.0.3 /docker_timer_1 map[]
172.17.0.2 /docker_consul_1 map[8600/tcp:[] 8300/tcp:[] 8500/tcp:[{0.0.0.0 8500}] 8302/tcp:[] 8302/udp:[] 8400/tcp:[{0.0.0.0 8400}] 8600/udp:[{0.0.0.0 8600}] 8301/tcp:[] 8301/udp:[]

您可以看到以下服务:

  • 172.17.0.6:3003 docker_gateway_1
  • 172.17.0.7:3306 docker_mariadb_1

这是docker-compose日志摘录

consul_1       |     2018/07/10 12:40:26 [WARN] agent: socket connection failed '172.17.0.8:3003': dial tcp 172.17.0.8:3003: getsockopt: connection refused
consul_1       |     2018/07/10 12:40:26 [WARN] agent: socket connection failed '172.17.0.2:3306': dial tcp 172.17.0.2:3306: getsockopt: connection refused

领事抱怨它无法连接到端口地址:

  • 172.17.0.8:3003
  • 172.17.0.2:3306

哪些地址是错误的。

您遇到了这个错误,也许找到了解决方案?

版本:

  • docker-compose版本1.21.2,内部版本a133471
  • Docker版本18.03.1-ce,内部版本9ee9f40
  • gliderlabs / registrator:v7

这是我的docker-compose.yml:

version: '2'
services:
  # Consul (service discovery)
  consul:
    image: consul:v0.7.0

    volumes:
      - ./volumes/consul/config:/consul/config # You can edit consul.json to customise consul behaviour

    network_mode: bridge
    ports:
      - 8400:8400
      - 8500:8500
      - 8600:8600/udp

    entrypoint: consul agent -ui -server -config-dir=/consul/config -bootstrap-expect=1

    labels:
      - SERVICE_IGNORE # Tell Registrator to ignore this container

  # Registrator (register containers as services in Consul)
  registrator:
    depends_on:
      - consul

    image: gliderlabs/registrator:v7

    network_mode: bridge
    links:
      - consul:consul

    volumes:
      - /var/run/docker.sock:/tmp/docker.sock

    # Thanks to the link above, the Consul container's IP can can resolved using the host name "consul"
    entrypoint: /bin/registrator consul://consul:8500

  fabio:
    depends_on:
      - consul

    image: magiconair/fabio:1.3-go1.7.1

    network_mode: bridge
    ports:
      - 5000:5000
      - 5001:5001

    links:
      - consul:consul

    volumes:
      - ./volumes/fabio/fabio.properties:/etc/fabio/fabio.properties

  rabbitmq:
    depends_on:
      - registrator
      - gateway # intentionally start after gateway to test its ability to handle brocker absense
      - worker

    image: rabbitmq:management

    network_mode: bridge
    ports:
      - 15672:15672
      - 5672:5672

    labels:
        "SERVICE_5672_NAME": "rabbitmq"
        "SERVICE_5672_CHECK_TCP": "true"
        "SERVICE_5672_CHECK_INTERVAL": "5s"
        "SERVICE_5672_CHECK_TIMEOT": "2s"
        "SERVICE_15672_IGNORE": "always"
#        "SERVICE_15672_NAME": "rabbitmq-gui"
#        "SERVICE_15672_CHECK_TCP": "true"
#        "SERVICE_15672_CHECK_INTERVAL": "5s"
#        "SERVICE_15672_CHECK_TIMEOT": "2s"

  mariadb:
    depends_on:
      - registrator
    image: mariadb:10.3.7
    restart: always
    network_mode: bridge
    ports:
      - 3306:3306
    volumes:
      - ./volumes/mariadb:/var/lib/mysql
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_DATABASE=mariner-dev
    labels:
        "SERVICE_3306_NAME": "mariadb"
        "SERVICE_3306_CHECK_TCP": "true"
        "SERVICE_3306_CHECK_INTERVAL": "5s"
        "SERVICE_3306_CHECK_TIMEOT": "2s"

  gateway:
    depends_on:
      - registrator
#      - rabbitmq
    links:
      - consul

    image: mariner/gateway:latest

    environment:
      - "MARINER_JWT_SECRET=${MARINER_JWT_SECRET}"

    network_mode: bridge
    ports:
      - 3003:3003

    # These labels (SERVICE_*) control how Registrator registers the container as a service in Consul
    labels:
        "SERVICE_3003_NAME": "mariner-gateway"
        "SERVICE_TAGS": "gateway,v1,urlprefix-/ws/,proto ws"
        "SERVICE_3003_CHECK_TCP": "true"
        "SERVICE_3003_CHECK_INTERVAL": "5s"
        "SERVICE_3003_CHECK_TIMEOT": "2s"

  worker:
    links:
      - consul
      - mariadb # This for standalone migrations only

    image: mariner/worker:latest

    network_mode: bridge

  timer:
    links:
      - consul

    image: mariner/timer:latest

    network_mode: bridge

0 个答案:

没有答案