KVM + OVS + libvirt + iSCSI 热迁移
试验环境
2台物理主机 ubuntu 14.04 Server x64 ubuntu110 192.168.85.110 ubuntu145 192.168.85.145 1台中心存储 iSCSI CentOS 6.5 Server x64 [192.168.85.195]物理主机—基础环境配置
#更新apt source apt-get update#安装iSCSI工具 apt-get install -y open-iscsi
#安装lvm工具 apt-get install -y lvm2
#安装KVM,libvirt apt-get install -y qemu-kvm libvirt-bin virt-manager bridge-utils virt-viewer
#配置ubuntu110 host vim /etc/hosts 127.0.0.1 ubuntu110 192.168.85.145 ubuntu145
#配置ubuntu145 host vim /etc/hosts 127.0.0.1 ubuntu145 192.168.85.145 ubuntu110
#配置libvirt hostuuid #默认libvirt的host_uuid为0,而使用dmidecode -s system-uuid去生成uuid #但是dmidecode并不能生成一个唯一有效的uuid,所以我们需要手动生成一个有效唯一的uuid #要不然后面热迁移虚拟机的时候,会提升相同的host主机不能热迁移 #具体参考 https://alteeve.ca/w/2-Node_Red_Hat_KVM_Cluster_Tutorial-_Troubleshooting
mv /usr/sbin/dmidecode /usr/sbin/dmidecode.orig wget -c https://alteeve.ca/files/dmidecode -O /usr/sbin/dmidecode chmod 755 /usr/sbin/dmidecode dmidecode -q -t 0,1,4,17 | grep UUID
#把得到的uuid写进 /etc/libvirt/libvirtd.conf 配置文件里面的host_uuid,然后重启libvirt服务 /etc/init.d/libvirt-bin reload
#检查libvirt uuid是否修改成功,运行 virsh sysinfo | grep uuid
物理主机—安装OVS
#安装依赖环境 apt-get install -y build-essential fakeroot debhelper \ autoconf automake bzip2 libssl-dev \ openssl graphviz python-all procps \ python-qt4 python-zopeinterface \ python-twisted-conch libtool#下载ovs源码包 wget http://openvswitch.org/releases/openvswitch-2.3.1.tar.gz tar zxf openvswitch-2.3.1.tar.gz cd openvswitch-2.3.1/
#可以用dpkg-checkbuilddeps检查下是否依赖包已经安装完毕 dpkg-checkbuilddeps
#编译构建deb包,生成的deb包会放在上一级目录 DEB_BUILD_OPTIONS=’parallel=8 nocheck’ fakeroot debian/rules binary
#返回上级目录,安装ovs dpkg -i openvswitch-common_2.3.1-1_amd64.deb openvswitch-switch_2.3.1-1_amd64.deb
#检查ovs是否成功安装运行 lsmod | grep open
物理主机—OVS网络配置
#创建网桥br0 ovs-vsctl add-br br0 #把物理机网卡1接入网桥br0,网络会断开 #可以为服务器接双网卡,em1作为管理接口,em2作为ovs桥 ovs-vsctl add-port br0 em1 ifconfig em1 0.0.0.0 up #把IP配在网桥br0上 ifconfig br0 192.168.85.xxx netmask 255.255.255.0 upiSCSI存储—配置
#查看tgt配置文件 /etc/tgt/targets.conf tgt-admin -p —dump
1 2 3 4 5 6 7 8 9 10 11 |
default-driver iscsi |
iSCSI存储—创建虚拟卷
#在逻辑卷vgxxx下面创建一个20G大小的虚拟空间作为虚拟机的虚拟磁盘 lvcreate -L 20G vgxxx物理主机—iSCSI配置
#登录到iSCSI存储 iscsiadm -m node -T iqn.2015-02.cc.efly:yanfa.xenstorage1 -p 192.168.85.195:3260 —login物理主机—使用virt-manager配置环境(ubuntu145主机执行一下操作)
#执行virt-manager virt-manager#添加iSCSI存储资源池,右键localhost本地的qemu,然后选择Details
virt-manager
#选择Storage页面,然后点击左下角的+按钮添加存储池
virt-manager
#填写好存储池的名称,选择存储池类型为iSCSI,最后点击Forward
#最后通过连接之后点击Finish即可完成添加存储池
virt-manager
#使用命令行工具virsh,我们可以查看刚才图形界面操作所对应的xml配置文件内容 #执行virsh,进入libvirt的命令行模式 virsh
#执行pool-list查看刚才添加成功的存储池iscsi195 pool-list
1 2 3 4 5 |
virsh # pool-list Name State Autostart ------------------------------------------- default active yes iscsi195 active yes |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<pool type='iscsi'> <name>iscsi195</name> <uuid>c8973a3d-f5b9-d141-6b5f-72aaf07d3f17</uuid> <capacity unit='bytes'>1099522497024</capacity> <allocation unit='bytes'>1099522497024</allocation> <available unit='bytes'>0</available> <source> <host name='192.168.85.195' port='3260'/> <device path='iqn.2015-02.cc.efly:yanfa.xenstorage1'/> </source> <target> <path>/dev/disk/by-path</path> <permissions> <mode>0711</mode> <owner>-1</owner> <group>-1</group> </permissions> </target> </pool> |

