KVM 中安装 WebVirtMgr

发布于 2022-04-23  142 次阅读


KVM 中安装 WebVirtMgr

WebVirtMgr采用Python开发,其前端是基于Python的Django,后端是基于Libvirt的Python接口。WebVirtMgr 是一个基于 Web 的 KVM 虚拟机管理工具,它允许用户通过 Web 界面轻松管理 KVM 虚拟机。WebVirtMgr 提供了创建、启动、关闭、暂停、恢复、迁移、克隆和删除虚拟机等功能,同时还可以管理虚拟机磁盘、网络、CPU 和内存等配置。此外,WebVirtMgr 还支持 VNC 和 SPICE 远程访问虚拟机控制台

一、WebVirtMgr 安装

在安装WebVirtMgr 前需要先安装好KVM环境

安装epel-release 源,用于安装第三方软件如python-pip包使用

yum -y install epel-release

安装WebVirtMgr 依赖包

yum -y install git python-pip python-libvirt python-libxml2 novnc supervisor

以下是WebVirtMgr git源码包地址:https://github.com/retspen/webvirtmgr

mkdir /home/www 
cd /home/www/ && git clone git://github.com/retspen/webvirtmgr.git

添加ssh免密(我们这里WebVirtMgr 和kvm 在同一台主机,如果在不同主机就需要配置WebVirtMgr 主机免密SSH到kvm主机) 1

ssh-keygen -t rsa
cd /root/.ssh
cat id_rsa.pub >>authorized_keys

WebVirtMgr 安装

cd /home/www/webvirtmgr
#安装 WebVirtMgr 依赖包
pip install -r requirements.txt
##初始化环境
./manage.py syncdb
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'): admin #管理员用户
Email address: #管理员用户邮箱,可以不填写
Password: #管理员用户密码
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
-----------------------------------
##配置Django 静态页面
./manage.py collectstatic

启动 WebVirtMgr,端口设置为80

./manage.py runserver 0:80

到这里就可以访问WebVirtMgr 页面,但是该命令是前台执行,需要守护进程

image-20230327143833147

二、配置 supervisor 进程守护

检查super配置,将该内容加上去

tail -n10 /etc/supervisord.conf
[include]
files = supervisord.d/*.ini

创建webvirtmgr-console 和manage.py runserver的守护进程

program:webvirtmgr 用于

cat /etc/supervisord.d/webvirtmgr.ini
[program:webvirtmgr]
command=/usr/bin/python2 /home/www/webvirtmgr/manage.py run_gunicorn -c /home/www/webvirtmgr/conf/gunicorn.conf.py
directory=/home/www/webvirtmgr
autostart=true
autorestart=true
logfile=/var/log/supervisor/webvirtmgr.log
log_stderr=true
user=root
[program:webvirtmgr-console]
command=/usr/bin/python2 /home/www/webvirtmgr/console/webvirtmgr-console
directory=/home/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=root

修改gunicorn.conf.py 文件

grep "bind" /home/www/webvirtmgr/conf/gunicorn.conf.py
# bind - The socket to bind.
bind = '127.0.0.1:8000'
#######修改端口以及指定ip
sed -i 's/127.0.0.1\:8000/10.203.60.65:80/g' /home/www/webvirtmgr/conf/gunicorn.conf.py

设置super开机自启动

systemctl restart supervisord && systemctl enable supervisord

三、使用WebVirtMgr 创建虚拟机

3.1 存储池

image-20230327144406662

由于我的kvm 中之前安装过虚拟机,webvirtmgr 根据之前安装虚拟机的记录,自动添加vm 和iso 的存储池

如果没有通过New Storage添加存储池,建议至少添加两个存储池,一个用于储存kvm 文件,一个存储镜像存储池并上传镜像

image-20230327145442509

根据环境情况填写自己指定的kvm 文件以及镜像文件存储位置,创建对应存储即可

3.2 网络池

创建桥接网络,选择br0 ,名称自定义

image-20230327152748048

3.3 安装虚拟机

创建一块磁盘用于存储kvm 数据,在存储池中选择存储kvm 数据的池,点击添加镜像

image-20230327154232584

填写虚拟机存储磁盘名称以及容量

image-20230327154512445

通过New Instance →Custom Instance 创建虚拟机 ,填写名称、cpu、内存、在创建虚拟机的对话框里,点击“Add Image”添加刚才创建的 vm72.img 镜像,然后再 "Add network" 添加类型为 bridge 的桥接网络。

image-20230327154615503

在设置 →Media 中连接iso 镜像文件

image-20230327155756084

在虚拟机实例中选择刚刚创建的vm72 ,在power 中启动虚拟机

image-20230327154957620

在Access 中点击控制台既可以安装虚拟机

image-20230327173507413

3.4 添加vnc 配置(vm-install.sh)

使用 命令创建虚拟机无法使用webvirtmgr 的vnc

vm-install.sh  IP_ADDRESS NETMASK GATEWAY HOSTNAME CPU MEM(G) DISK(G)

需要先关闭虚拟机,然后在设置→XML 通过编辑在keyboard 下面加入vnc 的配置

    <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>

完成后是如下配置,就可以正常使用控制台的vnc

image-20230329104040806