跳转至

04-kvm界面管理

04-kvm界面管理

部署web界面管理kvm

部署web界面管理kvm http://www.it165.net/os/html/201604/16673.html 官方github https://github.com/retspen/webvirtmgr/wiki/Install-WebVirtMgr

KVM WEB管理工具webvirtmgr安装和使用

http://www.cnblogs.com/olinux/p/4511159.html

kvm管理平台webvirtmgr的部署

http://www.it165.net/os/html/201604/16673.html

软件部署

生产环境的KVM宿主机越来越多,需要对宿主机的状态进行调控。这里用webvirtmgr进行管理。图形化的WEB,让人能更方便的查看kvm 宿主机的情况和操作

*准备环境:开启防火墙与selinux*

1 安装支持的软件源

yum -y install http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

2 安装相关软件

yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx

3 从git-hub中下载相关的webvirtmgr代码

cd /usr/local/src/
git clone git://github.com/retspen/webvirtmgr.git

4 安装webvirtmgr

cd webvirtmgr/
pip install -r requirements.txt

5 安装数据库

yum install python-sqlite2

6 对django进行环境配置

./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: 2733176200@qq.com
Password:*********
Password (again):*********

生成配置文件及添加管理员账号

./manage.py collectstatic #生成配置文件
./manage.py createsuperuser #添加管理员账号

7 拷贝web到 相关目录

cd ..
mkdir -pv /var/www
cp -Rv webvirtmgr /var/www/webvirtmgr

8 设置ssh

ssh-keygen
ssh-copy-id 192.168.2.32
ssh 192.168.2.32 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:6080

9 编辑nginx配置文件

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 $remote_addr;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        client_max_body_size 1024M; # Set higher depending on your needs
    }
}

将nginx的默认配置文件更改名称

mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak

10 启动nginx

/etc/init.d/nginx restart

11 修改防火墙规则

/usr/sbin/setsebool httpd_can_network_connect true

12 设置 supervisor

chown -R nginx:nginx /var/www/webvirtmgr
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

修改/var/www/webvirtmgr/conf/gunicorn.conf.py

vim /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = "0:8000"

13 设置开机启动

chkconfig supervisord on
vim /etc/rc.local
/usr/sbin/setsebool httpd_can_network_connect true

14 启动进程

/etc/init.d/supervisord restart

15查看进程

netstat -lnpt           #即可以看到6080和8000已经启动

16 web访问

http://192.168.0.194/login/

配置宿主机环境

system: centos 6.6

libvirt: 0.10.2

1.下载并执行脚本:libvirt-bootstrap.sh

如果虚拟机比较多,该脚本执行时间会比较长,因为会执行service libvirt-guests restart,会将所有运行的虚拟机挂起然后再恢复

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

2.设置防火墙:

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

3.设置TCP认证Setup TCP authorization

参考:https://github.com/retspen/webvirtmgr/wiki/Setup-TCP-authorization,webvirtmgr新建服务器连接时需要此账号

$ sudo saslpasswd2 -a libvirt fred
Password: xxxxxx
Again (for verification): xxxxxx

查看新加的账号

sudo sasldblistusers2 -f /etc/libvirt/passwd.db
fred@webvirtmgr.net: userPassword

删除新加的账号:(可选)

sudo saslpasswd2 -a libvirt -d fred

验证新加的账号:

virsh -c qemu+tcp://IP_address/system nodeinfo
Please enter your authentication name: fred@webvirtmgr.net
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,如fred@webvirtmgr.net

4.设置ssh认证

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-Authorization

5.使用tcp认证连接服务器

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

img

使用TCP连接,label填写ip地址,FQDN/IP 填写ip地址,用户名填写第三步设置tcp认证所增加的账号,这里的用户名不用加@hostname,点击添加完成。如下图:

img

img

6.使用ssh认证连接服务器

访问访问:http://xxxx:8000,xxxx是webvirtmgr的ip地址,点击new connection,使用ssh认证涉及到一个问题,比如我用账号webvirtmgr登录了宿主机,但是使用命令virsh list的时候,并不能列出虚拟机信息,只有root用户使用virsh list时才能查看信息。于是我设置了nginx到宿主机root的ssh认证,但是在使用webvirtmgr网页登录时,发现并不能登录,需要手动输入,而且手动输入后发现,是可用的。

img

后来找到原因了,因为我用root跑的webvirtmgr,所以需要设置webvirtmgr服务器的root用户宿主机的root用户的无密码登录,设置后,可以使用。

