Install and use CentOS 7 or RHEL 7 as KVM virtualization host

前端之家收集整理的这篇文章主要介绍了Install and use CentOS 7 or RHEL 7 as KVM virtualization host前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


@H_404_4@ When thinking about virtualization,everybody immediately thinks about VMWare. And it must be said,the product they offer is very decent but also comes with a “decent” price. As an alternative,it’s worth looking into KVM for your virtualization. As with the VMWare product range,KVM offers full virtualization and it can compete with VMWare regarding stability and performance.

@H_404_4@

Virtualization-terminology

@H_404_4@ To prevent things getting confused I would first like to clear out some terminology used for virtualization. For somebody working on a daily basis in virtual environments,these might be clear but can be rather confusing for others.

@H_404_4@ Host: the machine that hosts other system,KVM will be installed on this machine
Guest: the system running on the host,also referred to as VM,Virtual Machine or domain.
Hypervisor: the piece of software that enables virtualization on the host. For example: KVM,ESXi,Xen,…

Part1: KVM installation and preparation

KVM hypervisor and VM-extensions

@H_404_4@ As mentioned earlier,KVM offers,as VMWare,full virtualization. This means that a full system,which looks like a real physical system to the guest-OS,will be offered. Besides full virtualization,there is also such a thing as paravirtualization,as Xen can offer. Paravirtualization givesyou higher performance but needs a modified guest-OS and is basically limited to *nix-systems. Full virtualization enables you to run unmodified guest-systems and thus also most proprietary systems as Windows. In order to be able to use full virtualization,you either need some virtualization-extensions on your cpu or use emulation.

@H_404_4@ First thing to do is to check if the host-machinesupports VM-extensions. On the x86 platofrom,those are either AMD-V or Intel’s VT-X. In order to check if the installed cpu’s support those extensions,we need to check if the vmx (for VT-X) or svm (for AMD-V) flag exists in the cpuinfo-output:

@H_404_4@ When the output is 0,meaning that neither vmx or svm is found in the flags,it probably means that yourcpu doesn’t support those extensions and there is little you can do. When the extensions are listed,be sure to check if they are enabled in the systems BIOS since that would cause problems later on. In case your cpu doesn’t support VM-extensions,you are limited to QEMU-emulation in combination with KVM,which delivers a much worse performance in comparison. For this tutorial,I’ll assume that the VM-extensions are supported and enabled in the BIOS of the host-system.

KVM installation

@H_404_4@ The first step in the KVM installation is installing the necessary packages. Package virt-manager,xauth and dejavu-lgc-sans-fonts are also needed if you want to manage KVM with the graphical interface in combination withX11 forwarding. (for more information,checkthis previous post about X11 forwarding)

@H_404_4@ To install the required packages

Networking @H_404_4@ For the networking part,our KVM-host will act as a router for its guests and wewill need to create a bridge interface to allow the guest to communicate out of the host. Guests will use NAT on the host to connect to the real network. To allow such type of setup it’s neededto allow ip forwarding in the kernel parameters.

@H_404_4@ After allowing the host to do ip forwarding,we need to change the network configuration. Basically we will keep our original physical interface as it is but will assign its IP-address to the brige. In the example host-machinethere is one real interface called eno16777736 and the script in/etc/sysconfig/network-scripts/ifcfg-eno16777736 looks like this:

@H_404_4@ The first thing to change here,is to comment out everything that is IP-related and tell the interface which interface will be the bridge. Resulting in /etc/sysconfig/network-scripts/ifcfg-eno16777736 to look like this:

8
DEVICE "eno16777736"
ONBOOT =yes
#IPADDR "192.168.202.111"
#NETMASK "255.255.255.0"
#GATEWAY "192.168.202.2"
HWADDR "00:0c:29:32:d0:4c"
#DNS1 "192.168.202.2"
BRIDGE =virbr0
@H_404_4@ Next,we can create the config-script for the bridge interface virbr0 in/etc/sysconfig/network-scripts/ifcfg-virbr0. Most details can be copied from the original script foreno16777736:

DEVICE "virbr0"
TYPE =BRIDGE
ONBOOT =yes
BOOTPROTO =static
IPADDR "192.168.202.111"
NETMASK "255.255.255.0"
GATEWAY "192.168.202.2"
DNS1 Finish and check the KVM installation @H_404_4@ Basically all components are now ok but before KVM can be used it’s a good idea to perform a reboot in order to load the kvm-modules and to relaod the new network settings.

@H_404_4@ After the reboot,we should check if the necessary kernel modules are loaded,which means that KVM successfully can handle the VM-extensions of our cpu:

@H_404_4@ Check if the bridge is installed and in an up-state:

@H_404_4@ Last thing to check is if we can connect to KVM by asking for a simple list of systems:

@H_404_4@ If it returns something else,then you should go trough the earlier steps to check where something went wrong.

Part 2: Using KVM with the CLI

@H_404_4@ After completing the KVM installation,it’s time to start using the host. First thing we need to do is to create a new domain or VM.

Adding a new VM

@H_404_4@ To create a new virtual machine using the CLI,we need to know which template we will use to install the system. To get a list of templates that are known in our KVM installation,you can do the following:

