KVM虚拟化技术(一)虚拟化简介以及按安装

kvm简介

kvm(kernel+based Virtaul Machine)是目前主流的开源虚拟化解决方案,它是基于x86架构上的Linux操作系统的全虚拟化解决方案,在centos6.3系统中,kvm已经被集成在内核之中,相当于使用内核来做虚拟机的管理程序。由于其工作在内核层,所以执行效率要比传统的虚拟机化技术高很多,KVM虚拟化需要intel VT 或者 AMD-V技术的支持。需要确保操作系统支持虚拟机化。

调整BIOS

进入BIOS找到cpu相关设置,以华硕某主板为例

1

然后开启虚拟化

2

KVM的组件

KVM的虚拟机化组件包括Virtualization、Virtualization Client、Virtualization Platform、Virtualization Tools,其中

  • Virtualization 是提供虚拟化环境的
  • Viratualization Client 是提供虚拟机实例的客户端软件
  • Virtualition Platform 是提供访问和控制虚拟机和容器的接口
  • Virtualization Tools 是提供脱机虚拟机镜像管理工具

##KVM的存储方式

KVM的存储方式支持:本地磁盘文件、物理磁盘分区、LVM逻辑卷、iSCSI磁盘、GFS2文件系统、光纤LUNs设备等。

安装

1.实验环境:

  • 操作系统:Centos6.8
  • 内核:# Linux KVM 2.6.32-642.el6.x86_64

查看系统是否支持

[root@localhost ~]# lsmod | grep kvm
kvm_intel             170181  0
kvm                   554609  1 kvm_intel
irqbypass              13503  1 kvm

2.安装Virtualization 组

yum -y install kvm python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v
yum -y install libguestfs-tools

##创建虚拟机
虚拟机的创建可以通过图形化管理界面创建和管理也可以通过命令行工具实现。
1.图形化界面管理
使用virt-manager 来启动图形化界面创建

  • 命令行运行virt-manger 来启动kvm的管理程序

3

  • 然后点击新建按钮,新建一个虚拟机

4

报错信息:Warning:KVM is not available. This may mean the KVM package is not installed, or the KVM kernel modules are not loaded. your virtual machines may perform poorly. 是由于未启用虚拟机化,我这边使用的是VMware 虚拟机。关闭虚拟机后配置让其支持虚拟化。

5

  • 选择一种安装操作系统的方式,我这里选择第一种,使用本地的光驱或IOS文件

6

  • 加载镜像或光驱,并且选择操作系统类型和版本

7

  • 分配内存和CPU

8

  • 分配磁盘,其实和VMware安装虚拟机的步骤基本一致

9

-最后,配置网络确认配置无误后结束配置,创建完虚拟机

10

  • 创建完成后会自动启动虚拟机,安装操作系统就不讲了。。

11

2.命令行方式

使用 virt-install 来进行控制和管理虚拟机

