kvm+webvirtmgr部署

虚拟化介绍

虚拟化是云计算的基础。简单的说,虚拟化使得在一台物理的服务器上可以跑多台虚拟机,虚拟机共享物理机的 CPU、内存、IO 硬件资源,但逻辑上虚拟机之间是相互隔离的。

物理机我们一般称为宿主机(Host),宿主机上面的虚拟机称为客户机(Guest)。

那么 Host 是如何将自己的硬件资源虚拟化,并提供给 Guest 使用的呢?
这个主要是通过一个叫做 Hypervisor 的程序实现的。

根据 Hypervisor 的实现方式和所处的位置,虚拟化又分为两种:

  • 全虚拟化
  • 半虚拟化

全虚拟化:
Hypervisor 直接安装在物理机上,多个虚拟机在 Hypervisor 上运行。Hypervisor 实现方式一般是一个特殊定制的 Linux 系统。Xen 和 VMWare 的 ESXi 都属于这个类型
img

半虚拟化:
物理机上首先安装常规的操作系统,比如 Redhat、Ubuntu 和 Windows。Hypervisor 作为 OS 上的一个程序模块运行,并对管理虚拟机进行管理。KVM、VirtualBox 和 VMWare Workstation 都属于这个类型
img

理论上讲:
全虚拟化一般对硬件虚拟化功能进行了特别优化,性能上比半虚拟化要高;
半虚拟化因为基于普通的操作系统,会比较灵活,比如支持虚拟机嵌套。嵌套意味着可以在KVM虚拟机中再运行KVM。

kvm介绍

kVM 全称是 Kernel-Based Virtual Machine。也就是说 KVM 是基于 Linux 内核实现的。
KVM有一个内核模块叫 kvm.ko,只用于管理虚拟 CPU 和内存。

那 IO 的虚拟化,比如存储和网络设备则是由 Linux 内核与Qemu来实现。

作为一个 Hypervisor,KVM 本身只关注虚拟机调度和内存管理这两个方面。IO 外设的任务交给 Linux 内核和 Qemu。

大家在网上看 KVM 相关文章的时候肯定经常会看到 Libvirt 这个东西。

Libvirt 就是 KVM 的管理工具。

其实,Libvirt 除了能管理 KVM 这种 Hypervisor,还能管理 Xen,VirtualBox 等。

Libvirt 包含 3 个东西:后台 daemon 程序 libvirtd、API 库和命令行工具 virsh

  • libvirtd是服务程序,接收和处理 API 请求;
  • API 库使得其他人可以开发基于 Libvirt 的高级工具,比如 virt-manager,这是个图形化的 KVM 管理工具;
  • virsh 是我们经常要用的 KVM 命令行工具

kvm安装

部署前请确保你的CPU虚拟化功能已开启。分为两种情况:

  • 虚拟机要关机设置CPU虚拟化
  • 物理机要在BIOS里开启CPU虚拟化

kvm主机安装

