KVM 来宾的正确网络配置使其位于主机的同一网络上

Ste*_*sen 5 networking linux firewall bridge kvm-virtualization

我在 Lenny 上运行 Debian Linux 服务器。在其中,我正在使用 KVM 运行另一个 Lenny 实例。两台服务器都是外部可用的,具有公共 IP,以及用于 LAN 的具有私有 IP 的第二个接口。一切正常,除了 VM 将所有网络流量视为源自主机服务器。我怀疑这可能与我在主机上运行的基于 iptables 的防火墙有关。

我想弄清楚的是:如何正确配置主机的网络以满足所有这些要求?

  1. 主机和虚拟机都有 2 个网络接口(公共和私有)。
  2. 主机和虚拟机都可以独立设置防火墙。
  3. 理想情况下,VM 流量不必穿过主机防火墙。
  4. VM 看到的是真正的远程 IP 地址,而不是主机的。

目前,主机的网络接口被配置为网桥。eth0 和 eth1 没有分配给它们的 IP 地址,但 br0 和 br1 有。

/etc/network/interfaces 在主机上:

# The primary network interface
auto br1
iface br1 inet static
    address 24.123.138.34
    netmask 255.255.255.248
    network 24.123.138.32
    broadcast 24.123.138.39
    gateway 24.123.138.33
    bridge_ports eth1
    bridge_stp off

auto br1:0
iface br1:0 inet static
    address 24.123.138.36
    netmask 255.255.255.248
    network 24.123.138.32
    broadcast 24.123.138.39

# Internal network
auto br0
iface br0 inet static
    address 192.168.1.1
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    bridge_ports eth0
    bridge_stp off
Run Code Online (Sandbox Code Playgroud)

这是 VM 的 libvirt/qemu 配置文件:

<domain type='kvm'>
  <name>apps</name>
  <uuid>636b6620-0949-bc88-3197-37153b88772e</uuid>
  <memory>393216</memory>
  <currentMemory>393216</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type arch='i686' machine='pc'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='cdrom'>
      <target dev='hdc' bus='ide'/>
      <readonly/>
    </disk>
    <disk type='file' device='disk'>
      <source file='/raid/kvm-images/apps.qcow2'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <interface type='bridge'>
      <mac address='54:52:00:27:5e:02'/>
      <source bridge='br0'/>
      <model type='virtio'/>
    </interface>
    <interface type='bridge'>
      <mac address='54:52:00:40:cc:7f'/>
      <source bridge='br1'/>
      <model type='virtio'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target port='0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>
  </devices>
</domain>
Run Code Online (Sandbox Code Playgroud)

除了我的防火墙规则的其余部分,防火墙脚本还包含以下命令来传递发往 KVM 来宾的数据包:

# Allow bridged packets to pass (for KVM guests).
iptables -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

(不适用于这个问题,但我的桥接配置的一个副作用似乎是我永远无法干净地关闭。内核最终告诉我“unregister_netdevice: waiting for br1 to be free”,我必须硬重置系统。也许是我做了一些愚蠢的事情的迹象?)

dya*_*sny 2

为什么 br1:0 需要别名?
除了别名之外,这可能是一种方式 ,想法是使用以下方案:
eth0->br0 <--VM 的 Tap 设备
主机应该能够使用 br0,因为它是 IF 并且 VM 将使用 Tap 设备作为插入虚拟交换机的虚拟网卡(br0 实际上变成了这里)

当然,每个网络都是如此,因此对于 eth1,您必须设置 br1,并启动要插入 br1 的虚拟机