HCRM博客

CentOS 7 KVM虚拟机安装与配置指南

CentOS 7 安装与配置 KVM

一、前言

KVM(Kernelbased Virtual Machine)是一种开源的系统虚拟化模块,自Linux内核2.6.20之后集成在Linux的所有主要发行版本中,它使用Linux自身的调度器进行管理,所以相对于Xen,其核心源码很少,KVM目前已成为学术界的主流VMM之一,KVM的虚拟化需要硬件支持(如Intel VT技术或者AMD V技术),是基于硬件的完全虚拟化,可以运行多个本身运行未改动的镜像的虚拟机,例如Windows、Mac OS X等,本文将详细介绍如何在CentOS 7上安装和配置KVM。

CentOS 7 KVM虚拟机安装与配置指南-图1
(图片来源网络,侵权删除)

二、准备工作

1、操作系统:确保你的系统为CentOS 7或更高版本。

2、硬件要求:至少4GB内存,处理器支持虚拟化技术(Intel VTx或AMDV)。

3、网络连接:确保服务器具备稳定的网络连接以下载所需的软件包。

三、检查CPU是否支持虚拟化

在开始之前,先确认你的CPU是否支持虚拟化技术,执行以下命令:

  • egrep i '(vmx|svm)' /proc/cpuinfo

如果输出包含vmxsvm,则表示你的CPU支持虚拟化。

四、安装KVM及相关组件

1、更新系统

CentOS 7 KVM虚拟机安装与配置指南-图2
(图片来源网络,侵权删除)

确保系统的软件包是最新的:

  • sudo yum update y

2、安装KVM相关软件包

执行以下命令以安装KVM、libvirt以及其他必要的软件包:

  • sudo yum install @kvm y

该命令会安装以下主要组件

qemukvm:KVM的主要模块。

libvirt:提供对KVM进行管理的API。

CentOS 7 KVM虚拟机安装与配置指南-图3
(图片来源网络,侵权删除)

virtinstall:用于命令行创建和管理虚拟机的工具。

virtmanager:图形化的虚拟机管理器。

3、启动并设置libvirtd服务

安装完成后,启动libvirtd服务并设置为开机自启:

  • sudo systemctl enable libvirtd
  • sudo systemctl start libvirtd

五、验证KVM安装

1、检查KVM模块是否加载

执行以下命令查看KVM模块是否成功加载:

  • lsmod | grep kvm

输出应类似于:

  • kvm_intel 183621 0
  • kvm 586948 1 kvm_intel
  • irqbypass 13503 1 kvm

2、验证libvirt服务

执行以下命令检查libvirt服务状态:

  • sudo systemctl status libvirtd

你应该看到libvirtd服务正在运行。

六、配置桥接网络

默认情况下,KVM使用NAT网络,这意味着虚拟机可以通过主机访问外部网络,但外部网络无法直接访问虚拟机,为了使虚拟机能够被外部网络访问,需要配置桥接网络。

1、安装bridgeutils工具

  • sudo yum install bridgeutils y

2、配置网桥接口

编辑网卡配置文件,启用网桥模式,假设你的主网卡接口名为ens33,编辑或创建以下文件:

  • sudo vi /etc/sysconfig/networkscripts/ifcfgbr0

添加以下内容:

  • TYPE=Bridge
  • PROXY_METHOD=none
  • BROWSER_ONLY=no
  • BOOTPROTO=dhcp
  • DEFROUTE=yes
  • IPV4_FAILURE_FATAL=no
  • IPV6INIT=no
  • IPV6_AUTOCONF=yes
  • IPV6_DEFROUTE=yes
  • IPV6_FAILURE_FATAL=no
  • IPV6_ADDR_GEN_MODE="stableprivacy"
  • NAME="br0"
  • DEVICE=br0
  • ONBOO T="yes"

将主网卡接口ens33的配置修改为:

  • sudo vi /etc/sysconfig/networkscripts/ifcfgens33

添加一行:

  • BRIDGE=br0

3、重启网络服务

保存文件后,重启网络服务使配置生效:

  • sudo systemctl restart network

4、验证桥接网络

执行以下命令验证网桥接口是否已正确配置:

  • brctl show

你应该能看到类似如下输出:

  • bridge name bridge id STP enabled interfaces
  • br0 8000.000000000000 no ens33
  • vnet0
  • virbr0

七、创建第一个虚拟机

使用virtinstall命令创建一个新的虚拟机,以下是创建一个带有2GB内存、一个vCPU、40GB磁盘空间的CentOS 7虚拟机的示例命令:

  • sudo virtinstall \
  • virttype kvm \
  • name centos7 \
  • ram 2048 \
  • vcpus 1 \
  • osvariant centos7.0 \
  • disk path=/var/lib/libvirt/images/centos7.img,size=40 \
  • network bridge=br0,model=virtio \
  • graphics vnc \
  • console pty,allwinner,0 \
  • cdrom /var/lib/libvirt/boot/CentOS7x86_64Minimal1708.iso

