虚拟化技术

虚拟化介绍

虚拟化是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机(对计算机物理资源的抽象,实现资源的模拟、隔离和共享)。在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互不影响,从而显著提高计算机的工作效率。

虚拟化使用软件的方法重新定义划分IT资源,可以实现IT资源的动态分配、灵活调度、跨域共享,提高IT资源利用率,降低成本,加快部署,极大增强系统整体安全性和可靠性。使IT资源能够真正成为社会基础设施,服务于各行各业中灵活多变的应用需求

虚拟化与云计算的关系

虚拟化重点对资源的虚拟,比如把一台大型的服务器虚拟成多台小定的服务器。一个侧重虚拟的这种技术。

云计算的重点是对资源池(可以是经过虚拟化之后)进行统一的管理和调度。一种侧重对虚拟化之后的资源进行管理和调度.

虚拟化结构

1. 寄居虚拟化结构

寄居虚拟化架构指在宿主操作系统之上安装和运行虚拟化程序,依赖于宿主操作系统对设备的支持和物理资源的管理。(类似 Vmware Workstation 的程序)

企业部署虚拟化的意义 虚拟化部署是什么意思_企业部署虚拟化的意义

2. 裸金属虚拟化架构

裸金属虚拟化架构指直接在硬件上面安装虚拟化软件,再在其上安装操作系统和应用,依赖虚拟层内核和服务器控制台进行管理

企业部署虚拟化的意义 虚拟化部署是什么意思_虚拟化_02

3. 操作系统虚拟化架构

操作系统虚拟化架构在操作系统层面增加虚拟服务器功能。操作系统虚拟化架构把单个的操作系统划分为多个容器,使用容器管理器来进行管理。
宿主操作系统负责在多个虚拟服务器(即容器)之间分配硬件资源,并且让这些服务器彼此独立。

企业部署虚拟化的意义 虚拟化部署是什么意思_企业部署虚拟化的意义_03

虚拟化类型

1. 全虚拟化(Full Virtualization)

该模型使用虚拟机协调guest操作系统和原始硬件,Hypervisor(VMM)在guest操作系统和裸硬件之间用于工作协调,guset操作系统认为自己还是直接处于原始硬件上,一些受保护指令必须由Hypervisor(虚拟机管理程序)来捕获处理。

产品有:VirtualBox,KVM,VMware Workstation和VMware ESX

企业部署虚拟化的意义 虚拟化部署是什么意思_虚拟化_04

  • 优点:Guest OS 无需修改,速度和功能非常不错,使用简单。
  • 缺点:中间层Hypervisor需占用一定的资源

2. 半虚拟化(Para Virtualization)(准虚拟化)

半虚拟化是另一种类似于全虚拟化的技术,它使用Hypervisor分享存取底层的硬件,但是它的guest操作系统集成了虚拟化方面的代码,guest OS能配合Hypervisor来协作实现虚拟化。(guest操作系统意识到自己是处于虚拟化环境)

企业部署虚拟化的意义 虚拟化部署是什么意思_虚拟化_05

  • 优点:与全虚拟化相比,架构更精简,整体速度会有一定优势。
  • 缺点:需要对Guest OS修改,所以用户体验比较麻烦

3. 硬件辅助虚拟化(Hardware Assisted Virtualization)

Intel/AMD等硬件厂商通过对部分全虚拟化和半虚拟化使用到的软件技术进行硬件化来提高性能。硬件辅助虚拟化技术常用于优化全虚拟化和半虚拟化产品,而不是独创一派。(VMware Workstation,VirtualBox,KVM,VMware ESX和Xen)

  • 优点:通过引入硬件技术,使虚拟技术更接近物理机速度。

KVM部署

环境

系统

主机IP

CentOS8

192.168.197.151

部署步骤

  • 安装常用工具
