KVM Web管理平台 WebVirtMgr


WebVirtMgr介紹

  WebVirtMgr是一個KVM管理平台,讓kvm管理變得更為可視化,對中小型kvm應用場景帶來了更多方便。WebVirtMgr采用幾乎純Python開發,其前端是基於Python的Django,后端是基於Libvirt的Python接口,將日常kvm的管理操作變的更加的可視化。
WebVirtMgr 特點
操作簡單,易於使用 、通過libvirt的API接口對kvm進行管理、提供對虛擬機生命周期管理
WebVirtMgr 功能
宿主機管理支持以下功能、CPU利用率、內存利用率、網絡資源池管理、存儲資源池管理、虛擬機鏡像、虛擬機克隆、快照管理、日志管理、虛機遷移、虛擬機管理支持以下功能、CPU利用率、內存利用率、光盤管理、關/開/暫停虛擬機、安裝虛擬機、VNC console連接、創建快照
 
##盡量參考官方文檔##
 
官方文檔
 

WebVirtMgr 管理工具安裝

本次安裝是以centos7.4 為例
 

一、安裝前的部署

安裝一些依賴包
yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx gcc python-devel wget vim net-tools lrzsz 

 

1 安裝pip

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip -V  #查看pip版本

 

 
 
pip install numpy

 

 

2.安裝python的需要包和配置Django環境

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

3.安裝nginx

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

 

4.安裝supervisor

 
安裝參考
開機自啟參考
 
cat  /etc/supervisord.conf

 

如果沒有這個文件按照一下步驟安裝
有的話忽略此步驟
pip install supervisor
mkdir /etc/supervisord.d/
echo_supervisord_conf > /etc/supervisord.conf
sudo su - root -c "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

 

二、安裝環境

cd webvirtmgr
pip install -r requirements.txt 

 

# or python-pip (RedHat, Fedora, CentOS, OpenSuse)
 
./manage.py syncdb

 

1.創建用戶

輸入以下用戶信息
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

 

2.配置一個超級用戶

./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.

 

3.設置nginx

a、使用:8000端口
(1)移動這個webvirtmgr目錄到/var/www下
cd  ..
mv webvirtmgr /var/www/

 

webvirtmgr 目錄下還有一個名稱為webvirtmgr 的文件夾
不要單獨移動 webvirtmgr/webvirtmgr 文件
(2)編輯配置文件
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 
    }
}
 

 

(3)啟動nginx並設置開機自啟動(如果不設置開機自啟動,重啟服務器supervisor無法管理Django進程),並開機自啟動supervisord
/etc/init.d/nginx start
或者
systemctl restart   nginx
systemctl enable supervisord
分配權限
chown nginx.nginx /var/www/webvirtmgr
 

4.設置supervisor

在/etc/supervisord.conf末尾加入下面的配置:
vim /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    
 
進程無法啟動或者報錯 可以選擇吧 log 注釋取消
 

5、重啟supervisord

開機自啟參考
設置完之后重啟即可
systemctl restart  supervisord.service
systemctl enable  supervisord.service
systemctl status   supervisord.service 
 

6.更新

cd /var/www/webvirtmgr
git pull
./manage.py collectstatic
systemctl  restart supervisord
創建用戶
 
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)
/usr/bin/python2 manage.py syncdb //最終發現使用python2執行這個命令就不報錯了
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):
/usr/bin/python2 manage.py collectstatic //生成配置文件(同樣使用python2版本執行,不要使用默認的python執行)
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
..........
/usr/bin/python2 manage.py createsuperuser //添加管理員賬號(同樣使用python2版本執行,不要使用默認的python執行)
WARNING:root:No local_settings file found.
Username: ops //這個是管理員賬號,用上面的admin和這個管理員賬號都可以登陸webvirtmgr的web界面管理平台
Email address:
Password:
Password (again):
Superuser created successfully.
 

7、如果有錯誤或不運行

(僅用於調試面板或顯影)
./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,命令創建。登錄后如下圖所示
 

8、配置虛擬機所在宿主機

webvirtmgr客戶端就這樣搭建完了,接下來需要配置虛擬機所在宿主機的,參考git地址.
 
配置宿主機
配置宿主機環境:
system: centos 7.4
1.下載並執行腳本
libvirt-bootstrap.sh
如果虛擬機比較多,該腳本執行時間會比較長,因為會執行service libvirt-guests restart,會將所有運行的虛擬機掛起然后再恢復,感覺這一步不是必須的,因為我有一台只設置ssh認證,也可以正常連接。
curl http://retspen.github.io/libvirt-bootstrap.sh | sudo sh
或者用wget
wget -O - http://retspen.github.io/libvirt-bootstrap.sh | sudo sh

9、.配置防火牆

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

 

設置TCP授權

webvirtmgr新建服務器連接時需要此賬號
(1)用saslpasswd2命令給libvirt的用戶lenovo設置密碼
(1)用saslpasswd2命令給libvirt的用戶lenovo設置密碼
saslpasswd2 -a libvirt lenovo
Password: xuewenlong
Again (for verification): xuewenlong
(2)生成一個密碼庫
sasldblistusers2 -f /etc/libvirt/passwd.db
lenovo@webvirtmgr.net: userPassword
(3)設置ssh授權 
ssh-keygen -t rsa //產生公私鑰

 

直接回車,回車,回車
ssh-copy-id 192.168.1.120

 

/由於這里webvirtmgr和kvm服務部署在同一台機器,所以這里本地信任。如果kvm部署在其他機器,那么這個是其他它的ip 同時也要設置ssh key密鑰
提示輸入密碼的時候直接輸入之前1.120的密碼
ssh 192.168.1.120 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:6080
/*--原配置內容--------------/
 
          
ssh-copy-id 1.1.1.1 ssh 1.1.1.1 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:6080
 
          
/*--原配置內容--------------/
 
其他kvm 主機加入已經部署的kvm web 操作
之前的web管理
在部署web管理的主機上執行命令
ssh-keygen -t rsa
然后在執行
ssh-copy-id 192.168.1.165
添加新的kvm宿主機
 
查看新加的kvm宿主機狀態 看有無報錯
 
 
刪除新加的賬號:
sudo saslpasswd2 -a libvirt -d lenovo

 

10.確認驗證新加的賬號配置

virsh -c qemu+tcp://IP_address/system nodeinfo
(virsh -c qemu+tcp://192.168.1.50/system nodeinfo)
Please enter your authentication name: lenovo
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

 

注意:賬號全名帶hostname,如lenovo@webvirtmgr .NET
測試的時候這一步測試沒有成功 但是可以鏈接
 

11.設置ssh認證

ssh和tcp設置一種即可,其實就是設置無密碼登錄,要注意的是從webvirtmgr的什么用戶到宿主機的什么用戶的無密碼登錄,比如我用root跑的django webvirtmgr,而宿主機也是root跑的virsh,所以需要設置root到root的無密碼登錄。而git官網推薦的是用nginx用戶跑django webvirtmgr,webvirtmgr用戶跑的virsh,所以設置的是nginx用戶到宿主機webvirtmgr用戶的無密碼登錄。
 
 
 

12.使用tcp認證連接服務器

訪問: http://192.168.1.120:8000,xxxx是webvirtmgr的ip地址,點擊new connection
 

13.填寫kvm宿主機的一些信息

基礎架構可以看到一些vm虛擬機
 
 
網頁控制台 遠程鏈接報錯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 &

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM