6.12.1、通過Xmanager - Passive管理kvm虛擬機(首先要安裝xmanager):
1、安裝虛擬化管理軟件:
[root@centos7 ~]# yum install -y virt-manager openssh-askpass
#virt-manager:圖形化管理虛擬機工具;openssh-askpass:遠程連接KVM主機;
[root@centos7 ~]# yum groupinstall -y "Fonts"
[root@centos7 ~]# yum install -y dejavu-lgc-sans-fonts
#上面兩行是解決xmanager-passive亂碼的問題;
[root@centos7 ~]# yum install -y xorg-x11-font-utils xorg-x11-server-utils xorg-x11-utils xorg-x11-xauth xorg-x11-xinit
#安裝x11圖形界面工具;
[root@centos7 ~]# vim /etc/ssh/sshd_config
X11Forwarding yes
[root@centos7 ~]# systemctl restart sshd
#上面兩行是開啟ssh的X11轉發功能;
2、在xshell中設置:
3、運行xmanager-passive:
提示:運行后是不會顯示界面的,會在電腦任務欄的右下角出現一個"X"圖標;
4、kvm宿主機主動連接xmanager-passive:
[root@centos7 ~]# export DISPLAY=172.16.1.254:0.0
#設定連接的ip端口;
[root@centos7 ~]# virt-manager &
[1] 3369
#執行該命令后xmanager-passive會自動跳出;
[root@centos7 ~]# echo $DISPLAY
172.16.1.254:0.0
[1]+ 完成 virt-manager
說明:當關掉Xmanager - Passive后virt-manager進程也會結束掉;
5、管理虛擬主機:
(1)查看虛擬機連接配置:
1)
2)
3)
4)
(2)查看虛擬機的相關配置項:
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
(3)克隆:
1)
6.12.2、通過網頁方式管理kvm虛擬機:
1、安裝WebVirtMgr依賴包:
[root@centos7 ~]# yum -y install gcc python-devel git python-pip libvirt-python libxml2-python python-websockify supervisor nginx novnc
[root@centos7 ~]# pip install numpy
2、下載webvirtmgr包和安裝Python的Django環境:
(1)下載webvirtmgr包:
[root@centos7 ~]# git clone git://github.com/retspen/webvirtmgr.git
(2)安裝django環境:
[root@centos7 ~]# cd webvirtmgr/
[root@centos7 webvirtmgr]# pip install -r requirements.txt
(3)設置管理用戶信息:
[root@centos7 webvirtmgr]# ./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'): #回車
Email address: #回車
Password: 123456 #輸入密碼
Password (again): 123456 #再次輸入密碼
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
(4)生成配置文件:
[root@centos7 webvirtmgr]# ./manage.py collectstatic
(5)補充:添加其它管理用戶的方法:
[root@centos7 webvirtmgr]# ./manage.py createsuperuser
3、配置nginx:
(1)拷貝web包到nginx站點目錄並授權:
[root@centos7 webvirtmgr]# cd ..
[root@centos7 ~]# mv webvirtmgr /var/www/
[root@centos7 ~]# chown -R nginx:nginx /var/www/webvirtmgr
(2)配置nginx.conf:
主要配置的是server部分;
[root@centos7 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
server_name $hostname;
location /static/ {
root /var/www/webvirtmgr/webvirtmgr;
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;
}
}
}
(3)檢查nginx的配置文件:
[root@centos7 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
(4)啟動nginx並加入到開機自啟動:
[root@centos7 ~]# systemctl start nginx
[root@centos7 ~]# systemctl enable nginx
4、創建文件/etc/supervisord.d/webvirtmgr.ini如下內容:
[root@centos7 ~]# vim /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
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
5、啟動supervisord並加入開機自啟動:
[root@centos7 ~]# systemctl start supervisord.service
[root@centos7 ~]# systemctl enable supervisord.service
6、配置ssh認證:
(1)說明:
ssh和tcp設置一種即可,其實就是設置無密碼登錄,要注意的是從webvirtmgr的什么用戶到宿主機的什么用戶的無密碼登錄,
比如我用nginx用戶跑的django webvirtmgr,而宿主機是root跑的virsh,所以需要設置nginx到root的無密碼登錄,git官網推
薦的是用nginx用戶跑django webvirtmgr,webvirtmgr用戶跑的virsh,所以設置的是nginx用戶到宿主機webvirtmgr用戶的
無密碼登錄。
(2)發送認證公鑰:
[root@centos7 ~]# su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen
-bash-4.2$ touch ~/.ssh/config
-bash-4.2$ echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config
-bash-4.2$ ssh-copy-id root@localhost
#如果有其它的kvm宿主機,也需要將公鑰發送到其它的宿主機上;
7、在網頁端管理虛擬機:
1、登錄:
2、連接宿主機:
(1)
(2)
(3)
3、管理宿主機:
(1)添加存儲池(存放虛擬機的虛擬磁盤用):
(2)添加橋接網絡:
(3)虛擬機配置:
6.12.3、小結:
1、使用可視化工具連接kvm宿主機后能夠在管理工具上查看虛擬機的界面,主要用的是vnc,默認的vnc監聽是"0.0.0.0:0";
2、以上內容沒有寫如何在kvm可視化管理工具中如何安裝操作系統,因為安裝較為簡單,這里只寫出了一些重要參數的設置
項,在生產中建議使用命令安裝虛擬機,然后使用kvm可視化管理工具進行克隆即可;
3、使用manager-passvie管理kvm虛擬機需要在每台宿主機上安裝"virt-manager"等管理工具,且每次用時都需要在宿主機
上執行"virt-manager &"命令,非常的不方便;WebVirtMgr管理平台可以單獨裝一台服務器,然后通過ssh免密連接到其它
的kvm宿主機,實現對多台kvm宿主機的管理;
4、以上kvm可視化管理工具用於小規模的服務器kvm虛擬化管理,如果是大規模的kvm虛擬化管理,需要使用openstack;