简体   繁体   English

无法访问在 docker swarm 集群中运行的服务

[英]Unable to access service running in docker swarm cluster

I have a docker swarm cluster with following nodes我有一个带有以下节点的 docker 集群

NAME       ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
manager1   -        virtualbox   Running   tcp://192.168.99.113:2376           v19.03.12   
worker1    -        virtualbox   Running   tcp://192.168.99.114:2376           v19.03.12   
worker2    -        virtualbox   Running   tcp://192.168.99.115:2376           v19.03.12 

I have created service on manager node as follows我在管理器节点上创建了如下服务

$ docker service create --name registry --publish 5000:5000 registry:2

Now I can access the service from any swarm node现在我可以从任何 swarm 节点访问该服务

$ curl localhost:5000/v2/_catalog                                     
{"repositories":[]}

But If I try to access the service from outside swarm cluster I get this error但是,如果我尝试从 swarm 集群外部访问该服务,我会收到此错误

$ curl localhost:5000/v2/_catalog
curl: (7) Failed to connect to localhost port 5000: Connection refused

How can we access service running in swarm cluster from host machine我们如何从主机访问在 swarm 集群中运行的服务

PUBLISH A SERVICE'S PORTS USING THE ROUTING MESH使用路由网格发布服务的端口

To publish a service's ports externally to the swarm, use the --publish: flag.要将服务的端口发布到 swarm 外部,请使用 --publish: 标志。 The swarm makes the service accessible at the published port on every swarm node. swarm 使服务可以在每个 swarm 节点上的已发布端口上访问。 If an external host connects to that port on any swarm node, the routing mesh routes it to a task.如果外部主机连接到任何 swarm 节点上的该端口,路由网格会将其路由到任务。 The external host does not need to know the IP addresses or internally-used ports of the service tasks to interact with the service.外部主机不需要知道服务任务的 IP 地址或内部使用的端口即可与服务交互。 When a user or process connects to a service, any worker node running a service task may respond.当用户或进程连接到服务时,任何运行服务任务的工作节点都可能响应。 For more details about swarm service networking, see Manage swarm service networks .有关 swarm 服务网络的更多详细信息,请参阅管理 swarm 服务网络

Example: Run a three-task Nginx service on 10-node swarm Imagine that you have a 10-node swarm, and you deploy an Nginx service running three tasks on a 10-node swarm:示例:在 10 节点集群上运行三任务 Nginx 服务假设您有一个 10 节点集群,并且您部署了一个 Nginx 服务在 10 节点集群上运行三个任务:

$ docker service create --name my_web \
                        --replicas 3 \
                        --publish published=8080,target=80 \
                        nginx

Three tasks run on up to three nodes.三个任务最多在三个节点上运行。 You don't need to know which nodes are running the tasks;您不需要知道哪些节点正在运行任务; connecting to port 8080 on any of the 10 nodes connects you to one of the three nginx tasks.连接到 10 个节点中的任何一个上的端口 8080 会将您连接到三个 nginx 任务之一。 You can test this using curl.您可以使用 curl 对此进行测试。 The following example assumes that localhost is one of the swarm nodes.以下示例假设 localhost 是 swarm 节点之一。 If this is not the case, or localhost does not resolve to an IP address on your host, substitute the host's IP address or resolvable host name.如果不是这种情况,或者 localhost 无法解析为主机上的 IP 地址,请替换主机的 IP 地址或可解析的主机名。

The HTML output is truncated: HTML output 被截断:

$ curl localhost:8080

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...truncated...
</html>

Subsequent connections may be routed to the same swarm node or a different one.后续连接可能会路由到同一个 Swarm 节点或不同的节点。

Finally I was able to access service with this command curl <manager/worker ip>:5000/v2/_catalog最后我能够使用这个命令curl <manager/worker ip>:5000/v2/_catalog访问服务

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

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