在OpenStack中为虚拟机配置VIP(虚拟IP)通常用于高可用性场景(如负载均衡、主备切换等)。以下是详细的实现步骤及注意事项:
OpenStack Neutron提供LBaaS(Load Balancer as a Service),可直接创建VIP并绑定后端实例。
1. 启用LBaaS插件
- 确保Neutron已启用octavia
(现代版本默认)或旧版neutron-lbaas
。
- 检查Neutron配置文件中是否有:
ini
service_plugins = octavia
创建负载均衡器
openstack loadbalancer create --name my-lb --vip-subnet-id <subnet_id>
添加监听器和后端池
# 创建后端池
openstack loadbalancer pool create --name my-pool --lb-algorithm ROUND_ROBIN --protocol HTTP --loadbalancer my-lb
# 添加后端成员(虚拟机)
openstack loadbalancer member create --subnet-id <subnet_id> --address <vm1_ip> --protocol-port 80 my-pool
openstack loadbalancer member create --subnet-id <subnet_id> --address <vm2_ip> --protocol-port 80 my-pool
# 创建监听器
openstack loadbalancer listener create --name my-listener --protocol HTTP --protocol-port 80 my-lb
绑定浮动IP(可选)
openstack floating ip set --port <lb_vip_port_id> <floating_ip>
若需更灵活控制(如非HTTP协议),可在虚拟机内手动部署Keepalived。
1. 在虚拟机上安装Keepalived
bash
apt-get install keepalived # Debian/Ubuntu
yum install keepalived # CentOS/RHEL
配置Keepalived主节点(Master)
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
<VIP>/24 dev eth0
}
}
配置Keepalived备节点(Backup)
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
<VIP>/24 dev eth0
}
}
允许VRRP协议
112
)和组播流量。网络配置
--shared
)或路由。安全组规则
bash
openstack security group rule create --protocol vrrp --remote-ip 0.0.0.0/0 <sec_group>
ARP问题
proxy_arp
(Linux虚拟机):
bash
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
高可用性
--healthmonitor
)自动剔除故障节点。LBaaS验证
openstack loadbalancer show my-lb # 检查状态是否为ACTIVE
curl http://<VIP> # 测试流量分发
Keepalived验证
bash
ip addr show eth0 | grep <VIP>
virtual_router_id
唯一,网络延迟低。/var/log/octavia/octavia-worker.log
。通过上述步骤,您可以在OpenStack中高效配置VIP,并根据需求选择LBaaS或Keepalived方案。建议生产环境优先使用LBaaS(Octavia),以获得OpenStack原生支持。