命令记不住

登陆

KVM 调整虚拟机硬件

KVM调整虚拟机硬件

生产环境中调整虚拟机硬件

生产环境中比较常见的虚拟机硬件调整就是CPU、内存、硬盘以及网卡.

1.虚拟机CPU调整

虚拟机CPU调整分为Linux以及Windows Server和Windows 7虚拟机,特别注意Windows 7操作系统虚拟机可能存在一些特殊的调整方式。

1.Linux虚拟机在线热添加cpu[--vcpus 1,maxcpus=10]
1.1 安装时设置最大支持cpu数

[root@KVM ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web05 --memory 512,maxmemory=2048 --vcpus 1,maxvcpus=10 --disk /data/web05.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

1.2 修改xml配置文件以支持热添加vcpu

<vcpu placement='static'>2</vcpu>                   //修改前

<vcpu plancement='static' current='4'>10</vcpu>     //修改后

1.3 热添加cpu核数

[root@Web_node01 ~]# setvcpus web05 4 --live
1.4 永久添加cpu核数

[root@Web_node01 ~]# setvcpus web05 4 --config
2.Linux和Window虚拟机CPU调整具体操作

(1) Linux虚拟机CPU调整
对于Linux虚拟机调整CPU数量实际就是修改虚拟机配置文件。

第1步,使用命令“virsh dominfo centos7-03”查看虚拟机硬件信息,虚拟机centos7-03目前CPU数量为1。

[root@kvm-centos7 ~]# virsh dominfo centos7-03
Id:             2
名称:       centos7-03
UUID:           6cf38c45-be49-4612-afe9-114aa203b3f0
OS 类型:    hvm
状态:       running
CPU:          1   #CPU数量为1
CPU 时间:   20.3s
最大内存: 2097152 KiB
使用的内存: 2097152 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: selinux
安全性 DOI: 0
安全性标签: system_u:system_r:svirt_t:s0:c404,c721 (enforcing)

第2步,修改虚拟机CPU数量需要关闭虚拟机电源,使用命令“virsh shutdown centos7-03”关闭虚拟机电源。

[root@kvm-centos7 ~]# virsh shutdown centos7-03
域 centos7-03 被关闭

第3步,使用命令“virsh edit centos7-03”修改虚拟机配置文件,将CPU数量调整为4。

[root@kvm-centos7 ~]# virsh edit centos7-03  
<domain type='kvm'>
<name>centos7-03</name>
<uuid>6cf38c45-be49-4612-afe9-114aa203b3f0</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>4</vcpu>  #调整CPU数量为4
<os>

第4步,使用命令“virsh start centos7-03”打开虚拟机电源。

[root@kvm-centos7 ~]# virsh start centos7-03   
域 centos7-03 已开始

第5步,使用命令“virsh dominfo centos7-03”查看虚拟机硬件信息,调整后虚拟机centos7-03 CPU数量为4。

[root@kvm-centos7 ~]# virsh dominfo centos7-03  
Id:             3
名称:       centos7-03
UUID:           6cf38c45-be49-4612-afe9-114aa203b3f0
OS 类型:    hvm
状态:       running
CPU:          4    #调整后CPU数量为4
CPU 时间:   18.2s
最大内存: 2097152 KiB
使用的内存: 2097152 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: selinux
安全性 DOI: 0
安全性标签: system_u:system_r:svirt_t:s0:c684,c911 (enforcing)

(2) Windows 2012虚拟机CPU调整
Windows 2012虚拟机CPU调整与Linux虚拟机相同,同样修改虚拟机配置文件。

第1步,使用命令“virsh dominfo win2012”查看虚拟机硬件信息,虚拟机win2012目前CPU数量为2。

root@kvm-server:/home/admin# virsh dominfo win2012
Id:             8
Name:           win2012
UUID:           1aaea0ca-d562-43dd-8c6b-6e67f4e3b806
OS Type:        hvm
State:          running
CPU(s):         2   #CPU数量为2
CPU time:       43402.9s
Max memory:     4194304 KiB
Used memory:    4194304 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: apparmor
Security DOI:   0
Security label: libvirt-1aaea0ca-d562-43dd-8c6b-6e67f4e3b806 (enforcing)

第2步,使用命令“virsh edit win2012”修改虚拟机配置文件,将CPU数量调整为4。

root@kvm-server:/home/admin# virsh edit win2012
<domain type='kvm'>
<name>win2012</name>
<uuid>1aaea0ca-d562-43dd-8c6b-6e67f4e3b806</uuid>
<memory unit='KiB'>4194304</memory> 
<currentMemory unit='KiB'>4194304</currentMemory>
<vcpu placement='static'>4</vcpu> #调整CPU数量为4
<os>

第3步,使用命令“virsh start win2012”打开虚拟机电源。

root@kvm-server:/home/admin# virsh start win2012   
Domain win2012 started

第4步,使用命令“virsh dominfo win2012”查看虚拟机硬件信息,调整后虚拟机win2012 CPU数量为4。

root@kvm-server:/home/admin# virsh dominfo win2012 
Id:             47
Name:           win2012
UUID:           1aaea0ca-d562-43dd-8c6b-6e67f4e3b806
OS Type:        hvm
State:          running
CPU(s):         4    #调整后CPU数量为4
CPU time:       2.8s
Max memory:     4194304 KiB
Used memory:    4194304 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: apparmor
Security DOI:   0
Security label: libvirt-1aaea0ca-d562-43dd-8c6b-6e67f4e3b806 (enforcing)

第5步,使用vnc viewer工具连接到虚拟机win2012,查看CPU数量为4个。

(3) Windows 7虚拟机CPU调整
Windows 7虚拟机调整CPU与 Windows 2012同样是修改虚拟机配置文件,但需要注意Windows 7操作系统2个以上CPU的调整后系统不能识别问题。

第1步,使用命令“virsh dominfo win7-02”查看虚拟机硬件信息,虚拟机win7-02目前CPU数量为2。

root@kvm-server:/home/admin# virsh dominfo win7-02       
Id: 49
Name: win7-02
UUID: 27e0e8ed-2c9c-4c3f-8b18-9a88790d8737
OS Type: hvm
State: running
CPU(s): 2
CPU time: 3.2s
Max memory: 4194304 KiB
Used memory: 4194304 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: apparmor
Security DOI: 0
Security label: libvirt-27e0e8ed-2c9c-4c3f-8b18-9a88790d8737 (enforcing)

第2步,使用命令“virsh edit win7-02”修改虚拟机配置文件,将CPU数量调整为4。

root@kvm-server:/home/admin# virsh edit win7-02

win7-02
27e0e8ed-2c9c-4c3f-8b18-9a88790d8737
4194304
4194304
4 #调整CPU数量为4

第3步,使用命令“virsh start win7-02”打开虚拟机电源。

root@kvm-server:/home/admin# virsh start win7-02   
Domain win7-02 started

第4步,使用命令“virsh dominfo win7-02”查看虚拟机硬件信息,调整后虚拟机win7-02 CPU数量为4。

root@kvm-server:/home/admin# virsh dominfo win7-02
Id:             50
Name:           win7-02
UUID:           27e0e8ed-2c9c-4c3f-8b18-9a88790d8737
OS Type:        hvm
State:          running
CPU(s):         4   #调整后CPU数量为4
CPU time:       3.1s
Max memory:     4194304 KiB
Used memory:    4194304 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: apparmor
Security DOI:   0
Security label: libvirt-27e0e8ed-2c9c-4c3f-8b18-9a88790d8737 (enforcing)

第5步,使用vnc viewer工具连接到虚拟机win7-02,查看CPU数量没有变化,依然为2个其原因是KVM缺省每个CPU模拟一个socket,必须修改虚拟机CPU的topology,才能使用多个CPU。

第6步,使用命令“virsh edit win7-02”修改虚拟机配置文件,注意数量匹配。

root@kvm-server:/home/admin# virsh dominfo win7-02
<domain type='kvm'>
<name>win7-02</name>
<uuid>27e0e8ed-2c9c-4c3f-8b18-9a88790d8737</uuid>
<memory unit='KiB'>4194304</memory>
<currentMemory unit='KiB'>4194304</currentMemory>
<vcpu placement='static'>4</vcpu>   #调整CPU数量为4
<os>
<cpu mode='custom' match='exact' check='partial'>
 <model fallback='allow'>Westmere</model>
 <topology sockets='1' cores='4' threads='1'/>  #修改topology sockets参数。
</cpu>  

第7步,关闭虚拟机电源后再使用vnc viewer工具连接到虚拟机win7-02,查看CPU数量为4个。

至此,Linux以及Windows虚拟机CPU调整完成,需要注意的是,硬件调整需要在虚拟机电源关闭情况下进行。

2.虚拟机内存调整

1.kvm虚拟机在线热添加内存[--memory 512,maxmemory=2048]
1)安装时设置最大内存以支持热添加

