Merge pull request #8004 from MjSeven/master

20180127 How to install KVM on CentOS 7 - RHEL 7 Headless Server.md 翻译完毕
This commit is contained in:
Lv Feng 2018-03-05 19:40:27 +08:00 committed by GitHub
commit 885ba8b6f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,58 +1,56 @@
Translating by MjSeven
How to install KVM on CentOS 7 / RHEL 7 Headless Server
如何在 CentOS 7 / RHEL 7 终端服务器上安装 KVM
======
如何在 CnetOS 7 或 RHEL 7( Red Hat 企业版 Linux) 服务器上安装和配置 KVM(基于内核的虚拟机)?如何在 CnetOS 7 上设置 KMV 并使用云镜像/ cloud-init 来安装客户虚拟机?
How do I install and configure KVM (Kernel-based Virtual Machine) on a CentOS 7 or RHEL (Red Hat Enterprise Linux) 7 server? How can I setup KMV on a CentOS 7 and use cloud images/cloud-init for installing guest VM?
Kernel-based Virtual Machine (KVM) is virtualization software for CentOS or RHEL 7. KVM turn your server into a hypervisor. This page shows how to setup and manage a virtualized environment with KVM in CentOS 7 or RHEL 7. It also described how to install and administer Virtual Machines (VMs) on a physical server using the CLI. Make sure that **Virtualization Technology (VT)** is enabled in your server 's BIOS. You can also run the following command [to test if CPU Support Intel VT and AMD-V Virtualization tech][1]
基于内核的虚拟机KVM是 CentOS 或 RHEL 7 的虚拟化软件。KVM 将你的服务器变成虚拟机管理程序。本文介绍如何在 CentOS 7 或 RHEL 7 中使用 KVM 设置和管理虚拟化环境。还介绍了如何使用 CLI 在物理服务器上安装和管理虚拟机VM。确保在服务器的 BIOS 中启用了**虚拟化技术(vt)**。你也可以运行以下命令[测试 CPU 是否支持 Intel VT 和 AMD_V 虚拟化技术][1]。
```
$ lscpu | grep Virtualization
Virtualization: VT-x
```
### 按照 CentOS 7/RHEL 7 终端服务器上的 KVM 安装步骤进行操作
#### 步骤 1: 安装 kvm
### Follow installation steps of KVM on CentOS 7/RHEL 7 headless sever
#### Step 1: Install kvm
Type the following [yum command][2]:
输入以下 [yum 命令][2]:
`# yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install`
[![How to install KVM on CentOS 7 RHEL 7 Headless Server][3]][3]
Start the libvirtd service:
启动 libvirtd 服务:
```
# systemctl enable libvirtd
# systemctl start libvirtd
```
#### Step 2: Verify kvm installation
#### 步骤 2: 确认 kvm 安装
Make sure KVM module loaded using lsmod command and [grep command][4]:
确保使用 lsmod 命令和 [grep命令][4] 加载 KVM 模块:
`# lsmod | grep -i kvm`
#### Step 3: Configure bridged networking
#### 步骤 3: 配置桥接网络
By default dhcpd based network bridge configured by libvirtd. You can verify that with the following commands:
默认情况下,由 libvirtd 配置的基于 dhcpd 的网桥。你可以使用以下命令验证:
```
# brctl show
# virsh net-list
```
[![KVM default networking][5]][5]
All VMs (guest machine) only have network access to other VMs on the same server. A private network 192.168.122.0/24 created for you. Verify it:
所有虚拟机(客户机器)只能在同一台服务器上对其他虚拟机进行网络访问。为你创建的私有网络是 192.168.122.0/24。验证
`# virsh net-dumpxml default`
If you want your VMs avilable to other servers on your LAN, setup a a network bridge on the server that connected to the your LAN. Update your nic config file such as ifcfg-enp3s0 or em1:
如果你希望你的虚拟机可用于 LAN 上的其他服务器,请在连接到你的 LAN 的服务器上设置一个网桥。更新你的网卡配置文件,如 ifcfg-enp3s0 或 em1:
`# vi /etc/sysconfig/network-scripts/enp3s0 `
Add line:
添加一行:
```
BRIDGE=br0
```
[Save and close the file in vi][6]. Edit /etc/sysconfig/network-scripts/ifcfg-br0 and add:
[使用 vi 保存并关闭文件][6]。编辑 /etc/sysconfig/network-scripts/ifcfg-br0 :
`# vi /etc/sysconfig/network-scripts/ifcfg-br0`
Append the following:
添加以下东西:
```
DEVICE="br0"
# I am getting ip from DHCP server #
@ -64,27 +62,29 @@ TYPE="Bridge"
DELAY="0"
```
Restart the networking service (warning ssh command will disconnect, it is better to reboot the box):
重新启动网络服务警告ssh命令将断开连接最好重新启动该设备
`# systemctl restart NetworkManager`
Verify it with brctl command:
用 brctl 命令验证它:
`# brctl show`
#### Step 4: Create your first virtual machine
#### 步骤 4: 创建你的第一个虚拟机
I am going to create a CentOS 7.x VM. First, grab CentOS 7.x latest ISO image using the wget command:
我将会创建一个 CentOS 7.x 虚拟机。首先,使用 wget 命令获取 CentOS 7.x 最新的 ISO 镜像:
```
# cd /var/lib/libvirt/boot/
# wget https://mirrors.kernel.org/centos/7.4.1708/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso
```
Verify ISO images:
验证 ISO 镜像:
```
# wget https://mirrors.kernel.org/centos/7.4.1708/isos/x86_64/sha256sum.txt
# sha256sum -c sha256sum.txt
```
##### Create CentOS 7.x VM
##### 创建 CentOS 7.x 虚拟机
In this example, I'm creating CentOS 7.x VM with 2GB RAM, 2 CPU core, 1 nics and 40GB disk space, enter:
在这个例子中,我创建了 2GB RAM2 个 CPU 核心1 个网卡和 40 GB 磁盘空间的 CentOS 7.x 虚拟机,输入:
```
# virt-install \
--virt-type=kvm \
@ -97,31 +97,36 @@ In this example, I'm creating CentOS 7.x VM with 2GB RAM, 2 CPU core, 1 nics and
--graphics vnc \
--disk path=/var/lib/libvirt/images/centos7.qcow2,size=40,bus=virtio,format=qcow2
```
To configure vnc login from another terminal over ssh and type:
从另一个终端通过 ssh 和 type 配置 vnc 登录:
```
# virsh dumpxml centos7 | grep vnc
# virsh dumpxml centos7 | grep v nc
<graphics type='vnc' port='5901' autoport='yes' listen='127.0.0.1'>
```
Please note down the port value (i.e. 5901). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. Type the following SSH port forwarding command from your client/desktop/macbook pro system:
请记录下端口值(即 5901。你需要使用 SSH 客户端来建立隧道和 VNC 客户端才能访问远程 vnc 服务区。在客户端/桌面/ macbook pro 系统中输入以下 SSH 端口转化命令:
`$ ssh vivek@server1.cyberciti.biz -L 5901:127.0.0.1:5901`
Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5901 as follows:
一旦你建立了 ssh 隧道,你可以将你的 VNC 客户端指向你自己的 127.0.0.1 (localhost) 地址和端口 5901如下所示
[![][7]][7]
You should see CentOS Linux 7 guest installation screen as follows:
你应该看到 CentOS Linux 7 客户虚拟机安装屏幕如下:
[![][8]][8]
Now just follow on screen instructions and install CentOS 7. Once installed, go ahead and click the reboot button. The remote server closed the connection to our VNC client. You can reconnect via KVM client to configure the rest of the server including SSH based session or firewall.
#### Step 5: Using cloud images
现在只需按照屏幕说明进行操作并安装CentOS 7。一旦安装完成后请继续并单击重启按钮。 远程服务器关闭了我们的 VNC 客户端的连接。 你可以通过 KVM 客户端重新连接,以配置服务器的其余部分,包括基于 SSH 的会话或防火墙。
The above installation method is okay for learning purpose or a single VM. Do you need to deploy lots of VMs? Try cloud images. You can modify pre built cloud images as per your needs. For example, add users, ssh keys, setup time zone, and more using [Cloud-init][9] which is the defacto multi-distribution package that handles early initialization of a cloud instance. Let us see how to create CentOS 7 vm with 1024MB ram, 20GB disk space, and 1 vCPU.
#### 步骤 5: 使用云镜像
##### Grab CentOS 7 cloud image
以上安装方法对于学习目的或单个虚拟机而言是可行的。你需要部署大量的虚拟机吗? 尝试云镜像。你可以根据需要修改预先构建的云图像。例如,使用 [Cloud-init][9] 添加用户ssh 密钥,设置时区等等,这是处理云实例的早期初始化的事实上的多分发包。让我们看看如何创建带有 1024MB RAM20GB 磁盘空间和 1 个 vCPU 的 CentOS 7 虚拟机。(译注: vCPU 即电脑中的虚拟处理器)
##### 获取 CentOS 7 云镜像
```
# cd /var/lib/libvirt/boot
# wget http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2
```
##### Create required directories
##### 创建所需的目录
```
# D=/var/lib/libvirt/images
@ -130,29 +135,31 @@ The above installation method is okay for learning purpose or a single VM. Do yo
mkdir: created directory '/var/lib/libvirt/images/centos7-vm1'
```
##### Create meta-data file
##### 创建元数据文件
```
# cd $D/$VM
# vi meta-data
```
Append the following:
添加以下东西:
```
instance-id: centos7-vm1
local-hostname: centos7-vm1
```
##### Crete user-data file
##### 创建用户数据文件
I am going to login into VM using ssh keys. So make sure you have ssh-keys in place:
我将使用 ssh 密钥登录到虚拟机。所以确保你有 ssh-keys
`# ssh-keygen -t ed25519 -C "VM Login ssh key"`
[![ssh-keygen command][10]][11]
See "[How To Setup SSH Keys on a Linux / Unix System][12]" for more info. Edit user-data as follows:
请参阅 "[如何在 Linux/Unix 系统上设置 SSH 密钥][12]" 来获取更多信息。编辑用户数据如下:
```
# cd $D/$VM
# vi user-data
```
Add as follows (replace hostname, users, ssh-authorized-keys as per your setup):
添加如下根据你的设置替换主机名用户ssh-authorized-keys
```
#cloud-config
@ -192,14 +199,14 @@ runcmd:
- yum -y remove cloud-init
```
##### Copy cloud image
##### 复制云镜像
```
# cd $D/$VM
# cp /var/lib/libvirt/boot/CentOS-7-x86_64-GenericCloud.qcow2 $VM.qcow2
```
##### Create 20GB disk image
##### 创建 20GB 磁盘映像
```
# cd $D/$VM
@ -208,25 +215,25 @@ runcmd:
# virt-resize --quiet --expand /dev/sda1 $VM.qcow2 $VM.new.image
```
[![Set VM image disk size][13]][13]
Overwrite it resized image:
覆盖它的缩放图片:
```
# cd $D/$VM
# mv $VM.new.image $VM.qcow2
```
##### Creating a cloud-init ISO
##### 创建一个 cloud-init ISO
`# mkisofs -o $VM-cidata.iso -V cidata -J -r user-data meta-data`
[![Creating a cloud-init ISO][14]][14]
##### Creating a pool
##### 创建一个 pool
```
# virsh pool-create-as --name $VM --type dir --target $D/$VM
Pool centos7-vm1 created
```
##### Installing a CentOS 7 VM
##### 安装 CentOS 7 虚拟机
```
# cd $D/$VM
@ -240,58 +247,59 @@ Pool centos7-vm1 created
--graphics spice \
--noautoconsole
```
Delete unwanted files:
删除不需要的文件:
```
# cd $D/$VM
# virsh change-media $VM hda --eject --config
# rm meta-data user-data centos7-vm1-cidata.iso
```
##### Find out IP address of VM
##### 查找虚拟机的 IP 地址
`# virsh net-dhcp-leases default`
[![CentOS7-VM1- Created][15]][15]
##### Log in to your VM
##### 登录到你的虚拟机
Use ssh command:
使用 ssh 命令:
`# ssh vivek@192.168.122.85`
[![Sample VM session][16]][16]
### Useful commands
### 有用的命令
Let us see some useful commands for managing VMs.
让我们看看管理虚拟机的一些有用的命令。
#### List all VMs
#### 列出所有虚拟机
`# virsh list --all`
#### Get VM info
#### 获取虚拟机信息
```
# virsh dominfo vmName
# virsh dominfo centos7-vm1
```
#### Stop/shutdown a VM
#### 停止/关闭虚拟机
`# virsh shutdown centos7-vm1`
#### Start VM
#### 开启虚拟机
`# virsh start centos7-vm1`
#### Mark VM for autostart at boot time
#### 将虚拟机标记为在引导时自动启动
`# virsh autostart centos7-vm1`
#### Reboot (soft & safe reboot) VM
#### 重新启动(软安全重启)虚拟机
`# virsh reboot centos7-vm1`
Reset (hard reset/not safe) VM
重置(硬重置/不安全)虚拟机
`# virsh reset centos7-vm1`
#### Delete VM
#### 删除虚拟机
```
# virsh shutdown centos7-vm1
@ -301,23 +309,22 @@ Reset (hard reset/not safe) VM
# VM=centos7-vm1
# rm -ri $D/$VM
```
To see a complete list of virsh command type
查看 virsh 命令类型的完整列表
```
# virsh help | less
# virsh help | grep reboot
```
### 关于作者
### About the author
The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on [Twitter][17], [Facebook][18], [Google+][19].
作者是 nixCraft 的创建者,也是经验丰富的系统管理员和 Linux 操作系统/ Unix shell 脚本的培训师。 他曾与全球客户以及 IT教育国防和空间研究以及非营利部门等多个行业合作。 在 [Twitter][17][Facebook][18][Google +][19] 上关注他。
--------------------------------------------------------------------------------
via: https://www.cyberciti.biz/faq/how-to-install-kvm-on-centos-7-rhel-7-headless-server/
via: [https://www.cyberciti.biz/faq/how-to-install-kvm-on-centos-7-rhel-7-headless-server/](https://www.cyberciti.biz/faq/how-to-install-kvm-on-centos-7-rhel-7-headless-server/)
作者:[Vivek Gite][a]
译者:[译者ID](https://github.com/译者ID)
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出