KVM虚拟化
文章目录
虚拟化分类
虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机。在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互相不影响,从而显著提高计算机的工作效率
虚拟化使用软件的方法重新定义划分it资源,可以实现it资源的动态分配、灵活调度、跨域共享,提高it资源利用率,使it资源能够真正成为社会基础设施,服务于各行各业中灵活多变的应用请求
虚拟化层次种类
-
完全虚拟化—最流行的虚拟化方法,使用名为hyperviosr的一种软件,在虚拟服务器和硬件底层之间建立一个抽象层。VMware和微软的VirtualPC是代表该方法的两个商用产品,而基于核心的虚拟机 (KVM) 是面向 Linux 系统的开源产品hypervisor 可以捕获 CPU 指令,为指令访问硬件控制器和外设充当中介。因而,完全虚拟化技术几乎能让任何一款操作系统不用改动就能安装到虚拟服务器上,而它们不知道自己运行在虚拟化环境下。主要缺点是, hypervisor 给处理器带来开销。
-
准虚拟化—完全虚拟化是处理器密集型技术,因为它要求 hypervisor管理各个虚拟服务器,并让它们彼此独立。减轻这种负担的一种方法就是,改动客户端操作系统,让它以为自己运行在虚拟环境下,能够与hypervisor 协同工作。这种方法就叫准虚拟化 (para-virtualization)Xen 是开源准虚拟化技术的一个例子。操作系统作为虚拟服务器在 Xen hypervisor 上运行之前,它必须在核心层面进行某些改变。因此, Xen 适用于 BSD 、 Linux 、 Solaris 及其他开源操作系统,但不适合对像Windows 这些专有的操作系统进行虚拟化处理,因为它们无法改动。准虚拟化技术的优点是性能高。经过准虚拟化处理的服务器可与hypervisor 协同工作,其响应能力几乎不亚于未经过虚拟化处理的服务器。准虚拟化与完全虚拟化相比优点明显,以至于微软和 VMware 都在开发这项技术,以完善各自的产品。
-
系统虚拟化—就操作系统层的虚拟化而言,没有独立的hypervisor 层。相反,主机操作系统本身就负责在多个虚拟服务器之间分配硬件资源,并且让这些服务器彼此独立。一个明显的区别是,如果使用操作系统层虚拟化,所有虚拟服务器必须运行同一操作系统 ( 不过每个实例有各自的应用程序和用户。
账户 ) 。虽然操作系统层虚拟化的灵活性比较差,但本机速度性能比较高。此外,由于架构在所有虚拟服务器上使用单一、标准的操作系统,管理起来比异构环境要容易。
-
桌面虚拟化—服务器虚拟化主要针对服务器而言,而虚拟化最接近用户的还是要算的上桌面虚拟化了,桌面虚拟化主要功能是将分散的桌面环境集中保存并管理起来,包括桌面环境的集中下发,集中更新,集中管理。桌面虚拟化使得桌面管理变得简单,不用每台终端单独进行维护,每台终端进行更新。终端数据可以集中存储在中心机房里,安全性相对传统桌面应用要高很多。桌面虚拟化可以使得一个人拥有多个桌面环境,也可以把一个桌面环境供多人使用。
虚拟化架构分类
Hypervisor 直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的 Linux 系统。Xen 和 VMWare 的 ESXi 都属于这个类型。
-
半虚拟化
物理机上首先安装常规的操作系统,比如 Redhat、Ubuntu 和 Windows。Hypervisor 作为 OS 上的一个程序模块运行,并对管理虚拟机进行管理。KVM、VirtualBox 和 VMWare Workstation 都属于这个类型。
虚拟化KVM
KVM
KVM — 全称是基于内核的虚拟机(Kernel-based Virtual Machine),是一个开源软件,基于内核的虚拟化技术,实际是嵌入系统的一个虚拟化模块,通过优化内核来使用虚拟技术,该内核模块使得 Linux 变成了一个Hypervisor,虚拟机使用 Linux 自身的调度器进行管理。
KVM 是基于虚拟化扩展(Intel VT 或者 AMD-V)的 X86 硬件的开源的 Linux 原生的全虚拟化解决方案。KVM 中,虚拟机被实现为常规的 Linux 进程,由标准 Linux 调度程序进行调度;虚机的每个虚拟 CPU 被实现为一个常规的 Linux 进程。这使得 KMV 能够使用 Linux 内核的已有功能。但是,KVM 本身不执行任何硬件模拟,需要客户空间程序通过 /dev/kvm 接口设置一个客户机虚拟服务器的地址空间,向它提供模拟的 I/O,并将它的视频显示映射回宿主的显示屏。目前这个应用程序是 QEMU。
kvm.ko
KVM有一个内核模块叫kvm.ko,只用于管理虚拟 CPU 和内存。IO 的虚拟化,就交给 Linux 内核和qemu来实现。
Libvirt:是 KVM 的管理工具。Libvirt 除了能管理 KVM 这种 Hypervisor,还能管理 Xen,VirtualBox 等。OpenStack 底层也使用 Libvirt。
Libvirt 包含 3 个东西:后台 daemon 程序 libvirtd、API 库和命令行工具 virsh
(1)libvirtd是服务程序,接收和处理 API 请求;
(2)API 库使得其他人可以开发基于 Libvirt 的高级工具,比如 virt-manager,这是个图形化的 KVM 管理工具,后面我们也会介绍;
(3)virsh 是我们经常要用的 KVM 命令行工具,后面会有使用的示例。作为 KVM 和 OpenStack 的实施人员,virsh 和 virt-manager 是一定要会用的。
KVM部署
kvm安装
部署前请确保你的CPU虚拟化功能已开启。分为两种情况:
- 虚拟机要关机设置CPU虚拟化
- 物理机要在BIOS里开启CPU虚拟化
关闭防火墙与SELinux
[root@KVM ~]# systemctl disable --now firewalld.service
[root@KVM ~]# setenforce 0
[root@KVM ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@KVM ~]# reboot
验证CPU是否支持KVM,如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU支持
[root@KVM ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx //这里出现几个vmx就说明处理器有多少核心,svm同理
kvm安装
[root@kvm ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
[root@kvm ~]# yum -y install qemu-kvm qemu-kvm-common qemu-img virt-manager libvirt python3-libvirt libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools
因为虚拟机中网络,我们一般都是和公司的其他服务器在同一个网段,所以我们需要把KVM服务器的网卡配置成桥接模式。这样的话KVM虚拟机就可以通过该桥接网卡和公司内部其他服务器处于同一网段
[root@KVM ~]# cd /etc/sysconfig/network-scripts/
[root@KVM network-scripts]# ls
ifcfg-ens33
[root@KVM network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@KVM network-scripts]# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.100.110
NETMASK=255.255.255.0
GATEWAY=192.168.100.2
DNS1=114.114.114.114
[root@KVM network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
重启网络
[root@KVM ~]# systemctl restart NetworkManager
[root@KVM ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000
link/ether 00:0c:29:f2:c3:12 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:f2:c3:12 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.110/24 brd 192.168.100.255 scope global noprefixroute br0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fef2:c312/64 scope link
valid_lft forever preferred_lft forever
启动服务
[root@KVM ~]# systemctl enable --now libvirtd
验证安装结果
[root@KVM ~]# virsh -c qemu:///system list
Id Name State
--------------------
[root@KVM ~]# virsh --version
6.0.0
[root@KVM ~]# virt-install --version
2.2.1
[root@KVM ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@KVM ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 Oct 20 04:13 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@KVM ~]# lsmod |grep kvm
kvm_intel 311296 0
kvm 839680 1 kvm_intel
irqbypass 16384 1 kvm
[root@KVM ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29f2c312 no ens33
virbr0 8000.525400303de8 yes virbr0-nic
kvm web管理web界面安装
kvm 的 web 管理界面是由 webvirtmgr 程序提供的(只能安装在centos7上)
安装相关依赖
[root@kvm-webvirtmgr ~]# yum -y install epel-release
[root@kvm-webvirtmgr ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
升级pip
[root@kvm-webvirtmgr ~]# pip install --upgrade pip
从github上下载webvirtmgr代码
[root@kvm-manager ~]# cd /usr/local/src/
[root@kvm-manager src]# git clone git://github.com/retspen/webvirtmgr.git
Cloning into 'webvirtmgr'...
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
Receiving objects: 100% (5614/5614), 2.97 MiB | 843.00 KiB/s, done.
Resolving deltas: 100% (3606/3606), done.
安装webvirtmgr
[root@kvm-manager src]# cd webvirtmgr/
[root@kvm-webvirtmgr webvirtmgr]# pip install -i https://pypi.douban.com/simple -r requirements.txt
Collecting django==1.5.5 (from -r requirements.txt (line 1))
Downloading https://pypi.doubanio.com/packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6eb57be879ae13ad4/Django-1.5.5.tar.gz (8.1MB)
100% |████████████████████████████████| 8.1MB 98kB/s
Collecting gunicorn==19.5.0 (from -r requirements.txt (line 2))
Downloading https://pypi.doubanio.com/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51b489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113kB)
100% |████████████████████████████████| 122kB 1.6MB/s
Collecting lockfile>=0.9 (from -r requirements.txt (line 5))
Downloading https://pypi.doubanio.com/packages/c8/22/9460e311f340cb62d26a38c419b1381b8593b0bb6b5d1f056938b086d362/lockfile-0.12.2-py2.py3-none-any.whl
Installing collected packages: django, gunicorn, lockfile
Running setup.py install for django ... done
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
检查sqlite3是否安装
[root@kvm-webvirtmgr webvirtmgr]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
初始化账号信息
[root@kvm-webvirtmgr 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: 123@abc.com //设置超级管理员邮箱
Password: //设置超级管理员密码
Password (again): //再次输入超级管理员密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
拷贝web网页至指定目录
[root@kvm-webvirtmgr webvirtmgr]# mkdir /var/www
[root@kvm-webvirtmgr webvirtmgr]# cp -r /usr/local/src/webvirtmgr /var/www/
[root@kvm-webvirtmgr webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
生成密钥
[root@kvm-webvirtmgr 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:ycb9GpXdy0lpsU8IjDW0DYD+ZMeaZYUSqkjF4aC4tfk root@kvm-webvirtmgr
The key's randomart image is:
+---[RSA 2048]----+
| ..o. .o+=. |
| . . +. ...+.=. |
|. o . ... .o+... |
| o + . +.oo =+ o+|
|. o . . S+.*o o=o|
| . . +o +.+|
| E . . +.|
| o |
| . |
+----[SHA256]-----+
将密钥传输到kvm主机上
[root@kvm-webvirtmgr webvirtmgr]# ssh-copy-id 192.168.100.110
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.100.110 (192.168.100.110)' can't be established.
ECDSA key fingerprint is SHA256:voOAHxsYXQXaTgyngg7WT8MMJTl1vgQ1cNM2SQnQ3io.
ECDSA key fingerprint is MD5:3b:c0:7b:d8:e2:f8:09:f1:1e:cd:91:37:2f:f6:79:b7.
Are you sure you want to continue connecting (yes/no)? 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@192.168.100.110's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.100.110'"
and check to make sure that only the key(s) you wanted were added.
这里因为kvm与webvirtmgr部署在同一台服务器,所以这里要配置端口转发
[root@KVM ~]# ssh 192.168.100.110 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Wed Oct 20 03:34:19 2021 from 192.168.100.1
[root@KVM ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6080 0.0.0.0:*
LISTEN 0 128 127.0.0.1:8000 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:6080 [::]:*
LISTEN 0 128 [::1]:8000 [::]:*
配置nginx
[root@kvm-webvirtmgr webvirtmgr]# vi /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 {
}
}
}
[root@kvm-webvirtmgr webvirtmgr]# vi /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@KVM ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
......
bind = '0.0.0.0:8000' //确保此处绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
backlog = 2048
重启nginx
[root@kvm-webvirtmgr ~]# systemctl restart nginx
[root@kvm-webvirtmgr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6080 0.0.0.0:*
LISTEN 0 128 127.0.0.1:8000 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:6080 [::]:*
LISTEN 0 128 [::1]:8000 [::]:*
设置supervisor
[root@kvm-webvirtmgr ~]# vi /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
启动supervisor并设置开机自启
[root@kvm-webvirtmgr ~]# systemctl enable --now supervisord.service
[root@kvm-webvirtmgr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:8000 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
配置nginx用户
[root@kvm-webvirtmgr ~]# su - nginx -s /bin/bash
Last login: Wed Oct 20 09:15:09 EDT 2021 on pts/0
-bash-4.2$ 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:Xm+1MmGryPwHpWwMTvQkOBT2T0y8RIvvjTEfUrv+ANY nginx@kvm-webvirtmgr
The key's randomart image is:
+---[RSA 2048]----+
| .+o oo |
| .o.o++. |
| oo=+.. |
| o+oo.. |
| oS+XoE . |
| ..+*@ * . |
| ..o.@ . |
| o . +.+ |
| +.o.... |
+----[SHA256]-----+
-bash-4.2$
-bash-4.2$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config
-bash-4.2$ ssh-copy-id root@192.168.100.160
/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 '192.168.100.160' (ECDSA) to the list of known hosts.
root@192.168.100.160's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.100.160'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ exit
logout
[root@kvm-webvirtmgr ~]# 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@kvm-webvirtmgr ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@kvm-webvirtmgr ~]# systemctl restart nginx
[root@kvm-webvirtmgr ~]# systemctl restart libvirtd
kvm web界面管理
磁盘管理
通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images
[root@KVM ~]# cd /var/lib/libvirt/images
[root@KVM images]# ls
CentOS-7-x86_64-DVD-1804-7.5.iso
网络管理
添加桥接网络
实例管理
实例创建
虚拟机插入光盘
设置在web上访问虚拟机的密码
启动虚拟机
添加kvm主机
[