简体   繁体   English

无法通过主机上的已发布端口访问 Docker Swarm 服务

[英]Cannot access a Docker Swarm service through its published port on the host

I have deployed a Docker Swarm cluster on several machines and I am now trying to access to the server running in Docker from the host.我已经在多台机器上部署了一个 Docker Swarm 集群,现在我正在尝试从主机访问在 Docker 中运行的服务器。

I use docker compose file to define my service and the exposed port appears when I inspect the service:我使用 docker compose 文件来定义我的服务,当我检查服务时会出现暴露的端口:

"Endpoint": {
        "Spec": {
            "Mode": "vip",
            "Ports": [
                {
                    "Protocol": "tcp",
                    "TargetPort": 27017,
                    "PublishedPort": 3017,
                    "PublishMode": "host"
                }
            ]
        },
        "Ports": [
            {
                "Protocol": "tcp",
                "TargetPort": 27017,
                "PublishedPort": 3017,
                "PublishMode": "host"
            }
        ],
        "VirtualIPs": [
            {
                "NetworkID": "**********",
                "Addr": "10.0.0.34/24"
            }
        ]
    }

I use host mode because the service is constrained to run on a particular machine, and I want it accessible only from this machine.我使用主机模式是因为该服务被限制在特定机器上运行,并且我希望它只能从这台机器上访问。

But when I list the processes listening on ports on the host machine, the port doesn't appear.但是当我列出侦听主机端口的进程时,端口没有出现。

And of course I cannot connect to the server from the host through the exposed port.当然,我无法通过暴露的端口从主机连接到服务器。

I am using iptables as firewall and restrains as much as possible the open ports, but the Docker Swarm needed ones are opened.我使用 iptables 作为防火墙并尽可能限制开放的端口,但 Docker Swarm 需要的端口是开放的。

Here is my docker-compose.yml file:这是我的 docker-compose.yml 文件:

version: '3.4'
services:
  mongo-router:
    image: mongo
    networks:
      - mongo-cluster
    volumes:
      - db-data-router:/data/db
      - db-config-router:/data/configdb
    ports:
      - target: 27017
        published: 3017
        protocol: tcp
        mode: host
    deploy:
      placement:
        constraints:
          - node.labels.mongo.router == true
    command: mongos --configdb cnf/mongodb-cnf_mongo-cnf-1:27017,mongodb-cnf_mongo-cnf-2:27017,mongodb-cnf_mongo-cnf-3:27017

volumes:
  db-data-router:
  db-config-router:

networks:
  mongo-cluster:
    external: true

The network is an overlay network on which all services are subscribing.该网络是一个覆盖网络,所有服务都在其上订阅。

I had a similar issue.我有一个类似的问题。 After installing hyper-v feature on windows (even though the cpu did not support hyper-v) I was able to access published ports from the host (even in ingress mode).Windows 上安装 hyper-v 功能后(即使 cpu 不支持 hyper-v),我能够从主机访问已发布的端口(即使在入口模式下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM