VxLAN 介绍
虚拟可扩展局域网(virtual extensible LAN,VxLAN)是一种基于 UDP 协议的网络隧道协议,用于在 IP 网络上传输二层数据流量。例如,运行在不同主机上的虚拟机(VM)可以通过 VxLAN 隧道进行通信。这些主机可以位于不同的子网,甚至位于全球不同的数据中心。VxLAN 提供了透明的二层通信。这些虚拟机在逻辑层面上表现得像在同一个网络中
环境准备
主机名 | IP 地址 |
---|---|
server1 | 10.0.1.10/24 |
gateway1 | 172.16.0.150/24, 10.0.1.254/24 |
gateway2 | 172.16.0.160/24, 10.0.2.254/24 |
server2 | 10.0.2.10/24 |
server1 配置
测试网关是否可达
root@server1:~# ip route
default via 10.0.1.254 dev eth0 proto static
10.0.1.0/24 dev eth0 proto kernel scope link src 10.0.1.10
root@server1:~# ping -c 3 10.0.1.254
PING 10.0.1.254 (10.0.1.254) 56(84) bytes of data.
64 bytes from 10.0.1.254: icmp_seq=1 ttl=64 time=0.793 ms
64 bytes from 10.0.1.254: icmp_seq=2 ttl=64 time=1.25 ms
64 bytes from 10.0.1.254: icmp_seq=3 ttl=64 time=0.840 ms
--- 10.0.1.254 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.793/0.962/1.254/0.207 ms
server2 配置
测试网关是否可达
root@server2:~# ip route
default via 10.0.2.254 dev eth0 proto static
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.10
root@server2:~# ping -c 3 10.0.2.254
PING 10.0.2.254 (10.0.2.254) 56(84) bytes of data.
64 bytes from 10.0.2.254: icmp_seq=1 ttl=64 time=0.818 ms
64 bytes from 10.0.2.254: icmp_seq=2 ttl=64 time=0.845 ms
64 bytes from 10.0.2.254: icmp_seq=3 ttl=64 time=0.459 ms
--- 10.0.2.254 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2031ms
rtt min/avg/max/mdev = 0.459/0.707/0.845/0.175 ms
gateway1 配置
开启 IPv4 转发
root@gateway1:~# echo 1 > /proc/sys/net/ipv4/ip_forward
创建 VxLAN 接口
root@gateway1:~# ip link add vxlan0 type vxlan dev eth0 vni 10 local 172.16.0.150 remote 172.16.0.160 dstport 4789
root@gateway1:~# ip addr add 1.1.1.1/24 dev vxlan0
root@gateway1:~# ip link set vxlan0 up
root@gateway1:~# ip addr show vxlan0
4: vxlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default qlen 1000
link/ether ae:83:c6:00:91:78 brd ff:ff:ff:ff:ff:ff
inet 1.1.1.1/24 scope global vxlan0
valid_lft forever preferred_lft forever
inet6 fe80::ac83:c6ff:fe00:9178/64 scope link
valid_lft forever preferred_lft forever
添加路由
root@gateway1:~# ip route
default via 172.16.0.2 dev eth0 proto static
1.1.1.0/24 dev vxlan0 proto kernel scope link src 1.1.1.1
10.0.1.0/24 dev eth1 proto kernel scope link src 10.0.1.254
172.16.0.0/24 dev eth0 proto kernel scope link src 172.16.0.150
root@gateway1:~# ip route add 10.0.2.0/24 via 1.1.1.2 dev vxlan0
root@gateway1:~# ip route
default via 172.16.0.2 dev eth0 proto static
1.1.1.0/24 dev vxlan0 proto kernel scope link src 1.1.1.1
10.0.1.0/24 dev eth1 proto kernel scope link src 10.0.1.254
10.0.2.0/24 via 1.1.1.2 dev vxlan0
172.16.0.0/24 dev eth0 proto kernel scope link src 172.16.0.150
gateway2 配置
开启 IPv4 转发
root@gateway2:~# echo 1 > /proc/sys/net/ipv4/ip_forward
创建 VxLAN 接口
root@gateway2:~# ip link add vxlan0 type vxlan dev eth0 vni 10 local 172.16.0.160 remote 172.16.0.150 dstport 4789
root@gateway2:~# ip addr add 1.1.1.2/24 dev vxlan0
root@gateway2:~# ip link set vxlan0 up
root@gateway2:~# ip addr show vxlan0
4: vxlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default qlen 1000
link/ether ae:83:c6:00:91:78 brd ff:ff:ff:ff:ff:ff
inet 1.1.1.2/24 scope global vxlan0
valid_lft forever preferred_lft forever
inet6 fe80::ac83:c6ff:fe00:9178/64 scope link
valid_lft forever preferred_lft forever
gateway1 和 gateway2 的 vxlan0 接口的 MAC 地址冲突,需要修改 MAC 地址
root@gateway2:~# ip link set dev vxlan0 address ae:83:c6:00:91:80
添加路由
root@gateway2:~# ip route
default via 172.16.0.2 dev eth0 proto static
1.1.1.0/24 dev vxlan0 proto kernel scope link src 1.1.1.2
10.0.2.0/24 dev eth1 proto kernel scope link src 10.0.2.254
172.16.0.0/24 dev eth0 proto kernel scope link src 172.16.0.160
root@gateway2:~# ip route add 10.0.1.0/24 via 1.1.1.1 dev vxlan0
root@gateway2:~# ip route
default via 172.16.0.2 dev eth0 proto static
1.1.1.0/24 dev vxlan0 proto kernel scope link src 1.1.1.2
10.0.1.0/24 via 1.1.1.1 dev vxlan0
10.0.2.0/24 dev eth1 proto kernel scope link src 10.0.2.254
172.16.0.0/24 dev eth0 proto kernel scope link src 172.16.0.160
测试连通性
server1
root@server1:~# ping -c 3 10.0.2.10
PING 10.0.2.10 (10.0.2.10) 56(84) bytes of data.
64 bytes from 10.0.2.10: icmp_seq=1 ttl=62 time=2.01 ms
64 bytes from 10.0.2.10: icmp_seq=2 ttl=62 time=1.49 ms
64 bytes from 10.0.2.10: icmp_seq=3 ttl=62 time=2.05 ms
--- 10.0.2.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 1.488/1.848/2.048/0.255 ms
server2
root@server2:~# ping -c 3 10.0.1.10
PING 10.0.1.10 (10.0.1.10) 56(84) bytes of data.
64 bytes from 10.0.1.10: icmp_seq=1 ttl=62 time=2.74 ms
64 bytes from 10.0.1.10: icmp_seq=2 ttl=62 time=0.968 ms
64 bytes from 10.0.1.10: icmp_seq=3 ttl=62 time=1.33 ms
--- 10.0.1.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2006ms
rtt min/avg/max/mdev = 0.968/1.679/2.739/0.763 ms
抓包
root@gateway1:~# tcpdump -n -w vxlan.pcap
tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
^C6 packets captured
6 packets received by filter
0 packets dropped by kernel
总结
- 为什么要开启 IP 转发?
Linux 默认不会转发源 IP 地址和目的 IP 地址 均不是本地接口 IP 地址的数据包 - VxLAN 接口上配置 IP 地址的作用?
通过 ARP 请求获取对端 VxLAN 接口的 MAC 地址,即内层数据包的目的 MAC 地址 - 为什么不直接通过 VxLAN 接口传输数据包?
VxLAN 接口是虚拟接口,需要将封装号的数据包交由物理接口进行网络传输