[root@kvm-server]: virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web05 --memory 512,maxmemory=2048 --vcpus 1 --disk /data/web05.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

2)修改xml配置文件设置最大内存数以支持热添加

最大内存

1.临时热添加内存

[root@kvm-server]: setmem web04 1024M --live

2.永久增大内存

[root@kvm-server]: setmem web04 1024M --config

2.虚拟机内存调整与CPU调整相同,也是修改虚拟机配置文件,注意配置文件中内存的单位为KB。

第1步,使用命令“virsh dominfo centos7-03”查看虚拟机硬件信息,虚拟机centos7-03目前内存为2097152 KiB,也是就是2GB。

[root@kvm-centos7 ~]# virsh dominfo centos7-03
Id:             2
名称:       centos7-03
UUID:           6cf38c45-be49-4612-afe9-114aa203b3f0
OS 类型:    hvm
状态:       running
CPU:          1
CPU 时间:   20.3s
最大内存: 2097152 KiB
使用的内存: 2097152 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: selinux
安全性 DOI: 0
安全性标签: system_u:system_r:svirt_t:s0:c404,c721 (enforcing)

第2步,使用命令“virsh edit centos7-03”修改虚拟机配置文件,调整内存为4GB。

[root@kvm-centos7 ~]# virsh edit centos7-03  
<domain type='kvm'>
<name>centos7-03</name>
<uuid>6cf38c45-be49-4612-afe9-114aa203b3f0</uuid>
<memory unit='KiB'>4194304</memory>  #调整内存为4GB,单位KB
<currentMemory unit='KiB'>4194304</currentMemory>
<vcpu placement='static'>4</vcpu>
<os>

