在当今的云计算时代,虚拟化技术已经成为了企业和个人用户的首选。而在众多虚拟化技术中,KVM(Kernel-based Virtual Machine)虚拟化技术因其高性能、低成本和灵活性而备受青睐。本文将介绍KVM虚拟化技术的原理、特点以及应用场景。
一、KVM虚拟化技术简介
KVM(Kernel-based Virtual Machine)是一种基于内核的虚拟化技术,它最初是由德国Qumranet公司开发的。2007年,Red Hat收购了Qumranet,并将KVM作为其企业级虚拟化解决方案的一部分。如今,KVM已经成为了Linux操作系统的一个标准组件,支持大多数主流的Linux发行版。
KVM虚拟化技术的核心思想是将宿主机的硬件资源抽象为虚拟机,使多个虚拟机可以同时运行在一个物理服务器上。每个虚拟机都有自己的操作系统、应用程序和用户空间,彼此之间相互隔离。这种隔离性使得虚拟机之间的故障互不影响,从而提高了系统的稳定性和安全性。
二、KVM虚拟化技术原理
KVM虚拟化技术的实现依赖于CPU的虚拟化扩展(如Intel VT和AMD-V)。当宿主机启动时,KVM会加载一个轻量级的内核模块kvm.ko,该模块负责与硬件进行交互,实现虚拟机的创建、管理和调度。
KVM虚拟化技术的主要组件包括以下几个部分:
三、KVM虚拟化技术特点
四、安装KVM?
打开机器的虚拟化选项
然后打开虚拟机,安装kvm即可 ?
cat /proc/cpuinfo | grep -E 'vmx|svm' #查看cpu是否支持虚拟化
systemctl stop firewalld #关闭防火墙
setenforce 0 #关闭selinux
#需求内核(rhel6以上)
[root@kvm-server ~]# uname -r
#如果之前安装kvm了清理环境:卸载kvm
[root@kvm-server ~]# yum remove `rpm -qa | egrep 'qemu|virt|kvm'` -y
[root@kvm-server ~]# rm -rf /var/lib/libvirt /etc/libvirt/
#升级系统
[root@kvm-server ~]# yum upgrade --升级所有包同时也升级软件和系统内核
#安装软件
[root@kvm-server ~]# yum install *qemu* *virt* librbd1-devel -y
#启动服务
[root@kvm-server ~]# systemctl start libvirtd
KVM gustos图形方式部署安装虚拟机---需要掌握
[root@kvm-server ~]# virt-manager
#安装完成一台虚拟机之后,网络模式默认是NAT的。也只有这一种网络
?
?
?
模板镜像+配置文件 方式安装虚拟机
#拷贝模板镜像和配置文件
[root@kvm-server ~]# cp /etc/libvirt/qemu/vm2.xml /etc/libvirt/qemu/vm3.xml
[root@kvm-server ~]# cp /var/lib/libvirt/images/vm2.img /var/lib/libvirt/images/vm3.img
?
#修改配置文件
[root@kvm-server ~]# vim /etc/libvirt/qemu/vm3.xml
domain type='kvm'>
<name>vm3</name> #名字不能一样需要修改
<uuid>2e3fa6db-ff7f-41c3-bc8f-0428e81ebb57</uuid> #uuid不能一样需要修改
<memory unit='KiB'>1024000</memory> #内存,可选
<currentMemory unit='KiB'>1024000</currentMemory> #当前内存与上面定义一样
<vcpu placement='static'>2</vcpu> #cpu可选
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>SandyBridge-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/vm3.img'/> #磁盘镜像需要修改
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:82:d6:3c'/> #mac地址不能一样需要修改,只能修改后三段。
<source network='default'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
<address type='virtio-serial' controller='0' bus='0' port='2'/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='spice' autoport='yes'>
<listen type='address'/>
<image compression='off'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='2'/>
</redirdev>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='3'/>
</redirdev>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</memballoon>
<rng model='virtio'>
<backend model='random'>/dev/urandom</backend>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</rng>
</devices>
</domain>
#必须修改name,uuid,mac地址,磁盘文件名字,其余可选
#用vim修改完之后需要define一下配置文件
[root@kvm-server ~]# virsh define /etc/libvirt/qemu/vm3.xml
重启一下:
[root@kvm-server ~]# systemctl restart libvirtd
宿主机开启路由转发:
[root@kvm-server ~]# vim /etc/sysctl.conf
[root@kvm-server ~]# sysctl -p
net.ipv4.ip_forward = 1