在Linux上配置高可用(HA)虚拟化环境通常涉及以下几个关键组件: - 虚拟化平台(KVM/QEMU) - 集群管理工具(如Pacemaker/Corosync) - 共享存储(如iSCSI/NFS/DRBD) - 网络冗余配置
# 在所有节点上安装必要软件包(以Ubuntu为例)
sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager \
corosync pacemaker pcs fence-agents drbd-utils
# 在存储服务器上
sudo apt install -y targetcli-fb
sudo targetcli
/> backstores/block create name=shared_storage dev=/dev/sdb
/> iscsi/ create iqn.2023-08.example.com:storage
/> iscsi/iqn.2023-08.example.com:storage/tpg1/luns/ create /backstores/block/shared_storage
/> iscsi/iqn.2023-08.example.com:storage/tpg1/acls/ create iqn.2023-08.example.com:node1
/> iscsi/iqn.2023-08.example.com:storage/tpg1/acls/ create iqn.2023-08.example.com:node2
# 在两节点上配置
sudo modprobe drbd
sudo vim /etc/drbd.d/r0.res
resource r0 {
protocol C;
on node1 {
device /dev/drbd0;
disk /dev/sdb;
address 192.168.1.101:7788;
meta-disk internal;
}
on node2 {
device /dev/drbd0;
disk /dev/sdb;
address 192.168.1.102:7788;
meta-disk internal;
}
}
# 初始化并启动DRBD
sudo drbdadm create-md r0
sudo drbdadm up r0
# 在所有节点上设置hacluster密码
sudo passwd hacluster
# 在一个节点上初始化集群
sudo pcs cluster auth node1 node2 -u hacluster -p yourpassword
sudo pcs cluster setup --name ha_cluster node1 node2
sudo pcs cluster start --all
sudo pcs cluster enable --all
# 配置集群属性
sudo pcs property set stonith-enabled=false # 生产环境应配置STONITH
sudo pcs property set no-quorum-policy=ignore
# 创建虚拟机的XML定义文件
sudo virsh dumpxml vm1 > /shared_storage/vm1.xml
# 配置Pacemaker资源
sudo pcs resource create vm1 ocf:heartbeat:VirtualDomain \
config=/shared_storage/vm1.xml \
hypervisor="qemu:///system" \
migration_transport=ssh \
op monitor interval=30s \
op start timeout=120s \
op stop timeout=120s
# 设置资源约束(确保资源只在活动节点运行)
sudo pcs constraint location vm1 prefers node1=100 node2=50
# 创建绑定接口
sudo nmcli con add type bond con-name bond0 ifname bond0 mode active-backup
sudo nmcli con add type ethernet con-name bond0-slave1 ifname eth1 master bond0
sudo nmcli con add type ethernet con-name bond0-slave2 ifname eth2 master bond0
# 为KVM创建桥接
sudo nmcli con add type bridge con-name br0 ifname br0
sudo nmcli con add type bridge-slave con-name br0-slave ifname bond0 master br0
# 手动迁移虚拟机测试
sudo pcs resource move vm1 node2
# 模拟节点故障
sudo pcs cluster stop node1
# 检查资源状态
sudo pcs status
/var/log/messages
和/var/log/cluster/corosync.log
通过以上配置,您可以在Linux上建立一个高可用的虚拟化环境,确保虚拟机在节点故障时能够自动迁移到健康节点继续运行。