KVM虚拟化管理平台WebVirtMgr部署及使用

KVM虚拟化管理平台WebVirtMgr部署及使用

 
需求: 公司机房有一台2U的服务器(64G内存,32核),由于近期新增业务比较多,测试机也要新增,服务器资源十分有限。所以打算在这台2U服务器上部署kvm虚拟化,虚出多台VM出来,以应对新的测试需求。
 
方案:当KVM宿主机越来越多,需要对宿主机的状态进行调控,决定采用WebVirtMgr作为kvm虚拟化的web管理工具,图形化的WEB,让人能更方便的查看kvm 宿主机的情况和操作
 

WebVirtMgr介绍

WebVirtMgr是近两年来发展较快,比较活跃,非常清新的一个KVM管理平台,提供对宿主机和虚机的统一管理,它有别于kvm自带的图形管理工具(virtual machine manager),让kvm管理变得更为可视化,对中小型kvm应用场景带来了更多方便。
WebVirtMgr采用几乎纯Python开发,其前端是基于Python的Django,后端是基于Libvirt的Python接口,将日常kvm的管理操作变的更加的可视化。
 
WebVirtMgr特点
操作简单,易于使用
通过libvirt的API接口对kvm进行管理
提供对虚拟机生命周期管理
WebVirtMgr 功能
 
宿主机管理支持以下功能
CPU利用率
内存利用率
网络资源池管理
存储资源池管理
虚拟机镜像
虚拟机克隆
快照管理
日志管理
虚机迁移

 

虚拟机管理支持以下功能
CPU利用率
内存利用率
光盘管理
关/开/暂停虚拟机
安装虚拟机
VNC console连接
创建快照
 
一、准备工作
这里我将webvirtmgr服务器和kvm服务器放在同一台机器上部署的,即单机部署(测试)
系统:Centos 7.8
内存:5.6G
CPU:4核
ip:192.168.10.10(内网),10.3.134.35(外网)
 
二、KVM虚拟化管理工具WebVirtMgr安装
 
1 安装支持的软件源;
yum -y install https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-12.noarch.rpm
2 安装相关软件;
yum -y install git python-pip libvirt-python libxml2-python python-websockify upervisor nginx novnc python-virtinst libvirt vm ridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v tunctl unzip zip
3 从git-hub中下载相关的webvirtmgr代码;
cd /usr/local/src/
git clone git://github.com/retspen/webvirtmgr.git
4 安装webvirtmgr;
cd webvirtmgr/
pip install -r requirements.txt
6 对django进行环境配置;
./manage.py syncdb    # 默认是python执行,如下报错,换用其他版本的python
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: 邮箱
Password:密码
Password (again):密码
###==========================###
./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
..........
..........
###==========================###
./manage.py createsuperuser   #添加管理员账号
WARNING:root:No local_settings file found.
Username: sun                      //这个是管理员账号,用上面的admin和这个管理员账号都可以登陆webvirtmgr的web界面管理平台
Email address: 1916989848@qq.com
Password:
Password (again):
Superuser created successfully.
###==========================###
/usr/bin/python2 manage.py createsuperuser   #添加管理员账号(同样使用python2版本执行,不要使用默认的python执行)

 注意此处用默认的python执行上面命令,一般会报错,如下:

ImportError: No module named django.core.management

这个一般是由于python版本引起的,因为系统自带有好几个版本的python
[root@openstack webvirtmgr]# python       //按Tab键自查找
python python2.6
python2 python2.6-config python-config
[root@openstack webvirtmgr]# python -V
Python 2.6.6

由此可看出,系统默认的Python版本是2.6.6
说明上面命令默认是python2.6执行的

既然使用python2.6执行上面的命令报错,那就换用其他版本python2执行(如果当前是python3.3.0,那么就将下面的/usr/bin/python2换成/usr/bin/python2.6)