img

7.虚拟机控制台使用

点击虚机实例,每台虚机最后的按钮”控制台”,点击后发现报错,如下图所示

img

img

qqO6PGJyIC8+CkVSUk9SOmRqYW5nby5yZXF1ZXN0OkludGVybmFsIFNlcnZlciBFcnJvcjogL2NvbnNvbGUvPGJyIC8+ClRyYWNlYmFjayAobW9zdCByZWNlbnQgY2FsbCBsYXN0KTo8YnIgLz4KRmlsZSAmbGRxdW87L3Vzci9saWIvcHl0aG9uMi42L3NpdGUtcGFja2FnZXMvZGphbmdvL2NvcmUvaGFuZGxlcnMvYmFzZS5weSZyZHF1bzssIGxpbmUgMTEzLCBpbiBnZXRfcmVzcG9uc2U8YnIgLz4KcmVzcG9uc2UgPSBjYWxsYmFjayhyZXF1ZXN0LCAqY2FsbGJhY2tfYXJncywgKipjYWxsYmFja19rd2FyZ3MpPGJyIC8+CkZpbGUgJmxkcXVvOy9kYXRhL2FwcHMvd2VidmlydG1nci9jb25zb2xlL3ZpZXdzLnB5JnJkcXVvOywgbGluZSA1OCwgaW4gY29uc29sZTxiciAvPgpyZXNwb25zZS5zZXRfY29va2llKCZsc3F1bzt0b2tlbiZyc3F1bzssIHRva2VuKTxiciAvPgpBdHRyaWJ1dGVFcnJvcjogJmxzcXVvO3N0ciZyc3F1bzsgb2JqZWN0IGhhcyBubyBhdHRyaWJ1dGUgJmxzcXVvO3NldF9jb29raWUmcnNxdW87PGJyIC8+CrLp1dLG5NS0tPrC66Ost6LP1r/JxNy1xLTtzvPKx9PJ09rQ6bv6w7vT0LCy17B2bmO1yM280M653MDtuaS+36Os09rKx9Ta0Om7+rXEeG1sxeTWw87EvP7M7bzTyOfPwsXk1sOjujxiciAvPgp2aXJzaCBlZGl0IG9zcy53ZWJ2aXJ0bWdyLjAwMTwvcD4KPHByZSBjbGFzcz0="brush:java;"> <graphics type='vnc' port='5910' autoport='no' listen='0.0.0.0'> <listen type='address' address='0.0.0.0'/> </graphics>

再重启虚机,如果还是不行,就先undefine,然后重新根据xml配置create试试。下载个VNC-Viewer,输入IP:5910,看能否访问,如下图所示:说明设置VNC是成功

img

但是从网页点击控制台按钮,发现还是不行,报错是server disconnected

img

qqO6aHR0cDovL2p1Lm91dG9mbWVtb3J5LmNuL2VudHJ5LzE0NTQ5NzxiciAvPgqwstewwctub3ZuY6OsyNTIu7GozazR+bXEtO2jrLDZtsi5yLjo0rLV0rK7tb3P4NOmtcTQxc+ioaN5dW0gLXkgaW5zdGFsbCBub3ZuY7rzo6zO3tLivOTU2rf+zvHG98nPt6LP1sHL1eK49sP8we5ub3ZuYy1zZXJ2ZXI8L3A+CjxwPjxpbWcgYWx0PQ=="这里写图片描述" src="http://www.it165.net/uploadfile/files/2016/0426/201604262001552882.png" title="" />
于是敲了如下命令:novnc_server –vnc IPADDR:5920,其中IPADDR为宿主机ip地址,5920是定义的虚拟机的vnc端口号。
[root@oss-webvirtmgr-001 ~]# novnc_server –vnc 192.168.200.230:5920
Warning: could not find self.pem
Starting webserver and WebSockets proxy on port 6080
WebSocket server settings:
- Listen on :6080
- Flash security policy server
- Web server. Web root: /usr/share/novnc
- No SSL/TLS support (no cert file)
- proxying from :6080 to 192.168.200.230:5920

Navigate to this URL:

http://oss-webvirtmgr-001:6080/vnc.html?host=oss-webvirtmgr-001&port=6080

Press Ctrl-C to exit

1: 10.1.196.211: Plain non-SSL (ws://) WebSocket connection

1: 10.1.196.211: Version hybi-13, base64: ‘False’

1: connecting to: 192.168.200.230:5920

结果居然ok了,console控制台起来了

img