答疑解惑-WebVirMgr管理平台的实现过程

442 阅读3分钟

这是我参与8月更文挑战的第10天,活动详情查看:8月更文挑战

今天有小伙伴私信求助,公司让他做部署一个WebVirMgr平台,从来没有接触过的他只好来找我求助。 于是我花了点时间,写了这篇文章,希望对你有用。

webvirmgr管理平台介绍

WebVirtMgr是近一年了发展较快,比较活跃,非常清新的一个KVM管理平台。一看名字就知道,定位非常清晰,就是要将VirtManager页面化。

整个平台是用python开发的,前端是基于python的 Django,后端是基于Libvirt的Python接口。风格也是Python的风格,特点是:

  • 容易使用
  • 基于libvirt的连接。
  • 虚拟机生命周期管理。
  • 在宿主机管理方面支持以下功能:
  • CPU利用率。
  • 内存利用率。
  • 网络资源池管理。
  • 存储资源池管理。
  • 虚拟机镜像。
  • 虚拟机克隆。
  • 快照管理。
  • 日子管理。

部署环境

操作系统内核: [root@itlaoxin-17 ~]# uname -r 3.10.0-1062.el7.x86_64

操作系统版本: [root@itlaoxin-17 ~]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core)

安装依赖包

根据官网,全部用yum安装 其中包括 7的源,nginx ,gcc,及python的依赖包等

yum -y install http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
 
yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
 
yum -y install gcc python-devel

查看pip是否已经安装 pip --help

查看pip安装好后,直接安装numpy

pip install numpy

注: NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

  1. 安装Python环境和Django环境

此处直接下载github上的源码即可

git clone git://github.com/retspen/webvirtmgr.git
 
cd webvirtmgr
pip install -r requirements.txt
 
pip install --upgrade pip(此命令可以不执行)
 
./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'): root
Email address: 1878547732@qq.com
Password: zhl****!
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s) 
下面的命令是管理员账号,用户名,密码就是上面设置的用户名密码。
./manage.py collectstatic  
 
This will overwrite existing files!
Are you sure you want to do this?
 
Type 'yes' to continue, or 'no' to cancel: yes 
./manage.py createsuperuser  添加另外一个用户 (不添加也行)

开始执行安装:

[root@iZ2zeeg42qkecbgssbgkqtZ webvirtmgr]# ./manage.py createsuperuser

WARNING:root:No local_settings file found.
Username: admin
Email address: 1878547732@qq.com
Password: zhl***!
Password (again):
Superuser created successfully.

[root@iZ2zeeg42qkecbgssbgkqtZ webvirtmgr]# #cd .. 此时路径在 /root 下 sudo mv webvirtmgr /var/www/

vim /etc/nginx/conf.d

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 / {
        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
    }
}
  

#表格里的全部注释掉 vim /etc/nginx/nginx.conf

#    server {
#        listen       80 default_server;
#        server_name  localhost;
#        root         /usr/share/nginx/html;
#
#        #charset koi8-r;
#
#        #access_log  /var/log/nginx/host.access.log  main;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        # redirect server error pages to the static page /40x.html
#        #
#        error_page  404              /404.html;
#        location = /40x.html {
#        }
#
#         redirect server error pages to the static page /50x.html
#        
#        error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#        }
#    } 

去掉#的小技巧

ctrl +v 选中#号 --按X即可去掉井号

接下来,我们需要给与权限,并启动nginx

赋予nginx的所有组和所有者
#chown -R nginx:nginx /var/www/webvirtmgr  
 
 重启nginx
#service nginx restart
#/usr/sbin/setsebool httpd_can_network_connect true 

设置开机启动
#chkconfig supervisord on
#chown -R nginx:nginx /var/www/webvirtmgr

这是我参与8月更文挑战的第10天,活动详情查看:8月更文挑战

接下来,我们配置webvirtmgr.ini

#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
  1. 重新启动supervisor守护程序 service supervisord stop

service supervisord start

  1. 访问测试 ./manage.py runserver 0:8000

http://39.97.97.79:8000/login/