Kvm

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > Kvm > KVM安装部署

KVM安装部署全过程

作者:海绵(宝ᴗ宝)

本文介绍KVM虚拟化技术的架构与原理,阐述其与QEMU的协作机制,涵盖Red Hat Linux下的安装步骤、环境配置及web管理界面部署,重点说明KVM支持硬件加速、资源超分和多种管理工具的集成应用

KVM安装部署

1.kvm简单介绍

1.1.KVM架构

KVM,是Keyboard Video Mouse的缩写,KVM 通过直接连接键盘、视频和鼠标 (KVM) 端口,能够访问和控制计算机。

KVM 技术无需目标服务器修改软件。这就意味着可以在BIOS环境下,随时访问目标计算机。

KVM 提供真正的主板级别访问,并支持多平台服务器和串行设备。KVM 技术已经从最初的基础SOHO办公型,发展成为企业 IT 基础机房设施管理系统。可以从kvm 客户端管理软件轻松的直接访问位于多个远程位置的服务器和设备。

KVM over IP 解决方案具备完善的多地点故障转移功能、符合新服务器管理标准 (IPMI) 的直接界面,以及将本地存储媒体映射至远程位置的功能。

1.2.KVM 是实现拦截虚机的 I/O 请求的原理

​现代 CPU 本身实现了对特殊指令的截获和重定向的硬件支持,甚至新硬件会提供额外的资源来帮助软件实现对关键硬件资源的虚拟化从而提高性能。

以 X86 平台为例,支持虚拟化技术的 CPU  带有特别优化过的指令集来控制虚拟化过程。

通过这些指令集,VMM 很容易将客户机置于一种受限制的模式下运行,一旦客户机试图访问物理资源,硬件会暂停客户机运行,将控制权交回给 VMM 处理。

VMM 还可以利用硬件的虚级化增强机制,将客户机在受限模式下对一些特定资源的访问,完全由硬件重定向到 VMM 指定的虚拟资源,整个过程不需要暂停客户机的运行和 VMM 的参与。

由于虚拟化硬件提供全新的架构,支持操作系统直接在上面运行,无需进行二进制转换,减少了相关的性能开销,极大简化了VMM的设计,使得VMM性能更加强大。

从 2005 年开始,Intel 在其处理器产品线中推广 Intel Virtualization Technology 即 IntelVT 技术。

1.3.QEMU-KVM

其实 QEMU 原本不是 KVM 的一部分,它自己就是一个纯软件实现的虚拟化系统,所以其性能低下。但是,QEMU 代码中包含整套的虚拟机实现,包括处理器虚拟化,内存虚拟化,以及 KVM需要使用到的虚拟设备模拟(网卡、显卡、存储控制器和硬盘等)。

为了简化代码,KVM 在 QEMU 的基础上做了修改。VM 运行期间,QEMU 会通过 KVM 模块提供的系统调用进入内核,由 KVM 负责将虚拟机置于处理的特殊模式运行。

当虚机进行 I/O 操作时,KVM 会从上次系统调用出口处返回 QEMU,由 QEMU 来负责解析和模拟这些设备。

从 QEMU 角度看,也可以说是 QEMU 使用了 KVM 模块的虚拟化功能,为自己的虚机提供了硬件虚拟化加速。除此以外,虚机的配置和创建、虚机运行所依赖的虚拟设备、虚机运行时的用户环境和交互,以及一些虚机的特定技术比如动态迁移,都是 QEMU 自己实现的。

1.4.KVM

KVM 内核模块在运行时按需加载进入内核空间运行。KVM 本身不执行任何设备模拟,需要 QEMU 通过 /dev/kvm 接口设置一个 GUEST OS 的地址空间,向它提供模拟的 I/O 设备,并将它的视频显示映射回宿主机的显示屏。

它是KVM 虚机的核心部分,其主要功能是初始化 CPU 硬件,打开虚拟化模式,然后将虚拟客户机运行在虚拟机模式下,并对虚机的运行提供一定的支持。

以在 Intel 上运行为例,KVM 模块被加载的时候,它:

首先初始化内部的数据结构;

做好准备后,KVM 模块检测当前的 CPU,然后打开 CPU 控制及存取 CR4 的虚拟化模式开关,并通过执行 VMXON 指令将宿主操作系统置于虚拟化模式的根模式;

