在KVM中,您可以將運(yùn)行中的虛擬機(jī)從一個KVM主機(jī)遷移到另一個KVM主機(jī),而不需要任何停機(jī)時間。如果兩個KVM主機(jī)都能夠訪問相同的存儲池,那么實(shí)時遷移工作就很好了。要使存儲池在KVM主機(jī)上都可訪問,您需要使用NFS或GFS2文件系統(tǒng)(集群文件系統(tǒng))。在本例中,我使用NFS文件系統(tǒng)來存儲VM映像。在遷移過程中,VM的“內(nèi)存”內(nèi)容將被復(fù)制到目標(biāo)KVM主機(jī),而在某個時間點(diǎn)上,將發(fā)生遷移VM。注意,當(dāng)您在KVM主機(jī)上共享文件系統(tǒng)時,將不會在網(wǎng)絡(luò)上復(fù)制VM的磁盤映像,因?yàn)閮蓚€KVM主機(jī)都可以訪問相同的存儲池。
migrate.png
虛擬機(jī)熱遷移演示
- 為kvm-node2安裝kvm軟件及手動配置網(wǎng)橋br0
- 關(guān)閉kvm-node2的防火墻
[root@kvm-node2 ~]# iptables -F
[root@kvm-node2 ~]# systemctl stop firewalld.service
- 登錄到kvm-node1上檢查vm運(yùn)行情況
[root@kvm-node1 ~]# virsh list
Id Name State
----------------------------------------------------
3 test running
- 打通kvm-node1到kvm-node2的ssh key通道
[root@kvm-node1 ~]# ssh-copy-id 192.168.57.202
The authenticity of host '192.168.57.202 (192.168.57.202)' can't be established.
ECDSA key fingerprint is cf:b5:cd:88:0c:05:fb:9b:b9:46:b0:c0:c4:38:cf:c1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.57.202's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.57.202'"
and check to make sure that only the key(s) you wanted were added.
[root@kvm-node1 ~]# ssh 192.168.57.202
Last login: Wed Jun 14 16:28:06 2017 from 192.168.57.1
[root@kvm-node2 ~]# exit
logout
- 登錄到vm實(shí)例“test”上,長ping“kvm-node1”,注意隨時觀察網(wǎng)絡(luò)是否有中斷。
[root@kvm-node1 ~]# virsh console test
Connected to domain test
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-514.el7.x86_64 on an x86_64
localhost login: root
Password:
Last login: Wed Jun 14 17:06:02 on ttyS0
[root@localhost ~]# uptime
17:07:12 up 2 min, 1 user, load average: 0.15, 0.13, 0.05
[root@localhost ~]# ping 192.168.57.201
PING 192.168.57.201 (192.168.57.201) 56(84) bytes of data.
64 bytes from 192.168.57.201: icmp_seq=1 ttl=64 time=0.269 ms
64 bytes from 192.168.57.201: icmp_seq=2 ttl=64 time=0.308 ms
64 bytes from 192.168.57.201: icmp_seq=3 ttl=64 time=0.788 ms
- 開始從KVM-node1上遷移vm機(jī)到KVM-node2上:
[root@kvm-node1 ~]# virsh migrate test qemu+ssh://root@192.168.57.202/system
error: Unsafe migration: Migration may lead to data corruption if disks use cache != none
- (⊙o⊙)哦,看起來我們沒發(fā)遷移vm,從報錯中我們很輕易的得知,熱遷移必須配置虛擬機(jī)的磁盤使用緩存,現(xiàn)在我們來配置這個選項:
[root@kvm-node1 ~]# virsh edit test
Domain test XML configuration edited.
############################################添加cache='none'
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/var/lib/libvirt/images/test.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
############################################
- 啟動虛擬機(jī)后,重復(fù)步驟5,注意觀察vm是否有業(yè)務(wù)中斷
- 我們再來嘗試一下遷移操作,沒有輸出,O(∩_∩)O~ 通常沒有結(jié)果就是最好的結(jié)果。
[root@kvm-node1 ~]# virsh migrate test qemu+ssh://root@192.168.57.202/system
- 下面我們登錄到kvm-node2上檢查一下虛擬機(jī)遷移是否成功
[root@kvm-node2 ~]# virsh list
Id Name State
----------------------------------------------------
3 test running
- 在整個遷移過程中,我們可以觀察到對虛擬機(jī)(vm)上跑的業(yè)務(wù)基本沒喲影響,我們長ping的進(jìn)程僅僅只有輕微額網(wǎng)絡(luò)抖動。當(dāng)然KVM熱遷移只適合遷移業(yè)務(wù)不繁忙的虛擬機(jī),如果業(yè)務(wù)過于繁忙,熱遷移可能由于數(shù)據(jù)同步無法達(dá)到讀寫一致而超時失敗。
[root@localhost ~]# ping 192.168.57.201
PING 192.168.57.201 (192.168.57.201) 56(84) bytes of data.
64 bytes from 192.168.57.201: icmp_seq=1 ttl=64 time=0.457 ms
64 bytes from 192.168.57.201: icmp_seq=2 ttl=64 time=0.928 ms
64 bytes from 192.168.57.201: icmp_seq=3 ttl=64 time=0.795 ms
64 bytes from 192.168.57.201: icmp_seq=4 ttl=64 time=0.683 ms
64 bytes from 192.168.57.201: icmp_seq=5 ttl=64 time=0.374 ms
64 bytes from 192.168.57.201: icmp_seq=6 ttl=64 time=0.584 ms
64 bytes from 192.168.57.201: icmp_seq=7 ttl=64 time=0.253 ms
64 bytes from 192.168.57.201: icmp_seq=8 ttl=64 time=0.667 ms
64 bytes from 192.168.57.201: icmp_seq=9 ttl=64 time=0.297 ms
64 bytes from 192.168.57.201: icmp_seq=10 ttl=64 time=0.268 ms
64 bytes from 192.168.57.201: icmp_seq=11 ttl=64 time=0.757 ms
64 bytes from 192.168.57.201: icmp_seq=12 ttl=64 time=0.193 ms
64 bytes from 192.168.57.201: icmp_seq=13 ttl=64 time=0.234 ms
64 bytes from 192.168.57.201: icmp_seq=14 ttl=64 time=0.298 ms
64 bytes from 192.168.57.201: icmp_seq=15 ttl=64 time=0.289 ms
64 bytes from 192.168.57.201: icmp_seq=16 ttl=64 time=0.171 ms
64 bytes from 192.168.57.201: icmp_seq=17 ttl=64 time=0.754 ms
64 bytes from 192.168.57.201: icmp_seq=18 ttl=64 time=0.731 ms
64 bytes from 192.168.57.201: icmp_seq=19 ttl=64 time=0.371 ms
64 bytes from 192.168.57.201: icmp_seq=20 ttl=64 time=2.05 ms # 輕微的網(wǎng)絡(luò)抖動
64 bytes from 192.168.57.201: icmp_seq=21 ttl=64 time=0.359 ms
64 bytes from 192.168.57.201: icmp_seq=22 ttl=64 time=0.662 ms
64 bytes from 192.168.57.201: icmp_seq=23 ttl=64 time=0.555 ms