環境準備:
2台機器防火牆全部關閉,禁用selinux
kvm01 | kvm02 |
---|---|
192.168.1.150 | 192.168.1.151 |
冷遷移(靜态遷移)
冷遷移和克隆差不多,都是需要對磁盤檔案和xml配置檔案進行操作
scp /etc/libvirt/qemu/test01.xml 192.168.1.151:/etc/libvirt/qemu/
scp /kvm-vm/test01.raw 192.168.1.151:/kvm-vm/
例(冷遷移):
[[email protected] ~]# virsh list --all
Id Name State
----------------------------------------------------
- test01 shut off
[[email protected] ~]# virsh list --all
Id Name State
----------------------------------------------------
[[email protected] ~]# virsh start test01 #啟動test01
[[email protected] ~]# echo 1111111111111111111 > test.txt #建立test.txt檔案,用來驗證
[[email protected] ~]# scp /etc/libvirt/qemu/test01.xml 192.168.1.151:/etc/libvirt/qemu/
[[email protected] ~]# scp /kvm-vm/test01.raw 192.168.1.151:/kvm-vm/
[[email protected] ~]# ls /kvm-vm/
test01.raw
[[email protected] ~]# ls /etc/libvirt/qemu/
networks test01.xml
[[email protected] ~]# cd /etc/libvirt/qemu/
[[email protected] qemu]# virsh define test01.xml
[[email protected] qemu]# virsh start test01
[[email protected] ~]# cat test.txt #驗證剛剛在kvm01上建立的test.txt檔案
1111111111111111111
熱遷移(動态遷移)
實驗環境:
(注意更改兩端主機名,否則會報主機名沖突),關閉防火牆以及禁用selinux
kvm01 | kvm02 | NFS |
---|---|---|
192.168.1.150 | 192.168.1.151 | 192.168.1.128 |
1、NFS伺服器釋出共享目錄
[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# mkdir /kvmshare
[[email protected] ~]# cat /etc/exports
/kvmshare *(rw,sync,no_root_squash)
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl enable rpcbind
[[email protected] ~]# systemctl start nfs-server
[[email protected] ~]# systemctl enable nfs-server
[[email protected] ~]# showmount -e
Export list for nfs:
/kvmshare *
PS:這裡也可以暫時先不挂載
kvm01檢視并挂載nfs共享
[[email protected] ~]# showmount -e 192.168.1.128
Export list for 192.168.1.128:
/kvmshare *
[[email protected] ~]# mkdir /kvm-share
[[email protected] ~]# mount -t nfs 192.168.1.128:/kvmshare /kvm-share/
[[email protected] ~]# mount | tail -1
192.168.1.128:/kvmshare on /kvm-share type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.150,local_lock=none,addr=192.168.1.128)
kvm02檢視并挂載nfs共享
[[email protected] qemu]# showmount -e 192.168.1.128
Export list for 192.168.1.128:
/kvmshare *
[[email protected] qemu]# mkdir /kvm-share
[[email protected] qemu]# mount -t nfs 192.168.1.128:/kvmshare /kvm-share/
[[email protected] qemu]# mount | tail -1
192.168.1.128:/kvmshare on /kvm-share type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.151,local_lock=none,addr=192.168.1.128)
驗證共享:
[[email protected] ~]# echo 111111122222222333333333 > /kvmshare/123.txt
[[email protected] ~]# cat /kvm-share/123.txt
111111122222222333333333
[[email protected] ~]# cat /kvm-share/123.txt
111111122222222333333333
2、在KVM01和KVM02上基于NFS服務建立存儲池
1)KVM01上基于NFS服務建立存儲池
2)在kvm02上操作,建立存儲池
這裡建立完成之後,會看到之前在nfs上建立的123.txt檔案。
3)在建立好的存儲池上建立存儲卷:
這裡建立完成之後,kvm02可以看到剛建立的test8存儲卷。
4、建立虛拟機
這裡在KVM01上,建立一個新的虛拟機域,需要注意,存儲池要切記使用 nfs-share(挂載到NFS伺服器上),保證虛拟機域安裝成功。(最小化) ……接下來最小化安裝完成即可
5、設定虛拟機網絡
虛拟機建立完成之後,配置虛拟機使用bridge橋接網路,使其能夠ping的通外網。并且在這裡我們執行一個ping百度的指令,并讓他保持一直是ping着的狀态,用來模拟遷移到kvm02上服務不中斷。當然如果虛拟機域遷移到第二台虛拟機扔向保持可用,第二台虛拟機也得支援橋接模式。
[[email protected] ~]# systemctl stop NetworkManager
[[email protected] ~]# virsh iface-bridge ens33 br0
Created bridge br0 with attached device ens33
Bridge interface br0 started
[[email protected] ~]# virsh destroy test8
[[email protected] ~]# virsh edit test8
<interface type='bridge'> #修改
<mac address='52:54:00:e7:79:75'/> #無須更改
<source bridge='br0'/> #修改
啟動進入虛拟機域,确認正确擷取IP位址,以及能連通外網
[[email protected] ~]# virsh start test8
[[email protected] ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=dhcp
ONBOOT=yes
[[email protected] ~]# systemctl restart network
[[email protected] ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:e7:79:75 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.153/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 1753sec preferred_lft 1753sec
inet6 fe80::c4bf:92d8:ce7e:a4f5/64 scope link
valid_lft forever preferred_lft forever
[[email protected] ~]# ping baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=29.8 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=29.9 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=37.7 ms
……
KVM02虛拟機設定橋接網絡
[[email protected] ~]# systemctl stop NetworkManager
[[email protected] ~]# virsh iface-bridge ens33 br0
Created bridge br0 with attached device ens33
Bridge interface br0 started
6、動态遷移
如果到這一步,沒有主動提示,那麼需要我們自己手動的下載下傳此安裝包
注意:遷移成功之後,IP位址不會變化,并且ping指令,不會終止
遷移虛拟機之前,把在kvm01和kvm02上挂載的目錄給一個777的權限,保證雙方root使用者都有權限去調用目錄。
接下來直接在virt-manager管理器中遷移就可以了,遷移完成之後,保證ping指令是不中斷的,就表示實驗完成了。