最后,KVM 模块创建特殊设备文件 /dev/kvm 并等待来自用户空间的指令。

​接下来的虚机的创建和运行将是 QEMU 和 KVM 相互配合的过程。两者的通信接口主要是一系列针对特殊设备文件 /dev/kvm 的 IOCTL 调用。其中最重要的是创建虚机。它可以理解成KVM 为了某个特定的虚机创建对应的内核数据结构,同时,KVM 返回一个文件句柄来代表所创建的虚机。

针对该句柄的调用可以对虚机做相应地管理,比如创建用户空间虚拟地址和客户机物理地址、真实物理地址之间的映射关系,再比如创建多个 vCPU。KVM 为每一个 vCPU 生成对应的文件句柄,对其相应地 IOCTL 调用,就可以对vCPU进行管理。其中最重要的就是“执行虚拟处理器”。通过它,虚机在 KVM 的支持下,被置于虚拟化模式的非根模式下,开始执行二进制指令。在非根模式下,所有敏感的二进制指令都被CPU捕捉到,CPU 在保存现场之后自动切换到根模式,由 KVM 决定如何处理。

除了 CPU 的虚拟化,内存虚拟化也由 KVM 实现。实际上,内存虚拟化往往是一个虚机实现中最复杂的部分。CPU 中的内存管理单元 MMU 是通过页表的形式将程序运行的虚拟地址转换成实际物理地址。在虚拟机模式下,MMU 的页表则必须在一次查询的时候完成两次地址转换。因为除了将客户机程序的虚拟地址转换了客户机的物理地址外,还要将客户机物理地址转化成真实物理地址。 

Linux 上的用户空间、内核空间和虚机:

2. KVM 的功能列表

KVM 所支持的功能包括:

3. KVM 工具集合

4. RedHat Linux KVM 安装

RedHat 有两款产品提供 KVM 虚拟化:

RedHat Linux KVM:

5.KVM安装

5.1.关闭防火墙与selinux

