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