Docker网络

Docker网络

1. evth-pair

一对的设备虚拟接口,成对出现,一端连协议,一端彼此相连;它充当着一种桥梁,连接着各种虚拟网络设备,比如OpenStack、Docker容器之间的连接等

2. 容器互通(–link)

所有的容器共用一个Docker提供的路由器,Docker会给每个容器分配一个该子网内的IP地址

查看容器IP地址

docker inspect --format='{{.NetworkSettings.IPAddress}}' aa09de4e582b
  • 如何解决容器重启后IP地址变动的问题?

    该技术已经因为此问题被遗弃

docker run -d -P --name tomcat-3 --link tomcat-2 tomcat
aa09de4e582bd3085789a640ba9c262a81d4e52d17f06e358f821dce862c69be
docker exec -it tomcat-3 ping tomcat-2
PING tomcat-2 (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat-2 (172.17.0.3): icmp_seq=1 ttl=64 time=0.050 ms
64 bytes from tomcat-2 (172.17.0.3): icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from tomcat-2 (172.17.0.3): icmp_seq=3 ttl=64 time=0.098 ms
64 bytes from tomcat-2 (172.17.0.3): icmp_seq=4 ttl=64 time=0.035 ms

--link能够使容器之前相互连接的原因在于修改了etc/hosts的IP地址映射

docker exec -it aa09de4e582b 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      tomcat-2 c322a9dcb5b0
172.17.0.4      aa09de4e582b

3. 自定义网络

容器启动时如果不指定会自动分配bridge网络

docker run -d -P --name tomcat-1 --net bridge tomcat
#创建网络
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 calong

自定义网络可以不使用--link实现容器互联

docker run -d -P --name tomcat-2 --net calong tomcat
ca8ebaceefe0e8d44f0c3dec821ecb4bf14310910548c90da94d3c70b91d1bb5
docker exec -it f350da5f26e2 ping tomcat-1
PING tomcat-1 (192.168.0.2) 56(84) bytes of data.
64 bytes from f350da5f26e2 (192.168.0.2): icmp_seq=1 ttl=64 time=0.012 ms
64 bytes from f350da5f26e2 (192.168.0.2): icmp_seq=2 ttl=64 time=0.031 ms
64 bytes from f350da5f26e2 (192.168.0.2): icmp_seq=3 ttl=64 time=0.048 ms
64 bytes from f350da5f26e2 (192.168.0.2): icmp_seq=4 ttl=64 time=0.024 ms
--- tomcat-1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 71ms
rtt min/avg/max/mdev = 0.012/0.028/0.048/0.014 ms
docker exec -it f350da5f26e2 ping 192.168.0.2
PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.020 ms
64 bytes from 192.168.0.2: icmp_seq=2 ttl=64 time=0.030 ms
64 bytes from 192.168.0.2: icmp_seq=3 ttl=64 time=0.052 ms
64 bytes from 192.168.0.2: icmp_seq=4 ttl=64 time=0.066 ms
--- 192.168.0.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 101ms
rtt min/avg/max/mdev = 0.020/0.042/0.066/0.018 ms
  • 搭建集群时让不同的集群使用不同的网络更利于维护,更能保证集群的安全和健康

4. 网络互通

处于不同网段的容器可以通过docker connect进行联通,实现方式是为容器分配多个IP地址

#将一个容器连接到一个网络
docker network connect calong tomcat-2
docker exec -it tomcat-2 ping tomcat-1
PING tomcat-1 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-1.calong (192.168.0.2): icmp_seq=1 ttl=64 time=0.096 ms
64 bytes from tomcat-1.calong (192.168.0.2): icmp_seq=2 ttl=64 time=0.051 ms
64 bytes from tomcat-1.calong (192.168.0.2): icmp_seq=3 ttl=64 time=0.047 ms
64 bytes from tomcat-1.calong (192.168.0.2): icmp_seq=4 ttl=64 time=0.051 ms
--- tomcat-1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 108ms
rtt min/avg/max/mdev = 0.039/0.074/0.161/0.043 ms
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!