基本原理
单机版多容器实例网络交互原理
- 在宿主机上面打开两张网卡eth0与eth1,打通两张网卡的链路
- 在test1上面启动一个veth网卡,创建一个namespace;并桥接到eth0上面
- 在test2上面启动一个veth网卡,创建一个namespace;并桥接到eth1上面
- 对test1与test2进行地址配置
- 测试网络交互情况
模拟实验
添加两个namespace
[[email protected]2 ~]# ip netns add test1 [[email protected]-2 ~]# ip netns add test2
查看添加状况
[[email protected]2 ~]# ip netns list test2 test1 [[email protected]-2 ~]# ip netns exec test1 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 [[email protected]-2 ~]# ip netns exec test2 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 PS:这里可以看到namespace还没有mac地址
添加一对虚拟网卡
[[email protected]2 ~]# ip link add veth-test1 type veth peer name veth-test2
对虚拟网卡与namespace做捆绑
[[email protected]2 ~]# ip link set veth-test1 netns test1 [[email protected]-2 ~]# ip link set veth-test2 netns test2
对namespace中的虚拟网卡做地址设定,并激活
[[email protected]2 ~]# ip netns exec test1 ip addr add 10.1.1.100/24 dev veth-test1 [[email protected]-2 ~]# ip netns exec test2 ip addr add 10.1.1.200/24 dev veth-test2 [[email protected]-2 ~]# ip netns exec test1 ip link set dev veth-test1 up [[email protected]-2 ~]# ip netns exec test2 ip link set dev veth-test2 up
查看设置的状况
[[email protected]2 ~]# ip netns exec test1 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 9: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether f2:c2:eb:2b:08:d6 brd ff:ff:ff:ff:ff:ff link-netnsid 1 inet 10.1.1.100/24 scope global veth-test1 valid_lft forever preferred_lft forever inet6 fe80::f0c2:ebff:fe2b:8d6/64 scope link valid_lft forever preferred_lft forever [[email protected]-2 ~]# ip netns exec test2 ip addr 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 8: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 5e:06:df:2b:3d:fe brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.1.1.200/24 scope global veth-test2 valid_lft forever preferred_lft forever inet6 fe80::5c06:dfff:fe2b:3dfe/64 scope link valid_lft forever preferred_lft forever
测试实验结果
[[email protected]2 ~]# ip netns exec test1 ping 10.1.1.200 PING 10.1.1.200 (10.1.1.200) 56(84) bytes of data. 64 bytes from 10.1.1.200: icmp_seq=1 ttl=64 time=0.141 ms ^C --- 10.1.1.200 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.141/0.141/0.141/0.000 ms
原文地址:https://www.cnblogs.com/guge-94/p/11082108.html
时间: 2024-10-11 16:00:21