关于kvm:快速搭建-kvm-web-管理工具-WebVirtMgr

14次阅读

共计 7085 个字符,预计需要花费 18 分钟才能阅读完成。

    • 作者:SRE 运维博客

      博客地址:https://www.cnsre.cn/

      文章地址:https://www.cnsre.cn/posts/211117937177/

      相干话题:https://www.cnsre.cn/tags/kvm/


    • WebVirtMgr是近两年来倒退较快,比拟沉闷,十分清爽的一个 KVM 治理平台,提供对宿主机和虚机的对立治理,它有别于 kvm 自带的图形管理工具(virtual machine manager),让 kvm 治理变得更为可视化,对中小型 kvm 利用场景带来了更多不便。

WebVirtMgr 介绍

WebVirtMgr 采纳简直纯 Python 开发,其前端是基于 Python 的 Django,后端是基于 Libvirt 的 Python 接口,将日常 kvm 的治理操作变的更加的可视化。

  • WebVirtMgr 特点

操作简略,易于应用、通过 libvirt 的 API 接口对 kvm 进行治理、提供对虚拟机生命周期治理

  • WebVirtMgr 性能

宿主机治理反对以下性能、CPU 利用率、内存利用率、网络资源池治理、存储资源池治理、虚拟机镜像、虚拟机克隆、快照治理、日志治理、虚机迁徙、虚拟机治理反对以下性能、CPU 利用率、内存利用率、光盘治理、关 / 开 / 暂停虚拟机、装置虚拟机、VNC console 连贯、创立快照

官网文档

https://github.com/retspen/we…

装置前的部署

装置一些依赖包

yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx gcc python-devel wget vim net-tools lrzsz 

装置 pip

wget https://bootstrap.pypa.io/get-pip.py 
python get-pip.py 
pip -V 

pip install numpy

装置 python 的须要包和配置 Django 环境

git clone git://github.com/retspen/webvirtmgr.git

装置 nginx

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 
yum install nginx -y

装置 supervisor

装置参考

https://www.2cto.com/kf/20171…

开机自启参考

https://blog.csdn.net/binggoo…

cat  /etc/supervisord.conf

{{< alert theme=”warning” dir=”ltr” >}}
⚠️ 留神

如果没有这个文件依照一下步骤装置

有的话疏忽此步骤
{{< /alert >}}

pip install supervisor
mkdir /etc/supervisord.d/
echo_supervisord_conf > /etc/supervisord.conf

新建文件夹

vim /etc/supervisord.d/app.conf

配置文件 app.conf

内容为

[program:appname]
command=/root/soft/push.api
directory=/root/soft/push.api
autostart=true
autorestart=true
user=root
stdout_logfile = /var/log/supervisor/pushapi.log
stderr_logfile = /var/log/supervisor/pushapi-error.log

批改 在配置文件最下方批改为

