将docker容器部署到GCE时,我无法建立连接。如果我在本地构建和运行docker容器,那么我没有任何问题。
我在Dockerfile中公开了端口50051,并将构建的映像部署到GCR。
我有一个Ubuntu 20.04LTS VM,已经安装了docker。
我尝试使用以下命令启动服务器
server.bindAsync('0.0.0.0:50051', grpcLibrary.ServerCredentials.createInsecure(), (error, port) => {
try {
if (error)
reject(error);
server.start();
console.log('started on ' + port);
resolve(server);
}
catch (e) {
reject(e);
}
});
和
server.bindAsync('127.0.0.1:50051', grpcLibrary.ServerCredentials.createInsecure(), (error, port) => {
try {
if (error)
reject(error);
server.start();
console.log('started on ' + port);
resolve(server);
}
catch (e) {
reject(e);
}
});
我正在使用以下命令运行容器 docker run -p 50051:50051 -it -d eu.gcr.io/myimage
VM上的IP表显示
sudo iptables -t nat -n -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0
MASQUERADE tcp -- 172.17.0.2 172.17.0.2 tcp dpt:50051
Chain DOCKER (2 references)
target prot opt source destination
RETURN all -- 0.0.0.0/0 0.0.0.0/0
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:50051 to:172.17.0.2:50051
docker ps
表演
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e32bdcae366 eu.gcr.io/myimage:latest "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 0.0.0.0:50051->50051/tcp youthful_wright
和ss -tulpn在容器中
ss -tulpn
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 511 *:50051 *:* users:(("node",pid=1,fd=18))
我在VPC网络上也有以下防火墙规则
Name
Type
Targets
Filters
Protocols / ports
Action
Priority
Network
Logs
Hit count
Last hit
Insights
allow-grpc
Ingress
Apply to all
IP ranges: 0.0.0.0/0
tcp:50051
Allow
1000
default
On
85
2020-07-08 (11:33:00)
有谁知道为什么我无法从本地计算机连接或出现相同问题?
我觉得这是路由中的问题,但我无法解决
我在其中一个实例中进行了复制,但没有任何问题。
我正在使用默认的nginx官方映像进行快速测试。
也许您缺少防火墙规则中的某些内容。除此之外,没有什么特别的事情要做。
我启动了一个虚拟的nginx图像。我选择发布:8095并将流量转发到容器内的:80:
docker run --name some-nginx -d -p 8095:80 nginx
我可以在本地到达它,而无需其他步骤:
# curl localhost:8095
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
尚无法从外部访问,因为没有防火墙规则允许进入我选择发布的端口的入口:
neo@neo-desktop:~$ telnet 34.107.14.223 80
Trying 34.107.14.223...
telnet: Unable to connect to remote host: Connection refused
然后创建一个防火墙规则,该规则允许进入已发布的端口,我在上一步中将规则应用于所有已将标签分配给实例的实例:
添加规则后,我可以在容器中运行的Nginx中顺利卷曲:
neo@neo-desktop:~$ curl 34.107.14.223:8095
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句