0%

KVM Live Migration 筆記

Demo影片

https://www.youtube.com/watch?v=lACHT3BaQz8

Environment

  • 兩台 Ubuntu 15.04 安裝KVM、vmbuilder、virt-manager
  • IP
    1. X.X.X.X
    2. Y.Y.Y.Y

NFS

Server(X.X.X.X)

安裝
sudo apt install nfs-kernel-server nfs-common
分享目錄要權限
sudo chmod -R 777 /var/lib/libvirt/images

/etc/exports
1
/var/lib/libvirt/images Y.Y.Y.Y(rw,sync,no_root_squash,no_all_squash)

sudo systemctl nfs-kernel-server restart
sudo systemctl nfs-kernel-server status

Client(Y.Y.Y.Y)

sudo apt install nfs-common
sudo mount -t nfs X.X.X.X:/var/lib/libvirt/images /var/lib/libvirt/images

GUI

sudo virt-manager

Virsh

列出正在執行
sudo virsh list
列出包含關機的
sudo virsh list –all
列出所有snapshot
sudo virsh snapshot-list vm1
刪除snapshot
sudo virsh snapshot-delete vm1 [number]
列出遠端的VM
sudo virsh -c qemu+ssh://Y.Y.Y.Y/system list –all
Migration VM
sudo virsh migrate vm1 qemu+ssh://root@Y.Y.Y.Y/system
sudo virsh migrate –live vm1 qemu+ssh://root@Y.Y.Y.Y/system

DNS

sudo vim /etc/hosts

Y.Y.Y.Y HOSTNAME

SSH(不一定要加)

vim .ssh/authorized_keys
sudo systemctl restart sshd

Code Sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python
import libvirt
import sys
import time
conn = libvirt.open("qemu:///system")
dconn = libvirt.open("qemu+ssh://Y.Y.Y.Y/system")
while True:
for id in conn.listDomainsID():
vm = conn.lookupByID(id)
# dir(vm) 會列出所有指令
#print 'vm.name:', vm.name()
#print vm.info()[0] # 1 start, 3 suspend
#print "vm.isActive():", vm.isActive()
print "The Program will run every second..."
print vm.name(), "is active now"
if vm.info()[0] == 3:
print vm.name(), "is suspend now .. start migration"
new_dom = vm.migrate(dconn, 0, None, None, 0)
if new_dom == None:
print vm.name() + " is fail to migrate!"
break;
else:
new_dom.resume()
print vm.name() + " was migrated successfully!"
break;
time.sleep(1)
# for test
#i = raw_input("Press Enter To Stop")
#if not i:
#break
dconn.close()
conn.close()