大布景
同三个用户端上运转数个罐子,每一罐子相联系关系不异的办事项目,当办事项目间必要展开通信时(好比说:http通信)就牵扯到罐子间数据办事项目标难题,按照过往的实战经历得出三种同时实现的体例
ip罐子间透过ip门商标展开通信。 长处很显著,罐子的ip门商标并不是陈旧见解的,罐子从头启动或是北迁单项招致ip门商标的变更,进而引致办事项目间通信失利。
network=container:xxx [root@t32 ~]# docker run -itd --name myapp docker.io/ikubernetes/myapp:v2 cfb3370a23e314ad407f0a5e98db5aecd445e94774b41f5f6cbab8a847af74b0 [root@t32 ~]# docker run -it --network=container:myapp --name test centos:7.5.1804 bash [root@cfb3370a23e3 /]# curl localhost:80 Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> [root@cfb3370a23e3 /]# 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.10 cfb3370a23e3罐子myapp与罐子test在同三个network namespace,因而在罐子test下能接纳localhost
link [root@t32 ~]# docker run -it --link=myapp --name test centos:7.5.1804 bash [root@b41364a8bdc4 /]# curl localhost:80 curl: (7) Failed connect to localhost:80; Connection refused [root@b41364a8bdc4 /]# curl myapp:80 Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> [root@b41364a8bdc4 /]# 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.10 myapp cfb3370a23e3 172.17.0.11 b41364a8bdc4罐子myapp与罐子test无此同三个network namespace下,但test相连myapp后,会将myapp和相联系关系ip门商标载入到test下的/etc/hosts下,进而能透过containerName:port通信
network=container:xxx与link的体例在数个罐子必要彼此通信的情况下,会引致开启指示过火冗杂,FEA差network=hostnetwork=host是docker国际尺度互联网体例寡所周知,选定罐子与用户端共享资本同三个network namespace。
数个罐子共享资本host的network,罐子间能透过localhost顺利完成通信。
但,此种体例的长处极为显著,不克不及充实操纵罐子的便当性,好比说布署三个mysql材料库,假设都选用host互联网,则会有三个mysql罐子开启失利(即便5432路由器早已被挤占)
bridge最末上场的,也是最爱崇的配套办法,即成立三个bridge,数个罐子镜像到同三个bridge下面,进而透过containerName:port通信。
[root@t32 ~]# docker network create my-net2 7135ba540b5fd164b16c7f85629d53df5de9d014378b9984a641a8d0f3fddb18 [root@t32 ~]# docker network ls |grep my-net2 7135ba540b5f my-net2 bridge local [root@t32 ~]# docker run -itd --name myapp --network=my-net2 docker.io/ikubernetes/myapp:v2 85011452d8df633642224e1b1dd5b91ca0d408774296010ff808eb9227b90379 [root@t32 ~]# docker run -itd --name myapp1 --network=my-net2 docker.io/ikubernetes/myapp:v3 048e70a79e65f064e21c0ef48b26e3142280b627dedf5864b28dc76d269cc857 [root@t32 ~]# docker run -it --name test --network=my-net2 centos:7.5.1804 bash [root@f6cd7182338f /]# 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.21.0.4 f6cd7182338f [root@f6cd7182338f /]# cat /etc/resolv.conf nameserver 127.0.0.11 options ndots:0 [root@f6cd7182338f /]# curl myapp Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a> [root@f6cd7182338f /]# curl myapp1 Hello MyApp | Version: v3 | <a href="hostname.html">Pod Name</a>bridge体例中的/etc/resolv.conf与其他体例不异,其他体例共享资本用户端上的/etc/resolve.conf(即dns解析),而bridge则接纳本身的dns解析信息数个罐子全数接纳bridge,能便于多罐子数据办事项目查看bridge的信息 [root@t32 ~]# docker inspect my-net2 [ { "Name": "my-net2", "Id": "7135ba540b5fd164b16c7f85629d53df5de9d014378b9984a641a8d0f3fddb18", "Created": "2020-10-14T21:11:04.941285678+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.21.0.0/16", "Gateway": "172.21.0.1" } ] }, "Internal": false, "Attachable": false, "Containers": { "048e70a79e65f064e21c0ef48b26e3142280b627dedf5864b28dc76d269cc857": { "Name": "myapp1", "EndpointID": "31ad488d3c0b94a4b223fd6026e8e59586ac7214b2a4319a7b17c050eeb95f28", "MacAddress": "02:42:ac:15:00:03", "IPv4Address": "172.21.0.3/16", "IPv6Address": "" }, "85011452d8df633642224e1b1dd5b91ca0d408774296010ff808eb9227b90379": { "Name": "myapp", "EndpointID": "c8cb27609c1ac8d5bc6ed717f5be7945e0db2f1cd387cc12ba230125e1c544a0", "MacAddress": "02:42:ac:15:00:02", "IPv4Address": "172.21.0.2/16", "IPv6Address": "" }, "9a1526e9d2fe2c07081c1f1e3b08e564468d9d908e1b0965095e90e94e6de914": { "Name": "test", "EndpointID": "1e013be5fd8307400999501c28a2d43871d75c2c306e0cb180d3938a1b6d4fcf", "MacAddress": "02:42:ac:15:00:04", "IPv4Address": "172.21.0.4/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ]能查看挂载在my-net2下面的罐子不异的bridge分配不异的网段,数个罐子在同三个网段下。
0