如何在VirtualBox中创建虚拟IP/端口并模拟SDN网络实现IPVS负载均衡?
Hey there, let's break down your questions step by step—covering VirtualBox virtual IP/port setup, simulating SDN-like networks, IPVS load balancing, and your proposed alternative solutions.
Virtual IPs
VirtualBox doesn't natively assign virtual IPs to VMs, but you can configure them directly inside your guest OS:
- First, set up a Host-Only or Internal Network in VirtualBox (these let VMs communicate with each other and/or the host without exposing them to the public internet).
- Inside the guest VM, add an extra IP address to your network interface using this command (adjust the IP and interface name to match your setup):
ip addr add 192.168.56.100/24 dev enp0s8 - To make this persistent, add the IP to your network config file (e.g.,
/etc/netplan/00-installer-config.yamlfor Ubuntu, or/etc/sysconfig/network-scripts/ifcfg-enp0s8for RHEL-based systems).
Port Forwarding
If you need to route traffic from your host machine to a VM's port (or vice versa), use VirtualBox's NAT port forwarding:
- For GUI: Go to your VM's Settings > Network > Advanced > Port Forwarding. Add a rule (e.g., forward host port 8080 to guest port 80).
- For CLI: Use this command (replace
VM Name, port numbers, and protocol as needed):VBoxManage modifyvm "VM Name" --natpf1 "web-forward,tcp,,8080,,80"
To replicate a VPC-style isolated network with subnets and routing:
- Create isolated subnets: Use VirtualBox's Internal Network feature to create separate networks (e.g.,
subnet-1andsubnet-2). Assign VMs to these networks based on their "subnet" role. - Deploy a router VM: Set up a VM with two network adapters—one connected to each internal subnet. Enable IP forwarding on this router:
echo 1 > /proc/sys/net/ipv4/ip_forward - Configure routing: Add static routes on each VM to point to the router's IP for cross-subnet traffic. For example, on a VM in
subnet-1:ip route add 192.168.60.0/24 via 192.168.56.1 dev enp0s8 - Add firewall rules: Use
iptableson the router VM to simulate security groups—allowing/blocking traffic between subnets as needed.
To set up IPVS for load balancing your virtual IP across multiple backend VMs:
- Install ipvsadm on your load balancer VM:
sudo apt install ipvsadm # Debian/Ubuntu sudo dnf install ipvsadm # RHEL/CentOS - Create a virtual service (replace the VIP and port with your own):
Theipvsadm -A -t 192.168.56.200:80 -s rr-s rrflag uses round-robin scheduling—other options includewrr(weighted round-robin) orlc(least connections). - Add backend servers:
# Add server 1 (NAT mode) ipvsadm -a -t 192.168.56.200:80 -r 192.168.56.101:80 -m # Add server 2 ipvsadm -a -t 192.168.56.200:80 -r 192.168.56.102:80 -m - Configure backend routing: Set each backend VM's default gateway to the load balancer's IP so return traffic flows back through it.
HAProxy Lightweight VM
HAProxy is a great user-friendly alternative to IPVS for load balancing:
- Install HAProxy on a lightweight VM, then edit its config file (
/etc/haproxy/haproxy.cfg) with a frontend/backend setup:frontend http_front bind 192.168.56.200:80 default_backend http_back backend http_back balance roundrobin server web1 192.168.56.101:80 check server web2 192.168.56.102:80 check - Restart HAProxy:
sudo systemctl restart haproxy - This setup is easier to configure and maintain than IPVS, plus it adds built-in health checks for backend servers.
Iptables-based Percentage Routing VM
If you want simple percentage-based routing without extra software, use iptables's statistic module:
- Run these commands on your routing VM to route 30% of traffic to server 1 and 70% to server 2:
# 30% of HTTP traffic to server 1 iptables -A PREROUTING -t nat -d 192.168.56.200 --dport 80 -m statistic --mode random --probability 0.3 -j DNAT --to-destination 192.168.56.101:80 # Remaining 70% to server 2 iptables -A PREROUTING -t nat -d 192.168.56.200 --dport 80 -j DNAT --to-destination 192.168.56.102:80 - This is a lightweight option but lacks advanced features like health checks or dynamic scheduling adjustments.
内容的提问来源于stack exchange,提问作者jayunit100

