# host1 vxlan config # host1上基于host1-eth0网口创建vxlan类型接口vxlan0,vni=4096 host1.cmd("ip link add vxlan0 type vxlan id 4096 dev host1-eth0 dstport 4789") # 设置vxlan0接口mac地址 host1.cmd("ip link set vxlan0 address 00:10:10:00:01:01") # 设置vxlan0接口ip地址 host1.cmd("ip addr add 10.0.0.1/32 dev vxlan0") # 接口使能 host1.cmd("ip link set vxlan0 up")
# 设置fbd表项,目的地址192.168.20.20通过vxlan0基于mac=00:10:10:00:01:02封装转发出去 host1.cmd("bridge fdb add 00:10:10:00:01:02 dev vxlan0 dst 192.168.20.20") # 设置10.0.1.0/24网关为10.0.1.1 host1.cmd("ip route add 10.0.1.0/24 via 10.0.1.1 dev vxlan0 onlink") # 设置10.0.0.1网关arp表项mac=00:10:10:00:01:02 host1.cmd("arp -s 10.0.1.1 00:10:10:00:01:02 -i vxlan0") # 创建namespace=net1的网络命名空间 host1.cmd("ip netns del net1") host1.cmd("ip netns add net1") # 使能net1中lo接口 host1.cmd("ip netns exec net1 ip link set lo up") # 创建veth-pair用于连接host1主机网络及net1命名空间网络 host1.cmd("ip link add type veth") host1.cmd("ip link set veth0 netns net1") host1.cmd("ip netns exec net1 ip link set veth0 name eth0") # 设置net1中eth0网卡ip地址 host1.cmd("ip netns exec net1 ip addr add 10.0.0.10 dev eth0") host1.cmd("ip netns exec net1 ip link set eth0 up") # 设置veth1网卡mac地址 host1.cmd("ip link set veth1 address 00:00:20:00:01:01") host1.cmd("ip link set veth1 up") # 使能arp代理 host1.cmd("echo 1 > /proc/sys/net/ipv4/conf/veth1/proxy_arp") # 设置net1命名空间网络默认路由 host1.cmd("ip netns exec net1 ip route add 169.254.1.1 dev eth0 scope link") host1.cmd("ip netns exec net1 ip route add default via 169.254.1.1 dev eth0") # 设置host1主机网络到net1命名空间网络路由 host1.cmd("ip route add 10.0.0.10 dev veth1 scope link")
# host2 vxlan config, 配置说明参考host1中设置 host2.cmd("ip link add vxlan0 type vxlan id 4096 dev host2-eth0 dstport 4789") host2.cmd("ip link set vxlan0 address 00:10:10:00:01:02") host2.cmd("ip addr add 10.0.1.1/32 dev vxlan0") host2.cmd("ip link set vxlan0 up") host2.cmd("bridge fdb add 00:10:10:00:01:01 dev vxlan0 dst 192.168.10.10") host2.cmd("ip route add 10.0.0.0/24 via 10.0.0.1 dev vxlan0 onlink") host2.cmd("arp -s 10.0.0.1 00:10:10:00:01:01 -i vxlan0") host2.cmd("ip netns del net2") host2.cmd("ip netns add net2") host2.cmd("ip netns exec net2 ip link set lo up") host2.cmd("ip link add type veth") host2.cmd("ip link set veth0 netns net2") host2.cmd("ip netns exec net2 ip link set veth0 name eth0") host2.cmd("ip netns exec net2 ip addr add 10.0.1.10 dev eth0") host2.cmd("ip netns exec net2 ip link set eth0 up") host2.cmd("ip link set veth1 address 00:00:20:00:01:02") host2.cmd("ip link set veth1 up") host2.cmd("echo 1 > /proc/sys/net/ipv4/conf/veth1/proxy_arp") host2.cmd("ip netns exec net2 ip route add 169.254.1.1 dev eth0 scope link") host2.cmd("ip netns exec net2 ip route add default via 169.254.1.1 dev eth0") host2.cmd("ip route add 10.0.1.10 dev veth1 scope link")