zoukankan      html  css  js  c++  java
  • [ kvm ] 学习笔记 9:WebVirtMgr 基础及安装使用

     目录
    - 1. 前言
    - 2. webvirtmgr 简介
    - 3. webvirtmgr 部署实践
        - 3.1 配置 webvirtmgr 主机
        - 3.2 kvm node节点配置
        - 3.3 控制台和kvm节点交互配置

    1. 前言

    上一篇:[kvm] Ovirt基础及安装使用 介绍了 ovirt 的安装和基础使用,本篇则介绍更加轻量级的 webvirtmgr,相比较 ovirt,webvirtmgr则显得更加轻量化和简单易用。

    ovirt 比较适合较大的集群规模,比如虚拟机上千的集群,而对于虚拟机10-200 之间来说,采用 webvirtmgr 将更加便捷。

    2. webvirtmgr 简介

    webvirtmgr 特点

    操作简单,易于使用
    通过libvirt的API接口对kvm进行管理
    提供对虚拟机生命周期管理

    WebVirtMgr 功能

    宿主机管理支持以下功能:

    CPU利用率
    内存利用率
    网络资源池管理
    存储资源池管理
    虚拟机镜像
    虚拟机克隆
    快照管理
    日志管理
    虚机迁移

    虚拟机管理支持以下功能:

    CPU利用率
    内存利用率
    光盘管理
    关/开/暂停虚拟机
    安装虚拟机
    VNC console连接
    创建快照

    3. webvirtmgr 部署实践

    我们线上有也有在用 webvirtmgr,这里在测试环境搭建做下记录,环境如下:

    首先,selinux 和 防火墙都是关闭状态。

    修改主机名:

    [root@192.168.118.14 ~]#hostnamectl set-hostname webvirtmgr.super.com
    [root@192.168.118.14 ~]#echo -e "192.168.118.14    webvirtmgr.super.com
    192.168.118.15    node1.super.com" >> /etc/hosts
    
    [root@192.168.118.15 ~]#hostnamectl set-hostname node1.super.com
    [root@192.168.118.15 ~]#echo -e "192.168.118.14    webvirtmgr.super.com
    192.168.118.15    node1.super.com" >> /etc/hosts
    

     关闭 NetworkManager 服务,否则后面通过 virsh 创建网桥设备时,会创建失败。

    #systemctl stop  NetworkManager ; systemctl disable NetworkManager
    

    3.1 配置 webvirtmgr 主机

    启用epel的源,我一般是使用阿里云的centos源及epel源,见`http://mirrors.aliyun.com/help/centos`, `http://mirrors.aliyun.com/help/epel`

    #wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    #wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    

    首先安装必备的一些包

    [root@192.168.118.14 ~]#yum install gcc python-devel openssl-devel -y
    [root@192.168.118.14 ~]#yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
    

    配置国内 pip 源,默认国外 pip 太慢了。

    [root@192.168.118.14 ~]#mkdir -pv ~/.pip/
    mkdir: created directory ‘/root/.pip/’
    [root@192.168.118.14 ~]#cat ~/.pip/pip.conf
    [global]
    timeout = 6000
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    trusted-host = pypi.tuna.tsinghua.edu.cn
    

     
    安装 numpy 模块

    [root@192.168.118.14 ~]#pip install numpy
    

    本地是 python 2.7 最新版本的 numpy 不支持,这就需要手动下载安装。

    下载链接:https://pypi.tuna.tsinghua.edu.cn/packages/c4/33/8ec8dcdb4ede5d453047bbdbd01916dbaccdb63e98bba60989718f5f0876/numpy-1.16.2-cp27-cp27mu-manylinux1_x86_64.whl#sha256=fb3c83554f39f48f3fa3123b9c24aecf681b1c289f9334f8215c1d3c8e2f6e5b

    [root@192.168.118.14 ~]#wget https://pypi.tuna.tsinghua.edu.cn/packages/c4/33/8ec8dcdb4ede5d453047bbdbd01916dbaccdb63e98bba60989718f5f0876/numpy-1.16.2-cp27-cp27mu-manylinux1_x86_64.whl#sha256=fb3c83554f39f48f3fa3123b9c24aecf681b1c289f9334f8215c1d3c8e2f6e5b
    
    [root@192.168.118.14 ~]#pip install numpy-1.16.2-cp27-cp27mu-manylinux1_x86_64.whl
    
    测试是否安装成功:
    [root@192.168.118.14 ~]#python 
    Python 2.7.5 (default, Jun 20 2019, 20:27:34) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy
    

     没有报错,说明安装成功。

    下载  webvirtmgr 程序包

    [root@192.168.118.14 ~]#git clone git://github.com/retspen/webvirtmgr.git
    

    安装python程序依赖的库文件

    [root@192.168.118.14 ~/webvirtmgr]# pip install -r requirements.txt 
    [root@192.168.118.14 ~/webvirtmgr]#./manage.py syncdb
    

     

    生成配置文件

    创建管理员用户

    接下来,将 webvirtmgr 项目整个做成web可以访问的站点

    [root@192.168.118.14 ~]#mkdir -pv /var/www
    [root@192.168.118.14 ~]#cp -a webvirtmgr/ /var/www/
    [root@192.168.118.14 ~]#chown -R nginx:nginx /var/www/
    

    修改nginx 主配置文件:
    将默认的 server 段注释掉

    [root@192.168.118.14 ~]#vim /etc/nginx/nginx.conf
    

    新增配置文件 webvirtmgr.conf

    [root@192.168.118.14 ~]#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 
        }
    }
    

    检查语法:

    [root@192.168.118.14 ~]#nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@192.168.118.14 ~]#nginx 
    [root@192.168.118.14 ~]#netstat -ntplu | egrep nginx
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17710/nginx: master
    

    启动成功,通过 web 尝试访问。

    修改配置 supervisor 服务

    [root@192.168.118.14 ~]#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
    
    启动服务
    [root@192.168.118.14 ~]#systemctl start  supervisord
    [root@192.168.118.14 ~]#systemctl status   supervisord
    ● supervisord.service - Process Monitoring and Control Daemon
       Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
       Active: active (running) since Tue 2019-08-13 09:51:25 CST; 1s ago
      Process: 17741 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
     Main PID: 17744 (supervisord)
       CGroup: /system.slice/supervisord.service
               ├─17744 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
               ├─17745 /usr/bin/python /var/www/webvirtmgr/console/webvirtmgr-console
               ├─17746 /usr/bin/python /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
               ├─17755 /usr/bin/python /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
               ├─17756 /usr/bin/python /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
               ├─17757 /usr/bin/python /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
               ├─17758 /usr/bin/python /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
               ├─17759 /usr/bin/python /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
               └─17760 /usr/bin/python /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
    
    Aug 13 09:51:25 webvirtmgr.super.com systemd[1]: Starting Process Monitoring and Control Daemon...
    Aug 13 09:51:25 webvirtmgr.super.com systemd[1]: Started Process Monitoring and Control Daemon.
    

    浏览器尝试访问webvirtmgr  地址:http://192.168.118.14/login/

     上面创建的两个用户都可以登录。

     登录成功。到此,webvirtmgr控制台安装完毕。

    3.2 kvm node节点配置

    切换到 node1 节点(192.168.118.15)安装部署

    [root@192.168.118.15 ~]#yum install libvirt qemu-kvm virt-viewer -y
    

     这里有一步容易忽视,会造成后面虚拟机无法创建成功,需要注意下。

    [root@192.168.118.15 ~]#ln -vs /usr/libexec/qemu-kvm  /usr/bin/
    ‘/usr/bin/qemu-kvm’ -> ‘/usr/libexec/qemu-kvm’
    

    不需要默认nat网络模式,移除

    [root@192.168.118.15 ~]#mv /etc/libvirt/qemu/networks/default.xml /tmp/
    [root@192.168.118.15 ~]#systemctl start libvirtd
    [root@192.168.118.15 ~]#systemctl status  libvirtd
    ● libvirtd.service - Virtualization daemon
       Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
       Active: active (running) since Tue 2019-08-13 09:59:25 CST; 1s ago
         Docs: man:libvirtd(8)
               http://libvirt.org
     Main PID: 20741 (libvirtd)
       CGroup: /system.slice/libvirtd.service
               └─20741 /usr/sbin/libvirtd
    
    Aug 13 09:59:25 node1.super.com systemd[1]: Starting Virtualization daemon...
    Aug 13 09:59:25 node1.super.com systemd[1]: Started Virtualization daemon.
    

    3.3 控制台和kvm节点交互配置

    到此,kvm 主机算配置完成,接下来需要配置控制台到kvm主机交互的环节

    webvirtmgr 主机

    切换到 nginx 用户,生成密钥对,然后将公钥拷贝到 kvm主机节点,实现免密码登录,因为命令是通过nginx用户发送到kvm主机执行的。

    [root@192.168.118.14 ~]#su - nginx -s /bin/bash
    -bash-4.2$ ssh-keygen -t rsa -P ''
    Generating public/private rsa key pair.
    
    Enter file in which to save the key (/var/lib/nginx/.ssh/id_rsa): Created directory '/var/lib/nginx/.ssh'.
    Your identification has been saved in /var/lib/nginx/.ssh/id_rsa.
    Your public key has been saved in /var/lib/nginx/.ssh/id_rsa.pub.
    The key fingerprint is:
    b4:0d:f6:0e:2d:5e:94:2b:56:97:b4:e0:7b:e3:26:f9 nginx@webvirtmgr.super.com
    The key's randomart image is:
    +--[ RSA 2048]----+
    |          . .    |
    |         . + o   |
    |        + = +    |
    |       o O +     |
    |        S B o    |
    |       o * + .   |
    |        . + o    |
    |           +     |
    |            E    |
    +-----------------+

    # 将密钥拷贝到 KVM主机
    ssh-copy-id root@192.168.118.15
    注意:如果这里是非22端口则采用如下命令:
    ssh-copy-id '-p [ssh端口] root@192.168.118.15'

    可以在 webvirtmgr 安装 libvirt-client 进行测试:

    [root@192.168.118.14 ~]#yum install  libvirt-client -y 
    -bash-4.2$ virsh --connect qemu+ssh://root@192.168.118.15/system list --all
     Id    Name                           State
    ----------------------------------------------------
    

    访问成功,接下来就可以在 web 页面进行操作了。

    这里是直接使用 root用户,建议使用普通用户,但是要注意权限,点击添加

     点进来如果没有任何报错,那么基本上已经添加主机成功了。

    1. 创建网桥

    在创建网桥这一步要非常小心,因为稍微不注意网卡起不来就有可能直接无法连接服务器。

    关闭 NetworkManager 服务

    [root@192.168.118.15 ~]#systemctl stop  NetworkManager; systemctl disable NetworkManager
    

    点击创建。

    这里发现使用 web 页面创建网桥没有成功。

    [root@192.168.118.15 ~]#brctl show
    bridge name        bridge id                STP enabled        interfaces
    

    如果没有创建成功,则直接通过命令行创建:

    [root@192.168.118.15 ~]#virsh iface-bridge eth0 br0
    Created bridge br0 with attached device eth0
    Bridge interface br0 started
    [root@192.168.118.15 ~]#brctl show
    bridge name        bridge id                STP enabled        interfaces
    br0                8000.5254006205ad        yes                eth0
    

    创建成功,切换到web页面刷新

    创建的 br0 网桥已经出现。

    2. 创建网络池

    3. 创建存储池

    一般可以创建两个存储池:

      (1) 用来存放虚拟磁盘文件(本地)

      (2) 用来存放 iso 文件 (nfs)

    创建 iso 存储池需要在 webvirtmgr 节点安装 nfs 配置

    [root@192.168.118.14 ~]#yum install nfs-utils -y
    [root@192.168.118.14 ~]#cat /etc/exports
    /iso        *(rw,sync)
    [root@192.168.118.14 ~]#systemctl start rpcbind
    [root@192.168.118.14 ~]#systemctl start nfs
    

    在 kvm 主机上测试

    [root@192.168.118.15 ~]#showmount -e 192.168.118.14
    Export list for 192.168.118.14:
    /iso *
    [root@192.168.118.15 ~]#mkdir -pv /iso
    mkdir: created directory ‘/iso’
    

    ok,nfs是可以访问到的。切换到 web 界面进行配置

    两个存储池创建成功,接下来就可以尝试创建虚拟机了。

    4. 创建虚拟机

    (1)创建虚拟磁盘文件

    (2)创建虚拟机

     创建完成后,启动虚拟机

    可以直接通过控制台查看虚拟机。虚拟机创建完成。

    现在拷贝 Centos 7 镜像到 存储池的 iso 目录下。

    [root@192.168.118.14 ~]#scp 192.168.118.8:/usr/local/src/CentOS-7-x86_64-Minimal-1511.iso /iso/
    

     切换到web页面查看存储池。

    关闭当前虚拟机,为虚拟机添加 iso CDROM

    再次开启虚拟机,就可以为虚拟机安装操作系统了。

    完毕。

  • 相关阅读:
    C++ 注意事项
    Google protocol buffer的配置和使用(Linux&&Windows)
    网络编程的一些错误总结
    C++单例模式设计与实现
    TCP粘包处理
    C++ 局部静态变量,全局变量,全局静态变量,局部变量的区别和联系
    C++模板类注意事项
    Django框架(二十二)—— Django rest_framework-频率组件
    Django框架(二十一)—— Django rest_framework-权限组件
    Django框架(二十)—— Django rest_framework-认证组件
  • 原文地址:https://www.cnblogs.com/hukey/p/11344676.html
Copyright © 2011-2022 走看看