@H_404_4@ Virtual disk images for the KVM-guests can be placed in /var/lib/libvirt by default. In case you prefer to use another location to store the disk images,SELinux will,by default,prevent access and the security context of that location needs to be changed in order to use it for KVM. To change the SELinux context when storing the images in another location (/vm for example):

@H_404_4@ Now,to add a new VM,we can use virt-install.

Example to add a windows-guest:

@H_404_4@

@H_404_4@ Explanation of the arguments that were given to virt-install:

@H_404_4@ After launching the above command,you should be able to connect with VNC to the host and get on the console-display of the VM. The console displays what would normally,on a physical machine,appear on the attached monitor.

@H_404_4@ By default,VNC will use the first availablescreen on port 5900. To be sure which screen is used,we can use virsh to show the attached console-screens for VNC:

@H_404_4@ :0 means the first screen and real port 5900 as you can also see when checking with netstat which ports are currently listening:

404_4@

@H_404_4@ From this point,we can complete the windows installation as if it would be a normal physical system:

@H_404_4@ After completing the installation with VNC,we end up with a Windows-VM that is running on our KVM-host:

@H_404_4@ As for the networking part,we use the earlier created bridge (virbr0) to do NAT. This means that the KVM-host NAT’s all our connections to the real network connected to the KVM-host. If DHCP is active on that network,it can be used in the VM. Otherwise you will have to configure a static IP in the same subnet.

Example to add a Linux-guest: @H_404_4@ To add a Linux guest,next to the already added Windows-guest is quite similar:

@H_404_4@ As with the Windows-VM,after launching this command,you should be able to connect with VNC to the host and get on the console of the VM to complete theDebian installation.

@H_404_4@ To know which VNC-display number (and port) is used for a certain VM,the same command as used earlier should do:

@H_404_4@ Above command gives :1 as result,meaning that the guest vmdeb7 can be contacted with VNC on port 5901:

@H_404_4@ After finishing the installation,weend up with a Linux guest running on top of our KVM-host. Which Linux distro we are using doesn’t matter since we’re doing full virtualization.

@H_404_4@ Considering network,the same as with the Windows VM applies here. Our connections are NATted trough the KVM-host and we can use the DHCP-server of our real network.

More KVM actions @H_404_4@ Besides creating VM’s,it’s a good thing to know some basic operations regarding VM-managent.

List the active virtual machines:

@H_404_4@

Get more information about a guest: @H_404_4@

Stop a running guest: @H_404_4@ To stop a running VM in a clean way (as you would press the power button to start the shutdown sequence):

@H_404_4@ This triggers a normal,clean,shutdown on the guest:

@H_404_4@ To force stop a running VM that doesn’t want to shutdown in a clean way:

Start aguest: @H_404_4@

Delete aguest: @H_404_4@ First we need to make sure that the guest is stopped before it can be deleted. In case you don’t want the virtual disk image anymore either,you’ll have to delete it manually after undefining the guest.

@H_404_4@ After removing a disk-image,it’s a good thing to refresh the storage pool of KVM:

Automatically let a guest start when the host starts @H_404_4@ When rebooting your host,you probably want some or all the guests that are defined on thathost to start at the same time. By default,the guest are not automatically started.

@H_404_4@

Part 3: Using KVM with the virt-manager GUI

Starting the GUI

@H_404_4@ Managing KVM with the CLI is not so difficult and it can be very handy to script certain day-to-day tasks. Sometimes,you just need to keep an overview and require a little more user-friendliness. For that,you can use virt-manager,which is a graphical interface for libvirt and is mainly built for KVM. When you want to manage your guest with virt-manager,you can either do it on the host itself,by starting an X-server locally or use X11 forwarding on a headless server (more informationhere).

@H_404_4@ Make sure that you have enough permissions to use virt-manager and simply execute virt-manager from the command line:

@H_404_4@ If all goes well,you should be presented with the virt-manager GUI:

Basic actions @H_404_4@ From the initial start-up screen,you can immediately see a list of configured guests on this host and take actions on them like: Run,Pause,Shutdown,Reboot,Force off,…

@H_404_4@ When selecting a guest,you can also click on Open to display the console as we did earlier using VNC:

Advanced actions @H_404_4@ Other possibilities using the virt-manager interface:

@H_404_4@ Connect to another host-system to manage the VM’s running there,using File -> Add connection (like using the –connect on the CLI)

@H_404_4@ Migrate a VM to another KVM-host: right click on the VM and choose Migrate…

@H_404_4@ Clone a VM to the same or another KVM-host: right click on the VM and choose Clone…

@H_404_4@ As you can see,the virt-manager interface is not very complicated and most of the basic tasks don’t need any explanation.

@H_404_4@ After completing all of the above steps,basic installation and using KVM shouldn’t have any secrets anymore for you. The next thing to do is experiment and test a little more with KVM and hopefully start to use it in your production environment.

@H_404_4@

url---http://jensd.be/207/linux/install-and-use-centos-7-as-kvm-virtualization-host

猜你在找的CentOS相关文章