[root@localhost ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
......

complete!
  • 安装KVM及必备工具
[root@localhost ~]# yum -y install qemu-kvm qemu-kvm-core qemu-kvm-common qemu-img virt-manager libvirt python3-libvirt.x86_64 libvirt-client virt-install virt-viewer console-bridge* libguestfs-tools
......

compelete!
  • 配置桥接网卡
[root@localhost ~]# cd /etc/sysconfig/network-scripts/

[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0

[root@localhost network-scripts]# vim ifcfg-br0
TYPE=Bridge
DEVICE=br0
BOOTPROTO=static
NAME=br0
ONBOOT=yes
IPADDR=192.168.197.151
NETMASK=255.255.255.0
GATEWAY=192.168.197.2
DNS1=114.114.114.114

## 重启网卡服务并重启网卡连接

[root@localhost ~]# systemctl restart NetworkManager
[root@localhost ~]# ip a
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:12:eb:f3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.151/24 brd 192.168.197.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe12:ebf3/64 scope link 
       valid_lft forever preferred_lft forever
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:06:2e:b3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
  • 开启libvirt服务及验证
[root@localhost ~]# systemctl enable --now libvirtd



# 验证KVM内核模块
[root@localhost ~]# lsmod |grep kvm
kvm_amd               110592  0
ccp                    98304  1 kvm_amd
kvm                   786432  1 kvm_amd
irqbypass              16384  1 kvm

# 创建qemeu工具创建软链接
[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
  • 安装webvirtmgr及其需要的软件工具
[root@localhost ~]# yum install -y python3-pip python2-pip git python3-libxml2 python36-devel supervisor nginx
......
compelete!

## 升级PIP包
[root@localhost src]# pip2 install --upgrade pip
......
root@localhost src]# pip2.7 install --upgrade pip
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip2.7 install --user` instead.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/55/73/bce122d1ed0217b3c1a3439ab16dfa94bbeabd0d31755fcf907493abf39b/pip-20.3-py2.py3-none-any.whl (1.5MB)
    100% |████████████████████████████████| 1.5MB 52kB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.3
    Uninstalling pip-9.0.3:
      Successfully uninstalled pip-9.0.3
Successfully installed pip-20.3


## 通过git克隆远程仓库的webvirtmgr包

[root@localhost ~]# cd /usr/src/
[root@localhost 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.98 MiB | 41.00 KiB/s, done.
Resolving deltas: 100% (3602/3602), done.

## 通过pip,升级安装webvirtmgr
[root@localhost src]# cd webvirtmgr
[root@localhost webvirtmgr]# ls
conf     dev-requirements.txt  interfaces   networks          serverlog  templates
console  hostdetail            locale       README.rst        servers    Vagrantfile
create   images                manage.py    requirements.txt  setup.py   vrtManager
deploy   instance              MANIFEST.in  secrets           storages   webvirtmgr
[root@localhost webvirtmgr]# pip2.7 install -r requirements.txt
......
Collecting django==1.5.5
  Using cached Django-1.5.5.tar.gz (8.1 MB)
Collecting gunicorn==19.5.0
  Downloading gunicorn-19.5.0-py2.py3-none-any.whl (113 kB)
     |████████████████████████████████| 113 kB 186 kB/s 
Collecting lockfile>=0.9
  Downloading lockfile-0.12.2-py2.py3-none-any.whl (13 kB)
Using legacy 'setup.py install' for django, since package 'wheel' is not installed.
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


## 检查sqlite模块
[root@localhost webvirtmgr]# python2
Python 2.7.17 (default, Jun  5 2020, 03:38:32) 
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
  • 账号初始化
[root@localhost webvirtmgr]# python2 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'): 
Email address: 1@2.com
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
...
  • index文件设置
[root@localhost webvirtmgr]# mkdir /var/www
[root@localhost webvirtmgr]# cp -r /usr/src/webvirtmgr /var/www/
  • 密钥生成
[root@localhost ~]# 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:Kx3EhxkBT0eYUcqMrb9WJuRSf3/OsqwuA/HOebjA0p4 root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|      ..=B+      |
|       Oo*       |
|      . @ .      |
|       o+.       |
|      .+S+       |
|      .=+o= .    |
|      o.BB + .   |
|       +.+B ..o..|
|       .E .*o.o=o|
+----[SHA256]-----+

[root@localhost ~]# ssh-copy-id 192.168.197.151
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.197.151 (192.168.197.151)' can't be established.
ECDSA key fingerprint is SHA256:K3DiSgTea9SZDSV16lnniqbWiYvkQpT1hKyFxkq/ixc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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.197.151's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.197.151'"
and check to make sure that only the key(s) you wanted were added.
  • 转发端口号
[root@localhost ~]# ssh 192.168.197.151 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60

[root@localhost ~]# ss -antl
State        Recv-Q       Send-Q             Local Address:Port             Peer Address:Port      
LISTEN       0            128                      0.0.0.0:5355                  0.0.0.0:*         
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                         [::]:5355                     [::]:*         
LISTEN       0            128                         [::]:111                      [::]:*         
LISTEN       0            128                         [::]:22                       [::]:*         
LISTEN       0            128                        [::1]:6080                     [::]:*         
LISTEN       0            128                        [::1]:8000                     [::]:*
  • 配置nginx
[root@localhost nginx]# vim nginx.conf.bak 

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 {
        }
    }
}
  • 配置webvirtmgr文件
[root@localhost conf.d]# vim 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绑定ip
[root@localhost conf.d]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
......
bind = '0.0.0.0:8000'
......
  • 重启nginx服务
[root@localhost conf.d]# systemctl restart nginx
[root@localhost conf.d]# ss -antl
State                  Recv-Q                 Send-Q                                 Local Address:Port                                  Peer Address:Port                 
LISTEN                 0                      128                                          0.0.0.0:5355                                       0.0.0.0:*                    
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                                             [::]:5355                                          [::]:*                    
LISTEN                 0                      128                                             [::]:111                                           [::]:*                    
LISTEN                 0                      128                                             [::]:22                                            [::]:*                    
LISTEN                 0                      128                                            [::1]:6080                                          [::]:*                    
LISTEN                 0                      128                                            [::1]:8000                                          [::]:*
  • 设置supervisor
[root@localhost conf.d]# cat >> /etc/supervisord.conf << 'EOF'
> [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
> EOF
  • 打开服务
[root@localhost conf.d]# systemctl enable --now supervisord
[root@localhost conf.d]# ss -antl
State                  Recv-Q                 Send-Q                                 Local Address:Port                                  Peer Address:Port                 
LISTEN                 0                      128                                          0.0.0.0:5355                                       0.0.0.0:*                    
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                                             [::]:5355                                          [::]:*                    
LISTEN                 0                      128                                             [::]:111                                           [::]:*                    
LISTEN                 0                      128                                             [::]:22                                            [::]:*                    
LISTEN                 0                      128                                            [::1]:6080                                          [::]:*                    
LISTEN                 0                      128                                            [::1]:8000                                          [::]:*
  • 配置nginx用户
[root@localhost conf.d]# su - nginx -s /bin/bash

[nginx@localhost ~]$ 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:RnEFbWrrgXEunpwdhkzOkcKvOSwYdCqmNaGNZoPtlhw nginx@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|        . o+.    |
|         o  o    |
|     .  .. o     |
|  o . o.= +      |
|.* +   BSO .     |
|==E    .O *      |
|==.* . = B o     |
|. * . = = o      |
| .   . .         |
+----[SHA256]-----+


[nginx@localhost ~]$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
[nginx@localhost ~]$ chmod 0600 ~/.ssh/config
[nginx@localhost ~]$ ssh-copy-id root@192.168.197.151
......
root@192.168.197.151's password: 

Number of key(s) added: 1

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


[root@localhost conf.d]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[bvirt SSH access]
Identity=unix-user:root
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes

[root@localhost conf.d]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@localhost conf.d]# systemctl restart nginx libvirtd

Webvirmgr

  • 访问Nginx服务器地址进入web管理页
  • 企业部署虚拟化的意义 虚拟化部署是什么意思_虚拟化_06

  • 创建新的连接
  • 企业部署虚拟化的意义 虚拟化部署是什么意思_python_07

  • 创建存储池
  • 企业部署虚拟化的意义 虚拟化部署是什么意思_python_08

  • 定义新的网络
  • 企业部署虚拟化的意义 虚拟化部署是什么意思_django_09


  • 企业部署虚拟化的意义 虚拟化部署是什么意思_虚拟化_10

  • 存储池内将ISO镜像生成为img系统镜像
  • 企业部署虚拟化的意义 虚拟化部署是什么意思_python_11

  • 创建新的虚拟机实例
  • 企业部署虚拟化的意义 虚拟化部署是什么意思_企业部署虚拟化的意义_12

  • 创建完毕,装机使用
  • 企业部署虚拟化的意义 虚拟化部署是什么意思_django_13