作者:SRE運維博客
博客地址: https://www.cnsre.cn/
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/webvirtmgr/wiki/Install-WebVirtMgr
安裝前的部署
安裝一些依賴包
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/201712/702837.html
開機自啟參考
https://blog.csdn.net/binggoogle/article/details/53203991
cat /etc/supervisord.conf
{{< alert theme="warning" dir="ltr" >}}
⚠️ 注意
如果沒有這個文件按照一下步驟安裝
有的話忽略此步驟
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
安裝環境
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 文件
編輯配置文件
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 注釋取消
重啟supervisord
開機自啟參考
https://blog.csdn.net/binggoogle/article/details/53203991
設置完之后重啟即可
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密鑰
提示輸入密碼的時候直接輸入之前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
測試的時候這一步測試沒有成功 但是可以鏈接
設置ssh認證
{{< notice warning "注意" >}}
ssh和tcp設置一種即可,其實就是設置無密碼登錄,要注意的是從webvirtmgr的什么用戶到宿主機的什么用戶的無密碼登錄,比如我用root跑的django webvirtmgr,而宿主機也是root跑的virsh,所以需要設置root到root的無密碼登錄。而git官網推薦的是用nginx用戶跑django webvirtmgr,webvirtmgr用戶跑的virsh,所以設置的是nginx用戶到宿主機webvirtmgr用戶的無密碼登錄。
參考: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/