欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker网络

程序员文章站 2022-03-07 13:14:00
...

Docker网络

理解Docker0

docker是如何处理容器网络访问的?

# C:\Users\Mr.li>docker run -d -P --name tomcat01 tomcat

# 查看容器的内部网络地址 ip addr ,发现容器启动的时候会得到一个 aaa@qq.com  ip地址,docker分配的!
C:\Users\Mr.li>docker exec -it tomcat01 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1000
    link/tunnel6 :: brd ::
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
       
# 思考, linux能不能 ping 通容器内部!

# linux可以 ping 通 docker 容器内部!

原理

  1. 每安装一个docker容器,docker就会给docker分配一个ip,就会有一个网卡docker0桥接模式,使用的技术是veth-pair技术!
  2. 再启动一个测试
# 我们发现这个容器网卡,都是一对一对的
# veth-pair 就是一对的虚拟设备接口,他们都是成对出现的,一端连着协议,一端彼此相连
# 正因为有这个特性, veth-pair 充当一个桥梁,连接各种虚拟网络设备的
# OpenStac, Docker容器之间的连接,ovs的连接,都是使用 veth-pair 技术!
  1. tomcat01和tomcat02
C:\Users\Mr.li>docker exec -it tomcat01 ping 172.17.0.3

# 容器和容器之间是可以互相 ping 通的!

结论: tomcat01 和 tomcat02是公用的一个路由器, docker0.

所有容器不指定网络的情况下,都是docker0 路由的,docker会给我们的容器分配一个默认的可用ip

小结

Docker 使用的是linux桥接,宿主机中一个Docker容器的桥接

Docker 中的所有的网络接口都是虚拟的,虚拟的转发效率高!

只要容器删除,对应网桥一对就没了!

–link

容器之间的网络

C:\Users\Mr.li>docker exec -it tomcat01 ping tomcat02
ping: tomcat02: No address associated with hostname

# 如何可以解决呢?
C:\Users\Mr.li>docker run -d -P --name tomcat03 --link tomcat02 tomcat
56e05741739c33cbae108f8b8fba7b5597d45928cebd73b6c20c9271040e2129

C:\Users\Mr.li>docker exec -it tomcat03 ping tomcat02
PING tomcat02 (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.152 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.158 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.072 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=4 ttl=64 time=0.162 ms

# 反向可以ping通吗
C:\Users\Mr.li>docker exec -it tomcat02 ping tomcat03
ping: tomcat03: No address associated with hostname

其实这个tomcat03 就是在本地配置了tomcat02的配置?

# 查看容器
C:\Users\Mr.li>docker exec -it tomcat03 cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3      tomcat02 61f78eec9955
172.17.0.4      56e05741739c

本质探究:–link就是我们在hosts配置中增加了一个172.17.0.3 tomcat02 61f78eec9955

不建议使用–link了

自定义网路!不适用docker0!

docker0问题:不支持容器名连接访问!

自定义网络

查看所有的docker网络

Docker网络
网络模式

bridge :桥接docker(默认, 自己创建也使用bridge模式)

none : 不配置网络

host :和宿主机共享网络

测试

# 我们直接启动的命令 --net bridge 而这个就是我们的docker0
docker run -d -P --name tomcat01 --net bridge tomcat

# docker0特点, 默认, 域名不能访问, --link可以打通连接!

# 我们可以自定义一个网络
# --driver bridge
# --subnet 192.168.0.0/16
# --gateway 192.168.0.1
C:\Users\Mr.li>docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
a3f1af39237cc15973b781c3c6a896fe259e37ce2c032ec801d69a81acca0ecb

C:\Users\Mr.li>docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
4c794dc070e2        bridge              bridge              local
d1477c928977        host                host                local
a3f1af39237c        mynet               bridge              local
52e086c1470f        none                null                local
C:\Users\Mr.li>docker run -d -P --name tomcat-net-01 --net mynet tomcat
f4d36a63fe4fca223a1d47d98bb73334344edede9969bd7749ca94ff1c5a88d5

C:\Users\Mr.li>docker run -d -P --name tomcat-net-02 --net mynet tomcat
ac979388b847e75431c9bd63bc654ca4883c597ab7f7dd6579b05cbae457127d

C:\Users\Mr.li>docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "a3f1af39237cc15973b781c3c6a896fe259e37ce2c032ec801d69a81acca0ecb",
        "Created": "2020-05-30T11:07:29.31899686Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "ac979388b847e75431c9bd63bc654ca4883c597ab7f7dd6579b05cbae457127d": {
                "Name": "tomcat-net-02",
                "EndpointID": "037b9283324500a5a50458fcc0b3c0d80f15e579577441450c2363fffa79cdb6",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            },
            "f4d36a63fe4fca223a1d47d98bb73334344edede9969bd7749ca94ff1c5a88d5": {
                "Name": "tomcat-net-01",
                "EndpointID": "00b0cadd4c4402cd6f67ae5638689198aa85e905622983276604fa7c50e05ca4",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

# 再次测试ping连接
C:\Users\Mr.li>docker exec -it tomcat-net-01 ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.129 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.090 ms
64 bytes from 192.168.0.3: icmp_seq=4 ttl=64 time=0.083 ms
^C
--- 192.168.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 88ms
rtt min/avg/max/mdev = 0.076/0.094/0.129/0.022 ms

# 现在不使用--link也可以ping名字了
C:\Users\Mr.li>docker exec -it tomcat-net-01 ping tomcat-net-02
PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.128 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.065 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.127 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.224 ms
^C
--- tomcat-net-02 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 74ms
rtt min/avg/max/mdev = 0.065/0.136/0.224/0.056 ms

我们自定义的网络docker都已经帮我们维护好了对应的关系,推荐我们平时这样使用网络!

好处:

不同的集群使用不同的网络,保证集群是安全和健康的

网络连通

Docker网络
Docker网络

# 测试,打通 tomcat01 - mynet

# 连通之后就是将 tomcat01 放到了 mynet 网络下?

# 一个容器两个ip地址!

Docker网络

# 01 连通
C:\Users\Mr.li>docker exec -it tomcat01 ping tomcat-net-01
PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.173 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.146 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.209 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=4 ttl=64 time=0.185 ms
^C
--- tomcat-net-01 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 83ms
rtt min/avg/max/mdev = 0.146/0.178/0.209/0.024 ms

# 02依旧打不通的
C:\Users\Mr.li>docker exec -it tomcat02 ping tomcat-net-01
ping: tomcat-net-01: No address associated with hostname

结论:假设要跨网络操作别人,就需要使用docker network connect 连通!