注:实验环境:WebVirtMgr主机(CentOS7.9)

KVM主机(CentOS7以上)

搭建环境这里使用root用户,偶有切换到其他用户,这是服务器上部署WebVirtMgr,默认视为已部署好KVM平台,这里部署WebVirtMgr的服务器与KVM主机不是一台服务器,以下操作大多在部署WebVirtMgr的服务器上,在KVM主机的操作仅在步骤12里的部分操作(步骤前有注明在KVM主机)


0. 关闭防火墙和SELinux

systemctl stop firewalld
systemctl disable firewalld
vim /etc/selinux/config
第七行改为SELINUX=disabled
reboot

1. yum源的配置及安装所需软件包

yum -y install epel-release
rpm -Uvh https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm     //安装nginx源
yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
yum -y install gcc python-devel

2. 由于pip版本过低,无法使用,直接升级最新版也报错,所以先升级到过渡版,再使用(由于pip21不支持python 2.7,所以无法升级到pip最新版)

yum -y install wget  //安装wget命令,如果wget 可使用,无需输入该条命令
wget https://files.pythonhosted.org/packages/0b/f5/be8e741434a4bf4ce5dbc235aa28ed0666178ea8986ddc10d035023744e6/pip-20.2.4.tar.gz
tar -zxvf pip-20.2.4.tar.gz 
cd pip-20.2.4/
python setup.py install
pip install -U pip
pip install numpy 

3. 下载安装webvirtmgr.git软件

cd /usr/local/src/
git clone git://github.com/retspen/webvirtmgr.git
cd webvirtmgr/
pip install -r requirements.txt

4. 安装数据库

yum install python-sqlite2          //默认情况下,centos系统一般会自带sqlite软件(执行sqlite3 命令,不报错就说明已经安装了)

5. 对django进行环境配置

pwd // 显示为/usr/local/src/webvirtmgr,如不是,cd 到该目录
[root@localhost webvirtmgr]# ./manage.py syncdb  //创建用户
WARNING:root:No local_settings file found. #先无视掉
···
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  #填yes
Username (leave blank to use 'root'): user #用户名,随便起的
Email address: user@admin.com #邮箱,随便写的
Password: #密码自定
Password (again): #重复输入,确认密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
[root@localhost webvirtmgr]# ./manage.py collectstatic     //生成配置文件
WARNING:root:No local_settings file found. #先无视掉

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes #填yes
···
[root@localhost webvirtmgr]# ./manage.py createsuperuser //添加管理员账号
WARNING:root:No local_settings file found. #先无视掉
Username (leave blank to use 'root'): admin  #管理员名
Email address: admin@admin.com #邮箱,随便写的
Password: #密码自定
Password (again): #重复输入,确认密码
Superuser created successfully.

6. 拷贝web到 相关目录

mkdir -pv /var/www
cp -Rv /usr/local/src/webvirtmgr /var/www/webvirtmgr

7. 设置Nginx

[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; # or /srv instead of /var
        expires max;
    }

    location ~ .*\.(js|css)$ {
           proxy_pass http://127.0.0.1:8000;
    }

    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 $scheme;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        client_max_body_size 1024M; # Set higher depending on your needs 
    }
}
[root@localhost ~]# vim /etc/nginx/nginx.conf //将下面的注释掉
#    server {
#        listen       80;
#        listen       [::]:80;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }
另外,要确定确定nginx.conf里http{ }里有include /etc/nginx/conf.d/*.conf;这条代码
[root@localhost ~]#systemctl restart nginx
[root@localhost ~]systemctl enable nginx

8. 启动并开机自启supervisord服务,

systemctl start supervisord
systemctl enable supervisord

9. 设置supervisor

chown -R nginx:nginx /var/www/webvirtmgr
[root@localhost ~]# vim /etc/supervisord.d/webvirtmgr.ini
[program:webvirtmgr]
command=/usr/bin/python /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/python /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

10. 重启supervisord服务

systemctl restart supervisord

11. 更新

cd /var/www/webvirtmgr
git pull
[root@localhost webvirtmgr]# ./manage.py collectstatic
WARNING:root:No local_settings file found. #无视掉

You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes #填yes

0 static files copied, 75 unmodified.
systemctl restart supervisord

12. SSH连接KVM主机的相关设置

cd /home/
mkdir nginx
chown nginx:nginx nginx/ 
chmod 700 nginx/ -R
[root@localhost home]# su - nginx -s /bin/bash
[root@localhost home]# su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen 
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: #直接回车
···
-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$ exit
logout
[root@localhost home]# su - nginx -s /bin/bash
[root@localhost home]# su - nginx -s /bin/bash
Last login: Sun Sep 12 22:39:24 CST 2021 on pts/1
-bash-4.2$ ssh-copy-id root@X.X.X.X #输入KVM主机IP
/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 '117.34.25.45' (ECDSA) to the list of known hosts.
root@117.34.25.45's password: 

Number of key(s) added: 1

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

-bash-4.2$ exit
logout

登录KVM主机操作

[root@localhost ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:root #注意这里采用的是root用户
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
chown -R root:root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
systemctl restart libvirtd

登录WebVirtMgr主机操作

systemctl restart nginx

13. 登录WebVirtMgr页面并添加KVM主机

CentOS7部署kvm虚拟化管理平台WebVirtMgr_centos

CentOS7部署kvm虚拟化管理平台WebVirtMgr_nginx_02

CentOS7部署kvm虚拟化管理平台WebVirtMgr_python_03

CentOS7部署kvm虚拟化管理平台WebVirtMgr_centos_04

参考资料:kvm虚拟化管理平台WebVirtMgr部署-完整记录(1)

github上的WebVirtMgr的wiki