首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >码头工人在码头主机内监听RabbitMQ,而不是从外部监听,为什么?

码头工人在码头主机内监听RabbitMQ,而不是从外部监听,为什么?
EN

Stack Overflow用户
提问于 2021-05-04 07:31:59
回答 1查看 332关注 0票数 3

我就是这样运行rabbitMQ映像的:

代码语言:javascript
运行
复制
docker run -d --restart always --hostname host-rabbit --name cg-rabbit -p 5029:5672 -p 5020:15672 -e RABBITMQ_DEFAULT_VHOST=sample_vhost -e RABBITMQ_DEFAULT_USER=sampleuser -e RABBITMQ_DEFAULT_PASS=samplepass rabbitmq:3-management

现在在netstat -nltp

代码语言:javascript
运行
复制
ubuntu@infra:~$ netstat -nltp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp6       0      0 :::22                   :::*                    LISTEN      -
tcp6       0      0 :::5020                 :::*                    LISTEN      -
tcp6       0      0 :::5029                 :::*                    LISTEN      -

--我不确定为什么会看到tcp6,当tcp6将端口公开给主机时,以及它是否会产生问题!

现在,当我从服务器内部进行telnet时,我可以看到端口是打开的:

代码语言:javascript
运行
复制
ubuntu@infra:~$ telnet MY-SERVER-IP-ADDRESS 5029
Trying MY-SERVER-IP-ADDRESS...
Connected to MY-SERVER-IP-ADDRESS.
Escape character is '^]'.
^]

telnet> Connection closed.

但是在我的机器中,当我尝试telnet (或来自另一台服务器)时:

代码语言:javascript
运行
复制
$ telnet MY-SERVER-IP-ADDRESS 5020
Trying MY-SERVER-IP-ADDRESS...
^C

iptables -L报告:

代码语言:javascript
运行
复制
sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:5020
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:5029
ACCEPT     tcp  --  anywhere             anywhere

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:amqp
ACCEPT     tcp  --  anywhere             172.17.0.2           tcp dpt:15672

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (1 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

很好地注意到,我已经在服务器(非码头用户)中安装了一个redis服务器,并且我能够从外部对它进行telnet

编辑-1:

代码语言:javascript
运行
复制
sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DOCKER     all  --  anywhere             anywhere             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  --  anywhere            !localhost/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  172.17.0.0/16        anywhere
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:15672
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:amqp

Chain DOCKER (2 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere
DNAT       tcp  --  anywhere             anywhere             tcp dpt:15672 to:172.17.0.2:15672
DNAT       tcp  --  anywhere             anywhere             tcp dpt:amqp to:172.17.0.2:5672

EDIT-2:码头配置:

代码语言:javascript
运行
复制
ubuntu@infra:~$ sudo cat /var/snap/docker/796/config/daemon.json
{
    "log-level":        "error",
    "storage-driver":   "overlay2"
}
EN

回答 1

Stack Overflow用户

发布于 2021-05-04 08:47:59

这真的很奇怪。通过在NAT中刷新iptables,一切都如预期的那样工作:

代码语言:javascript
运行
复制
iptables -t nat -F

脸红前我的nat:

代码语言:javascript
运行
复制
ubuntu@infra:~$ sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DOCKER     all  --  anywhere             anywhere             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  --  anywhere            !localhost/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  172.17.0.0/16        anywhere
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:15672
MASQUERADE  tcp  --  172.17.0.2           172.17.0.2           tcp dpt:amqp

Chain DOCKER (2 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere
DNAT       tcp  --  anywhere             anywhere             tcp dpt:15672 to:172.17.0.2:15672
DNAT       tcp  --  anywhere             anywhere             tcp dpt:amqp to:172.17.0.2:5672

现在,在冲过脸之后,一切都消失了:

代码语言:javascript
运行
复制
ubuntu@infra:~$ sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (0 references)
target     prot opt source               destination

注意:通过通过sudo snap restart docker重新启动对接规则又回来了,我不得不再次冲洗NAT!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67380511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档