关闭防火墙selinux
[root@kvm ~]# systemctl disable --now firewalld
[root@kvm ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@kvm ~]# setenforce 0
安装kvm
[root@kvm ~]# yum -y install epel-release.noarch
[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
网卡桥接,让虚拟机中的网段和公司中其它服务器是同一网段
[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.10.201
GATEWAY=192.168.10.2
PREFIX=24
DNS1=114.114.114.114
[root@kvm network-scripts]# vim ifcfg-ens33 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br02

##重启网卡让它生效
[root@kvm network-scripts]# systemctl restart NetworkManager
[root@kvm ~]# ifdown ens33 ;ifup ens33
[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:77:cc:9a 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:77:cc:9a brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.201/24 brd 192.168.10.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe77:cc9a/64 scope link 
       valid_lft forever preferred_lft forever
[root@kvm ~]#

##查看网桥
[root@kvm ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c2977cc9a       no              ens33
virbr0          8000.5254009e099c       yes             virbr0-nic
[root@kvm ~]#
启动服务
[root@kvm ~]# systemctl enable --now libvirtd

##查看是否可用
[root@kvm ~]# virsh --version
6.0.0
[root@kvm ~]# virt-install --version
2.2.1
[root@kvm ~]# virsh list --all
 Id   Name   State
--------------------

[root@kvm ~]#
做软链接
[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 03:36 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm

kvm和web管理界面主机安装

在centos7.5上完成

kvm安装
关闭防火墙selinux
[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
安装kvm
##注意centos7和8上安装的包名有差异
[root@localhost ~]# yum -y install epel-release.noarch
[root@localhost ~]# 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
网卡桥接,让虚拟机中的网段和公司中其它服务器是同一网段
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@kvm network-scripts]# vim ifcfg-br0 
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
NM_CONTROLLED=no
ONBOOT=yes
IPADDR=192.168.10.100
PREFIX=24
GATEWAY=192.168.10.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
NM_CONTROLLED=no


##重启网卡让它生效
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# 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 pfifo_fast master br0 state UP group default qlen 1000
    link/ether 00:0c:29:30:39:b9 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::20c:29ff:fe30:39b9/64 scope link 
       valid_lft forever preferred_lft forever
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:30:39:b9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.100/24 brd 192.168.10.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe30:39b9/64 scope link 
       valid_lft forever preferred_lft forever

启动服务
[root@localhost ~]# systemctl enable --now libvirtd

##查看是否可用
[root@localhost ~]# virsh --version
4.5.0
[root@localhost ~]# virt-install --version
1.5.0
[root@localhost ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------

[root@localhost ~]#
做软链接
[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@localhost ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 Oct 20 09:42 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
web管理界面安装
安装依赖包
##注意centos7和8上安装的包名有差异
[root@localhost ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
拉取webvirtmgr代码仓库
[root@localhost ~]# cd /usr/local/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.97 MiB | 1.37 MiB/s, done.
Resolving deltas: 100% (3606/3606), done.
[root@localhost src]# ls
webvirtmgr
[root@localhost src]#
安装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]# pip install -r requirements.txt
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting django==1.5.5 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6eb57be879ae13ad4/Django-1.5.5.tar.gz (8.1MB)
    100% |████████████████████████████████| 8.1MB 72kB/s 
Collecting gunicorn==19.5.0 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51b489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113kB)
    100% |████████████████████████████████| 122kB 141kB/s 
Collecting lockfile>=0.9 (from -r requirements.txt (line 5))
  Downloading https://files.pythonhosted.org/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
[root@localhost webvirtmgr]#

##如果上面报错就用下面的
[root@localhost webvirtmgr]# pip install -i https://pypi.douban.com/simple -r requirements.txt
检查sqlite3是否安装
[root@localhost 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@localhost webvirtmgr]#
初始化账号信息
[root@localhost webvirtmgr]# ./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: 1@2.com     			 //设置超级管理员邮箱
Password:     						 //设置超级管理员密码
Password (again):    				  //再次输入超级管理员密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
[root@localhost webvirtmgr]#
拷贝web网页到指定目录
[root@localhost webvirtmgr]# mkdir /var/www
[root@localhost webvirtmgr]# cp -r /usr/local/src/webvirtmgr /var/www/
[root@localhost webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
[root@localhost webvirtmgr]# ll /var/www/
total 4
drwxr-xr-x 20 nginx nginx 4096 Oct 20 09:59 webvirtmgr
[root@localhost webvirtmgr]#
做免密登陆,用于管理kvm主机
[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:zxFcKamvgE64FxFlDQkj1AXMkxsnDrDLIIl6Z1DMT50 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|.oo*=*== . ...   |
|.o.o%o+ E.o..    |
|* .o X   .o.     |
|=. .+ . .  .     |
|o...oo  S..      |
| ..o+ .  o..     |
|   + . . .o      |
|  . o   .        |
|   .             |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id 192.168.10.100
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.10.100 (192.168.10.100)' can't be established.
ECDSA key fingerprint is SHA256:IAuymHdb708+KP7sLb9jLHhRdPadOx7RkkIREO59LFI.
ECDSA key fingerprint is MD5:5f:f2:0c:89:2d:86:d8:99:26:c0:be:2b:30:ca:d7:94.
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.10.100's password: 

Number of key(s) added: 1

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

[root@localhost ~]# 
配置端口转发
[root@localhost ~]# ssh 192.168.10.100 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Wed Oct 20 09:44:51 2021 from 192.168.10.1
配置nginx
[root@localhost ~]# vim /etc/nginx/nginx.conf
 36     include /etc/nginx/conf.d/*.conf;
 37 
 38     server {
 39         listen       80;
 40         server_name  localhost;
 41 
 42         # Load configuration files for the default server block.
 43         include /etc/nginx/default.d/*.conf;
 44 
 45         location / {
 46             root html;
 47             index index.html index.htm;
 48         }
 49 
 50         error_page 404 /404.html;
 51             location = /40x.html {
 52         }
 53 
 54         error_page 500 502 503 504 /50x.html;
 55             location = /50x.html {
 56         }
 57     }
[root@localhost ~]# 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@localhost ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '127.0.0.1:8000'
backlog = 2048


##重启nginx
[root@localhost ~]# systemctl restart nginx.service 
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port
LISTEN      0      128            127.0.0.1:6080                               *:*
LISTEN      0      128            127.0.0.1:8000                               *:*
LISTEN      0      128                    *:80                                 *:*
LISTEN      0      5          192.168.122.1:53                                 *:*
LISTEN      0      128                    *:22                                 *:*
LISTEN      0      128                  ::1:6080                              :::*
LISTEN      0      128                  ::1:8000                              :::*
LISTEN      0      128                   :::22                                :::*
[root@localhost ~]#
设置supervisor
[root@kvm-webmanager ~]# 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

##重启supervisor
[root@localhost ~]# systemctl restart supervisord.service
[root@localhost ~]# systemctl enable supervisord.service
[root@localhost ~]# ss -antl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port
LISTEN      0      128            127.0.0.1:6080                               *:*
LISTEN      0      128            127.0.0.1:8000                               *:*
LISTEN      0      128                    *:80                                 *:*
LISTEN      0      5          192.168.122.1:53                                 *:*
LISTEN      0      128                    *:22                                 *:*
LISTEN      0      128                  ::1:6080                              :::*
LISTEN      0      128                  ::1:8000                              :::*
LISTEN      0      128                   :::22                                :::*
配置nginx用户
[root@localhost ~]# su - nginx -s /bin/bash
-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:M+HR5xKs4wmYOFyfzvOJmridq/5cEYIOh81LkvnQA/Q nginx@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|o.               |
| O..     o       |
|B OEo . o + .    |
| X = = + + +     |
|  B o = S . .    |
|   . o + = .     |
|      = o        |
|   + + + .       |
| .=+Oo. o        |
+----[SHA256]-----+
-bash-4.2$ 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.10.100
/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.10.100' (ECDSA) to the list of known hosts.
root@192.168.10.100's password: 

Number of key(s) added: 1

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

-bash-4.2$ exit
logout
[root@localhost ~]#
重启nginx和libvirt
[root@localhost ~]# systemctl restart nginx.service 
[root@localhost ~]# systemctl restart supervisord.service
访问ip

把KVM主机也添加进来
做免密登陆
[root@localhost ~]# su - nginx -s /bin/bash
Last login: Wed Oct 20 10:36:19 EDT 2021 on pts/1
-bash-4.2$ ssh-copy-id root@192.168.10.201
/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.10.201' (ECDSA) to the list of known hosts.
root@192.168.10.201's password: 

Number of key(s) added: 1

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

-bash-4.2$ exit
logout
[root@localhost ~]#

通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images/
[root@kvm ~]# ls /var/lib/libvirt/images/
CentOS-7-x86_64-DVD-1804-7.5.iso
[root@kvm ~]#

创建系统安装镜像

网络管理

实例管理

虚拟机插入光盘

启动虚拟机

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值