天天看點

[ 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使用率

記憶體使用率

網絡資源池管理

存儲資源池管理

虛拟機鏡像

虛拟機克隆

快照管理

日志管理

虛機遷移

虛拟機管理支援以下功能:

CD光牒管理

關/開/暫停虛拟機

安裝虛拟機

VNC console連接配接

建立快照

3. webvirtmgr 部署實踐

我們線上有也有在用 webvirtmgr,這裡在測試環境搭建做下記錄,環境如下:

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

首先,selinux 和 防火牆都是關閉狀态。

修改主機名:

[[email protected] ~]#hostnamectl set-hostname webvirtmgr.super.com
[[email protected] ~]#echo -e "192.168.118.14    webvirtmgr.super.com\n192.168.118.15    node1.super.com" >> /etc/hosts

[[email protected] ~]#hostnamectl set-hostname node1.super.com
[[email protected] ~]#echo -e "192.168.118.14    webvirtmgr.super.com\n192.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
      

首先安裝必備的一些包

[[email protected] ~]#yum install gcc python-devel openssl-devel -y
[[email protected] ~]#yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
      

配置國内 pip 源,預設國外 pip 太慢了。

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

安裝 numpy 子產品

[[email protected] ~]#pip install numpy
      
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

本地是 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

[[email protected] ~]#wget https://pypi.tuna.tsinghua.edu.cn/packages/c4/33/8ec8dcdb4ede5d453047bbdbd01916dbaccdb63e98bba60989718f5f0876/numpy-1.16.2-cp27-cp27mu-manylinux1_x86_64.whl#sha256=fb3c83554f39f48f3fa3123b9c24aecf681b1c289f9334f8215c1d3c8e2f6e5b

[[email protected] ~]#pip install numpy-1.16.2-cp27-cp27mu-manylinux1_x86_64.whl

測試是否安裝成功:
[[email protected] ~]#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 程式包

[[email protected] ~]#git clone git://github.com/retspen/webvirtmgr.git
      

安裝python程式依賴的庫檔案

[[email protected] ~/webvirtmgr]# pip install -r requirements.txt 
[[email protected] ~/webvirtmgr]#./manage.py syncdb
      
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

生成配置檔案

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

建立管理者使用者

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

接下來,将 webvirtmgr 項目整個做成web可以通路的站點

[[email protected] ~]#mkdir -pv /var/www
[[email protected] ~]#cp -a webvirtmgr/ /var/www/
[[email protected] ~]#chown -R nginx:nginx /var/www/
      

修改nginx 主配置檔案:

将預設的 server 段注釋掉

[[email protected] ~]#vim /etc/nginx/nginx.conf
      
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

新增配置檔案 webvirtmgr.conf

[[email protected] ~]#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 
    }
}
      

檢查文法:

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

啟動成功,通過 web 嘗試通路。

修改配置 supervisor 服務

[[email protected] ~]#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

啟動服務
[[email protected] ~]#systemctl start  supervisord
[[email protected] ~]#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/

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

 上面建立的兩個使用者都可以登入。

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

 登入成功。到此,webvirtmgr控制台安裝完畢。

3.2 kvm node節點配置

切換到 node1 節點(192.168.118.15)安裝部署

[[email protected] ~]#yum install libvirt qemu-kvm virt-viewer -y
      

 這裡有一步容易忽視,會造成後面虛拟機無法建立成功,需要注意下。

[[email protected] ~]#ln -vs /usr/libexec/qemu-kvm  /usr/bin/
‘/usr/bin/qemu-kvm’ -> ‘/usr/libexec/qemu-kvm’
      

不需要預設nat網絡模式,移除

[[email protected] ~]#mv /etc/libvirt/qemu/networks/default.xml /tmp/
[[email protected] ~]#systemctl start libvirtd
[[email protected] ~]#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主機執行的。

[[email protected] ~]#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 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|          . .    |
|         . + o   |
|        + = +    |
|       o O +     |
|        S B o    |
|       o * + .   |
|        . + o    |
|           +     |
|            E    |
+-----------------+

# 将密鑰拷貝到 KVM主機      
ssh-copy-id [email protected]
注意:如果這裡是非22端口則采用如下指令:
ssh-copy-id '-p [ssh端口] [email protected]'      

可以在 webvirtmgr 安裝 libvirt-client 進行測試:

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

通路成功,接下來就可以在 web 頁面進行操作了。

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

這裡是直接使用 root使用者,建議使用普通使用者,但是要注意權限,點選添加

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

 點進來如果沒有任何報錯,那麼基本上已經添加主機成功了。

1. 建立網橋

在建立網橋這一步要非常小心,因為稍微不注意網卡起不來就有可能直接無法連接配接伺服器。

關閉 NetworkManager 服務

[[email protected] ~]#systemctl stop  NetworkManager; systemctl disable NetworkManager
      
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

點選建立。

這裡發現使用 web 頁面建立網橋沒有成功。

[[email protected] ~]#brctl show
bridge name        bridge id                STP enabled        interfaces
      

如果沒有建立成功,則直接通過指令行建立:

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

建立成功,切換到web頁面重新整理

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

建立的 br0 網橋已經出現。

2. 建立網絡池

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

3. 建立存儲池

一般可以建立兩個存儲池:

  (1) 用來存放虛拟磁盤檔案(本地)

  (2) 用來存放 iso 檔案 (nfs)

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

建立 iso 存儲池需要在 webvirtmgr 節點安裝 nfs 配置

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

在 kvm 主機上測試

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

ok,nfs是可以通路到的。切換到 web 界面進行配置

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

兩個存儲池建立成功,接下來就可以嘗試建立虛拟機了。

4. 建立虛拟機

(1)建立虛拟磁盤檔案

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

(2)建立虛拟機

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用
[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

 建立完成後,啟動虛拟機

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

可以直接通過控制台檢視虛拟機。虛拟機建立完成。

現在拷貝 Centos 7 鏡像到 存儲池的 iso 目錄下。

[[email protected] ~]#scp 192.168.118.8:/usr/local/src/CentOS-7-x86_64-Minimal-1511.iso /iso/
      

 切換到web頁面檢視存儲池。

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

關閉目前虛拟機,為虛拟機添加 iso CDROM

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

再次開啟虛拟機,就可以為虛拟機安裝作業系統了。

[ kvm ] 學習筆記 9:WebVirtMgr 基礎及安裝使用

完畢。