Docker容器网络地址 docker容器网络配置文件( 三 )
扩展:
创建veth pair , 一个给名称空间a1 , 一个给本机 , 并且都配上ip地址 , 两个能相互通信吗?
# 创建名称空间a1[rootlocalhost ~]# ip netns add a1[rootlocalhost ~]# ip netns lista1# 创建veth pair[rootlocalhost ~]# ip link add type veth# 将veth1给a1[rootlocalhost ~]# ip link set veth1 netns a1# 启用主机和a1的虚拟网卡[rootlocalhost ~]# ip link set veth0 up[rootlocalhost ~]# ip netns exec a1 ip link set lo up[rootlocalhost ~]# ip netns exec a1 ip link set veth1 up# 给主机的veth0配上ip地址[rootlocalhost ~]# ip addr add 192.168.200.1/24 dev veth0[rootlocalhost ~]# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000link/ether 00:0c:29:bb:22:82 brd ff:ff:ff:ff:ff:ffinet 192.168.111.135/24 brd 192.168.111.255 scope global dynamic noprefixroute ens160valid_lft 1524sec preferred_lft 1524secinet6 fe80::3d5c:b9d6:55f:48e9/64 scope link noprefixroutevalid_lft forever preferred_lft forever3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group defaultlink/ether 02:42:ac:46:f8:53 brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0valid_lft forever preferred_lft forever4: veth0if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000link/ether 2e:f3:88:07:d4:97 brd ff:ff:ff:ff:ff:ff link-netns a1inet 192.168.200.1/24 scope global veth0valid_lft forever preferred_lft foreverinet6 fe80::2cf3:88ff:fe07:d497/64 scope linkvalid_lft forever preferred_lft forever# 给a1的veth1配上ip地址[rootlocalhost ~]# ip netns exec a1 ip addr add 192.168.200.2/24 dev veth1[rootlocalhost ~]# ip netns exec a1 ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever5: veth1if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000link/ether 4a:87:cf:df:f6:46 brd ff:ff:ff:ff:ff:ff link-netnsid 0inet 192.168.200.2/24 scope global veth1valid_lft forever preferred_lft foreverinet6 fe80::4887:cfff:fedf:f646/64 scope linkvalid_lft forever preferred_lft forever# 用主机ping名称空间a1的veth1测试[rootlocalhost ~]#ping 192.168.200.2PING 192.168.200.2 (192.168.200.2) 56(84) bytes of data.64 bytes from 192.168.200.2: icmp_seq=1 ttl=64 time=0.098 ms64 bytes from 192.168.200.2: icmp_seq=2 ttl=64 time=0.095 ms^C--- 192.168.200.2 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1045msrtt min/avg/max/mdev = 0.095/0.096/0.098/0.009 ms
从上得知是可以相互通信的
2、四种网络模式配置
2.1 bridge模式配置
在创建容器时添加--network bridge与不加--network选项效果是一致的
# 添加--network bridge[rootlocalhost ~]# docker run -it --rm --network bridge busybox/ # ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever6: eth0if7: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueuelink/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ffinet 172.17.0.2/16 brd 172.17.255.255 scope global eth0valid_lft forever preferred_lft forever# 不加--network bridge[rootlocalhost ~]# docker run -it --rm busybox/ # ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever8: eth0if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueuelink/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ffinet 172.17.0.2/16 brd 172.17.255.255 scope global eth0valid_lft forever preferred_lft forever
2.2 none模式配置
[rootlocalhost ~]# docker run -it --rm --network none busybox/ # ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever# 以上是docker容器帮你做好了 , 与自己开辟名字空间效果相同[rootlocalhost ~]# ip netns add z1[rootlocalhost ~]# ip netns exec z1 ip link set lo up [rootlocalhost ~]# ip netns exec z1 ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever
推荐阅读
- 什么是网络电视机 电视机工作原理
- 八个症状证明你被辐射
- 网络用语快乐星球什么意思 看完这篇文章你知道了吗
- 手机远程家用无线摄像机,超长待机,该如何选择网络
- 网络无internet访问权限怎么办? 无internet访问权限
- 策划书范文案例 网络营销方案范文
- 网络推广与网络营销一样吗 网络营销推广的误区有哪些案例
- 如何组建一个网络营销团队?企业如何进行网络营销团队建设
- 平安文明祭祀弘扬社会新风?清明网络文明祭扫活动
- 容器云和虚拟云?云主机和容器的区别