KVM的热添加技术之CPU

本文涉及的产品
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介: 这篇文章介绍了如何在KVM虚拟机中热添加CPU资源,包括查看当前CPU配置、修改CPU核心数、永久性修改CPU配置以及注意事项等操作步骤。

                                              作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.修改CPU的内存上限

1.查看当前CPU信息

[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 61
Model name:            Intel Core Processor (Broadwell, IBRS)
Stepping:              2
CPU MHz:               2208.000
BogoMIPS:              4416.00
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0,1
Flags:                 fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#

2.修改cpu的最大核心数

[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 --config --maximum 10

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh edit yinzhengjie-kvm01 
<domain type='kvm'>
  <name>yinzhengjie-kvm01</name>
  ...
  <vcpu placement='static' current='2'>10</vcpu>
  ...



温馨提示:
    建议大家在虚拟机关机的时候调整CPU的最大值,否则可能配置不会立刻生效哟~

3.重启虚拟机使得配置生效

[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh destroy yinzhengjie-kvm01 
Domain yinzhengjie-kvm01 destroyed

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     yinzhengjie-kvm01              shut off

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh start yinzhengjie-kvm01 
Domain yinzhengjie-kvm01 started

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]#

4.修改当前的cpu核心数量

[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 4

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             4
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 61
Model name:            Intel Core Processor (Broadwell, IBRS)
Stepping:              2
CPU MHz:               2208.000
BogoMIPS:              4416.00
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0-3
Flags:                 fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#

5.再次调整CPU核心数量

[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             8
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 61
Model name:            Intel Core Processor (Broadwell, IBRS)
Stepping:              2
CPU MHz:               2208.000
BogoMIPS:              4416.00
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0-7
Flags:                 fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#

二.其他注意事项

1.永久性修改cpu核心配置

- 热添加CPU核心数量
virsh setvcpus yinzhengjie-kvm01 8

- 永久添加CPU核心数量
virsh setvcpus yinzhengjie-kvm01 8 --config

- 调整虚拟机支持CPU的最大值
virsh setvcpus yinzhengjie-kvm01 --maximum 20 --config

2.不支持热修改CPU数量

[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 9  # 可以调大CPU数量

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 5  # 但是不可以调小CPU数量
error: unsupported configuration: failed to find appropriate hotpluggable vcpus to reach the desired target vcpu count

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh setvcpus yinzhengjie-kvm01 5 --config  # 永久修改CPU数量需要重启虚拟机才能生效

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                9
On-line CPU(s) list:   0-8
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             9
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 61
Model name:            Intel Core Processor (Broadwell, IBRS)
Stepping:              2
CPU MHz:               2208.000
BogoMIPS:              4416.00
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0-8
Flags:                 fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]# 
[root@yinzhengjie-kvm01 ~]# 
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh reboot yinzhengjie-kvm01 
Domain yinzhengjie-kvm01 is being rebooted

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh destroy yinzhengjie-kvm01 
Domain yinzhengjie-kvm01 destroyed

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh start yinzhengjie-kvm01 
Domain yinzhengjie-kvm01 started

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64

yinzhengjie-kvm01 login: root
Password: 
Last login: Wed Apr 10 21:42:52 on ttyS0
[root@yinzhengjie-kvm01 ~]# 
[root@yinzhengjie-kvm01 ~]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                5
On-line CPU(s) list:   0-4
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             5
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 61
Model name:            Intel Core Processor (Broadwell, IBRS)
Stepping:              2
CPU MHz:               2208.000
BogoMIPS:              4416.00
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
NUMA node0 CPU(s):     0-4
Flags:                 fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm 3dnowprefetch invpcid_single ssbd ibrs ibpb fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt md_clear spec_ctrl
[root@yinzhengjie-kvm01 ~]#
相关实践学习
快速体验PolarDB开源数据库
本实验环境已内置PostgreSQL数据库以及PolarDB开源数据库:PolarDB PostgreSQL版和PolarDB分布式版,支持一键拉起使用,方便各位开发者学习使用。
目录
相关文章
|
12月前
|
算法 编译器
【计算机架构】响应时间和吞吐量 | 相对性能 | 计算 CPU 时间 | 指令技术与 CPI | T=CC/CR, CC=IC*CPI
【计算机架构】响应时间和吞吐量 | 相对性能 | 计算 CPU 时间 | 指令技术与 CPI | T=CC/CR, CC=IC*CPI
1081 1
|
16天前
|
缓存 运维 监控
CPU被打满/CPU 100%:高效应对策略与技术干货分享
【10月更文挑战第3天】在信息技术高速发展的今天,无论是开发人员、运维人员还是数据分析师,都可能遇到CPU被打满(即CPU使用率达到100%)的情况。这不仅会影响系统的响应速度,严重时甚至会导致服务中断。本文将从诊断、分析与解决三个方面,详细介绍处理CPU 100%问题的技术干货。
63 3
|
24天前
|
安全 编译器 异构计算
在CPU设计中,为了提高能效比并减少能源消耗,采用了多种节能技术
【10月更文挑战第2天】在CPU设计中,为了提高能效比并减少能源消耗,采用了多种节能技术
39 4
|
24天前
|
安全 编译器 异构计算
现代CPU的节能技术
【10月更文挑战第2天】现代CPU的节能技术
30 3
|
3月前
|
KVM 虚拟化
[kvm]cpu内存硬盘配置
[kvm]cpu内存硬盘配置
|
5月前
|
监控 Java API
使用Java检测当前CPU负载状态的技术博客
使用Java检测当前CPU负载状态的技术博客
81 0
|
6月前
|
异构计算
振南技术干货集:CPU,你省省心吧!(2)
振南技术干货集:CPU,你省省心吧!(2)
|
12月前
|
存储 Linux Docker
跨cpu架构部署容器技术点:怎样修改Linux 的内核版本
在使用Docker 进行跨平台部署之后,我们还可以尝试进行跨架构部署。 从X86 架构上移植到 aarch64 上。
246 0
|
12月前
|
NoSQL Shell Linux
跨cpu架构部署容器技术点:怎么将容器启动时的1号进程挂载到systemctl
--privileged=true:是Docker中的一个参数,用于授予容器的特权权限。当一个容器被设置为特权容器时,它将拥有与主机操作系统相同的权限,可以执行一些高级操作,如访问主机设备、加载内核模块等。
91 0
|
关系型数据库 C语言
VT技术(二)检测CPU支持
1.CPUID指令检测 在进入VMX Opreation之前必须要检测CPU是否支持VMX技术,可以通过CPUID指令进行查询,在执行CPUID指令之后,返回值存入EAX,EBX,ECX,EDX中,查看ECX.VMX[5]位是否为1,否则不支持VMX技术,关于CPUID指令的介绍可以参考Intel白皮书卷二第三章第三节 Instruction-CPUID Identification详细介绍了CPUID的参数
441 0