vim  /etc/supervisord.conf
[include]
files = /etc/supervisord.d/*.ini

supervisord -c /etc/supervisord.conf
/usr/bin/supervisorctl start all
/usr/bin/supervisorctl stop all

<script async src=”https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4855142804875926″

 crossorigin="anonymous"></script>

<ins class=”adsbygoogle”

 style="display:block; text-align:center;"
 data-ad-layout="in-article"
 data-ad-format="fluid"
 data-ad-client="ca-pub-4855142804875926"
 data-ad-slot="5670838583"></ins>

<script>

 (adsbygoogle = window.adsbygoogle || []).push({});

</script>

装置环境

cd 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: 275301281@qq.com
Password: admin
Password (again):admin 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
./manage.py collectstatic

配置一个超级用户

./manage.py createsuperuser

WARNING:root:No local_settings file found.
Username (leave blank to use 'root'): yes
Email address: 275301281@qq.com
Password: Lenovo@123
Password (again): Lenovo@123
Superuser created successfully.

设置 nginx

a、应用:8000 端口

挪动这个 webvirtmgr 目录到 /var/www

cd  ..
mv webvirtmgr /var/www/

{{< alert theme=”warning” dir=”ltr” >}}
⚠️ 留神:
webvirtmgr 目录下还有一个名称为 webvirtmgr 的文件夹
不要独自挪动 webvirtmgr/webvirtmgr 文件
{{< /alert >}}

编辑配置文件

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

启动 nginx 并设置开机自启动

(如果不设置开机自启动,重启服务器 supervisor 无奈治理 Django 过程),并开机自启动 supervisord

/etc/init.d/nginx start

或者

systemctl restart   nginx 
systemctl enable supervisord 

调配权限

chown nginx.nginx /var/www/webvirtmgr

设置 supervisor

/etc/supervisord.conf 开端退出上面的配置:

vi /etc/supervisord.conf


[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    

{{< alert theme=”warning” dir=”ltr” >}}
⚠️ 留神

过程无奈启动或者报错 能够抉择吧 log 正文勾销
{{< /alert >}}

重启 supervisord

开机自启参考

https://blog.csdn.net/binggoo…

设置完之后重启即可

systemctl restart  supervisord.service
systemctl enable  supervisord.service
systemctl status   supervisord.service 

更新

cd /var/www/webvirtmgr git pull

./manage.py collectstatic

systemctl  restart supervisord

如果有谬误或不运行

 ./manage.py runserver 0:8000
#或者后盾运行脚本
nohup python  /var/www/webvirtmgr/manage.py runserver 0:8000  >/dev/null &
nohup python  /var/www/console/webvirtmgr-console   >/dev/null &

拜访:http://x.x.x.x:8000(x.x.x.x – your server IP address),输出创立的用户和明码,如果没有创立,请用 python manager.py createsuperuser, 命令创立。登录后如下图所示

配置虚拟机所在宿主机

webvirtmgr 客户端就这样搭建完了,接下来须要配置虚拟机所在宿主机的,参考 git 地址.

配置宿主机

下载并执行脚本

如果虚拟机比拟多,该脚本执行工夫会比拟长,因为会执行 service libvirt-guests restart,会将所有运行的虚拟机挂起而后再复原,感觉这一步不是必须的,因为我有一台只设置 ssh 认证,也能够失常连贯。

curl http://retspen.github.io/libvirt-bootstrap.sh | sudo sh

如果没有 curl 就用 wget

wget -O - http://retspen.github.io/libvirt-bootstrap.sh | sudo sh

配置防火墙

iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 16509 -j ACCEPT

设置 TCP 受权

参考:https://github.com/retspen/webvirtmgr/wiki/Setup-TCP-authorization

webvirtmgr 新建服务器连贯时须要此账号

用 saslpasswd2 命令给 libvirt 的用户 cnsre 设置明码

saslpasswd2 -a libvirt cnsre
Password: cnsre
Again (for verification): cnsre

生成一个明码库

sasldblistusers2 -f /etc/libvirt/passwd.db 

cnsre@webvirtmgr.cn: userPassword

设置 ssh 受权

ssh-keygen -t rsa       # 产生公私钥

间接回车,回车,回车

ssh-copy-id 192.168.1.120  

{{< alert theme=”warning” dir=”ltr” >}}
⚠️ 留神

因为这里 webvirtmgr 和 kvm 服务部署在同一台机器,所以这里本地信赖。

如果 kvm 部署在其余机器,那么这个是其余它的 ip 同时也要设置 ssh key 密钥
{{< /alert >}}

提醒输出明码的时候间接输出之前 1.120 的明码

ssh 192.168.1.120 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:6080

web 平台退出其余 kvm 宿主机

在部署 web 治理的主机上执行命令

ssh-keygen -t rsa

而后在执行

ssh-copy-id 192.168.1.165

增加新的 kvm 宿主机

查看新加的 kvm 宿主机状态 看有无报错

删除新加的账号

sudo saslpasswd2 -a libvirt -d cnsre

确认验证新加的账号配置

virsh -c qemu+tcp://IP_address/system nodeinfo
(virsh -c qemu+tcp://192.168.1.50/system nodeinfo)
Please enter your authentication name: cnsre
Please enter your password: xxxxxx
CPU model:           x86_64
CPU(s):              2
CPU frequency:       2611 MHz
CPU socket(s):       1
Core(s) per socket:  2
Thread(s) per core:  1
NUMA cell(s):        1
Memory size:         2019260 kB

{{< alert theme=”warning” dir=”ltr” >}}
⚠️ 留神

账号全名带 hostname,如 cnsre@webvirtmgr.cn

测试的时候这一步测试没有胜利 然而能够链接
{{< /alert >}}

设置 ssh 认证

{{< notice warning “ 留神 ” >}}
ssh 和 tcp 设置一种即可,其实就是设置无明码登录,要留神的是从 webvirtmgr 的什么用户到宿主机的什么用户的无明码登录,比方我用 root 跑的 django webvirtmgr,而宿主机也是 root 跑的 virsh,所以须要设置 root 到 root 的无明码登录。而 git 官网举荐的是用 nginx 用户跑 django webvirtmgr,webvirtmgr 用户跑的 virsh,所以设置的是 nginx 用户到宿主机 webvirtmgr 用户的无明码登录。
{{< /notice >}}

参考:https://github.com/retspen/webvirtmgr/wiki/Setup-SSH-Authorizatio

应用 tcp 认证连贯服务器

拜访:http://192.168.1.120:8000,xxxx 是 webvirtmgr 的 ip 地址,点击 new connection

填写 kvm 宿主机的一些信息

基础架构能够看到一些 vm 虚拟机

KVM WEB 治理常见报错

网页控制台 近程链接报错 1006

装置 vnc 即可

yum install -y novnc

网页控制台 近程链接报错 505

cd /var/www/console/
./webvirtmgr-console  &

后盾运行脚本

nohup python  /var/www/webvirtmgr/manage.py runserver 0:8000  >/dev/null & 
nohup python  /var/www/console/webvirtmgr-console   >/dev/null &
    • 作者:SRE 运维博客

      博客地址:https://www.cnsre.cn/

      文章地址:https://www.cnsre.cn/posts/211117937177/

      相干话题:https://www.cnsre.cn/tags/kvm/

正文完
 0