背景
该工具用于管理多台 KVM 宿主机,为提供统一后台操作各节点上的 KVM 提供便利。
安装
安装依赖
yum环境
yum -y install epel-release
yum -y install libvirt-devel glibc gcc git iproute-tc cyrus-sasl-md5 libsasl2-dev libldap2-dev libssl-dev
- 需要自行准备 python3.7 的虚拟环境。
sqlite
- 该后台django版本要求安装大于 3.9.0 版本的sqlite,而 yum 安装目前最高只有 3.7.17 ,只好从官方下载自行编译使用。
wget https://www.sqlite.org/2021/sqlite-autoconf-3360000.tar.gz
tar zxvf sqlite-autoconf-3360000.tar.gz
cd sqlite-autoconf-3360000
./configure --prefix=/usr/local/sqlite-3.36.0
make && make install
下载webvirtcloud
groupadd webvirtcloud
useradd -g webvirtcloud webvirtcloud
mkdir -p /data1/py_virt_envs/webvirtcloud/ProjEnvs
chown -R webvirtcloud.webvirtcloud /data1/py_virt_envs/webvirtcloud
cd /data1/web_static
git clone https://github.com/retspen/webvirtcloud.git
chown -R webvirtcloud.webvirtcloud webvirtcloud
配置环境
因为本机需要存在多个sqlite版本,所以需要在用户下单独申明环境变量以达到该后台可以使用 3.36.0 版本的 sqlite
su - webvirtcloud
vim ~/.bashrc
# 声明 sqlite 版本
export PATH=/usr/local/sqlite-3.36.0/bin:$PATH
export LD_LIBRARY_PATH="/usr/local/sqlite-3.36.0/lib:/usr/local/lib"
# 声明 python 虚拟环境
export WORKON_HOME=/data1/py_virt_envs/webvirtcloud
export PROJECT_HOME=/data1/py_virt_envs/webvirtcloud/ProjEnvs
source /usr/bin/virtualenvwrapper.sh
导入环境变量
source ~/.bashrc
创建虚拟环境
mkvirtualenv webvirtcloud -p python3.7
创建配置文件
cd /data1/web_static/webvirtcloud
# 项目路径下还有一层webvirtcloud
cp webvirtcloud/settings.py.template webvirtcloud/settings.py
运行python,生成secret key
import random, string
haystack = string.ascii_letters + string.digits + string.punctuation
print(''.join([random.SystemRandom().choice(haystack) for _ in range(50)]))
将生成的key置入配置文件中
vim webvirtcloud/settings.py
# 修改SECRET_KEY
SECRET_KEY = "123ds#d@Q2!139#Q"
安装依赖
pip install -r conf/requirements.txt
初始化数据
python3 manage.py migrate
修改VNC端口 ( 用于存在前端代理时统一都走443端口访问 )vim console/templates/console-vnc-lite.html
- 指定成443
let port = readQueryVariable('port', '443');
配置supervisor
- 由于存在独立环境,需要单独声明 environment
vim /etc/supervisor/conf.d/webvirtcloud.ini
[program:webvirtcloud]
command=/data1/py_virt_envs/webvirtcloud/webvirtcloud/bin/gunicorn webvirtcloud.wsgi:application -c /data1/web_static/webvirtcloud/gunicorn.conf.py
directory=/data1/web_static/webvirtcloud
user=webvirtcloud
autostart=true
autorestart=true
redirect_stderr=true
logfile=/var/log/supervisor/webvirtcloud.log
log_stderr=true
stderr_logfile=/var/log/supervisor/webvirtcloud_error.log
stderr_logfile_maxbytes=20MB
stderr_logfile_backups=20
environment=PATH="/data1/py_virt_envs/webvirtcloud/webvirtcloud/bin:/usr/local/sqlite-3.36.0/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/var/lib/snapd/snap/bin:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre:/usr/local/jdk1.8/jre/bin:/usr/local/go-1.14/bin:/home/webvirtcloud/.local/bin:/home/webvirtcloud/bin",LD_LIBRARY_PATH="/usr/local/sqlite-3.36.0/lib:/usr/local/lib",WORKON_HOME="/data1/py_virt_envs/webvirtcloud",PROJECT_HOME="/data1/py_virt_envs/webvirtcloud/ProjEnvs"
[program:novncd]
command=/data1/py_virt_envs/webvirtcloud/webvirtcloud/bin/python /data1/web_static/webvirtcloud/console/novncd
directory=/data1/web_static/webvirtcloud
user=webvirtcloud
autostart=true
autorestart=true
redirect_stderr=true
logfile=/var/log/supervisor/webvirtcloud-novncd.log
log_stderr=true
stderr_logfile=/var/log/supervisor/webvirtcloud-novncd_error.log
stderr_logfile_maxbytes=20MB
stderr_logfile_backups=20
environment=PATH="/data1/py_virt_envs/webvirtcloud/webvirtcloud/bin:/usr/local/sqlite-3.36.0/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/var/lib/snapd/snap/bin:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre:/usr/local/jdk1.8/jre/bin:/usr/local/go-1.14/bin:/home/webvirtcloud/.local/bin:/home/webvirtcloud/bin",LD_LIBRARY_PATH="/usr/local/sqlite-3.36.0/lib:/usr/local/lib",WORKON_HOME="/data1/py_virt_envs/webvirtcloud",PROJECT_HOME="/data1/py_virt_envs/webvirtcloud/ProjEnvs"
配置Nginx
- 本次采用后台部署,本机 Nginx 暴露统一后端端口,再由前端 Nginx 反代后端服务( 因为存在静态资源 )
- 代理配置
upstream webvirtcloud-server { server 127.0.0.1:12345; } upstream webvirtcloud-wsnovncd { server 127.0.0.1:23456; } server { listen 11223; #server_name webvirtcloud.example.com; charset utf-8; access_log logs/webvirtcloud.access.main.log main; error_log logs/webvirtcloud.error.log error; location /static/ { root /data1/web_static/webvirtcloud; expires max; } location / { proxy_pass http://webvirtcloud-server; 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_set_header X-Forwarded-Ssl off; proxy_connect_timeout 1800; proxy_read_timeout 1800; proxy_send_timeout 1800; client_max_body_size 1024M; } location /novncd/ { proxy_pass http://webvirtcloud-wsnovncd; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
- 前端nginx
upstream webvirtcloud_prx { #ip_hash; server 192.168.7.55:11223; } server { listen 80; server_name abc.nestealin.com ; rewrite ^(.*)$ https://$host$1 permanent; } server { listen 443 ssl; listen 8443 ssl; server_name abc.nestealin.com; charset utf-8; access_log logs/abc.nestealin.com.access.main.log main; error_log logs/abc.nestealin.com.error.log error; #client_max_body_size 1000m; #keepalive_timeout 3600s; #客户端链接超时时间。为0的时候禁用长连接。 #keepalive_requests 10000; ssl_certificate /data/keys/server.cer; ssl_certificate_key /data/keys/server.key; ssl_session_timeout 5m; ssl_prefer_server_ciphers on; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; include firewall.conf; location /novncd/ { proxy_pass http://webvirtcloud_prx; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location / { #allow 14.28.151.69; include firewall.conf; proxy_pass http://webvirtcloud_prx; 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_set_header X-Forwarded-Ssl off; proxy_connect_timeout 1800; proxy_read_timeout 1800; proxy_send_timeout 1800; client_max_body_size 1024M; } }
使用及配置
连接配置
本次以 SSH 方式连接为例
在平台主机上向宿主机节点做免密登陆配置su - webvirtcloud # 创建公私钥,期间输入yes后直接回车,回车 ssh-keygen # 关闭公钥检查 cat > ~/.ssh/config << EOF Host * StrictHostKeyChecking no EOF chmod 600 ~/.ssh/config # 推送公钥 ssh-copy-id root@10.20.30.40 -p 12345
后台配置
浏览器访问域名 abc.nestealin.com 登陆
- 默认账号密码
login: admin
password: admin
添加节点
- 本次以 SSH 方式连接为例
在 “计算节点” 页面下选择 “SSH” 类型 添加远程主机信息 回到页面即可查看连接状态,也可点击进入 “实例” 页刷新
如果添加失败,一直处于”未连接”状态,可以到远程主机上查看message日志。
配置远程存储类型
- 本次为指定宿主机本地的 image 存放路径,所以选择 DIR 类型的存储池
后台使用
使用页面VNC
可能遇到的问题
查看实例信息时,出现如下提示
# 页面报错 libvirt 错误- internal error: failed to get number of host interfaces: unspecified error - errors in loading some config files # 对端宿主机 message libvirtd: 2021-09-05 01:49:53.905+0000: 9910: error : netcfConnectNumOfInterfacesImpl:351 : internal error: failed to get number of host interfaces: unspecified error - errors in loading some config files
原因
- 对端宿主机网卡配置存在空格配置,但未用双引号括起来导致报错。
解决方法
检查对端宿主机网卡配置,是否出现上述情况,例如
cd /etc/sysconfig/network-scripts vim ifcfg-bond0
BONDING_OPTS=mode=802.3ad miimon=100 xmit_hash_policy=layer3+4 >>> 修改成 BONDING_OPTS="mode=802.3ad miimon=100 xmit_hash_policy=layer3+4"
保存后重新回到页面刷新即可