Docker网络原理
理解docker0
Dokcer是如何处理网络访问的
[root@docker01 ~]
发现容器启动的时候会得到一个eth0@if32 ip地址,这是docker分配的。
[root@docker01 ~]
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
31: eth0@if32: <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
[root@docker01 ~]
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
31: eth0@if32: <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
[root@docker01 ~]
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.075 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.053 ms
结论:linux主机可以ping通dokcer容器内部
evth-pair技术
1. 我们每启动一个docker容器,docker就会给docker容器分配一个ip,我们只有安装了dokcer,就会有一个默认的网卡docker0。 桥接模式,使用的技术是evth-pair技术! 我们发现这个容器带来的网卡,都是一对对的 evth-pair 就是一对虚拟设备接口,他们都是成对出现的,一段连着协议,一段彼此相连 正因为有这个特性,evth-pair 充当一个桥梁,连接各种虚拟网络设备 Openstac,Docker容器之间的连接,OVS的连接,都是使用的evth-pair技术
我们来测试一下tomcat01和tomcat02是否可以ping通!
[root@docker01 ~]
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.268 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.041 ms
结论:容器和容器之间是可以互相ping通的!!!
Docker使用的是Linux的桥接,宿主机中的Docker0(路由器)是容器与容器之间通信的网桥 结论: 1. tomcat01 和 tomcat02 是公用的一个路由器,docker0!!! 2. 所有的容器不指定网络的情况下,都是docker0路由的,docker会给我们容器分配一个默认的可用IP。 3. Docker中所有的网络接口都是虚拟的,虚拟的网络接口转发效率高!!!(内网传递文件) 4. 只要容器删除,对应的一对网桥就没了
– link原理详解
思考一个场景,我们编写了一个微服务,database url=ip,项目不重启,数据库ip换掉了,我们希望可以处理这个问题,可以使用名字来进行访问容器
[root@docker01 ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
82cc06227f07 tomcat "catalina.sh run" 24 seconds ago Up 22 seconds 0.0.0.0:49153->8080/tcp,
ded1034bc67c tomcat "catalina.sh run" 10 hours ago Up 4 seconds 0.0.0.0:49154->8080/tcp,
[root@docker01 ~]
ping: tomcat01: Name or service not known
[root@docker01 ~]
a380f84173f727160ac3e1873fc02b048eae8739be78f67832e4598f0760dea8
[root@docker01 ~]
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.088 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.122 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.094 ms
[root@docker01 ~]
ping: tomcat03: Name or service not known
[root@docker01 ~]
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 ded1034bc67c
172.17.0.4 a380f84173f7
[root@docker01 ~]
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 ded1034bc67c
结论: --link就是我们在hosts配置中增加了一个172.17.0.3 tomcat02 ded1034bc67c
结论: 其实这个tomcat03就是在本地配置了tomcat02的配置 我们现在使用Docker以及不建议使用–link了 自定义网络,不适用Docker0 docker0的问题,它不支持容器名称连接访问!!!
自定义网络
[root@docker01 ~]
NETWORK ID NAME DRIVER SCOPE
8d86283631e5 bridge bridge local
e185b787c28e host host local
01c19cbebe06 none null local
网络模式
bridge:桥接模式 dodker (默认,自己创建也使用bridge)
none:不配置网络
host:和宿主机共享网络
container:容器内网络连通!(用的少!局限性很大)
[root@docker01 ~]
docker run -d -P --name tomcat01 --net bridge tomcat
docker run -d -P --name tomcat01 tomcat
docker0的特点,默认,域名不能访问,--link可以打通连接!
- 我们可以自定义一个网络
[root@docker01 ~]
05a0b41b0ec470afcf9cee3af9e1afd2f4e6ab7665912ca949aa65d79af3d4b0
[root@docker01 ~]
46c0ca2ee004fc2f9480c94670b5919b48d056931c4c8ee87a2f745942a3d2fe
[root@docker01 ~]
5a72dfc63a90a2ee92d05e256f3cab1db860efe48ee93bf756612e6f78d62e28
[root@docker01 ~]
[
{
"Name": "mynet",
"Id": "05a0b41b0ec470afcf9cee3af9e1afd2f4e6ab7665912ca949aa65d79af3d4b0",
"Created": "2021-07-30T11:19:14.217679899+08:00",
"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": {
"46c0ca2ee004fc2f9480c94670b5919b48d056931c4c8ee87a2f745942a3d2fe": {
"Name": "tomcat-net-01",
"EndpointID": "072216838c62a2d8c436483f21a28d1b0cc84eb875442f7eceec9af101ea8bac",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"5a72dfc63a90a2ee92d05e256f3cab1db860efe48ee93bf756612e6f78d62e28": {
"Name": "tomcat-net-02",
"EndpointID": "327922b893c524d0416f6baf8d4d378199b5c7b412c7addce617467c09716f77",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@docker01 ~]
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.141 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.085 ms
[root@docker01 ~]
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.032 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.084 ms
结论: 我们自定义的网络docker都已经帮我们维护好了对应的关系,推荐我们平时这样使用网络! 不同的集群可以使用不同的网络,保证了集群的安全和健康!!!
[root@docker01 ~]
ba47cdb9d5a62bbe8d805361c74f8f991452731514a4ba6f9a1041b7012c18db
[root@docker01 ~]
c079eb26867546f60054de96ebcdeaa0538fbd4ee7ed264355340158375365c7
[root@docker01 ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c079eb268675 tomcat "catalina.sh run" 2 seconds ago Up 2 seconds 0.0.0.0:49160->8080/tcp, :::49160->8080/tcp tomcat02
5a72dfc63a90 tomcat "catalina.sh run" 55 minutes ago Up 55 minutes 0.0.0.0:49159->8080/tcp, :::49159->8080/tcp tomcat-net-02
46c0ca2ee004 tomcat "catalina.sh run" 55 minutes ago Up 55 minutes 0.0.0.0:49158->8080/tcp, :::49158->8080/tcp tomcat-net-01
ba47cdb9d5a6 tomcat "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:49157->8080/tcp, :::49157->8080/tcp tomcat01
|