linux虚拟网络之vxlan设备

 

只要数据包进入了vxlan设备,那么就会被封装成vxlan报文;不管源ip是通过bridge连接到vxlan,还是源ip就是vxlan自身设备的IP

复制代码
实验1:两台host通过vxlan 设备IP 进行通信
背景:两台host IP分别为192.168.1.110、192.168.1.111;分别配置vxlan设备IP 10.0.0.110、10.0.0.111

==============host1配置vxlan=============================================================
ip link add vxlan1 type vxlan id 1 remote 192.168.1.111 dstport 4789 dev eth0           #创建vxlan设备
ip link set vxlan1 up
ip addr add 10.0.0.110/24 dev vxlan1


1.id: VNI标识是1。
2.remote: 作为一个VTEP设备来封装和解封VXLAN报文,需要知道将封装好的VXLAN报文发送到哪个对端VTEP。
    Linux上可以利用group指定组播组地址,或者利用remote指定对端单播地址。在实验的云环境中默认不支持组播,这里利用remote指定点对点的对端IP地址为172.31.0.1073.dstport: 指定目的端口为4789。
    因为当Linux内核3.7版本首次实现VXLAN时,UDP端口还并没有规定下来。很多厂商利用了8472这个端口,Linux也采用了相同的端口。后来IANA分配了4789作为VXLAN的目的UDP端口。如果你需要使用IANA端口,需要用dstport指定。
4.dev: 指定VTEP通过哪个物理device来通信,这里是使用eth0。


==============host2配置vxlan=============================================================
ip link add vxlan1 type vxlan id 1 remote 192.168.1.110 dstport 4789 dev eth0           #创建vxlan设备
ip link set vxlan1 up
ip addr add 10.0.0.111/24 dev vxlan1