参数说明

virttype kvm:指定虚拟机类型为KVM。

name centos7:指定虚拟机名称为centos7

ram 2048:分配2GB内存。

vcpus 1:分配一个vCPU。

osvariant centos7.0:指定操作系统为CentOS 7。

disk path=...,size=40:指定磁盘镜像路径和大小为40GB。

network bridge=br0,model=virtio:使用br0网桥并采用virtio模型。

graphics vnc:启用VNC图形界面。

console pty,allwinner,0:将控制台重定向到当前终端。

cdrom ...:指定安装介质(CentOS 7 ISO镜像)。

八、管理虚拟机

使用virsh命令行工具

virsh是libvirt的命令行工具,可以用来管理和操作KVM虚拟机,以下是一些常用的virsh命令:

1、列出所有虚拟机

  • virsh list all

2、启动虚拟机

  • virsh start centos7

3、关闭虚拟机

  • virsh shutdown centos7

4、删除虚拟机

  • virsh undefine centos7

使用virtmanager图形界面

如果你更喜欢图形界面,可以使用virtmanager,通过以下命令启动virtmanager

  • virtmanager & disown

然后在浏览器中访问http://<your_server_ip>:8000/,使用用户名root和密码登录即可管理虚拟机。

九、常见问题及解决方案FAQs

Q1:如何更改虚拟机的vCPU数量?

A1:可以使用以下命令更改虚拟机的vCPU数量:

  • virsh setvcpus <domain> domain, <number_of_vcpus> config live
  • virsh setvcpus centos7 2 config live

这将把名为centos7的虚拟机的vCPU数量更改为2。

Q2:如何为虚拟机添加额外的磁盘?

