You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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.

1. Creating Virtual IPs & Ports in VirtualBox

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.yaml for Ubuntu, or /etc/sysconfig/network-scripts/ifcfg-enp0s8 for 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"
    
2. Simulating AWS VPC/OpenStack-like Networks in VirtualBox

To replicate a VPC-style isolated network with subnets and routing:

  1. Create isolated subnets: Use VirtualBox's Internal Network feature to create separate networks (e.g., subnet-1 and subnet-2). Assign VMs to these networks based on their "subnet" role.
  2. 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
    
  3. 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
    
  4. Add firewall rules: Use iptables on the router VM to simulate security groups—allowing/blocking traffic between subnets as needed.
3. IPVS-based Load Balancing Across VMs

To set up IPVS for load balancing your virtual IP across multiple backend VMs:

  1. Install ipvsadm on your load balancer VM:
    sudo apt install ipvsadm  # Debian/Ubuntu
    sudo dnf install ipvsadm  # RHEL/CentOS
    
  2. Create a virtual service (replace the VIP and port with your own):
    ipvsadm -A -t 192.168.56.200:80 -s rr
    
    The -s rr flag uses round-robin scheduling—other options include wrr (weighted round-robin) or lc (least connections).
  3. 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
    
  4. Configure backend routing: Set each backend VM's default gateway to the load balancer's IP so return traffic flows back through it.
4. Your Alternative Solutions Explained

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:02:55