[root@yefeng ~]# ip r s
default via 192.168.1.1 dev eth1 proto dhcp metric 100
10.0.0.0/24 dev vxlan1 proto kernel scope link src 10.0.0.111
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
172.18.0.0/16 dev br-f07017a6d46d proto kernel scope link src 172.18.0.1
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.115 metric 100
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.111 metric 101
[root@yefeng ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 eth1
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 vxlan1
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-f07017a6d46d
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     101    0        0 eth0



==============在host2上进行测试=============================================================
[root@yefeng ~]# ping -I vxlan1 10.0.0.110 -c1      #直接用host进行ping包,抓包结果确认为vxlan报文
PING 10.0.0.110 (10.0.0.110) from 10.0.0.111 vxlan1: 56(84) bytes of data.
64 bytes from 10.0.0.110: icmp_seq=1 ttl=64 time=0.190 ms

--- 10.0.0.110 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.190/0.190/0.190/0.000 ms
实验1:两台host通过vxlan 设备IP 进行通信
复制代码

 

复制代码
实验2:两台host的同网段network namespace通过vxlan设备通信
背景:两台host IP分别为192.168.1.110、192.168.1.111###host1操作
ip link add vxlan1 type vxlan id 1 remote 192.168.1.111 dstport 4789 dev eth0           #host1创建vxlan设备
ip link set vxlan1 up

ip link add br_vxlan1_eth type veth peer name host1_ns1_eth0                            #创建veth pair设备

#创建network namespace,并将veth pair设备放入network namespace
ip netns add host1_ns1                                                                  #创建network namespace
ip link set host1_ns1_eth0 netns host1_ns1                                              #放入veth pair设备
ip netns exec host1_ns1 ip link set host1_ns1_eth0 up
ip netns exec host1_ns1 ip addr add 10.0.1.100/24 dev host1_ns1_eth0


#创建bridge,并将veth pair设备、vxlan设备 attach到bridge
brctl addbr br_vxlan1                                                                   #创建bridge
brctl addif br_vxlan1 vxlan1                                                            #将vxlan设备attach到bridge
brctl addif br_vxlan1 br_vxlan1_eth                                                     #将veth pair设备attach到bridge

ip link set br_vxlan1_eth up
ip link set br_vxlan1 up

###host2操作
ip link add vxlan1 type vxlan id 1 remote 192.168.1.110 dstport 4789 dev eth0           #host2创建vxlan设备
ip link set vxlan1 up

ip link add br_vxlan1_eth type veth peer name host2_ns1_eth0                            #创建veth pair设备

#创建network namespace,并将veth pair设备放入network namespace
ip netns add host2_ns1                                                                  #创建network namespace
ip link set host2_ns1_eth0 netns host2_ns1                                              #放入veth pair设备
ip netns exec host2_ns1 ip link set host2_ns1_eth0 up
ip netns exec host2_ns1 ip addr add 10.0.1.101/24 dev host2_ns1_eth0


#创建bridge,并将veth pair设备、vxlan设备 attach到bridge
brctl addbr br_vxlan1                                                                   #创建bridge
brctl addif br_vxlan1 vxlan1                                                            #将vxlan设备attach到bridge
brctl addif br_vxlan1 br_vxlan1_eth                                                     #将veth pair设备attach到bridge

ip link set br_vxlan1_eth up
ip link set br_vxlan1 up


########################ping包并进行抓包验证###############################
[root@yefeng ~]# ip netns exec host2_ns1 ping 10.0.1.100 -c1
PING 10.0.1.100 (10.0.1.100) 56(84) bytes of data.
64 bytes from 10.0.1.100: icmp_seq=1 ttl=64 time=0.246 ms

--- 10.0.1.100 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.246/0.246/0.246/0.000 ms

经抓包验证,确实被封装为vxlan报文
实验2:两台host的同网段network namespace通过vxlan设备通信
复制代码

 

复制代码
实验3:容器跨主机通信(参考文章实验)
本实验未实际操作,因为实验2也可以体现实验效果
host IP分别为172.31.0.106、172.31.0.107
#两台host均创建相同的docker网络
docker network create --subnet 172.18.0.0/16 mynetwork
#在2台host上分别run2个容器
docker run -itd --net mynetwork --ip 172.18.0.2 centos
docker run -itd --net mynetwork --ip 172.18.0.3 centos


#host1创建vxlan设备,并将vxlan设备attach到docker bridge
ip link add vxlan_docker type vxlan id 200 remote 172.31.0.107 dstport 4789 dev eth0
ip link set vxlan_docker up
brctl addif br-3231f89d69f6 vxlan_docker        #将vxlan设备attach到docker bridge

#host2创建vxlan设备,并将vxlan设备attach到docker bridge
ip link add vxlan_docker type vxlan id 200 remote 172.31.0.106 dstport 4789 dev eth0
ip link set vxlan_docker up
brctl addif br-f4b35af34313 vxlan_docker        #将vxlan设备attach到docker bridge

------------------开始测试-------------------------------------------
# docker exec -it 16bbaeaaebfc ping 172.18.0.3 -c 2             #ping另一台host上的容器
PING 172.18.0.3 (172.18.0.3) 56(84) bytes of data.
bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.544 ms
bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.396 ms

--- 172.18.0.3 ping statistics ---
packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.396/0.470/0.544/0.074 ms
#
# docker exec -it 16bbaeaaebfc ping 172.18.0.1 -c 2             #ping本地host的另一台容器
PING 172.18.0.1 (172.18.0.1) 56(84) bytes of data.
bytes from 172.18.0.1: icmp_seq=1 ttl=64 time=0.072 ms
bytes from 172.18.0.1: icmp_seq=2 ttl=64 time=0.072 ms

--- 172.18.0.1 ping statistics ---
packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.072/0.072/0.072/0.000 ms

注意RTT的时间,ping 172.18.0.3的RTT在10^(-1)毫秒级别,ping 172.18.0.1的RTT在10^(-2)毫秒级别,前者是走物理网络的延迟,后者是协议栈的延迟,两者有量级上的差别。
实验3:容器跨主机通信(参考文章实验)
复制代码

 

参考文章:
什么是 VxLAN? https://segmentfault.com/a/1190000019662412
Linux 下实践 VxLAN https://segmentfault.com/a/1190000019905778

posted @   雲淡風輕333  阅读(469)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示