目录
密码保护:Kvm管理之WebVirtMgr
1. 前言
当Kvm宿主机越来越多,需要对宿主机的状态进行调控,决定采用WebVirtMgr作为Kvm虚拟化的web管理工具,图形化的WEB,让人能更方便的查看Kvm宿主机的情况和操作。
WebVirtMgr是近两年来发展较快,比较活跃,非常清新的一个Kvm管理平台,提供对宿主机和虚机的统一管理,它有别于Kvm自带的图形管理工具(virtual machine manager),让Kvm管理变得更为可视化,对中小型Kvm应用场景带来了更多方便。
WebVirtMgr采用几乎纯Python开发,其前端是基于Python的Django,后端是基于Libvirt的Python接口,将日常Kvm的管理操作变的更加的可视化。
2. 特点
操作简单,易于使用通过libvirt的API接口对Kvm进行管理提供对虚拟机生命周期管理
3. 功能
宿主机管理支持以下功能CPU利用率内存利用率网络资源池管理存储资源池管理虚拟机镜像虚拟机克隆快照管理日志管理虚机迁移
虚拟机管理支持以下功能
CPU利用率
内存利用率
光盘管理
关/开/暂停虚拟机
安装虚拟机VNC
console连接创建快照
4. 部署
1)、安装相关依赖
查看官网:https://github.com/retspen/webvirtmgr/wiki/Install-WebVirtMgr
首先要安装Kvm虚拟化环境,这里我将WebVirtMgr服务器和Kvm服务器放在同一台机器上部署的,即单机部署。
#1.安装epel源
[root@kvm02 ~]# yum install epel-release
#2.安装依赖及环境
[root@kvm02 ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
#3.安装编译软件
[root@kvm02 ~]# yum -y install gcc python-devel
#4.安装numpy(Python进行科学计算的基础软件包)
[root@kvm02 webvirtmgr]# pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
[root@kvm02 ~]# pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
2)、安装Python需求环境
#1.从git-hub中下载相关的webvirtmgr代码
[root@kvm02 ~]# cd /usr/local/src/
[root@kvm02 /usr/local/src]# git clone git://github.com/retspen/webvirtmgr.git
#2.安装WebVirtMgr
[root@kvm02 /usr/local/src]# cd webvirtmgr/
[root@kvm02 /usr/local/src/webvirtmgr]# pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
#3.检查sqlite3 (备注:自带不需要安装,导入模块检查一下。)
[root@kvm02 /usr/local/src/webvirtmgr]# python
Python 2.7.5 (default, Aug 7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> exit()
#4.初始化账号
[root@kvm02 /usr/local/src/webvirtmgr]# chmod +x manage.py
[root@kvm02 /usr/local/src/webvirtmgr]# ./manage.py syncdb
#5.生成配置文件
[root@kvm02 /usr/local/src/webvirtmgr]# ./manage.py collectstatic
#6.添加管理员账号,这个是管理员账号,用上面的admin和这个账号都可以登录
[root@kvm02 /usr/local/src/webvirtmgr]# ./manage.py createsuperuser
WARNING:root:No local_settings file found.
Username (leave blank to use 'root'): ops
Email address: 1176494252@qq.com
Password:
Password (again):
Superuser created successfully.
#7.拷贝web到相关目录
[root@kvm02 /usr/local/src/webvirtmgr]# mkdir -pv /var/www
mkdir: created directory '/var/www'
[root@kvm02 /usr/local/src/webvirtmgr]# cp -rp /usr/local/src/webvirtmgr /var/www/webvirtmgr
#8.设置ssh
[root@kvm02 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:CAIi5mok4liJ/kdhHu45iknOPhfla3rG+NLyNVSPwCk root@kvm02
The key's randomart image is:
+---[RSA 2048]----+
|+. |
|=o . . . |
|+o+ .E + . |
|O. .+o+.o o |
|o+ o+..S. . |
|. ..o.o |
| . .==.o |
|+.o=oBo . |
|.*ooXo |
+----[SHA256]-----+
#传密钥给当前主机
[root@kvm02 ~]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.162
#设置加密连接
[root@kvm02 ~]# ssh 10.0.0.162 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
3)、配置Nginx
#1.打开/etc/nginx/nginx.conf文件,注释server段
#2.编辑配置文件
[root@kvm02 ~]# cat /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服务
[root@kvm02 ~]# systemctl restart nginx
#4.设置权限
[root@kvm02 ~]# chown -R nginx:nginx /var/www/webvirtmgr
#5.创建/etc/supervisord.d/webvirtmgr.ini具有以下内容的文件
[root@kvm02 ~]# cat /etc/supervisord.d/webvirtmgr.ini
[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
stdout_logfile=/var/log/supervisor/webvirtmgr.log
redirect_stderr=true
user=nginx
[program:webvirtmgri-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
#6.启动守护进程supervisord
[root@kvm02 ~]# systemctl start supervisord.service
[root@kvm02 ~]# systemctl enable supervisord.service
#7.查看端口
[root@kvm02 ~]# netstat -lntp |grep -E '6080|8000'
tcp 0 0 127.0.0.1:6080 0.0.0.0:* LISTEN 14866/ssh
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 14866/ssh
tcp6 0 0 ::1:6080 :::* LISTEN 14866/ssh
tcp6 0 0 ::1:8000 :::* LISTEN 14866/ssh
8.浏览器IP地址访问http://10.0.0.182/login/
只有超级管理员登陆后才能看到"基础构架"窗口![img]
4)、 远程连接
#使用tcp进行对远程libvirtd进行连接访问的配置如下:
#1.修改文件/etc/sysconfig/libvirtd,用来启用tcp的端口(修改内容如下过滤出来的所示)
[root@kvm02 ~]# grep -Ev "^$|#" /etc/sysconfig/libvirtd
LIBVIRTD_CONFIG=/etc/libvirt/libvirtd.conf
LIBVIRTD_ARGS="--listen"
#2.修改文件/etc/libvirt/libvirtd.conf,(修改过滤出来的内容)
[root@kvm02 ~]# grep -Ev "^$|#" /etc/libvirt/libvirtd.conf
listen_tls = 0
listen_tcp = 1
tcp_port = "16509"
listen_addr = "0.0.0.0"
auth_tcp = "none"
#3.重启libvirtd
[root@kvm02 ~]# systemctl restart libvirtd.service
#4.查看端口16509是否启动
[root@kvm02 ~]# netstat -lntp|grep 16509
tcp 0 0 0.0.0.0:16509 0.0.0.0:* LISTEN 15344/libvirtd
#5.在source host连接dest host远程libvirtd查看信息
[root@kvm02 ~]# virsh -c qemu+tcp://10.0.0.182/system
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
[root@kvm02 ~]# echo " * - nofile 65534" >> /etc/security/limits.conf
virsh # #表示成功
5)、更新
[root@kvm02 conf.d]# cd /var/www/webvirtmgr
[root@kvm02 webvirtmgr]# git pull
[root@kvm02 webvirtmgr]# ./manage.py collectstatic
[root@kvm02 webvirtmgr]# service supervisord restart