Building KVM Virtualization Network: Bridge Mode

Click on the “IT那活儿” public account — focusing on full-stack operation and maintenance technology sharing for enterprises, no matter what IT tasks, just get it done!!! One

Principle of Bridge Mode

Bridge mode is one of the most commonly used network configurations for KVM virtual machines. It allows virtual machines to connect directly to the physical network of the host machine, obtaining an independent IP address in the same subnet as the host, enabling direct communication with external networks.1.1 Working MethodCreate a virtual bridge (e.g., br0) on the host machine and bind the physical network card (e.g., eth0) to the bridge.The virtual network card of the virtual machine (vnetX) connects to the bridge, equivalent to directly accessing the physical network.1.2 FeaturesThe virtual machine and the host are in the same local area network, with IP addresses assigned by an external DHCP or manually configured;supports direct communication between virtual machines and between virtual machines and external devices.1.3 Essence of Bridge ModeA virtual switch is created at the data link layer (L2), allowing the virtual machine to share the physical network card with the host, remaining in the same broadcast domain.The virtual machine obtains an independent IP address in the same subnet as the host, and its network behavior is equivalent to that of a physical machine.1.4 Comparison with NAT Mode

Feature Bridge Mode NAT Mode
IP Assignment External DHCP or manual configuration Host internal DHCP (private IP)
Network Location Same as host network layer Behind the host (isolated subnet)
External Access Directly reachable Requires port mapping
Typical Use Server deployment, cluster environment Development testing, isolated environment

Two

Installing KVM Virtualization Module

2.1 Install KVMCheck if the system supports virtualization and whether the CPU supports virtualization:Building KVM Virtualization Network: Bridge ModeUse yum to install the main components and tools of KVM:Building KVM Virtualization Network: Bridge ModeAfter installation, confirm whether it was successful and check if the modules are loaded:Building KVM Virtualization Network: Bridge ModeEdit /etc/libvirt/qemu.conf, add vnc_listen = “0.0.0.0”:Building KVM Virtualization Network: Bridge Mode2.2 Restart libvirtd serviceBuilding KVM Virtualization Network: Bridge Mode2.3 Create a symbolic linkBuilding KVM Virtualization Network: Bridge Mode2.4 Check the image typeBuilding KVM Virtualization Network: Bridge ModeThree

Configure KVM Virtual Machine to Use NAT Network

3.1 Edit NAT startup scriptBuilding KVM Virtualization Network: Bridge Mode3.2 Create KVM virtual machine in NAT modeGrant execution permissions to the script:Building KVM Virtualization Network: Bridge ModeStart the virtual machine:Building KVM Virtualization Network: Bridge ModeUse VNC Viewer software to remotely access the virtual machine, listing the IP address, subnet mask, and other information of this virtual machine, and also view the routing information of this system:Building KVM Virtualization Network: Bridge ModeCheck the system’s bridge information, you can see the interface mounted on the virbro bridge:Building KVM Virtualization Network: Bridge ModeCheck the TAP interface:Building KVM Virtualization Network: Bridge ModeFour

Configure KVM Virtual Machine to Use Bridge Network

4.1 Configure Basic EnvironmentEdit the qemu-ifup script file:Building KVM Virtualization Network: Bridge ModeEdit the qemu-ifdown file:Building KVM Virtualization Network: Bridge ModeInstall brctl and tunctl tools:Building KVM Virtualization Network: Bridge ModeCheck if the tun module is loaded:Building KVM Virtualization Network: Bridge ModeCreate a Bridge:Building KVM Virtualization Network: Bridge ModeCreate the corresponding bridge IP:Building KVM Virtualization Network: Bridge ModeBuilding KVM Virtualization Network: Bridge ModeAfter configuration, restart the network and check the network and bridge configuration information:Building KVM Virtualization Network: Bridge Mode4.2 Start the virtual machineUse the command to start the virtual machine:Building KVM Virtualization Network: Bridge ModeCheck the br0 bridge:Building KVM Virtualization Network: Bridge ModeFive

Conclusion

This experiment achieved seamless connection between the virtual machine and the physical network by configuring the Bridge network of KVM virtual machines.5.1 Experiment Steps

  • Environment PreparationCheck if the system supports virtualization and whether the CPU supports virtualization.Ensure that KVM virtualization support is functioning properly.
  • Bridge CreationUse brctl to create the virtual bridge br0, bind the physical network card eth0 to the bridge, release its original IP and transfer it to br0.
  • Virtual Machine ConfigurationWhen creating a virtual machine with virt-install, specify –network bridge=br0, or modify the XML configuration of an existing virtual machine to set the network interface type to bridge mode.
  • Validation TestInside the virtual machine, use ip addr to confirm that it has obtained an IP address in the same subnet as the host and can successfully access the external network.

5.2 Experiment ResultsThe virtual machine successfully connected to the physical network, with a communication delay of less than 1ms and performance close to that of a physical machine.5.3 Issues and SolutionsAfter the initial configuration, the virtual machine could not access the internet because the host did not correctly assign an IP to br0. This was resolved by dynamically obtaining an IP through dhclient br0.The communication delay between virtual machines was high, but performance significantly improved after disabling the STP protocol on the bridge.5.4 ConclusionBridge mode is suitable for scenarios where virtual machines need to be directly exposed to the physical network (such as server deployment), but attention must be paid to IP management and network security policies.Building KVM Virtualization Network: Bridge ModeEND

Author of this article: Zhang Shuai(Shanghai Xinjuzhongbei Team)

Source of this article: “IT那活儿” public account

Building KVM Virtualization Network: Bridge Mode

Leave a Comment