作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.调小虚拟机内存配置案例
1.查看虚拟机现有的内存配置
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
2 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh edit yinzhengjie-kvm01
<domain type='kvm'>
...(很明显,我的虚拟机内存应该只有2GB)
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
...
2.验证虚拟机内存大小
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
2 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]# free
total used free shared buff/cache available
Mem: 2046820 178792 1730784 41460 137244 1703180
Swap: 0 0 0
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# free -h
total used free shared buff/cache available
Mem: 2.0G 174M 1.7G 40M 134M 1.6G
Swap: 0B 0B 0B
[root@yinzhengjie-kvm01 ~]#
3.调整虚拟机的内存
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
2 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh dumpxml yinzhengjie-kvm01 | egrep "memory|currentMemory"
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh setmem yinzhengjie-kvm01 4096M
error: invalid argument: cannot set memory higher than max memory
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh setmem yinzhengjie-kvm01 1024M
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh dumpxml yinzhengjie-kvm01 | egrep "memory|currentMemory"
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
[root@yinzhengjie-kvm189 ~]#
4.调整虚拟机内存后进行验证
[root@yinzhengjie-kvm189 ~]# virsh list
Id Name State
----------------------------------------------------
2 yinzhengjie-kvm01 running
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]# free -h
total used free shared buff/cache available
Mem: 974M 174M 666M 40M 134M 639M
Swap: 0B 0B 0B
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# free
total used free shared buff/cache available
Mem: 998244 178264 682760 41460 137220 655148
Swap: 0 0 0
[root@yinzhengjie-kvm01 ~]#
二.调大虚拟机内存配置案例
1.手动方式修改虚拟机内存配置
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
2 yinzhengjie-kvm01 running
[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 edit yinzhengjie-kvm01
<domain type='kvm'>
<name>yinzhengjie-kvm01</name>
...
<memory unit='MiB'>4096</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
...
温馨提示:
修改后,重新打开虚拟机,你会发现虚拟机的内存限制的单位依旧是KB哟~
2.基于命令行方式修改虚拟机内存配置
[root@yinzhengjie-kvm189 ~]# virsh list --all
Id Name State
----------------------------------------------------
- yinzhengjie-kvm01 shut off
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh setmaxmem yinzhengjie-kvm01 8192M
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh edit yinzhengjie-kvm01
<domain type='kvm'>
<name>yinzhengjie-kvm01</name>
...
<memory unit='KiB'>8388608</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
...
3.手动修改虚拟机内存上限
1.修改内存前查看虚拟机状态
[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 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 17:55:50 on ttyS0
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 332M 1.4G 8.5M 97M 1.3G
Swap: 0B 0B 0B
[root@yinzhengjie-kvm01 ~]#
2.修改内存上限为4GB
[root@yinzhengjie-kvm189 ~]# virsh setmem yinzhengjie-kvm01 4096M
[root@yinzhengjie-kvm189 ~]#
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01
Connected to domain yinzhengjie-kvm01
Escape character is ^]
[root@yinzhengjie-kvm01 ~]#
[root@yinzhengjie-kvm01 ~]# free -h
total used free shared buff/cache available
Mem: 3.8G 330M 3.4G 8.5M 97M 3.3G
Swap: 0B 0B 0B
[root@yinzhengjie-kvm01 ~]#
三.其他注意事项
1.内存的永久配置
修改当前内存的两种方式:
- 临时配置
virsh setmem yinzhengjie-kvm01 4096M
- 永久配置
virsh setmem yinzhengjie-kvm01 4096M --config
修改内存上限的两种方式:
- 临时配置
virsh setmaxmem yinzhengjie-kvm01 8192M
- 永久配置
virsh setmaxmem yinzhengjie-kvm01 8192M --config
2.创建虚拟机时指定内存上限
1.只指定“--memory”参数时,默认是2GB
virt-install --virt-type kvm \
--os-type=linux \
--os-variant rhel7 \
--name yinzhengjie-kvm01 \
--memory 2048 \
--vcpus 2 \
--disk /opt/yinzhengjie-kvm01.raw,format=raw,size=10 \
--cdrom /opt/CentOS-7-x86_64-DVD-2009.iso \
--network network=default \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole
2.使用"--maxmemory"指定最大的内存上限
virt-install --virt-type kvm \
--os-type=linux \
--os-variant rhel7 \
--name yinzhengjie-kvm01 \
--memory 2048,--maxmemory=2048 \
--vcpus 2 \
--disk /opt/yinzhengjie-kvm01.raw,format=raw,size=10 \
--cdrom /opt/CentOS-7-x86_64-DVD-2009.iso \
--network network=default \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole
3.其他注意事项
- 创建虚拟机时,虚拟机的最大内存不能超过该虚拟机所在宿主机的最大内存。
举个例子:
目前有2台128GB内存的主机,那么能够实现创建一个256GB内存的虚拟机呢?答案是否定的。
虚拟机最大的内存应该小于128GB,因为宿主机的操作系统也是要占用一定内存的。
若真能实现,就不会淘汰旧的机器了,而且就算基于网络实现了该功能,那么性能估计会巨差且稳定性不高。
因为CPU的频率是GB赫兹,而网卡的传输速度万兆速度,完全不在一个维度。
- 在云平台购买虚拟机的时候,都会有最高限制,这是因为创建的虚拟机不得超过该虚拟机所在宿主机的总内存。