目錄
- 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\n192.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\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
首先安裝必備的一些包
[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 不支持,這就需要手動下載安裝。
[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
再次開啟虛擬機,就可以為虛擬機安裝操作系統了。
完畢。