Cloud Native 50 min read

Mastering VXLAN: Deep Dive into Linux Overlay Networks and Practical Configurations

This article explains the VXLAN protocol, its advantages over VLAN, the underlying encapsulation process, essential configuration commands, and hands‑on demos using Linux network namespaces, multicast groups, and bridge networking to build scalable overlay networks for cloud environments.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering VXLAN: Deep Dive into Linux Overlay Networks and Practical Configurations

写在前面

本文内容为 Linux 网络隧道技术 VXLAN 的认知,涉及:VXLAN 协议介绍、基本配置命令、基于 Linux 网络命名空间的 VXLAN 组网 Demo、自维护 VTEP 组,FDB 和 ARP 表自更新介绍。

vxlan 协议介绍

在构建网络时,需要将多个物理网络连接成一个虚拟网络,VXLAN(Virtual Extensible LAN)是一种在三层网络上构建二层虚拟网络的隧道技术。它在原始数据包上添加 VXLAN 头部,其中包含 24 位的 VNI(Virtual Network Identifier)用于区分不同的虚拟网络。

VXLAN 通过在 UDP(端口 4789)上封装二层以太网帧,实现物理网络与虚拟网络的解耦,支持大规模云计算环境。

A framework for overlaying virtualized layer 2 networks over lay 3 networks.

为什么需要 VXLAN

VLAN Header 仅有 12 位,最多支持 4096 个子网,无法满足云计算场景下的大规模主机需求。VXLAN 的 VNI 长度为 24 位,支持 2^24(约 1677 万)个子网,并提供多租户网络隔离和 VM/容器的大规模迁移能力。

VXLAN 协议原理简介

VXLAN 网络的每个端点都有一个 VTEP(VXLAN Tunnel Endpoint),负责 VXLAN 报文的封装和解封装。VTEP 可以是交换机或宿主机。

VNI 用于标识虚拟二层网络,VXLAN 头部仅包含 VNI、保留字段和标志。

VXLAN 基本配置命令

sudo ip link add vxlan0 type vxlan id 42 group 239.1.1.1 dev eth0 dstport 4789

创建 VXLAN 接口后,可使用以下命令查看: ip -d link show vxlan0 删除 VXLAN 接口:

sudo ip link delete vxlan0

VXLAN 网络实践

点对点的 VXLAN

使用两个 Linux 网络命名空间(node1、node2)模拟点对点 VXLAN:

sudo ip netns add node1
sudo ip netns add node2
sudo ip netns exec node1 ip link add veth1 netns node1 type veth peer name veth2 netns node2
# 配置 IP、启用转发、创建 VXLAN 接口等

在 node1 中创建 VXLAN 接口:

sudo ip netns exec node1 ip link add vxlan0 type vxlan id 42 dev veth1 dstport 4789 remote 192.168.1.3 local 192.168.1.2

为接口分配 IP 并启用:

sudo ip netns exec node1 ip addr add 172.17.1.2/24 dev vxlan0
sudo ip netns exec node1 ip link set vxlan0 up

在两端 ping 测试可实现互通。

多播的情况

创建 VXLAN 接口并加入多播组,实现自动发现 VTEP:

sudo ip netns exec node1 ip link add vxlan1 type vxlan id 43 dstport 4789 local 192.168.1.2 group 224.1.1.1 dev veth1

在另一命名空间使用相同 VNI 和多播组,配置 IP 后即可通过多播进行 ARP 学习和 ICMP 通信。

VXLAN+桥接网络

使用 Linux 网桥将多个虚拟网卡(或容器)接入同一 VXLAN 网络,实现大规模虚拟机/容器的互联。

分布式控制中心

在不支持多播的网络中,可通过集中式控制中心(agent)下发 VTEP 的 MAC 与 IP 信息,避免多播带来的广播风暴。

自维护 VTEP 组

通过手动维护 FDB 表,将全零的默认条目替换为点对点的 VTEP IP,实现无多播的 overlay 网络。

ip link add vxlan0 type vxlan id 42 dstport 4789 dev eth0
bridge fdb append 00:00:00:00:00:00 dev vxlan0 dst 192.168.8.101
bridge fdb append 00:00:00:00:00:00 dev vxlan0 dst 192.168.8.102

动态更新 FDB 和 ARP 表项

使用 nolearning proxy l2miss l3miss 参数让内核在缺失 FDB 或 ARP 条目时发送事件,用户空间程序可监听并动态添加对应记录。

ip monitor all dev vxlan0

总结

VXLAN 为云计算网络提供了灵活的二层覆盖能力,突破了 VLAN 的规模限制,但也带来了约 50‑54 字节的额外开销以及封装/解封装的计算成本。多播方案实现简单但会产生大量无用流量;自维护 FDB、分布式控制中心或动态事件机制可在不同场景下优化性能和可扩展性。

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Network VirtualizationOverlay NetworkLinux networkingVXLANIP routing
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.