基于linux平台的虚拟化.docx_第1页
基于linux平台的虚拟化.docx_第2页
基于linux平台的虚拟化.docx_第3页
基于linux平台的虚拟化.docx_第4页
基于linux平台的虚拟化.docx_第5页
已阅读5页,还剩68页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

目录1.1什么是虚拟化?11.1.1什么是hypervisor11.1.2什么是虚拟机11.1.3主机21.1.4虚拟化的分类:21.2KVM21.2.1什么是 kvm21.2.2KVM分层架构图31.2.3KVM 需要那些硬件平台支持?41.2.4KVM 支持那些系统平台虚拟guest41.2.5KVM是如何实现虚拟化的过程41.2.6KVM和libvirt和QEMU的关系?41.2.7Libvirt的功能41.2.8Libvirt支持的Hypervisor列表61.2.9KVM 对CPU的要求61.2.10使用kvm注意事项。81.3如何安装和使用KVM81.3.1如何在RHEL系统过程中安装kvm81.3.2如何在已经安装好的RHEL系统中安装KVM91.3.3使用virt-manager图形化安装虚拟机(推荐)121.3.4如何管理虚拟机161.4实验1:安装KVM,再安装一个虚拟机实例。261.5虚拟化网络271.5.1虚拟网络组件271.5.2KVM的网络管理271.5.3NAT网络模式271.5.4bridge网络模式281.6实验2:设置虚拟机网卡桥接模式311.7虚拟机在KVM启动模式311.8实验3设置虚拟机随机启动311.9KVM的存储池311.9.1kvm支持的存储311.10实验4:新建一个基于目录的存储池331.11使用存储池351.12qemu-img工具371.12.1虚拟磁盘工作原理371.12.2KVM和虚拟磁盘381.12.3创建一个磁盘。381.12.4实验5:扩容虚拟存储391.13实验6:利用KVM的特性搭建实验环境411.14kvm 高级功能和调优521.14.1半虚拟化驱动521.14.2半虚拟化驱动forwindows551.15实验7:安装一台windows的虚拟机,要求使用半虚拟化驱动。561.16KVM的一些额外问题561.16.1如何挂载虚拟机的镜像文件561.16.2KVM挂载USB设备601.17KVM在线迁移功能601.17.1KVM在线迁移功能611.17.2使用图形化迁移虚拟机621.17.3无共享磁盘场景下KVM迁移651.17.4使用图像化界面进行迁移虚拟机存储67 基于linux平台的虚拟化-KVM的理论和实战本课程内容理论部分参照“Red_Hat_Enterprise_Linux-6-Virtualization-en-US.pdf”文档和kvm的官方以及/page/Main_Page和其他众多开源爱好者的一些观念和知识。1.1 什么是虚拟化?Virtualization is a broad computing term for running software, usually operating systems,concurrently and isolated from other programs on one system. Most existing implementations of virtualization use a hypervisor, a software layer that controls hardware and provides guest operating systems with access to underlying hardware. The hypervisor allows multiple operating systems torun on the same physical system by offering virtualized hardware to the guest operating system.虚拟化:就是通过某种方式隐藏底层物理硬件的过程,从而让多个操作系统可以透明地使用和共享底层的硬件资源。市场份额比较多的虚拟化产品VMwareMicrosoftCitrixRedhatWorkstationHyper-VXenRHEVvsphereWindowsAzureXen desktopKVMVcloud .N多产品线1.1.1 什么是hypervisor通常提供平台虚拟化的层称为 hypervisor (有时称为虚拟机管理程序或VMM)。1.1.2 什么是虚拟机简而言之就是在虚拟化平台上运行的虚拟服务器。1.1.3 主机承载虚拟机运行环境,提供计算能力的机器称为主机。1.1.4 虚拟化的分类:虚拟化从应用的角度来分类可以分成N类,如服务器虚拟化、桌面虚拟化、平台虚拟化、应用虚拟化、网络虚拟化、I/O虚拟化、存储虚拟化等等。虚拟化从架构来分类,有裸金属架构、有宿主架构、有混合架构。虚拟化本事的体现方式来分类,有全虚拟化和半虚拟化和仿真器。There are various methods for virtualizing operating systems: Hardware-assisted virtualization is the technique used for full virtualization with KVM. Para-virtualization is a technique used by Xen to run Linux guests. Software virtualization or emulation. Software virtualization uses binary translation and otheremulation techniques to run unmodified operating systems. Software virtualization is significantlyslower than hardware-assisted virtualization or para-virtualization. Software virtualization, using QEMU without KVM, is unsupported by Red Hat Enterprise Linux.虚拟化有什么优势? 无非就说是节省投资,提供资源利用率、减少管理成本bala bala bala一套玩意。1.2 KVM1.2.1 什么是 kvmWhat is KVM?(KVM) is a Full virtualization solution for Linux on AMD64 and Intel 64 hardware. KVM is a Linux kernel module built for the standard Red Hat Enterprise Linux 6 kernel. KVM can run multiple, unmodified virtualized guest Windows and Linux operating systems.The KVM hypervisor in Red Hat Enterprise Linux is managed with the libvirt API and tools built for libvirt, virt-manager and virsh. Virtualized guests are run as Linux processes and threads which are controlled by these modules. Red Hat Enterprise Linux KVM hypervisors can be managed by the Red Hat Enterprise Virtualization Manager as an alternative to the virsh and virt-manager tools.The kvm package also contains Linux kernel modules which manage devices, memory and management APIs for the Hypervisor module itself.KVM 是第一个成为原生 Linux 内核(2.6.20)的一个 hypervisor,它是由 Avi Kivity 开发和维护的,现在归 Red Hat 所有。支持的平台有AMD64架构和INTEL 64架构。在RHEL6以上的版本,KVM内核模块已经集成在RHEL6发行版的内核里面。其他的一些发行版的linux同时也支持KVM,只是没有集成在内核里面,需要手动安装kvm才能使用。在RHEL里面,kvm是通过libvirt api、libvirt tool、virt-manager、virsh这四个工具来实现对KVM的管理。下面会依次介绍到这四个工具的特点和使用方法。在RHEL里面,kvm可以运行windows、linux、unix、solaris 系统。KVM 是作为内核模块实现的,因此 Linux 只要加载该模块就会成为一个hypervisor。可以简单地认为,一个标准的LINUX内核,只要加载了kvm模块,这个内核就成为了一个hypervisor。但是仅有hypervisor是不够的,毕竟hypervisor还是内核层面的程序,还需要把虚拟化在用户层面体现出来,这就需要一些模拟器来提供用户层面的操作。如qemu-kvm 程序。1.2.2 KVM分层架构图图为 linux hypervisord 基本架构:每个guest(通常我们称为虚拟机,下同)都是通过 /dev/kvm 设备映射的,它们拥有自己的虚拟地址空间,该虚拟地址空间映射到host内核的物理地址空间。KVM 使用底层硬件的虚拟化支持来提供完整的(原生)虚拟化。同时,guest的I/O 请求通过主机内核映射到在主机上(hypervisor)执行的 QEMU 进程。换言之,每个guest的I/O请求都是交给/dev/kvm这个虚拟设备,然后/dev/kvm通过hypervisor 访问到host底层的硬件资源。如,文件的读写,网络发送接受等等。rootlt # ll /dev/kvmcrw-rw-rw-+ 1 root kvm 10, 232 8月 16 09:04 /dev/kvm1.2.3 KVM 需要那些硬件平台支持?Intel? Virtualization Technology Intel VT AMD Virtualization AMD-V )。(关于CPU的 Intel VT 和 AMD-V的工作原理,请google找相关的文档进行查阅。)1.2.4 KVM 支持那些系统平台虚拟guest支持windows、linux、unix、solaris for x86架构的操作系统。1.2.5 KVM是如何实现虚拟化的过程kvm由两个组件实现。第一个是可加载的 KVM 模块,当在 Linux 内核安装该模块之后,它就可以管理虚拟化硬件,并通过 /proc 文件系统公开其功能。(在内核空间实现)第二个组件用于平台模拟,它是由修改版 QEMU 提供的。QEMU 作为用户空间进程执行,并且在guest请求方面与内核协调。(在用户空间实现)当新的guest 在 KVM 上启动时(通过一个称为kvm 的实用程序),它就成为宿主操作系统的一个进程,因此就可以像其他进程一样调度它。但与传统的 Linux 进程不一样,guest被 hypervisor 标识为处于 “来宾” 模式(独立于内核和用户模式)。每个guest都是通过/dev/kvm设备映射的,它们拥有自己的虚拟地址空间,该空间映射到主机内核的物理地址空间。如前所述,KVM 使用底层硬件的虚拟化支持来提供完整的(原生)虚拟化。I/O 请求通过主机内核映射到在主机上(hypervisor)执行的 QEMU 进程。(注意区分用户空间和内核空间的概念。)1.2.6 KVM和libvirt和QEMU的关系?Libvirt是一个软件集合,便于使用者管理虚拟机和其他虚拟化功能,比如存储和网络接口管理等等,QEMU组件用于平台模拟,它是由修改版 QEMU 提供的。类似vcenter,但功能没有vcenter那么强悍。简单的可以理解为,libvirt是一个工具的集合箱,用来管理KVM,面向底层管理和操作。Qemu是用来进行平台模拟,面向上层管理和操作。1.2.7 Libvirt的功能Libvirt is a hypervisor-independent virtualization API that is able to interact with the virtualization capabilities of a range of operating systems。 libvirt provides a common, generic and stable layer to securely manage virtualized guests on a host.libvirt provides a common interface for managing local systems and networked hosts. Libvirt provides all APIs required to provision, create, modify, monitor, control, migrate and stop virtualized guests if the hypervisor supports these operations. Although multiple hosts may be accessed with libvirt s imultaneously, the APIs are limited to single node operations. libvirt is designed as a building block for higher level management tools and applications. Libvirt focuses on managing single hosts, with the exception of migration capabilities. libvirt provides APIs to enumerate, monitor and use the resources available on the managed node, including CPUs, memory, storage, networking and Non-Uniform Memory Access (NUMA) partitions. The management tools can be located on separate physical machines from the host using secure protocols.Red Hat Enterprise Linux 6 supports libvirt and included libvirt-based tools as its default method for virtualization management. libvirt is free software available under the GNU Lesser General Public License. The libvirt project aims to provide a long term stable C API. The libvirt Open Source project currently supports Xen, Qemu, KVM, LXC, OpenVZ, VirtualBox, OpenNebula, and VMware ESX. The Red Hat Enterprise Linux 6 libvirt package supports Xen on Red Hat Enterprise Linux 5 and KVM on Red Hat Enterprise Linux 5 and Red Hat Enterprise Linux 6.libvirt的主要功能如下:虚拟机管理:包括不同的领域操作,比如:启动、停止、暂停、保存、恢复和迁移。支持多种设备类型的热插拔操作,包括:磁盘、网卡、内存和CPU。如在线给guest添加一个usb设备。比如,给一台windows添加一个U盘,或者一个银行的加密狗。远程机器支持:只要机器上运行了libvirtd daemon,包括远程机器,所有的libvirt 功能就都可以访问和使用。支持多种网络远程传输,使用最简单的SSH,不需要额外配置工作。如,使用一台带有kvm和libvirt工具的机器,可以远程访问,控制,操作其他所有的kvm虚拟机,大大地简化了通过远程登陆系统,在远程系统上管理kvm。降低了维护成本。存储管理:任何运行了libvirt daemon的主机都可以用来管理不同类型的存储。(在KVM平台上基本上是以为文件系统基础管理存储,暂时好像没有直接管理存储LUN和创建共享存储的功能。)可以创建不同格式的文件映像(qcow2、vmdk、raw等,vsphere的文件镜像是vmdk,两者做迁移的时候可以通过工具进行格式转换)网络接口管理:任何运行了libvirt daemon的主机都可以用来管理物理和逻辑的网络接口。(KVM只有一个虚拟交换机而且功能简单,只能做NAT和桥接两种模式,vsphere有标准虚拟交换机和分布式交换机两种,并且功能丰富,可以实现自己管理IP地址,可以划分VLAN区分网段和端口组,高级的组件还可以实现流量控制等)管理其他设备:列出现有的接口卡,配置、创建接口,以及桥接、vlan和关联设备等。总的来说,Libvirt提供一组工具和拓展接口来管理hypervisor,并且吹牛说可以管理多种不同hypervisor,例如xen、Vware、LXC等等。1.2.8 Libvirt支持的Hypervisor列表XenQEMULXC(有兴趣可以课后自己了解一下这个,腾讯公司使用比较多是XEN和LXC)TestOpenVZVirtualBox(基本)OpenNebulaVMware ESXKVM的对硬件要求Recommended system requirements 6GB plus the required disk space recommended by the guest operating system per guest. For mostoperating systems more than 6GB of disk space is recommended. One processor core or hyper-thread for each virtualized CPU and one for the hypervisor. 2GB of RAM plus additional RAM for virtualized guests.至少6G的磁盘空间。CPU支持多线程和支持hypervisor。2G以上的内存。1.2.9 KVM 对CPU的要求The KVM hypervisor requires: an Intel processor with the Intel VT and the Intel 64 extensions, or an AMD processor with the AMD-V and the AMD64 extensions.要求CPU支持intel-tv/amd-v的硬件支持。如果CPU不支持,将无法使用KVM。判断CPU是否支持虚拟化,可以进行如下的操作rootluotao # egrep (vmx|svm) -color=always /proc/cpuinfo(AMD) flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflushmmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow rep_good noplextd_apicid pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy 3dnowprefetch lbrvflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflushmmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow rep_good noplextd_apicid pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy 3dnowprefetch lbrvrootlt # egrep (vmx|svm) -color=always /proc/cpuinfo (INTEL)flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflushdts acpi mmx fxsr sse sse2 ss ht tm pbe nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopologynonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1sse4_2 popcnt lahf_lm arat dts tpr_shadow vnmi flexpriority ept vpidflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflushdts acpi mmx fxsr sse sse2 ss ht tm pbe nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopologynonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1sse4_2 popcnt lahf_lm arat dts tpr_shadow vnmi flexpriority ept vpidflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflushdts acpi mmx fxsr sse sse2 ss ht tm pbe nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopologynonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1sse4_2 popcnt lahf_lm arat dts tpr_shadow vnmi flexpriority ept vpidflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflushdts acpi mmx fxsr sse sse2 ss ht tm pbe nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopologynonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1sse4_2 popcnt lahf_lm arat dts tpr_shadow vnmi flexpriority ept vpid1.2.10 使用kvm注意事项。.The following applications should be avoided for their high I/O requirement reasons: kdump server netdump server在一些密集型的I/O应用,如数据库,因为对I/O吞吐要求比较高,推荐使用半虚拟化或者半虚拟化驱动来实现。同时guest应避免启动 kdump和netdump 两个服务。因为在在内核崩溃和转存过程中会消耗大量的资源。在RHEL中,默认是没有配置上述两个服务。如果在host上是可以考虑配置上述两个服务的。1.3 如何安装和使用KVMInstalling KVM with a new Red Hat Enterprise Linux installation。Installing KVM packages with Kickstart filesInstalling the KVM hypervisor with yum1.3.1 如何在RHEL系统过程中安装kvm1. Start an interactive Red Hat Enterprise Linux installation from the Red Hat Enterprise LinuxInstallation CD-ROM, DVD or PXE.2. You must enter a valid installation number when prompted to receive access to the virtualization and other Advanced Platform packages.3. Complete the other steps up to the package selection step.Select the Virtual Host server role to install a platform for virtualized guests. Alternatively, select the Customize Now radio button to specify individual packages.4. elect the Virtualization package group. This selects the KVM hypervisor, virt-manager,libvirt and virt-viewer for installation.5.Customize the packages (if required)Customize the Virtualization group if you require other virtualization packages. Press the Close button then the Next button to continue the installation.1.3.2 如何在已经安装好的RHEL系统中安装KVMInstalling the KVM hypervisor with yumTo use virtualization on Red Hat Enterprise Linux you require the kvm package. The kvm package contains the KVM kernel module providing the KVM hypervisor on the default Red Hat Enterprise Linux kernel.# yum install qemu-kvm(不同的linux发行版本,kvm软件包的名字可能有所不同.) rootkvm # yum list | grep kvmqemu-kvm.x86_64 2:-2.160.el6(redhat 64位企业版的KVM名字为qemu-kvm.x86_64) rootlt # yum list | grep kvm(FC系列的KVM名字叫做qemu-kvm.i686)qemu-kvm.i686 2:0.14.0-7.fc15上述两个方法都可以实现KVM的安装。但仅仅安装KVM还不是一个完整意义上的虚拟机,只是安装了一个hypervisor(这个步骤其实和单纯安装ESXI产品一样),必须安装一些管理工具软件包配合才能使用。安装hypervisor管理软件包下面这些软件包最好完全安装,提供更多的功能。python-virtinst#提供创建虚拟机的 virt-install 命令。Provides the virt-install command for creating virtual machines.libvirt#libvirt 是一个可与管理程序互动的 API 程序库。libvirt 使用 xm 虚拟化构架以及 virsh 命令行工具管理和控制虚拟机。The libvirt package provides the server and host side libraries for interacting with hypervisors and host systems. The libvirt package provides the libvirtd daemon that handles the library calls,manages virtualizes guests and controls the hypervisor.libvirt-python#libvirt-python 软件包中含有一个模块,它允许使用由 Python 编程语言编写的应用程序使用由The libvirt-python package contains a module that permits applications written in the Python programming language to use the interface supplied by the libvirt API.virt-manager#virt-manager,也称 Virtual Machine Manager,它可为管理虚拟机提供图形工具。它使用 libvirt程序库作为管理 API。virt-manager, also known as Virtual Machine Manager, provides a graphical tool for administering virtual machines. It uses libvirt-client library as the management API.libvirt-clientThe libvirt-client package provides the client-side APIs and libraries for accessing libvirt servers.The libvirt-client package includes the virsh command line tool to manage and control virtualized guests and hypervisors from the command line or a special virtualization shell.rootluotao # yum install virt-manager libvirt libvirt-python python-virtinst libvirt-client -y完成虚拟化管理扩展包的安装。这些软件包提供和非常丰富的工具用来管理kvm。有的是命令行工具,有的是图形化工具。都是基于GUN的开源软件。还有部分工具virt-v2v 和virt-v2p工具都是用来作迁移使用的。有兴趣可以测试和使用。使用命令行安装虚拟机?how to use kvm and how to install guest in kvm?1.Creating guests with virt-installrootluotao # virt-install -name rhel5 -hvm -ram 1024 -disk path=/tmp/win7.img,size=1-network network:default -vnc -os-variant vista -cdrom /mnt上面的操作命令,使用网络为默认的网络,guest的名字为rhel5.内存分配为1G。guest文件的位置为:/tmp/rhel5.img 使用/mnt为安装源。rootluotao # mount -o loop /home/luotao/rhel-server-5.7-i386-dvd.iso /mnt/mount: according to mtab /home/luotao/rhel-server-5.7-i386-dvd.iso is already mounted on /mnt/ as loop rootluotao # virt-install -name rhel5 -hvm -ram 1024 -disk path=/tmp/rhel5.img,size=1-network network:default -vnc -os-variant vista -cdrom /mnt开始安装.搜索文件 .treeinfo. | 876 B 00:00 .搜索文件 boot.iso. | 25 MB 00:00 .创建存储文件 rhel5.img | 1.0 GB 00:00创建域. | 0 B 00:00域安装仍在进行。请等待 完成安装。然后就会调用一个图形界面virt-viewer进行安装。更多的安装信息rootkvm # man virt-install值得注意的是:默认的镜像路径为:/var/lib/libvirt/images/,但是这个路径可以根据实际情况修改。 后面的章节会介绍如何更改镜像存储位置。(以上是使用命令行安装系统的过程,适合遭雷劈的场景。)1.3.3 使用virt-manager图形化安装虚拟机(推荐)使用virt-manager图形化的界面安装虚拟机。 rootluotao # virt-managerCreate a new guestThe virt-manager window allows you to create a new virtual machine. Click the New button to open the New VM wizard.Specify name and installation type这里输入rhel5.5做为guest的名称。点击下一步,选择一个安装源,这里选择从本地安装介质安装。如下图:安装源可以有以下几种:Local install media (ISO image or CDROM)This method uses a CD-ROM, DVD, or image of an installation disk (e.g. .iso).Network Install (HTTP, FTP, or NFS)Network installing involves the use of a mirrored Red Hat Enterprise Linux or Fedorainstallation tree to install a guest. The installation tree must be accessible through eitherHTTP, FTP, or NFS.Network Boot (PXE)This method uses a Preboot eXecution Environment (PXE) server to install the guest. Settingup a PXE server is covered in the Deployment Guide. To install via network boot, the guestmust have a routable IP address or shared network device. For information on the requirednetworking configuration for PXE installation, refer to Chapter 10, Network Configuration.Import existing disk imageThis method allows you to create a new guest and import a disk image (containing a pre-installed,bootableoperating system) to it.Configure CPU and memoryThe next step involves configuring the number of CPUs and amount of memory to allocate to the virtual machine. The wizard shows the number of CPUs and amount of memory you can allocate; configure these settings and click Forward. 这里分配1G内存和2个虚拟cpu资源给guest。如下图。给guest添加一个存储。注意的是,默认使用文件作为存储的话,文件的路径为/var/lib/libvirt/images/,如果没有把/var分区独立出来,那么很容易导致根分区磁盘空间不足。所以,这点尤其需要注意。但实际应用中,都会给kvm配置

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论