[root@KVM ~]# virt-
virt-clone          virt-convert        virt-host-validate  virt-image          virt-install        virt-manager        virt-pki-validate   virt-top            virt-viewer         virt-what           virt-xml-validate
[root@KVM ~]# virt-install --help
Usage: virt-install --name NAME --ram RAM STORAGE INSTALL [options]

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  --connect=URI         Connect to hypervisor with libvirt URI

  General Options:
    -n NAME, --name=NAME
                        Name of the guest instance
    -r MEMORY, --ram=MEMORY
                        Memory to allocate for guest instance in megabytes
    --vcpus=VCPUS       Number of vcpus to configure for your guest. Ex:
                        --vcpus 5
                        --vcpus 5,maxcpus=10
                        --vcpus sockets=2,cores=4,threads=2
    --cpuset=CPUSET     Set which physical CPUs domain can use.
    --cpu=CPU           CPU model and features. Ex: --cpu coreduo,+x2apic
    --description=DESCRIPTION
                        Human readable description of the VM to store in the
                        generated XML.
    --security=SECURITY
                        Set domain security driver configuration.
    --numatune=NUMATUNE
                        Tune NUMA policy for the domain process.

  Installation Method Options:
    -c CDROM, --cdrom=CDROM
                        CD-ROM installation media
    -l LOCATION, --location=LOCATION
                        Installation source (eg, nfs:host:/path,
                        http://host/path, ftp://host/path)
    --pxe               Boot from the network using the PXE protocol
    --import            Build guest around an existing disk image
    --init=INIT         Path to init binary for container guest. Ex:
                        --init /path/to/app (to contain an application)
                        --init /sbin/init (for a full OS container)
    --livecd            Treat the CD-ROM media as a Live CD
    -x EXTRA, --extra-args=EXTRA
                        Additional arguments to pass to the install kernel
                        booted from --location
    --initrd-inject=INITRD_INJECTIONS
                        Add given file to root of initrd from --location
    --os-type=DISTRO_TYPE
                        The OS type being installed, e.g. 'linux', 'unix',
                        'windows'
    --os-variant=DISTRO_VARIANT
                        The OS variant being installed guests, e.g. 'fedora6',
                        'rhel5', 'solaris10', 'win2k'
    --boot=BOOTOPTS     Optionally configure post-install boot order, menu,
                        permanent kernel boot, etc.

  Storage Configuration:
    --disk=DISKOPTS     Specify storage with various options. Ex.
                        --disk path=/my/existing/disk
                        --disk path=/my/new/disk,size=5 (in gigabytes)
                        --disk vol=poolname:volname,device=cdrom,bus=scsi,...
    --nodisks           Don't set up any disks for the guest.
    --filesystem=FILESYSTEMS
                        Pass host directory to the guest. Ex:
                        --filesystem /my/source/dir,/dir/in/guest
                        --filesystem template_name,/,type=template

  Networking Configuration:
    -w NETWORK, --network=NETWORK
                        Configure a guest network interface. Ex:
                        --network bridge=mybr0
                        --network network=my_libvirt_virtual_net
                        --network network=mynet,model=virtio,mac=00:11...
    --nonetworks        Don't create network interfaces for the guest.

  Graphics Configuration:
    --graphics=GRAPHICS
                        Configure guest display settings. Ex:
                        --graphics vnc
                        --graphics spice,port=5901,tlsport=5902
                        --graphics none
                        --graphics vnc,password=foobar,port=5910,keymap=ja
    --noautoconsole     Don't automatically try to connect to the guest
                        console

  Device Options:
    --serial=SERIALS    Configure a guest serial device
    --parallel=PARALLELS
                        Configure a guest parallel device
    --channel=CHANNELS  Configure a guest communication channel
    --console=CONSOLES  Configure a text console connection between the guest
                        and host
    --host-device=HOSTDEVS
                        Configure physical host devices attached to the guest
    --soundhw=SOUNDHW   Configure guest sound device emulation
    --watchdog=WATCHDOG
                        Configure a guest watchdog device
    --video=VIDEO       Configure guest video hardware.
    --smartcard=SMARTCARD
                        Configure a guest smartcard device. Ex:
                        --smartcard mode=passthrough
    --redirdev=REDIRDEV
                        Configure a guest redirection device. Ex:
                        --redirdev usb,type=tcp,server=192.168.1.1:4000
    --panic=PANIC       Configure a guest panic device. Ex:
                        --panic default

  Virtualization Platform Options:
    -v, --hvm           This guest should be a fully virtualized guest
    -p, --paravirt      This guest should be a paravirtualized guest
    --container         This guest should be a container guest
    --virt-type=HV_TYPE
                        Hypervisor name to use (kvm, qemu, xen, ...)
    --arch=ARCH         The CPU architecture to simulate
    --machine=MACHINE   The machine type to emulate
    --noapic            Disables APIC for fully virtualized guest (overrides
                        value in os-type/os-variant db)
    --noacpi            Disables ACPI for fully virtualized guest (overrides
                        value in os-type/os-variant db)
    -u UUID, --uuid=UUID
                        UUID for the guest.

  Miscellaneous Options:
    --autostart         Have domain autostart on host boot up.
    --print-xml         Print the generated domain XML rather than define the
                        guest.
    --print-step=XMLSTEP
                        Print XML of a specific install step (1, 2, 3, all)
                        rather than define the guest.
    --noreboot          Don't boot guest after completing install.
    --wait=WAIT         Time to wait (in minutes)
    --dry-run           Run through install process, but do not create devices
                        or define the guest.
    --force             Forces 'yes' for any applicable prompts, terminates
                        for all others
    -q, --quiet         Suppress non-error output
    --prompt            Request user input for ambiguous situations or
                        required options.
    -d, --debug         Print debugging information