Docker 容器网络访问原理,SNAT和DNAT

容器网络访问原理图

在这里插入图片描述

首先我们会有这个 宿主机命名空间 eth0(我们的公网 IP或内网 IP),然后 我们的容器 container 通过 veth 虚拟设备队(他的作用 就相当于管道,这头进 另一头出),来分配的 容器的网络命名空间。

我们查看一下:


[root@izbp1izjo7pl5ccghnbdiuz ~]# ifconfig
br-642af1c223ad: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:32:2e:f0:0a  txqueuelen 0  (Ethernet)
        RX packets 37  bytes 7182 (7.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 5440 (5.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:c4:5e:de:d7  txqueuelen 0  (Ethernet)
        RX packets 39854  bytes 2365296 (2.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40372  bytes 106483120 (101.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.57.19  netmask 255.255.240.0  broadcast 172.16.63.255
        ether 00:16:3e:0b:5c:f3  txqueuelen 1000  (Ethernet)
        RX packets 24245898  bytes 4480538360 (4.1 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24834168  bytes 4928454620 (4.5 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 2262  bytes 287264 (280.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2262  bytes 287264 (280.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 在这里
----------------------------------------------------------------------
veth58e889a: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500    |
        ether 56:70:7f:96:bd:42  txqueuelen 0  (Ethernet)            |
        RX packets 37  bytes 7182 (7.0 KiB)                          |
        RX errors 0  dropped 0  overruns 0  frame 0                  |
        TX packets 50  bytes 5440 (5.3 KiB)                          |
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0   |
----------------------------------------------------------------------

有了 veth就保证了 容器能够和宿主机进行通信。

网络访问的过程图

在这里插入图片描述

解析从 容器到外界:

  1. 进入到容器的伪终端,然后 ping百度

    此时 docker exec -it busybox bash 进入到容器操作也是可以的

    [root@izbp1izjo7pl5ccghnbdiuz ~]# docker run -it busybox
    / # 
    
    / # ping www.baidu.com
    PING www.baidu.com (180.101.49.12): 56 data bytes
    64 bytes from 180.101.49.12: seq=0 ttl=49 time=10.944 ms
    64 bytes from 180.101.49.12: seq=1 ttl=49 time=11.067 ms
    
  2. 容器将请求发送到宿主机的过程

    容器内: route是路由表的意思
    首先由容器内部的 eth0(也就是veth1),发送给容器的网关 route里的 eth0(也就是宿主机的网卡 veth0)
    在这里插入图片描述 在这里插入图片描述

    容器外:
    自定义网络的话,就会创建 一个linux网桥(br-5c710e739634),172.18.0.1 就是网关 。这个br-5c710e739634 整体就是宿主机的一个虚拟网卡。
    接收 ↑发送过来的数据。

    在这里插入图片描述
    刚才说到了:route里的 eth0(也就是宿主机的网卡 veth0),那么在 宿主机上是如何展示 的容器的连接通道 eth0(veth0)。
    cethc788816就是了

    在这里插入图片描述
    我们的路由表,可以看到 刚才介绍并使用的 网桥(br-5c710e739634)。
    但是内 我们ping百度的时候,宿主机发现 我们的目标地址是外部地址,本机处理不了。所以要走外部 默认网关 ens33
    在这里插入图片描述

外界到容器:

这个就很OK了,看操作图 都能理解个大概。

iptables -vnL   # 格式太乱

iptables-save

docker 的网络主要是利用了 iptables 来进行实现的

在这里插入图片描述
上一篇博客的操作,所以我的 docker网络环境下 会有99 端口访问宿主机(或容器)
docker五大网络模式 https://blog.csdn.net/weixin_44685869/article/details/105158124
在这里插入图片描述
我们看合格 99端口啊,大概意思就是 外部访问宿主机IP 的请求,通过DNAT 将目的地转换到我们的 容器网关接口。

https://blog.csdn.net/weixin_44685869/article/details/105158124
这个呢 就是识别我们的容器端口,找到对应的 veth1


具体的流程就是,把最上面的调换过来,没啥说的。



DOCKER SNAT与DNAT


映射容器端口到宿主主机的实现

默认情况下,容器可以主动访问到外部网络的连接,但是外部网络无法访问到容器。

容器访问外部实现

容器所有到外部网络的连接,源地址都会被 NAT 成本地系统的 IP 地址。这是使用 iptables 的源地址伪装操作实现的。

查看主机的 NAT 规则。

$ sudo iptables -t nat -nL
...
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  172.17.0.0/16       !172.17.0.0/16
...

其中,上述规则将所有源地址在 172.17.0.0/16 网段,目标地址为其他网段(外部网络)的流量动态伪装为从系统网卡发出。MASQUERADE 跟传统 SNAT 的好处是它能动态从网卡获取地址。

外部访问容器实现

容器允许外部访问,可以在 docker run 时候通过 -p 或 -P 参数来启用。

不管用那种办法,其实也是在本地的 iptable 的 nat 表中添加相应的规则。

  • 使用 -P 时:

    
    $ iptables -t nat -nL
    ...
    Chain DOCKER (2 references)
    target     prot opt source               destination
    DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:49153 to:172.17.0.2:80
    
    
  • 使用 -p 80:80 时:

    $ iptables -t nat -nL
    Chain DOCKER (2 references)
    target     prot opt source               destination
    DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 to:172.17.0.2:80
    
    

注意:

  • 这里的规则映射了 0.0.0.0,意味着将接受主机来自所有接口的流量。用户可以通过 -p IP:host_port:container_port 或 -p IP::port 来指定允许访问容器的主机上的 IP、接口等,以制定更严格的规则。

  • 如果希望永久绑定到某个固定的 IP 地址,可以在 Docker 配置文件 /etc/docker/daemon.json 中添加如下内容。

{
  "ip": "0.0.0.0"
}

SNAT和DNAT参考:https://www.cnblogs.com/justart/p/11687709.html

发布了102 篇原创文章 · 获赞 229 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/weixin_44685869/article/details/105251302
今日推荐