[Translated]RHCSA Series--Part 15--Essentials of Virtualization and Guest Administration with KVM.md

This commit is contained in:
Chang Liu 2015-10-04 17:11:58 +08:00
parent 63f12bf5f1
commit 4f03530ecd
2 changed files with 191 additions and 190 deletions

View File

@ -1,190 +0,0 @@
FSSlc translating
RHCSA Series: Essentials of Virtualization and Guest Administration with KVM Part 15
================================================================================
If you look up the word virtualize in a dictionary, you will find that it means “to create a virtual (rather than actual) version of something”. In computing, the term virtualization refers to the possibility of running multiple operating systems simultaneously and isolated one from another, on top of the same physical (hardware) system, known in the virtualization schema as host.
![KVM Virtualization Basics and KVM Guest Administration](http://www.tecmint.com/wp-content/uploads/2015/06/RHCSA-Part15.png)
RHCSA Series: Essentials of Virtualization and Guest Administration with KVM Part 15
Through the use of the virtual machine monitor (also known as hypervisor), virtual machines (referred to as guests) are provided virtual resources (i.e. CPU, RAM, storage, network interfaces, to name a few) from the underlying hardware.
With that in mind, it is plain to see that one of the main advantages of virtualization is cost savings (in equipment and network infrastructure and in terms of maintenance effort) and a substantial reduction in the physical space required to accommodate all the necessary hardware.
Since this brief how-to cannot cover all virtualization methods, I encourage you to refer to the documentation listed in the summary for further details on the subject.
Please keep in mind that the present article is intended to be a starting point to learn the basics of virtualization in RHEL 7 using [KVM][1] (Kernel-based Virtual Machine) with command-line utilities, and not an in-depth discussion of the topic.
### Verifying Hardware Requirements and Installing Packages ###
In order to set up virtualization, your CPU must support it. You can verify whether your system meets the requirements with the following command:
# grep -E 'svm|vmx' /proc/cpuinfo
In the following screenshot we can see that the current system (with an AMD microprocessor) supports virtualization, as indicated by svm. If we had an Intel-based processor, we would see vmx instead in the results of the above command.
![Check KVM Support](http://www.tecmint.com/wp-content/uploads/2015/06/Check-KVM-Support.png)
Check KVM Support
In addition, you will need to have virtualization capabilities enabled in the firmware of your host (BIOS or UEFI).
Now install the necessary packages:
- qemu-kvm is an open source virtualizer that provides hardware emulation for the KVM hypervisor whereas qemu-img provides a command line tool for manipulating disk images.
- libvirt includes the tools to interact with the virtualization capabilities of the operating system.
- libvirt-python contains a module that permits applications written in Python to use the interface supplied by libvirt.
- libguestfs-tools: miscellaneous system administrator command line tools for virtual machines.
- virt-install: other command-line utilities for virtual machine administration.
# yum update && yum install qemu-kvm qemu-img libvirt libvirt-python libguestfs-tools virt-install
Once the installation completes, make sure you start and enable the libvirtd service:
# systemctl start libvirtd.service
# systemctl enable libvirtd.service
By default, each virtual machine will only be able to communicate with the rest in the same physical server and with the host itself. To allow the guests to reach other machines inside our LAN and also the Internet, we need to set up a bridge interface in our host (say br0, for example) by,
1. adding the following line to our main NIC configuration (most likely `/etc/sysconfig/network-scripts/ifcfg-enp0s3`):
BRIDGE=br0
2. creating the configuration file for br0 (/etc/sysconfig/network-scripts/ifcfg-br0) with these contents (note that you may have to change the IP address, gateway address, and DNS information):
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
IPADDR=192.168.0.18
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
NM_CONTROLLED=no
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=br0
ONBOOT=yes
DNS1=8.8.8.8
DNS2=8.8.4.4
3. finally, enabling packet forwarding by making, in `/etc/sysctl.conf`,
net.ipv4.ip_forward = 1
and loading the changes to the current kernel configuration:
# sysctl -p
Note that you may also need to tell firewalld that this kind of traffic should be allowed. Remember that you can refer to the article on that topic in this same series ([Part 11: Network Traffic Control Using FirewallD and Iptables][2]) if you need help to do that.
### Creating VM Images ###
By default, VM images will be created to `/var/lib/libvirt/images` and you are strongly advised to not change this unless you really need to, know what youre doing, and want to handle SELinux settings yourself (such topic is out of the scope of this tutorial but you can refer to Part 13 of the RHCSA series: [Mandatory Access Control Essentials with SELinux][3] if you want to refresh your memory).
This means that you need to make sure that you have allocated the necessary space in that filesystem to accommodate your virtual machines.
The following command will create a virtual machine named `tecmint-virt01` with 1 virtual CPU, 1 GB (=1024 MB) of RAM, and 20 GB of disk space (represented by `/var/lib/libvirt/images/tecmint-virt01.img`) using the rhel-server-7.0-x86_64-dvd.iso image located inside /home/gacanepa/ISOs as installation media and the br0 as network bridge:
# virt-install \
--network bridge=br0
--name tecmint-virt01 \
--ram=1024 \
--vcpus=1 \
--disk path=/var/lib/libvirt/images/tecmint-virt01.img,size=20 \
--graphics none \
--cdrom /home/gacanepa/ISOs/rhel-server-7.0-x86_64-dvd.iso
--extra-args="console=tty0 console=ttyS0,115200"
If the installation file was located in a HTTP server instead of an image stored in your disk, you will have to replace the cdrom flag with location and indicate the address of the online repository.
As for the graphics none option, it tells the installer to perform the installation in text-mode exclusively. You can omit that flag if you are using a GUI interface and a VNC window to access the main VM console. Finally, with extra-args we are passing kernel boot parameters to the installer that set up a serial VM console.
The installation should now proceed as a regular (real) server now. If not, please review the steps listed above.
### Managing Virtual Machines ###
These are some typical administration tasks that you, as a system administrator, will need to perform on your virtual machines. Note that all of the following commands need to be run from your host:
**1. List all VMs:**
# virsh list --all
From the output of the above command you will have to note the Id for the virtual machine (although it will also return its name and current status) because you will need it for most administration tasks related to a particular VM.
**2. Display information about a guest:**
# virsh dominfo [VM Id]
**3. Start, restart, or stop a guest operating system:**
# virsh start | reboot | shutdown [VM Id]
**4. Access a VMs serial console if networking is not available and no X server is running on the host:**
# virsh console [VM Id]
**Note** that this will require that you add the serial console configuration information to the `/etc/grub.conf` file (refer to the argument passed to the extra-args option when the VM was created).
**5. Modify assigned memory or virtual CPUs:**
First, shutdown the guest:
# virsh shutdown [VM Id]
Edit the VM configuration for RAM:
# virsh edit [VM Id]
Then modify
<memory>[Memory size here without brackets]</memory>
Restart the VM with the new settings:
# virsh create /etc/libvirt/qemu/tecmint-virt01.xml
Finally, change the memory dynamically:
# virsh setmem [VM Id] [Memory size here without brackets]
For CPU:
# virsh edit [VM Id]
Then modify
<cpu>[Number of CPUs here without brackets]</cpu>
For further commands and details, please refer to table 26.1 in Chapter 26 of the RHEL 5 Virtualization guide (that guide, though a bit old, includes an exhaustive list of virsh commands used for guest administration).
### SUMMARY ###
In this article we have covered some basic aspects of virtualization with KVM in RHEL 7, which is both a vast and a fascinating topic, and I hope it will be helpful as a starting guide for you to later explore more advanced subjects found in the official [RHEL virtualization][4] getting started and [deployment / administration guides][5].
In addition, you can refer to the preceding articles in [this KVM series][6] in order to clarify or expand some of the concepts explained here.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/kvm-virtualization-basics-and-guest-administration/
作者:[Gabriel Cánepa][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/gacanepa/
[1]:http://www.linux-kvm.org/page/Main_Page
[2]:http://www.tecmint.com/firewalld-vs-iptables-and-control-network-traffic-in-firewall/
[3]:http://www.tecmint.com/selinux-essentials-and-control-filesystem-access/
[4]:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Getting_Started_Guide/index.html
[5]:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Deployment_and_Administration_Guide/index.html
[6]:http://www.tecmint.com/install-and-configure-kvm-in-linux/

View File

@ -0,0 +1,191 @@
RHCSA 系列: 虚拟化基础和使用 KVM 进行虚拟机管理 Part 15
================================================================================
假如你在词典中查一下单词 “virtualize”你将会发现它的意思是 “创造某些事物的一个虚拟物(而非真实的)”。在计算机行业中,术语虚拟化指的是:在相同的物理(硬件)系统上,同时运行多个操作系统,且这几个系统相互隔离的可能性,而那个硬件在虚拟化架构中被称作宿主机(host)。
![KVM 虚拟化基础和 KVM 虚拟机管理](http://www.tecmint.com/wp-content/uploads/2015/06/RHCSA-Part15.png)
RHCSA 系类: 虚拟化基础和使用 KVM 进行虚拟机管理 Part 15
通过使用虚拟机监视器(也被称为虚拟机管理程序 hypervisor虚拟机被称为 guest由底层的硬件来提供虚拟资源举几个例来说如 CPURAM存储介质网络接口等
考虑到这一点就可以清楚地看出,虚拟化的主要优点是节约成本(在设备和网络基础设施,及维护工作等方面)和显著地减少容纳所有必要硬件所需的物理空间。
由于这个简单的指南不能涵盖所有的虚拟化方法,我鼓励你参考在总结部分中列出的文档,以此对这个话题做更深入的了解。
请记住当前文章只是用于在 RHEL 7 中用命令行工具使用 [KVM][1] (Kernel-based Virtual Machine) 学习虚拟化基础知识的一个起点,而并不是对这个话题的深入探讨。
### 检查硬件要求并安装软件包 ###
为了设置虚拟化,你的 CPU 必须能够支持它。你可以使用下面的命令来查看你的系统是否满足这个要求:
# grep -E 'svm|vmx' /proc/cpuinfo
在下面的截图中,我们可以看到当前的系统(带有一个 AMD 的微处理器支持虚拟化svm 字样的存在暗示了这一点。假如我们有一个 Intel 系列的处理器,我们将会看到上面命令的结果将会出现 vmx 字样。
![检查 KVM 支持](http://www.tecmint.com/wp-content/uploads/2015/06/Check-KVM-Support.png)
检查 KVM 支持
另外你需要在你宿主机的硬件BIOS 或 UEFI中开启虚拟化。
现在,安装必要的软件包:
- qemu-kvm 是一个开源的虚拟机程序,为 KVM 虚拟机监视器提供硬件仿真,而 qemu-img 则提供了一个操纵磁盘镜像的命令行工具。
- libvirt 包含与操作系统的虚拟化功能交互的工具。
- libvirt-python 包含一个模块,它允许用 Python 写的应用来使用由 libvirt 提供的接口。
- libguestfs-tools 包含各式各样的针对虚拟机的系统管理员命令行工具。
- virt-install 包含针对虚拟机管理的其他命令行工具。
# yum update && yum install qemu-kvm qemu-img libvirt libvirt-python libguestfs-tools virt-install
一旦安装完全,请确保你启动并开启了 libvirtd 服务:
# systemctl start libvirtd.service
# systemctl enable libvirtd.service
默认情况下,每个虚拟机将只能够与相同的物理服务器和宿主机自身通信。要使得虚拟机能够访问位于局域网或因特网中的其他机器,我们需要像下面这样在我们的宿主机上设置一个桥接接口(比如说 br0
1. 添加下面的一行到我们的 NIC 主配置中(一般是 `/etc/sysconfig/network-scripts/ifcfg-enp0s3` 这个文件):
BRIDGE=br0
2. 使用下面的内容(注意,你可能必须更改 IP 地址,网关地址和 DNS 信息)为 br0 创建一个配置文件(`/etc/sysconfig/network-scripts/ifcfg-br0`
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
IPADDR=192.168.0.18
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
NM_CONTROLLED=no
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=br0
ONBOOT=yes
DNS1=8.8.8.8
DNS2=8.8.4.4
3. 最后通过使得文件`/etc/sysctl.conf` 中的
net.ipv4.ip_forward = 1
来开启包转发并加载更改到当前的内核配置中:
# sysctl -p
注意,你可能还需要告诉 firewalld 这类的流量应当被允许通过防火墙。假如你需要这样做,记住你可以参考这个系列的 [Part 11: 使用 firewalld 和 iptables 来进行网络流量控制][2]。
### 创建虚拟机镜像 ###
默认情况下,虚拟机镜像将会被创建到 `/var/lib/libvirt/images` 中,且强烈建议你不要更改这个设定,除非你真的需要那么做且知道你在做什么,并能自己处理有关 SELinux 的设定(这个话题已经超出了本教程的讨论范畴,但你可以参考这个系列的第 13 部分[使用 SELinux 来进行强制访问控制][3],假如你想更新你的知识的话)。
这意味着你需要确保你在文件系统中分配了必要的空间来容纳你的虚拟机。
下面的命令将使用位于 `/home/gacanepa/ISOs`目录下的 rhel-server-7.0-x86_64-dvd.iso 镜像文件和 br0 这个网桥来创建一个名为 `tecmint-virt01` 的虚拟机,它有一个虚拟 CPU1 GB=1024 MB的 RAM20 GB 的磁盘空间(由`/var/lib/libvirt/images/tecmint-virt01.img`所代表):
# virt-install \
--network bridge=br0
--name tecmint-virt01 \
--ram=1024 \
--vcpus=1 \
--disk path=/var/lib/libvirt/images/tecmint-virt01.img,size=20 \
--graphics none \
--cdrom /home/gacanepa/ISOs/rhel-server-7.0-x86_64-dvd.iso
--extra-args="console=tty0 console=ttyS0,115200"
假如安装文件位于一个 HTTP 服务器上,而不是存储在你磁盘中的镜像中,你必须将上面的 `-cdrom` 替换为 `-location`,并明显地指出在线存储仓库的地址。
至于上面的 `graphics none` 选项,它告诉安装程序只以文本模式执行安装过程。假如你使用一个 GUI 界面和一个 VNC 窗口来访问主虚拟机控制台,则可以省略这个选项。最后,使用 `extra-args`参数,我们将传递内核启动参数给安装程序,以此来设置一个串行的虚拟机控制台。
现在,安装应当作为一个正常的(真实的)服务来执行了。假如没有,请查看上面列出的步骤。
### 管理虚拟机 ###
作为一个系统管理员,还有一些典型的管理任务需要你在虚拟机上去完成。注:下面所有的命令都需要在你的宿主机上运行:
**1. 列出所有的虚拟机:**
# virsh list --all
你必须留意上面命令输出中的虚拟机 ID尽管上面的命令还会返回虚拟机的名称和当前的状态因为你需要它来执行有关某个虚拟机的大多数管理任务。
**2. 显示某个虚拟机的信息:**
# virsh dominfo [VM Id]
**3. 开启,重启或停止一个虚拟机操作系统:**
# virsh start | reboot | shutdown [VM Id]
**4. 假如网络无法连接且在宿主机上没有运行 X 服务器,可以使用下面的目录来访问虚拟机的串行控制台:**
# virsh console [VM Id]
**注** 这需要你添加一个串行控制台配置信息到 `/etc/grub.conf` 文件中(参考刚才创建虚拟机时传递给`extra-args`选项的参数)。
**5. 修改分配的内存或虚拟 CPU**
首先,关闭虚拟机:
# virsh shutdown [VM Id]
为 RAM 编辑虚拟机的配置:
# virsh edit [VM Id]
然后更改
<memory>[内存大小,这里没有括号]</memory>
使用新的设定重启虚拟机:
# virsh create /etc/libvirt/qemu/tecmint-virt01.xml
最后,可以使用下面的命令来动态地改变内存的大小:
# virsh setmem [VM Id] [内存大小,这里没有括号]
对于 CPU使用
# virsh edit [VM Id]
然后更改
<cpu>[CPU 数目,这里没有括号]</cpu>
至于更深入的命令和细节,请参考 RHEL 5 虚拟化指南(这个指南尽管有些陈旧,但包括了用于管理虚拟机的 virsh 命令的详尽清单)的第 26 章里的表 26.1。
### 总结 ###
在这篇文章中,我们涵盖了在 RHEL 7 中如何使用 KVM 和虚拟化的一些基本概念,这个话题是一个广泛且令人着迷的话题。并且我希望它能成为你在随后阅读官方的 [RHEL 虚拟化入门][4] 和 [RHEL 虚拟化部署和管理指南][5] ,探索更高级的主题时的起点教程,并给你带来帮助。
另外,为了分辨或拓展这里解释的某些概念,你还可以参考先前包含在 [KVM 系列][6] 中的文章。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/kvm-virtualization-basics-and-guest-administration/
作者:[Gabriel Cánepa][a]
译者:[FSSlc](https://github.com/FSSlc)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/gacanepa/
[1]:http://www.linux-kvm.org/page/Main_Page
[2]:http://www.tecmint.com/firewalld-vs-iptables-and-control-network-traffic-in-firewall/
[3]:http://www.tecmint.com/selinux-essentials-and-control-filesystem-access/
[4]:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Getting_Started_Guide/index.html
[5]:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Deployment_and_Administration_Guide/index.html
[6]:http://www.tecmint.com/install-and-configure-kvm-in-linux/