KVM VMs with public ip addresses and VLANs

3 min read 27-10-2024
KVM VMs with public ip addresses and VLANs

In the world of virtualization, KVM (Kernel-based Virtual Machine) has emerged as a popular choice for creating and managing virtual machines (VMs). One common requirement for many organizations is to assign public IP addresses to these VMs while also managing their network configuration via Virtual LANs (VLANs). This article explores how to effectively set up KVM VMs with public IP addresses and VLANs.

Problem Scenario

Many organizations need to deploy KVM virtual machines that require access to public IP addresses, while also ensuring that the network infrastructure is efficient and organized through VLANs. Here’s a basic example of the initial code snippet one might use to define a KVM VM:

# Create a VM with a public IP address
virt-install --name myvm \
  --ram 2048 \
  --disk path=/var/lib/libvirt/images/myvm.img,size=10 \
  --vcpus 2 \
  --os-type linux \
  --os-variant ubuntu20.04 \
  --network network=default,model=virtio \
  --graphics none \
  --location 'http://cloud-images.ubuntu.com/releases/focal/release/' \
  --extra-args 'console=ttyS0'

While the code above creates a basic KVM VM, it lacks the configuration necessary for public IP assignment and VLAN setup.

Assigning Public IP Addresses

To assign a public IP address to a KVM VM, you must ensure that the VM is connected to a bridge network that is configured to support public IP addresses. Here’s how you can enhance the original script to include a public IP assignment and VLAN configuration:

  1. Create a Bridge Interface: First, create a bridge interface on your host machine that connects to your network.

    brctl addbr br0
    brctl addif br0 eth0
    ifconfig eth0 0.0.0.0
    ifconfig br0 up
    
  2. Modify VM Creation Command: Now, modify the VM creation command to specify the bridge network and assign a public IP.

# Create a VM with a public IP address on VLAN
virt-install --name myvm \
  --ram 2048 \
  --disk path=/var/lib/libvirt/images/myvm.img,size=10 \
  --vcpus 2 \
  --os-type linux \
  --os-variant ubuntu20.04 \
  --network bridge=br0,model=virtio \
  --graphics none \
  --location 'http://cloud-images.ubuntu.com/releases/focal/release/' \
  --extra-args 'console=ttyS0' \
  --network vlan=100,model=virtio

In this command, bridge=br0 links your VM to the bridge interface you created, and vlan=100 ensures that traffic from the VM is tagged with VLAN ID 100.

Understanding VLANs and Their Importance

VLANs are crucial for segmenting network traffic, which enhances security and improves performance. By deploying VMs on different VLANs, organizations can isolate sensitive data traffic from general traffic. For instance, in a scenario where you run multiple applications, you can assign one VLAN for your web server traffic, another for database traffic, and yet another for application servers.

Practical Example of Configuration

Let's consider a practical setup in which you have two KVM VMs. VM1 is assigned a public IP of 192.0.2.10, and it resides in VLAN 100 for web servers, while VM2 has a public IP of 192.0.2.20, located in VLAN 200 for database servers. Your virt-install commands for these two VMs might look like this:

# Create web server VM
virt-install --name webserver \
  --ram 2048 \
  --disk path=/var/lib/libvirt/images/webserver.img,size=10 \
  --vcpus 2 \
  --os-type linux \
  --os-variant ubuntu20.04 \
  --network bridge=br0,model=virtio \
  --network vlan=100,model=virtio \
  --graphics none \
  --location 'http://cloud-images.ubuntu.com/releases/focal/release/' \
  --extra-args 'console=ttyS0'

# Create database server VM
virt-install --name dbserver \
  --ram 2048 \
  --disk path=/var/lib/libvirt/images/dbserver.img,size=10 \
  --vcpus 2 \
  --os-type linux \
  --os-variant ubuntu20.04 \
  --network bridge=br0,model=virtio \
  --network vlan=200,model=virtio \
  --graphics none \
  --location 'http://cloud-images.ubuntu.com/releases/focal/release/' \
  --extra-args 'console=ttyS0'

Conclusion

Setting up KVM VMs with public IP addresses and VLANs is essential for creating an efficient and secure virtualized environment. By carefully configuring bridge networks and VLANs, organizations can achieve a robust infrastructure capable of supporting diverse applications and services.

Additional Resources

This guide is designed to be useful for IT administrators looking to enhance their KVM setups. With the right configurations, they can ensure that their VMs not only operate efficiently but also maintain security and network integrity.