第3步,使用命令“virsh dominfo centos7-03”查看虚拟机硬件信息,调整后虚拟机centos7-03内存为4GB。

[root@kvm-centos7 ~]# virsh dominfo centos7-03
Id:             4
名称:       centos7-03
UUID:           6cf38c45-be49-4612-afe9-114aa203b3f0
OS 类型:    hvm
状态:       running
CPU:          4
CPU 时间:   18.4s
最大内存: 4194304 KiB  #调整后内存为4GB
使用的内存: 4194304KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: selinux
安全性 DOI: 0
安全性标签: system_u:system_r:svirt_t:s0:c533,c593 (enforcing)
至此,虚拟机内存调整完成,Windows虚拟机调整与Linux虚拟机方式一样,不存在类似CPU调整差异问题。

3.虚拟机网卡调整

1.kvm虚拟机在线热添加网卡
1.1 查看虚拟机有多少块网卡:

[root@kvm-centos7 ~]# virsh domiflist web04

1.2热添加网卡 (--config 永久生效)(--live 热添加)

[root@kvm-centos7 ~]# virsh attach-interface web04 --type bridge --source br0 --model virtio --config

1.3剥离网卡

[root@kvm-centos7 ~]# virsh detach-interface web04 --type bridge --mac 52:54:00:ea:fa:77 --config

第1步,使用命令“virsh edit win2012”修改虚拟机配置文件,新增网卡配置文件,mac地址以及address type重新生成。

root@kvm-server:/home/admin# virsh dominfo win2012
 <interface type='bridge'>
   <mac address='52:54:00:73:2e:d3'/>
   <source bridge='virbr0'/>
   <model type='rtl8139'/>
   <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
 </interface>
 <interface type='bridge'>
   <source bridge='virbr0'/> 
   <model type='rtl8139'/>
 </interface>

第2步,使用命令“virsh edit win2012”修改虚拟机配置文件,新增网卡配置mac地址以及address type已经重新生成。

