我正在关注docker教程,而我在part 4: swarms上涉及在两个虚拟机之间建立群集。我认为群组无法正确映射我的容器端口。当我启动带有swarm的应用程序时,尝试访问VM提供的IP地址时无法连接到我的应用程序。但是,如果我在SSH进入虚拟机时手动启动该应用程序,则可以访问它。
我认为swarm无法正确映射端口的原因是,当我通过swarm部署应用程序时,我尝试通过执行docker port CONTAINER_NAME
之类的操作来查看容器的端口映射,但未显示任何内容。手动部署后,对于指定的容器运行该命令,我看到类似80 / tcp-> 0.0.0.0:80
例如,当我尝试使用curl时,会发生以下情况
curl 192.168.99.100:80
curl: (7) Failed to connect to 192.168.99.100 port 80: Connection refused
我在this post上做了更详细的介绍,但是没有得到答复,我认为我掌握的新信息可能会有所帮助。
什么不起作用
当我尝试使用群集docker stack deploy -c docker-compose.yml getstartedlab
进行部署时,无法通过Web浏览器或curl连接到该应用程序。我相信一切都已正确部署,因为我可以运行docker stack ps getstartedlab
并查看两个虚拟机之间正在运行和分布的所有服务。
有效的方法
我用docker stack rm getstartedlab
删除了堆栈,然后将其缩进创建的两个虚拟机中,并手动启动了应用,并指定了端口映射,如下所示:
docker run -p 80:80 -td myusername/get-started:part2
然后浏览到192.168.99.100
,我可以看到该应用程序,不再遇到连接问题。我很确定这是Docker问题,而不是VM问题。
其他信息-可能有用
这是我的docker-compose.yml文件。我尝试同时使用“ 4000:80”和“ 80:80”的端口
version: "3"
services:
web:
image: myusername/get-started:part2
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "4000:80"
networks:
- webnet
networks:
webnet:
总而言之,当我尝试使用swarm启动应用程序时,我无法访问该应用程序。如果我从VM内手动启动并指定端口映射(不使用swarm),则可以从vm的IP访问该应用程序。
更新
我删除了正在运行的堆栈,重新启动了虚拟机,并使用docker stack deploy -c docker-compose.yml getstartedlab
重新启动
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
4kn46vue9ka3 getstartedlab_web replicated 5/5 myusername/get-started:part2 *:4000->80/tcp
$ docker service ps 4kn46vue9ka3
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
a34u4jijc5d6 getstartedlab_web.1 myusername/get-started:part2 myvm1 Running Running 20 minutes ago
8fvukpo95ko4 getstartedlab_web.2 myusername/get-started:part2 myvm2 Running Running 20 minutes ago
lywca2zwtjfa getstartedlab_web.3 myusername/get-started:part2 myvm2 Running Running 20 minutes ago
u3cw40tjmujb getstartedlab_web.4 myusername/get-started:part2 myvm1 Running Running 20 minutes ago
c0tiyxu5o5x5 getstartedlab_web.5 myusername/get-started:part2 myvm2 Running Running 20 minutes ago
这是日志
$ docker service logs 4kn46vue9ka3
getstartedlab_web.4.u3cw40tjmujb@myvm1 | * Serving Flask app "app" (lazy loading)
getstartedlab_web.4.u3cw40tjmujb@myvm1 | * Environment: production
getstartedlab_web.4.u3cw40tjmujb@myvm1 | WARNING: Do not use the development server in a production environment.
getstartedlab_web.4.u3cw40tjmujb@myvm1 | Use a production WSGI server instead.
getstartedlab_web.4.u3cw40tjmujb@myvm1 | * Debug mode: off
getstartedlab_web.4.u3cw40tjmujb@myvm1 | * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
getstartedlab_web.1.a34u4jijc5d6@myvm1 | * Serving Flask app "app" (lazy loading)
getstartedlab_web.1.a34u4jijc5d6@myvm1 | * Environment: production
getstartedlab_web.1.a34u4jijc5d6@myvm1 | WARNING: Do not use the development server in a production environment.
getstartedlab_web.1.a34u4jijc5d6@myvm1 | Use a production WSGI server instead.
getstartedlab_web.1.a34u4jijc5d6@myvm1 | * Debug mode: off
getstartedlab_web.1.a34u4jijc5d6@myvm1 | * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
getstartedlab_web.3.lywca2zwtjfa@myvm2 | * Serving Flask app "app" (lazy loading)
getstartedlab_web.3.lywca2zwtjfa@myvm2 | * Environment: production
getstartedlab_web.3.lywca2zwtjfa@myvm2 | WARNING: Do not use the development server in a production environment.
getstartedlab_web.3.lywca2zwtjfa@myvm2 | Use a production WSGI server instead.
getstartedlab_web.3.lywca2zwtjfa@myvm2 | * Debug mode: off
getstartedlab_web.3.lywca2zwtjfa@myvm2 | * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
getstartedlab_web.2.8fvukpo95ko4@myvm2 | * Serving Flask app "app" (lazy loading)
getstartedlab_web.2.8fvukpo95ko4@myvm2 | * Environment: production
getstartedlab_web.2.8fvukpo95ko4@myvm2 | WARNING: Do not use the development server in a production environment.
getstartedlab_web.2.8fvukpo95ko4@myvm2 | Use a production WSGI server instead.
getstartedlab_web.2.8fvukpo95ko4@myvm2 | * Debug mode: off
getstartedlab_web.2.8fvukpo95ko4@myvm2 | * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
getstartedlab_web.5.c0tiyxu5o5x5@myvm2 | * Serving Flask app "app" (lazy loading)
getstartedlab_web.5.c0tiyxu5o5x5@myvm2 | * Environment: production
getstartedlab_web.5.c0tiyxu5o5x5@myvm2 | WARNING: Do not use the development server in a production environment.
getstartedlab_web.5.c0tiyxu5o5x5@myvm2 | Use a production WSGI server instead.
getstartedlab_web.5.c0tiyxu5o5x5@myvm2 | * Debug mode: off
getstartedlab_web.5.c0tiyxu5o5x5@myvm2 | * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
我在本地计算机和虚拟机中都运行了此命令,结果是相同的
$ docker container exec getstartedlab_web.1.a34u4jijc5d69m7rqry1jpfo9 curl http://127.0.0.1
OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"curl\": executable file not found in $PATH": unknown
我也只运行了一个节点,仍然没有运气连接到192.168.99.100 /
$ docker-machine ssh myvm2
docker@myvm2:~$ docker swarm leave
然后回到我的本地计算机上
$ docker-machine stop myvm2
$ docker stack deploy -c docker-compose.yml getstartedlab
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
gjkde7aw2rznl9y68zti3lqrj * myvm1 Ready Active Leader 18.09.0
qu6qymppl1msxatll9m0sh7tn myvm2 Down Active 18.09.0
$ docker stack ps getstartedlab
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
vr0w3riw1cnz getstartedlab_web.1 myusername/get-started:part2 myvm1 Running Running 22 seconds ago
pvr93rqi64dw getstartedlab_web.2 myusername/get-started:part2 myvm1 Running Running 22 seconds ago
gucfa7asiwvx getstartedlab_web.3 myusername/get-started:part2 myvm1 Running Running 22 seconds ago
qzarr6jc5hzk getstartedlab_web.4 myusername/get-started:part2 myvm1 Running Running 22 seconds ago
p0hszupsl8wj getstartedlab_web.5 myusername/get-started:part2 myvm1 Running Running 22 seconds ago
我还检查了日志,按预期,任何地方都没有myvm2的迹象。
更多更新
我删除了所有映像,容器,虚拟机等...,并从头开始了本教程。为了确保不使用旧的代码/配置,我将虚拟机命名为vm myvm3,现在再次重新启动映像。这次,我仅启动了一个虚拟机作为单节点群集。还是没有运气,我遇到了同样的“连接被拒绝”错误
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm3 * virtualbox Running tcp://192.168.99.102:2376 v18.09.0
$ docker stack ps getstartedlab
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
hnbqdo9k3val getstartedlab_web.1 myusername/get-started-again:part2 myvm3 Running Running 7 minutes ago
bwc0ga954poo getstartedlab_web.2 myusername/get-started-again:part2 myvm3 Running Running 7 minutes ago
aleioewe4ivx getstartedlab_web.3 myusername/get-started-again:part2 myvm3 Running Running 7 minutes ago
fzys3tihrf0t getstartedlab_web.4 myusername/get-started-again:part2 myvm3 Running Running 7 minutes ago
4jyzzao11z96 getstartedlab_web.5 myusername/get-started-again:part2 myvm3 Running Running 7 minutes ago