05-KVM熱添加硬盤、網卡、內存、CPU及熱遷移

1、KVM熱添加硬盤

一、爲虛擬機web02添加一塊5G磁盤node

[root@kvm opt]# qemu-img create -f qcow2 web02-add01.qcow2 5G  #爲web02建立一塊磁盤
Formatting 'web02-add01.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@kvm opt]# virsh attach-disk web02 /opt/web02-add01.qcow2  vdb  --subdriver=qcow2 --config  #永久爲web02添加一塊磁盤
Disk attached successfully
[root@web02 ~]# mkfs.xfs /dev/vdb  #在web02虛擬機內格式化磁盤
meta-data=/dev/vdb               isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@web02 ~]# mount /dev/vdb /mnt  #掛載磁盤
[root@web02 ~]# df -h  #查看磁盤容量,會多一個5G磁盤
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  1.1G  9.0G  11% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  6.7M  490M   2% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb        5.0G   33M  5.0G   1% /mnt

-f #指定磁盤格式,默認爲raw格式
–config #永久添加磁盤
–subdriver #指定格式linux

在這裏插入圖片描述
在這裏插入圖片描述
二、擴張當前磁盤容量web

[root@web02 ~]# umount /mnt  #在虛擬機web02取消掛載
[root@kvm opt]# virsh detach-disk web02 vdb  #在kmv虛擬機剝離磁盤
Disk detached successfully     
[root@kvm opt]# qemu-img resize web02-add01.qcow2 10G  #爲web02擴容到10G
Image resized.
[root@kvm opt]# qemu-img info web02-add01.qcow2   #查看web02虛擬磁盤容量
image: web02-add01.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 27M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@kvm opt]# virsh attach-disk web02 /opt/web02-add01.qcow2 vdb --subdriver=qcow2  #永久爲web02添加磁盤
Disk attached successfully
[root@web02 ~]#  mount /dev/vdb /mnt   #掛載磁盤
[root@web02 ~]# df -h  #查看磁盤容量仍是5G空間,須要更新分區
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  1.1G  9.0G  11% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  6.7M  490M   2% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb        5.0G   33M  5.0G   1% /mnt
[root@web02 ~]# xfs_growfs /mnt  #更新分區容量
meta-data=/dev/vdb               isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1310720 to 2621440
[root@web02 ~]# df -h  #再次查看分區,已經變爲10G
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  1.1G  9.0G  11% /
devtmpfs        486M     0  486M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  6.7M  490M   2% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb         10G   33M   10G   1% /mnt

在這裏插入圖片描述
在這裏插入圖片描述

2、KVM熱添加網卡

virsh attach-interface web02 --type bridge --source br0 --model virtio   #爲web02添加一塊網卡
detach-interface web02 --type bridge --mac 52:54:00:35:d3:71  #剝離web02上的網卡,須要查看虛擬機的mac地址

3、KVM熱添加內存

[root@kvm opt]# virsh setmem web06 1024M  #臨時添加內存,縮減內存一樣,須要注意的是不要
[root@kvm opt]# virsh setmem web06 2048M --config  #永久添加內存
[root@kvm opt]# virsh setmaxmem web02 2048M --config  #設置最大內存,須要關機

4、KVM熱添加CPU

kvm虛擬機在線熱添加cpu
[root@kvm opt]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 512,maxmemory=2048 --vcpus 1,maxvcpus=10 --disk /data/web04.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
熱添加cpu核數
[root@kvm opt]# virsh setvcpus web02 4 --live
永久添加cpu核數
[root@kvm opt]# virsh setvcpus web04 4 --config

5、KVM熱遷移

一、在kvm01和kvm02上安裝kvm和nfs,配置橋接網卡
yum install libvirt* virt-* qemu-kvm* nfs-utils openssh-askpass -y
systemctl start libvirtd.service
virsh iface-bridge eth0 br0
二、準備一臺NFS服務器
yum install nfs-utils -y
mkdir /data
vim /etc/exports
/data 172.16.1.0/24(rw,async,no_root_squash,no_all_squash)
systemctl restart rpcbind
systemctl restart nfs  
showmount -e 172.16.1.53  #查看本地共享目錄
Export list for 172.16.1.53:
/data 172.16.1.0/24
四、在kvm01和kvm02掛載nfs共享目錄
[root@kvm01 ~]# mount -t nfs 172.16.1.53:/share /opt
[root@kvm02 ~]# mount -t nfs 172.16.1.53:/share /opt
五、安裝一臺基於橋接模式的虛擬機
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web02 --memory 512,maxmemory=2048 --vcpus 1 --disk /data/web02.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
六、熱遷移的命令:
virsh migrate --live --verbose web02 qemu+ssh://172.16.1.52/system --unsafe
將宿主機172.16.1.51上的kvm虛擬機kvm02遷移到172.16.1.52
七、在kvm01上安裝圖形界面、vnc服務端和virt-manager
yum groups install "GNOME Desktop" -y
yum install tigervnc-server.x86_64 -y
yum install virt-manager -y
八、啓動vnc服務端
vncserver :1 啓動5901端口的vnc服務端
vncserver -kill :1 關閉5901端口的vnc服務端
九、使用vnc鏈接宿主機,使用virt-manager進行遷移

在這裏插入圖片描述
**加粗樣式
**vim

6、ESXI遷移到KVM

  1. 關閉esxi須要導出的虛擬機;
  2. 將此虛擬機導出ova文件;
  3. 將虛擬機ova文件轉換爲qcow2+配置文件;
  4. define導入到kvm虛擬機
yum install libvirt* virt-* qemu-kvm* -y
virsh iface-bridge eth1 br1
systemctl start libvirtd.service
systemctl enable libvirtd.service
virt-v2v -i ova web01.ova -o local -os /opt/web02 -of qcow2 
virsh  define web02.xml 
virsh list --all