7 拷贝web到 相关目录;
cd ..
mkdir -pv /var/www
cp -Rv webvirtmgr /var/www/webvirtmg
chown -R nginx:nginx /var/www/webvirtmgr
8 设置ssh;
su - nginx -s /bin/bash
ssh-keygen #产生公私钥
ssh-copy-id (kvm-ip) #由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,那么这个是它的ip
9 辑nginx配置文件
提前确保/etc/nginx/nginx.conf文件里开启了“include /etc/nginx/conf.d/*.conf;”
[root@local]#vim /etc/nginx/conf.d/webvirtmgr.conf //添加下面内容到文件中
server {
listen 80;
server_name 10.3.134.35;
#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 $remote_addr;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
client_max_body_size 1024M; # Set higher depending on your needs
}
}
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
10 设置 supervisor;
[root@openstack ops]# vim /etc/supervisord.conf //在文件末尾添加,注意将默认的python改为python2,因为上面只有用这个版本执行才不报错!
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py    #启动8000端口***如果报错无法启动,查看/var/www/wvirtmgr权限是否正常!!!
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    #启动6080端口(这是控制台vnc端口)
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=nginx
[root@localhost ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 127.0.0.1:8000              0.0.0.0:*                   LISTEN      1698/python2        
tcp        0      0 0.0.0.0:6080                0.0.0.0:*                   LISTEN      1697/python2              
tcp        0      0 0.0.0.0:5900                0.0.0.0:*                   LISTEN      544/qemu-kvm               
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      32518/nginx
11 修改eth为物理网口,br为桥接网口配置文件
cp ifcfg-eth0 ifcfg-br0
[root@vfeelit network-scripts]# cat ifcfg-br0
TYPE=Bridge
NAME=br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=none
PEERDNS=yes
NM_CONTROLLED=no
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.2
DNS1=223.5.5.5
DNS2=223.6.6.6
[root@vfeelit network-scripts]# cat ifcfg-eth0
TYPE=Ethernet
BRIDGE=br0
BOOTPROTO=none
PEERDNS="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
NM_CONTROLLED="no"

12 web访问
http://10.2.134.35/login/

这里用超级管理员登陆,只有超级管理员登陆后才能看到“基础构架”窗口

普通用户登陆后,只能看到“WebVirtMgr”一个窗口

 

 

 

选择“SSH链接“,设置Label,IP,用户

注意:Label与IP要相同

 


 

以下为部分网友出现报错:

 

 

打开后,有报错!看来在上面使用ssh连接的配置环节有误所致!

解决措施:

1)在webvirtmgr服务器(服务端)上(这里kvm和WebVirtMgr部署在同一台机器上)创建nginx用户家目录(默认nginx服务安装时是没有nginx家目录的),生成nginx的公私钥
[root@openstack ops]# cd /home/
[root@openstack home]# mkdir nginx
[root@openstack home]# chown nginx.nginx nginx/
[root@openstack home]# chmod 700 nginx/ -R
[root@openstack home]# su - nginx -s /bin/bash
-bash-4.1$ ssh-keygen                             #期间输入yes后直接回车,回车
-bash-4.1$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.1$ chmod 0600 ~/.ssh/config

2)在kvm(客服端)服务器上(这里kvm和WebVirtMgr部署在同一台机器上)配置用户,这里默认采用root用户
---------------------------------------------------------------------------------------------------------------------
如果采用其他用户,比如webvirtmgr,操作如下:
[root@openstack ops]#useradd webvirtmgr
[root@openstack ops]#echo "123456" | passwd --stdin webvirtmgr
[root@openstack ops]#groupadd libvirt
[root@openstack ops]#usermod -G libvirt -a webvirtmgr
---------------------------------------------------------------------------------------------------------------------

3)在webvirtmgr服务器(服务端)上(这里kvm和WebVirtMgr部署在同一台机器上),将nginx用户的ssh-key上传到kvm服务器上(这里kvm和WebVirtMgr部署在同一台机器上)
[root@openstack ops]# su - nginx -s /bin/bash
-bash-4.1$ ssh-copy-id root@192.168.1.17
Warning: Permanently added '192.168.1.17' (RSA) to the list of known hosts.
root@192.168.1.17's password: #输入192.168.1.17即本机的root账号
Now try logging into the machine, with "ssh 'root@192.168.1.17'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.
---------------------------------------------------------------------------------------------------------------------
这里采用的是root用户,如果采用其他用户,比如上面假设的webvirtmgr用户,操作如下:
[root@openstack ops]#su - nginx -s /bin/bash
-bash-4.1$ssh-copy-id webvirtmgr@192.168.0.23
---------------------------------------------------------------------------------------------------------------------

4)在kvm(客服端)服务器上(这里kvm和WebVirtMgr部署在同一台机器上)配置 libvirt ssh授权
[root@openstack ops]# 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

[root@openstack ops]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
-------------------------------------------------------------------------------------------------------------------------------
这里采用的是root用户,如果采用其他用户,比如上面假设的webvirtmgr用户,操作如下:
[root@openstack ops]#vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:webvirtmgr #这里就设定webvirtmgr用户
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes

[root@openstack ops]#chown -R webvirtmgr.webvirtmgr /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
--------------------------------------------------------------------------------------------------------------------------------

5)重启 libvirtd 服务
/etc/init.d/libvirtd restart


 

 

 

 

 

 
 
 
 
 三、KVM虚拟化管理工具WebVirtMgr使用

web创建虚拟机;

1:首先创建iso目录和虚拟机文件的存储目录

 

 

 

 2:添加网络

 

 3:创建虚拟机

 

 

 

 

 

 

 

 

 

 

 
 
 
posted @ 2020-10-22 20:26  梦里花落知多少sl  阅读(1591)  评论(0编辑  收藏  举报