深入分析container网络-Bridge(五)

一、深入分析container网络-Bridge

1.1、docker0默认bridge

(1)查看centos的网络:ip a,可以发现
(2)查看容器tomcat01的网络:docker exec -it tomcat01 ip a,可以发现
(3)在centos中ping一下tomcat01的网络,发现可以ping通
(4)既然可以ping通,而且centos和tomcat1又属于不同的network namespace,是怎么连接的?画个图
(5)也就是说,在tomcat01中有一个eth0和centos的docker0中有一个veth3是成对的,类似于之前实战中的veth-ns1和veth-ns2,不妨再通过一个命令确认下:brctl
安装一下:yum install bridge-utils brctl show
(6)那为什么tomcat01和tomcat02能ping通呢?不多说,直接上图
(7)这种网络连接方法我们称之为Bridge,其实也可以通过命令查看docker中的网络模式:docker network lsbridge也是docker中默认的网络模式
(8)不妨检查一下bridge:docker network inspect bridge 
"Containers": { 
"6ad312b32f62b48935f3c95c58ae061df710bfebbd3d721b467507b9516eeb81": { "Name": "tomcat02",
"EndpointID":
"aa9c612c79f867e874d0cae1aab45374373b61e9cdbe79925d07ae2e89a1cca0",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16", 
"IPv6Address": ""
},
"f49fc396d8e04f2b330163d91bb5d1482715202b4e2fd0c7f42833722787742a": {
"Name": "tomcat01",
"EndpointID":
"c5440b063e8fc0c9c44f3f61bf68f577283417eb23cfa9a361d37973d01a8ba5",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
}
(9)在tomcat01容器中是可以访问互联网的,顺便把这张图画一下咯,NAT是通过iptables实现的

 

 1.2 创建自己的network

(1)创建一个network,类型为bridge
 
docker network create tomcat-net ordocker network create --subnet=172.18.0.0/24 tomcat-net
(2)查看已有的network:docker network ls
 
(3)查看tomcat-net详情信息:docker network inspect tomcat-net
(4)创建tomcat的容器,并且指定使用tomcat-net
docker run -d --name custom-net-tomcat --network tomcat-net tomcat
(5)查看custom-net-tomcat的网络信息
docker exec -it custom-net-tomcat ip a
(6)查看网卡信息
ip a
(7)查看网卡接口
brctl show
(8)此时在custom-net-tomcat容器中ping一下tomcat01的ip会如何?发现无法ping通
(9)此时如果tomcat01容器能够连接到tomcat-net上就可以
docker network connect tomcat-net tomcat01
(10)查看tomcat-net网络,可以发现tomcat01这个容器也在其中
(11)此时进入到tomcat01或者custom-net-tomcat中,不仅可以通过ip地址ping通,而且可以通过名字ping到,这时候因为都连接到了用户自定义的tomcat-net bridge上
docker exec -it tomcat01 bash
root@f49fc396d8e0:/usr/local/tomcat# ping 172.18.0.2 
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.048 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.040 ms
root@f49fc396d8e0:/usr/local/tomcat# ping custom-net-tomcat 
PING custom-net-tomcat (172.18.0.2) 56(84) bytes of data.
64 bytes from custom-net-tomcat.tomcat-net (172.18.0.2): icmp_seq=1 ttl=64 time=0.030 ms
64 bytes from custom-net-tomcat.tomcat-net (172.18.0.2): icmp_seq=2 ttl=64 time=0.264 ms
但是ping tomcat02是不通的
root@f49fc396d8e0:/usr/local/tomcat# ping 172.17.0.3 
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.066 ms
root@f49fc396d8e0:/usr/local/tomcat# ping tomcat02 
PING tomcat02 (220.250.64.26) 56(84) bytes of data.

二、深入分析Container网络-Host & None

2.1、Host

(1)创建一个tomcat容器,并且指定网络为none
docker run -d --name my-tomcat-host --network host tomcat
(2)查看ip地址
docker exec -it my-tomcat-host ip a 可以发现和centos是一样的
(3)检查host网络
"Containers": {
"e1f00d47db344b6688e99c0f5b393e232309fbe1a4d9c3fc3e1ce7c107f3312d": {
"Name": "my-tomcat-host",
"EndpointID":
"f08456d9dca024cf6f911f8d32329ba2587ea89554c96b77c32698ace6998525",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
}
}

2.2 None

(1)创建一个tomcat容器,并且指定网络为none
docker run -d --name my-tomcat-none --network none tomcat
(2)查看ip地址
docker exec -it my-tomcat-none ip a
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
(3)检查none网络
"Containers": {
"bb3f0db4fa76a25b5377da9c3bbf087ac7ef0de0a3f9c37a4ae959983d33105c": {
"Name": "my-tomcat-none",
"EndpointID":
"26055c08c968f9d6d03d10b3b66dfea004c35f5d2bd4067a2306566973e92f9e",
"MacAddress": "",
"IPv4Address": "",
"IPv6Address": ""
}
}

三、端口映射及折腾

3.1 端口映射

(1)创建一个tomcat容器,名称为port-tomcat
docker run -d --name port-tomcat tomcat
(2)思考一下要访问该tomcat怎么做?肯定是通过ip:port方式
docker exec -it port-tomcat bash 
curl localhost:8080
(3)那如果要在centos7上访问呢?
docker exec -it port-tomcat ip a ---->得到其ip地址,比如172.17.0.4 
curl 172.17.0.4:8080
小结 :之所以能够访问成功,是因为centos上的docker0连接了port-tomcat的network namespace
(4)那如果要在centos7通过curl localhost方式访问呢?显然就要将port-tomcat的8080端口映射到centos上
docker rm -f port-tomcat
docker run -d --name port-tomcat -p 8090:8080 tomcat
curl localhost:8090

3.2 折腾

(1)centos7是运行在win10上的虚拟机,如果想要在win10上通过ip:port方式访问呢?
#此时需要centos和win网络在同一个网段,所以在Vagrantfile文件中
#这种方式等同于桥接网络。也可以给该网络指定使用物理机哪一块网卡,比如
#config.vm.network"public_network",:bridge=>'en1: Wi-Fi (AirPort)'
config.vm.network"public_network"
centos7: ip a --->192.168.8.118
win10:浏览器访问 192.168.8.118:9080
(2)如果也想把centos7上的8090映射到win10的某个端口呢?然后浏览器访问localhost:port
#此时需要将centos7上的端口和win10上的端口做映射
config.vm.network"forwarded_port",guest:8098,host:8090
#记得vagrant reload生效一下
win10:浏览器访问 localhost:8098

 

 
posted @ 2022-01-12 21:36  童话述说我的结局  阅读(154)  评论(0编辑  收藏  举报