[root@ c7-41 ~]# systemctl stop firewalld
[root@ c7-41 ~]# systemctl disable firewalld
[root@ c7-41 ~]# setenforce 0
[root@ c7-41 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

5.2.安装依赖

[root@ c7-41 ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++

5.3.配置环境、修改网站访问并发量

[root@ c7-41 ~]# vim /etc/security/limits.conf   ###在末尾添加
* soft nofile 65535
* hard nofile 65535 
[root@ c7-41 ~]# cat /etc/security/limits.conf |grep -w '*' | grep -v '^#'
* soft nofile 65535
* hard nofile 65535

[root@ c7-41 ~]# vim /etc/pam.d/login        ###在末尾添加
session    required     /lib/security/pam_limits.so

#临时生效
[root@ c7-41 ~]# vim /etc/profile                   ###在末尾添加
ulimit -n 65535
[root@ c7-41 ~]# source /etc/profile          ###生效
[root@ c7-41 ~]# ulimit -n
65535
###星号代表全局, soft为软件,hard为硬件,nofile为这里指可打开文件数。

5.4.验证CPU是否支持KVM

如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的

关闭虚拟机设置(shutdown -h now)

[root@ c7-41 ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx

5.5.kvm安装

[root@ c7-41 ~]# yum -y install qemu-kvm qemu-kvm-tools qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools

5.6.启动服务,验证安装结果

[root@ c7-41 ~]# systemctl start libvirtd
[root@ c7-41 ~]# systemctl enable libvirtd
[root@ c7-41 ~]# systemctl status libvirtd
#检查也没有kvm模块
[root@ c7-41 ~]# lsmod|grep kvm
kvm_intel             188644  0
kvm                   621480  1 kvm_intel
irqbypass              13503  1 kvm
#查看当前系统里面有没有
[root@ c7-41 ~]# virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------
[root@ c7-41 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@ c7-41 ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 May  5 11:16 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm

kvm web管理界面安装

kvm 的 web 管理界面是由 webvirtmgr 程序提供的

5.7.安装依赖

[root@ c7-41 ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
##升级pip
[root@ c7-41 ~]# pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple   
Collecting pip
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/54/2e/df11ea7e23e7e761d484ed3740285a34e38548cf2bad2bed3dd5768ec8b9/pip-20.1-py2.py3-none-any.whl (1.5MB)
    100% |████████████████████████████████| 1.5MB 256kB/s
Installing collected packages: pip
  Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
      Successfully uninstalled pip-8.1.2
Successfully installed pip-20.1

pip 20.0.2文档 https://pip.pypa.io/en/stable/user_guide/#config-file

pip是一个很好用的第三方库安装方式,但是默认的源没法连接,就算有时候可以成功率也很低,所以换成国内镜像源比较方便。

将pip源更换到国内镜像

用pip管理工具安装库文件时,默认使用国外的源文件,因此在国内的下载速度会比较慢,可能只有50KB/s。幸好,国内的一些顶级科研机构已经给我们准备好了各种镜像,下载速度可达2MB/s。

临时方法

阿里云 https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) https://pypi.douban.com/simple/ 
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/

5.8.从github上下载webvirtmgr代码

[root@ c7-41 ~]# cd /usr/local/src/
[root@ c7-41 src]# git clone git://github.com/retspen/webvirtmgr.git

补充一点:

5.9.安装webvirtmgr

[root@ c7-41 src]# cd webvirtmgr/
##如果报错,换一个镜像(上面有)
[root@ c7-41 webvirtmgr]# pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

5.10.检查sqlite3是否安装

验证是否可以导入(如果不可以,会报错)

[root@ c7-41 webvirtmgr]# python
Python 2.7.5 (default, Aug  7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()

6.初始化帐号信息

[root@ c7-41 webvirtmgr]# python manage.py syncdb
WARNING:root:No local_settings file found.
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table servers_compute
Creating table instance_instance
Creating table create_flavor

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes  ##是否创建超级管理员帐号
Username (leave blank to use 'root'):  ##回车默认为root
Email address: 1279087939@qq.com  ##设置超级管理员邮箱
Password: 123456       ##设置超级管理员密码
Password (again):123456
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)

6.1.拷贝web网页至指定目录

[root@ c7-41 webvirtmgr]# mkdir /var/www
[root@ c7-41 webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@ c7-41 webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
6.1.1.生成密钥

首次生成直接回车,不是则Overwrite (y/n)? y 选择覆盖

##都是回车

[root@ c7-41 webvirtmgr]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:FBWgwlyptDF8dem65poTlImt5c1Qm63bf9Mp2WDJGSs root@c7-41
The key's randomart image is:
+---[RSA 2048]----+
|   .  .o+o+o     |
|   o=.o..o.      |
|   .+O.+.=       |
|    +.B.o o  .   |
|     = +So  . =  |
|    . o =  E B   |
|       . +  o +..|
|      ..+ .  ooo.|
|      o=.  ..... |
+----[SHA256]-----+

由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,则需要更换ip

[root@ c7-41 webvirtmgr]# ssh-copy-id 10.0.0.41
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.41 (10.0.0.41)' can't be established.
ECDSA key fingerprint is SHA256:AxAn8ho/KqTHrRuTeSOokFBDmQK36JAdH49GlnnjoOg.
ECDSA key fingerprint is MD5:57:79:69:72:65:46:7e:04:7d:f5:55:0e:c6:44:65:70.
Are you sure you want to continue connecting (yes/no)? yes     ##yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.41's password:        ##123456

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.0.0.41'"
and check to make sure that only the key(s) you wanted were added.
6.1.2.端口转发,查看
[root@ c7-41 ~]# ssh 10.0.0.41 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Tue May  5 11:50:04 2020 from 10.0.0.1
[root@ c7-41 ~]# ss -antl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port
LISTEN      0      100                   127.0.0.1:25                                        *:*
LISTEN      0      128                   127.0.0.1:6010                                      *:*
LISTEN      0      128                   127.0.0.1:6011                                      *:*
LISTEN      0      128                   127.0.0.1:6080                                      *:*
LISTEN      0      128                   127.0.0.1:8000                                      *:*
LISTEN      0      128                           *:111                                       *:*
LISTEN      0      5                 192.168.122.1:53                                        *:*
LISTEN      0      128                           *:22                                        *:*
LISTEN      0      100                       [::1]:25                                     [::]:*
LISTEN      0      128                       [::1]:6010                                   [::]:*
LISTEN      0      128                       [::1]:6011                                   [::]:*
LISTEN      0      128                       [::1]:6080                                   [::]:*
LISTEN      0      128                       [::1]:8000                                   [::]:*
LISTEN      0      128                        [::]:111                                    [::]:*
LISTEN      0      128                        [::]:22                                     [::]:*

6.2.配置nginx

[root@ c7-41 ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;

        include /etc/nginx/default.d/*.conf;

        location / {
                root html;
                index index.html index.htm;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

6.3.添加webvirtmgr.conf

[root@ c7-41 ~]# vim /etc/nginx/conf.d/webvirtmgr.conf
server {
listen 80 default_server;

server_name $hostname;
#access_log /var/log/nginx/webvirtmgr_access_log;

location /static/ {
    root /var/www/webvirtmgr/webvirtmgr;
    expires max;
   }

location / {
     proxy_pass http://127.0.0.1:8000;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
     proxy_set_header Host $host:$server_port;
     proxy_set_header X-Forwarded-Proto $remote_addr;
     proxy_connect_timeout 600;
     proxy_read_timeout 600;
     proxy_send_timeout 600;
     client_max_body_size 1024M;
   }
}

确保bind绑定的是本机的8000端口

[root@ c7-41 ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '127.0.0.1:8000'
backlog = 2048
#还有很多方法,来查看
[root@ c7-41 ~]# cat /var/www/webvirtmgr/conf/gunicorn.conf.py | grep -v '^#' | sed -n '5,6p'
bind = '127.0.0.1:8000'
backlog = 2048

6.4.启动nginx

[root@ c7-41 ~]# systemctl start nginx

6.5.设置supervisor

[root@ c7-41 ~]# vim /etc/supervisord.conf
##在末尾添加
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
logfile=/var/log/supervisor/webvirtmgr.log
log_stderr=true
user=nginx

[program:webvirtmgr-console]
command=/usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=nginx

6.6.启动supervisor并设置开机自动启动

[root@ c7-41 ~]# systemctl start supervisord
[root@ c7-41 ~]# systemctl enable supervisord
[root@ c7-41 ~]# systemctl status supervisord

6.7.配置nginx用户

未创建nginx用户,所以用su命令赋予它交互式登录的权限

直接回车

[root@ c7-41 ~]# su - nginx -s /bin/bash
[nginx@ c7-41 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/nginx/.ssh/id_rsa):
Created directory '/var/lib/nginx/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/nginx/.ssh/id_rsa.
Your public key has been saved in /var/lib/nginx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:yx8WDwCmUpIpg+jPDa5/6qPbxr58GIyVBb6sd40tC4A nginx@c7-41
The key's randomart image is:
+---[RSA 2048]----+
|o .+o o          |
|= +o + .         |
|.o..+   .        |
|...=.    .       |
|E.Boo   S o      |
| .o* .+. . +     |
| .ooo+ oo o .    |
| .+=ooo  o .     |
| oBO*.    .      |
+----[SHA256]-----+
[nginx@ c7-41 ~]$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >>~/.ssh/config
[nginx@ c7-41 ~]$ chmod 0600 ~/.ssh/config
[nginx@ c7-41 ~]$ ssh-copy-id root@10.0.0.41
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '10.0.0.41' (ECDSA) to the list of known hosts.
root@10.0.0.41's password:123456

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.0.0.41'"
and check to make sure that only the key(s) you wanted were added.

#退出
[nginx@ c7-41 ~]$ exit
logout
[root@ c7-41 ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:root
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
[root@ c7-41 ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@ c7-41 ~]# systemctl restart supervisord
[root@ c7-41 ~]#  systemctl restart libvirtd

7.kvm web界面管理

7.1.通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images/

[root@ c7-41 ~]# cd /var/lib/libvirt/images
[root@ c7-41 images]# ll
total 4554752
-rw-r--r-- 1 root root 4664066048 May  8 15:03 CentOS-7-x86_64-DVD-1908.iso
7.1.1.在web界面查看ISO镜像文件是否存在

7.1.2.创建系统安装镜像

7.1.3.kvm网络管理

7.2.实例管理

7.2.1.实例(虚拟机的创建)

7.2.2.设置在web上访问虚拟机的密码

7.3.虚拟机安装

7.4.正常的虚拟机安装过程

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文