root@kvm-server:/home/admin# virsh dominfo win2012
<interface type='bridge'>
<mac address='52:54:00:73:2e:d3'/>
<source bridge='virbr0'/>
<model type='rtl8139'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'>
<mac address='52:54:00:43:e1:2d'/>
<source bridge='virbr0'/>
<model type='rtl8139'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</interface>

4.kvm虚拟机在线热添加硬盘

4.1 相关命令:

[root@kvm-server]: qemu-img create -f qcow2 add01.qcow2 5G
//创建磁盘
[root@kvm-server]: virsh attach-disk centos7 /data/centos7-add01.qcow2 vdb  --subdriver=qcow2
//热添加磁盘
[root@kvm-server]: virsh detach-disk centos7 vdb
//卸载磁盘

4.2 kvm虚拟机在线热添加硬盘

1.在kvm虚拟机,卸载

[root@kvm-server]: virsh detach-disk centos7 vdb
[root@kvm-server]: qemu-img resize /data/centos7-add01.qcow2 +5G
[root@kvm-server]: virsh attach-disk centos7 /data/centos7-add01.qcow2 vdb --live --cache=none --subdriver=qcow2

2.临时生效

[root@kvm-server]: virsh  attach-disk web04 /opt/oldboy.qcow2 vdb --subdriver qcow2 

3.永久生效

[root@kvm-server]: virsh  attach-disk web04 /opt/oldboy.qcow2 vdb --subdriver qcow2 --config

4.在虚拟机中格式化磁盘,然后挂载:

[root@kvm-server]: xfs_growfs /dev/vdb
[root@kvm-server]: mount /dev/vdb  /mnt

// 更新分区统计信息
执行xfs_growfs /dev/vda1

根分区扩容:
1)在宿主机上关闭虚拟机并调整虚拟机磁盘大小

[root@kvm-server]: qemu-img resize oldboy.qcow2 +10G

2)虚拟机中fdisk重新分区

[root@kvm-server]: fdisk /dev/vda

3)重启之后,执行xfs_growfs /dev/vda1,

如果虚拟机磁盘文件系统是ext4:
[root@kvm-server]: resize2fs /dev/vda1
// 更新分区统计信息

具体操作可参考 [增加硬盘容量]

5.KVM磁盘扩容

KVM虚拟机中有两种磁盘格式:

  • raw格式: 直接占用物理磁盘, 写入快, 性能优, 但占用空间
  • Qcow2格式: 使用多少占多少磁盘, 支持压缩、快照、镜像

注意:raw不支持快照, Qcow2支持快照, 但两者文件类型是可以相互间转换
无论磁盘是raw qcow2格式, 扩展思路如下:
1.新添加一块磁盘加入至需要扩容的虚拟主机
2.使用lvm逻辑卷管理方式进行扩展

1.查看当前KVM虚拟机所使用的虚拟磁盘

[root@kvm-node1 ~]# virsh domblklist centos7u4-node1
Target     Source
------------------------------------------------
vda        /data/centos7u4-node1.qcow2

2.创建一块qcow2虚拟磁盘

[root@kvm-node1 ~]# qemu-img create -f qcow2 /data/centos7u4-node1-disk2.qcow2 10G

3.在线添加虚拟磁盘

//在线添加
[root@kvm-node1 ~]# virsh attach-disk centos7u4-node1 /data/centos7u4-node1-disk2.qcow2 vdb --cache=none --subdriver=qcow2
Disk attached successfully

//查看新增磁盘状态
[root@kvm-node1 ~]# virsh domblklist centos7u4-node1
Target     Source
------------------------------------------------
vda        /data/centos7u4-node1.qcow2
vdb        /data/centos7u4-node1-disk2.qcow2


//第二种扩容方式方式, 新增如下配置
[root@kvm-node1 ~]# virsh edit centos7u4-node1
<disk type='file' device='disk'>   
  <driver name='qemu' type='qcow2' cache='none'/>  
  <source file='/data/centos7u4-node1-disk2.qcow2'/>  
  <target dev='vdb' bus='virtio'/>  
 </disk>

4.通过vnc连接KVM虚拟机进行磁盘扩容

5.扩容步骤如下

[root@localhost ~]# pvcreate /dev/vdb
[root@localhost ~]# vgextend /dev/vdb centos
[root@localhost ~]# lvextend -l +100%FREE /dev/centos/root
[root@localhost ~]# xfs_growfs /dev/centos/root