KVM&OVS组网配置案例-3(解惑)

2025-02-15  本文已影响0人  rmbnet

1. 参数解析

virt-install 命令中,--network 参数用于定义虚拟机的网络配置。具体参数如下:

--network bridge=ovs-br0,model=virtio,virtualport_type=openvswitch,virtualport_options:tag=20

2. 验证虚拟机是否关联到 OVS 网桥

步骤 1:创建虚拟机

使用以下命令创建 vm1vm2

# 创建 vm1(VLAN 10)
sudo virt-install \
  --name vm1 \
  --ram 1024 \
  --vcpus 1 \
  --disk path=/var/lib/libvirt/images/vm1.qcow2 \
  --os-type linux \
  --os-variant ubuntu20.04 \
  --network bridge=ovs-br0,model=virtio,virtualport_type=openvswitch,virtualport_options:tag=10 \
  --graphics none \
  --console pty,target_type=serial \
  --cdrom /path/to/ubuntu-20.04.iso

# 创建 vm2(VLAN 20)
sudo virt-install \
  --name vm2 \
  --ram 1024 \
  --vcpus 1 \
  --disk path=/var/lib/libvirt/images/vm2.qcow2 \
  --os-type linux \
  --os-variant ubuntu20.04 \
  --network bridge=ovs-br0,model=virtio,virtualport_type=openvswitch,virtualport_options:tag=20 \
  --graphics none \
  --console pty,target_type=serial \
  --cdrom /path/to/ubuntu-20.04.iso

步骤 2:查看 OVS 端口

虚拟机启动后,检查 OVS 网桥 ovs-br0 的端口信息:

sudo ovs-vsctl show

输出应包含以下信息:

Bridge ovs-br0
    Port "vnet0"          # vm1 的虚拟网卡
        tag: 10
        Interface "vnet0"
    Port "vnet1"          # vm2 的虚拟网卡
        tag: 20
        Interface "vnet1"
    Port "eth0"           # 物理网卡
        trunks: [10, 20]
        Interface "eth0"

步骤 3:验证 VLAN 标签

检查端口 VLAN 配置:

# 查看 vnet0 的 VLAN 标签
sudo ovs-vsctl get port vnet0 tag
# 输出应为 10

# 查看 vnet1 的 VLAN 标签
sudo ovs-vsctl get port vnet1 tag
# 输出应为 20

3. 关键机制说明

1. Libvirt 与 OVS 的集成

2. 无需手动操作


4. 验证虚拟机网络

步骤 1:启动虚拟机并获取 IP

在虚拟机内部执行以下命令(假设物理交换机已配置 VLAN 10 和 20 的 DHCP):

# 在 vm1(VLAN 10)中
dhclient -v eth0
ip addr show eth0  # 应获取类似 192.168.10.100/24 的 IP

# 在 vm2(VLAN 20)中
dhclient -v eth0
ip addr show eth0  # 应获取类似 192.168.20.100/24 的 IP

步骤 2:测试网络连通性

# 在 vm1 中 ping VLAN 10 网关(假设网关为 192.168.10.1)
ping 192.168.10.1

# 在 vm2 中 ping VLAN 20 网关(假设网关为 192.168.20.1)
ping 192.168.20.1

5. 常见问题排查

问题 1:虚拟机无法获取 IP

问题 2:虚拟机之间无法通信


总结

通过以下参数:

--network bridge=ovs-br0,model=virtio,virtualport_type=openvswitch,virtualport_options:tag=20

最终,虚拟机将通过 OVS 接入指定 VLAN,并从物理交换机的 DHCP 服务获取 IP 地址。

上一篇 下一篇

猜你喜欢

热点阅读