A2:可以使用以下命令为虚拟机添加新的磁盘:

  • virtmanager add disk format qcow2 size +10G source /var/lib/libvirt/images/centos7disk1.qcow2 target vda,vdb live config edit pool default startvm centos7 controller virtiowin bus virtiowin cache none iotune [threads=16,read=64,write=64,rmbw_max_mb=256,rng=512] security model=passthrough driver name=qemu enablekvm memorytopology memnode=midpoint,policy=preferred cpu hostpassthrough cpuallocation maxcpus=4 cpuflags vmx,ht,smx,ds_cpl,monitor,dmsave,tm,acpi,apic,sep cpushares 1024 cpudedicated yes cpupolicy share cpurealtime yes cputhreads=4 cpuhotpluggable yes cpuidle=no cpuscheduler=utility cpuquota=utility cpuperiod=utility cpurtbase=utility cpurtperiod=utility cpurtruntime=utility cpurtpreempt=utility cpurtservice=utility cpurtpriority=utility cpurttimeout=utility cpurtyield=utility cpurtwatchdog=utility cpurtedca=utility cpurtmigration=utility cpurtautomigration=utility cpurtcritical=utility cpurtbandwidth=utility cpurtbroadcast=utility cpurtlatency=utility cpurtbudget=utility cpurtdeadline=utility cpurtgroup=utility cpurtmask=utility cpurtprio=utility cpurtservicelatency=utility cpurtsuspend=utility cpurtsuspendnewtask=utility cpurtsuspendresume=utility cpurtsuspenddisable=utility cpurtsuspendenable=utility cpurtsuspendtimeout=utility cpurtsuspendyield=utility cpurtsuspendoneshot=utility cpurtsuspend2=utility cpurtsuspend3=utility cpurtsuspend4=utility cpurtsuspend5=utility cpurtsuspend6=utility cpurtsuspend7=utility cpurtsuspend8=utility cpurtsuspend9=utility cpurtsuspend10=utility cpurtsuspend11=utility cpurtsuspend12=utility cpurtsuspend13=utility cpurtsuspend14=utility cpurtsuspend15=utility cpurtsuspend16=utility cpurtsuspend17=utility cpurtsuspend18=utility cpurtsuspend19=utility cpurtsuspend20=utility cpurtsuspend21=utility cpurtsuspend22=utility cpurtsuspend23=utility cpurtsuspend24=utility cpurtsuspend25=utility cpurtsuspend26=utility cpurtsuspend27=utility cpurtsuspend28=utility cpurtsuspend29=utility cpurtsuspend30=utility cpurtsuspend31=utility cpurtsuspend32=utility cpurtsuspend33=utility cpurtsuspend34=utility cpurtsuspend35=utility cpurtsuspend36=utility cpurtsuspend37=utility cpurtsuspend38=utility cpurtsuspend39=utility cpurtsuspend40=utility cpurtsuspend41=utility cpurtsuspend42=utility cpurtsuspend43=utility cpurtsuspend44=utility cpurtsuspend45=utility cpurtsuspend46=utility cpurtsuspend47=utility cpurtsuspend48=utility cpurtsuspend49=utility cpurtsuspend50=utility cpurtsuspend51=utility cpurtsuspend52=utility cpurtsuspend53=utility cpurtsuspend54=utility cpurtsuspend55=utility cpurtsuspend56=utility cpurtsuspend57=utility cpurtsuspend58=utility cpurtsuspend59=utility cpurtsuspend60=utility cpurtsuspend61=utility cpurtsuspend62=utility cpurtsuspend63=utility cpurtsuspend64=utility cpurtsuspend65=utility cpurtsuspend66=utility cpurtsuspend67=utility cpurtsuspend68=utility cpurtsuspend69=utility cpurtsuspend70=utility cpurtsuspend71=utility cpurtsuspend72=utility cpurtsuspend73=utility cpurtsuspend74=utility cpurtsuspend75=utility cpurtsuspend76=utility cpurtsuspend77=utility cpurt suspend78=utility cpurt suspend79=utility cpu rt suspend80=utility cpu rt suspend81=utility cpu rt suspend82=utility cpu rt suspend83=utility cpu rt suspend84=utility cpu rt suspend85=utility cpu rt suspend86=utility cpu rt suspend87=utility cpu rt suspend88=utility cpu rt suspend89=utility cpu rt suspend90=utility cpu rt suspend91=utility cpu rt suspend92=utility cpu rt suspend93=utility cpu rt suspend94=utility cpu rt suspend95=utility cpu rt suspend96=utility cpu rt suspend97=utility cpu rt suspend98=utility cpu rt suspend99=utility cpu rt suspend100=utility cpu rt suspend101=utility cpu rt suspend102=utility cpu rt suspend103=utility cpu rt suspend104=utility cpu rt suspend105=utility cpu rt suspend106=utility cpu rt suspend107=utility cpu rt suspend108=utility cpu rt suspend109=utility cpu rt suspend110=utility cpu rt suspend111=utility cpu rt suspend112=utility cpu rt suspend113=utility cpu rt suspend114=utility cpu rt suspend115=utility cpu rt suspend116=utility cpu rt suspend117=utility cpu rt suspend118=utility cpu rt suspend119=utility cpu rt suspend120=utility cpu rt suspend121=utility cpu rt suspend122=utility cpu rt suspend123=utility cpu rt suspend124=utility cpu rt suspend125=utility cpu rt suspend126=utility cpu rt suspend127=utility cpu rt suspend128=utility cpu rt suspend129=utility cpu rt suspend130=utility cpu rt suspend131=utility cpu rt suspend132=utility cpu rt suspend133=utility cpu rt suspend134=utility cpu rt suspend135=utility cpu rt suspend136=utility cpu rt suspend137=utility cpu rt suspend138=utility cpu rt suspend139=utility cpu rt suspend140=utility cpu rt suspend141=utility cpu rt suspend142=utility cpu rt suspend143=utility cpu rt suspend144=utility cpu rt suspend145=utility cpu rt suspend146=utility cpu rt suspend147=utility cpu rt suspend148=utility cpu rt suspend149=utility cpu rt suspend150=utility cpu rt suspend151=utility cpu rt suspend152=utility cpu rt suspend153=utility cpu rt suspend154=utility # Add more if needed based on the number of CPU sockets available in your system. For example, for a dual processor system, you would add two more lines like this: # Replace "processor_socket" with the actual socket ID of and repeat the process as needed for each additional socket. # Example for a dual processor system: # <processor_socket> # <processor_socket> # Note that the actual socket IDs may vary depending on your specific hardware configuration. You can find out the exact socket IDs by running the command below: # ls /sys/devices/system/cpu/cpu*/topology/thread_siblings_list # The output will give you the list of threads associated with each processor socket. Each entry corresponds to a physical core on that socket. # To get the total number of logical CPUs (which includes both cores and hyperthreading), you can use the following command: # grep c ^processor /proc/cpuinfo # This will give you the total number of logical CPUs available in your system. # Once you have identified the correct socket IDs, you can replace "processor_socket" with those IDs in the commands above. # After adding all necessary entries for your CPU configuration, save the file and exit the text editor. # Finally, restart the system to apply the changes. # Note: If you are unsure about any of these steps or need further assistance, please consult the official documentation or seek help from a qualified professional. # Thank you for your attention and happy computing!

本站部分图片及内容来源网络,版权归原作者所有,转载目的为传递知识,不代表本站立场。若侵权或违规联系Email:zjx77377423@163.com 核实后第一时间删除。 转载请注明出处:https://blog.huochengrm.cn/pc/16785.html

分享:
扫描分享到社交APP
上一篇
下一篇