#输入相关参数如下图,然后点击connect,输入ubuntu110的密码后即可
virt-manager
#命令行连接到远端ubuntu110的virsh命令行模式,执行命令如下 virsh -c qemu+ssh://root@192.168.85.110/system
#输入密码之后提示连接成功
1 2 3 4 5 6 |
root@ubuntu145:~# virsh -c qemu+ssh://root@192.168.85.110/system list root@192.168.85.110's password: Id Name State ---------------------------------------------------- |
物理主机—配置虚拟机(ubuntu145主机执行一下操作)
#配置虚拟机配置xml文件 ubuntu01.xml #只需要在其中一台物理机配置即可,另外一台用于热迁移使用<domain type='kvm'>
<name>ubuntu01</name> //虚拟机名称
<memory>524288</memory> //最大内存512MB
<currentMemory>524288</currentMemory> //可用内存512MB
<vcpu>1</vcpu> //虚拟cpu个数
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='cdrom'/> //光盘启动
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='localtime'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<!--<emulator>/usr/libexec/qemu-kvm</emulator>-->
<disk type='block' device='disk'>
//这个cache=none很关键,如果这里不为none是无法进行热迁移的
<driver name='qemu' type='raw' cache='none'/>
//虚拟磁盘,这里为之前创建的LV
<source dev='/dev/server/lvol0'/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
//光盘镜像路径
<source file='/opt/iso/ubuntu-14.04.2-server-amd64.iso'/>
<target dev='hdb' bus='ide'/>
</disk>
<interface type='bridge'> //虚拟机网络连接方式
<source bridge='br0'/>
//为虚拟机分配mac地址,务必唯一,否则dhcp获得同样ip,引起冲突
//mac地址以54:52:00开头,是否必要没研究过
<mac address="54:52:00:aa:aa:01"/>
<virtualport type='openvswitch'></virtualport>
<model type='virtio'/>
</interface>
<input type='mouse' bus='ps2'/>
//vnc方式登录,端口号自动分配,自动加1
<graphics type='vnc' port='-1' autoport='yes' listen = '0.0.0.0' keymap='en-us'/>
</devices>
</domain>
#启动虚拟机 virsh start ubuntu01
#连接虚拟机vnc virt-viewer ubuntu01
#这个时候也可以启动virt-manager使用图形界面的方式来操作
#安装正常流程安装完虚拟机之后,我们测试热迁移,操作很简单只需要在virt-manager右击虚拟机选择migrate
virt-manager
#然后提示如下图,选择需要迁移的目标主机
virt-manager
#之后点击mrgrate按钮进行迁移
virt-manager
#使用virsh命令行的方式热迁移把ubuntu110的虚拟机重新迁移回去ubuntu145 #在ubuntu110的virsh模式下执行如下命令,输入ubuntu145密码后即可
1 2 3 4 |
virsh # migrate --live ubuntu01 qemu+ssh://192.168.85.145/system root@192.168.85.145's password: |

版权声明:本文为博主原创文章,未经博主允许不得转载。本文地址 http://blog.eflypro.com/2015/07/27/kvm-iscsi/