準備服務器:
192.168.1.X 安裝路徑:
1、程序:路徑/var/www
數據庫:路徑/usr/local/application
虛擬機存放:路徑/data
一、首先要安裝KVM虛擬化環境
1、安裝虛擬機:
1)修改內核模式爲兼容內核啓動
[root@ops ~]# uname -a
Linux openstack 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@ops ~]#vim /boot/grub/grub.conf
......
default=1 #由默認的0改爲1,內核兼容模式
2)關閉selinux,重啓後生效
[root@ops ~]#vim /etc/sysconfig/selinux
......
SELINUX=disabled
[root@ops ~]#setenforce 0
setenforce: SELinux is disabled
[root@ops ~]#getenforce
Disabled
3)關閉防火牆
[root@ops ~]#chkconfig ip6tables off
[root@ops ~]#chkconfig iptables off
4)重啓
[root@ops ~]#reboot
虛擬化環境配置
1)查看是否支持虛擬機
說明1:半虛擬化是不能運行與安裝KVM虛擬機的。
[root@ops ~]#egrep '(vmx|svm)' --color=always /proc/cpuinfo
2)配置yum環境,確認yum源是好使的。(這一步驟省略,可以用默認yum源,也可以換用163或阿里的yum源,只要能正常使用就行)
3)安裝kvm軟件包
[root@ops ~]#yum -y install kvm python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v
補充安裝kvm虛擬化一些管理工具包
[root@ops ~]#yum -y install libguestfs-tools
---------------------------------------如果是centos7,操作如下---------------------------------------
yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install virt-manager python-virtinst libvirt-client virt-viewer
systemctl enable libvirtd && systemctl start libvirtd
-----------------------------------------------------------------------------------------------------
查看虛擬化環境
1)查看虛擬機環境
[root@ops ~]# /etc/init.d/libvirtd restart
Stopping libvirtd daemon: [ OK ]
Starting libvirtd daemon: [ OK ]
----------------------------------------------------------------------------------------------------------------------------------------------
啓動的時候報錯如下:
Starting libvirtd daemon: libvirtd: relocation error: libvirtd: symbol dm_task_get_info_with_deferred_remove, version Base not defined in file libdevmapper.so.1.02 with link time reference [FAILED]
解決辦法:
yum upgrade device-mapper-libs
----------------------------------------------------------------------------------------------------------------------------------------------
[root@ops ~]# virsh -c qemu:///system list
Id Name State
HWADDR=44:A8:42:21:A6:01
TYPE=Bridge
UUID=6dbfbd49-b6b9-4c51-b2d2-b3ecbead82e9
ONBOOT=yes
IPADDR=192.168.1.17
GATEWAY=192.168.1.1
NM_CONTROLLED=no
BOOTPROTO=static
2)查看kvm模塊支持
[root@ops ~]# lsmod |grep kvm
kvm_intel 54285 0
kvm 333172 1 kvm_intel
3)查看虛擬工具版本
[root@ops ~]# virsh --version
0.10.2
[root@ops ~]# virt-install --version
0.600.0
[root@ops ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
手動配置虛擬網橋
1)關閉networkmanager服務
[root@ops ~]# chkconfig NetWorkManager off
[root@ops ~]# /etc/init.d/NetWorkManager stop
2)創建br0網橋
[root@ops ~]#cd /etc/sysconfig/network-scripts/
[root@ops ~]#cp ifcfg-enp2s0f0 ifcfg-br0
[root@ops ~]# cat ifcfg-enp2s0f0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp2s0f0
UUID=32bd8cb0-4705-4685-9c69-8c5725d346cc
DEVICE=enp2s0f0
ONBOOT=yes
IPADDR=192.168.1.20
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
DNS2=222.246.129.80
NM_CONTROLLED=no
BRIDGE=br0
[root@ops ~]# cat ifcfg-br0
TYPE=Bridge
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp2s0f0
UUID=32bd8cb0-4705-4685-9c69-8c5725d346cc
DEVICE=enp2s0f0
ONBOOT=yes
IPADDR=192.168.1.20
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=114.114.114.114
DNS2=222.246.129.80
NM_CONTROLLED=no
DEVICE=br0
3)關閉了networkmanager服務之後,才能通過重啓網卡服務管理網絡。
[root@ops ~]# service network restart
4)查看網橋br0信息
[root@ops ~]# ifconfig
5)查看網橋
[root@ops ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.44a84221a601 no em2
virbr0 8000.5254005f95b7 yes virbr0-nic
到此kvm虛擬化環境安裝完畢。
一、Kvm的管理工具webvirtmgr安裝和使用
WebVirtMgr採用幾乎純Python開發,其前端是基於Python的Django,後端是基於Libvirt的Python接口,將日常kvm的管理操作變的更加的可視化。
WebVirtMgr特點:
操作簡單,易於使用
通過libvirt的API接口對kvm進行管理
提供對虛擬機生命週期管理
WebVirtMgr 功能
宿主機管理支持以下功能
CPU利用率
內存利用率
網絡資源池管理
存儲資源池管理
虛擬機鏡像
虛擬機克隆
快照管理
日誌管理
虛機遷移
虛擬機管理支持以下功能
CPU利用率
內存利用率
光盤管理
關/開/暫停虛擬機
安裝虛擬機
VNC console連接
創建快照
系統環境:
CentOS Linux release 7.2.1511 (Core)
1、yum源的配置及安裝所需軟件包
yum -y install http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
yum -y install Git Python-pip libvirt-python libxml2-python python-websockify supervisor nginx
yum -y install gcc python-devel
pip install numpy
2、下載webvirtmgr.git軟件
mkdir /application/
cd /application/
git clone git://github.com/retspen/webvirtmgr.git
git clone https://github.com/retspen/webvirtmgr.git
3、數據庫安裝
cd /application/
wget http://www.sqlite.org/sqlite-3.5.6.tar.gz
cd sqlite-3.5.6/
./configure --disable-tcl
make
make install
4、webvirtmgr安裝
cd /application/webvirtmgr
pip install -r requirements.txt
-----------------------------------------------------------------------------------------------------
./manage.py syncdb
WARNING:root:No local_settings file found.
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table servers_compute
Creating table instance_instance
Creating table create_flavor
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: [email protected]
Password: 123456
Password (again): 123456
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
--------------------------------------------------------------------------------------------------------
./manage.py collectstatic
WARNING:root:No local_settings file found.
You have requested to collect static files at the destination
location as specified in your settings.
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: yes
Copying '/application/webvirtmgr/webvirtmgr/static/css/bootstrap-multiselect.css'
Copying '/application/webvirtmgr/webvirtmgr/static/css/bootstrap.min.css'
Copying '/application/webvirtmgr/webvirtmgr/static/css/signin.css'
Copying '/application/webvirtmgr/webvirtmgr/static/css/table-sort.css'
Copying '/application/webvirtmgr/webvirtmgr/static/css/webvirtmgr.css'
Copying '/application/webvirtmgr/webvirtmgr/static/fonts/glyphicons-halflings-regular.eot'
Copying '/application/webvirtmgr/webvirtmgr/static/fonts/glyphicons-halflings-regular.svg'
Copying '/application/webvirtmgr/webvirtmgr/static/fonts/glyphicons-halflings-regular.ttf'
Copying '/application/webvirtmgr/webvirtmgr/static/fonts/glyphicons-halflings-regular.woff'
Copying '/application/webvirtmgr/webvirtmgr/static/img/asc.gif'
Copying '/application/webvirtmgr/webvirtmgr/static/img/bg.gif'
Copying '/application/webvirtmgr/webvirtmgr/static/img/desc.gif'
Copying '/application/webvirtmgr/webvirtmgr/static/img/favicon.ico'
Copying '/application/webvirtmgr/webvirtmgr/static/js/Chart.min.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/bootstrap-multiselect.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/bootstrap.min.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/infrastructure.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/jQuery-1.10.2.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/jquery-migrate-1.2.1.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/jquery.tablesorter.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/Orbitron700.ttf'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/Orbitron700.woff'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/base.css'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/base64.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/black.css'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/blue.css'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/des.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/display.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/input.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/jsunzip.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/logo.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/playback.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/rfb.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/ui.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/util.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/websock.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/webutil.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/chrome-app/tcp-client.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/web-socket-js/README.txt'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/web-socket-js/WebSocketMain.swf'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/web-socket-js/swfobject.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/novnc/web-socket-js/web_socket.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-HTML5/atKeynames.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-Html5/bitmap.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/cursor.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/display.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/enums.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/filexfer.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/inputs.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/jsbn.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/lz.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/main.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/playback.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/png.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/prng4.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/quic.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/resize.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/rng.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/rsa.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/sha1.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/simulatecursor.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/spicearraybuffer.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/spiceconn.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/spicedataview.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/spicemsg.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/spicetype.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/ticket.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/utils.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/webm.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/wire.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/thirdparty/jsbn.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/thirdparty/prng4.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/thirdparty/rng.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/thirdparty/rsa.js'
Copying '/application/webvirtmgr/webvirtmgr/static/js/spice-html5/thirdparty/sha1.js'
75 static files copied.
-------------------------------------------------------------------------------------------------------------
./manage.py createsuperuser
WARNING:root:No local_settings file found.
Username (leave blank to use 'root'): mgruser
Email address: [email protected]
Password: 123456
Password (again): 123456
Superuser created successfully.
---------------------------------------------------------------------------------------------------------------
5、webvirtmgr配置
mkdir -pv /var/www
cp -Rv /application/webvirtmgr /var/www/webvirtmgr
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
}
}
chown -R nginx:nginx /var/www/webvirtmgr
vim /etc/supervisord.conf
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py #啓動8000端口
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/python2 /var/www/webvirtmgr/console/webvirtmgr-console #啓動6080端口(這是控制檯vnc端口)
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=nginx
確保下面bind綁定的是本機的8000端口,這個在nginx配置中定義了,被代理的端口
grep '^bind =' /var/www/webvirtmgr/conf/gunicorn.conf.py
bind = '127.0.0.1:8000'
systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
vi /etc/nginx/nginx.conf
註釋掉39行
39 # listen 80 default_server;
systemctl restart nginx.service
systemctl start supervisord.service
後臺執行
nohup /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py &
三、KVM WEB管理工具——WebVirtMgr(二)日常配置
1、登錄WebVirtMgr管理平臺
2、添加宿主機
選擇首頁的WebVirtMgr -->Addd Connection
選擇“SSH鏈接“,設置Label,IP,用戶
注意:Label與IP要相同
- 虛機部署
1、創建存儲池
點擊前面創建的宿主機,進入虛擬機部署界面
點擊“存儲池”按鈕,創建存儲池(即創建磁盤鏡像存放的位置)
注意:
創建存儲池時,首先要在宿主機上創建一個目錄,然後在”路徑“設置欄中添加該目錄
例:mkdir /home/kvm/teststorage -p
2、添加磁盤鏡像
創建完成後如下:
3、宿主機網卡的橋接模式設置
4、創建網絡池
5、創建虛機
選擇“custom install”用戶自定義創建虛機
6、虛機掛載ISO鏡像
7、啓動虛機
點擊“啓動”按鈕後,打開虛機電源。
點擊控制檯按鈕,進入操作系統安裝界面
8、虛機IP地址設置
當虛機安裝完成後,給虛機設置靜態IP地址
例:[root@vmtest01~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=172.16.1.199
NETMASK=255.255.255.0
GATEWAY=172.16.1.1
DNS1=172.16.1.1
- webvirtmgr克隆虛機操作
1、關閉正在運行的虛機
2、進入虛機配置界面,在克隆配置項,爲新的虛機的“名字”,“MAC地址”,“磁盤鏡像”進行設置
3、啓動虛機,進入系統,對/etc/udev/rules.d/70-persistent-net.rules 文件做如下修改
修改後
4、編輯/etc/sysconfig/network-scripts/ifcfg-eth0 修改虛機的網卡配置,爲虛機分配新的IP地址
5、重啓系統 使網卡重新註冊,讓配置生效
reboot
- webvirtmgr虛機磁盤擴容(新增磁盤)
1 關閉虛機
2 對虛機的xml文件的disk域添加如下代碼
場景一:新增一塊磁盤
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/> ---添加改行代碼找到新增磁盤格式
<source file='/home/kvm/teststorage/entd01.img'/> --指定新增磁盤路徑
<target dev='vda' bus='virtio'/> --指定磁盤設備名稱,和傳輸總線類型
</disk>
場景二:新增多塊磁盤
已新增兩塊盤爲例:
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/home/kvm/teststorage/entd01.img'/>
<target dev='vda' bus='virtio'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/home/kvm/vm/waq02-clone.qcow2'/>
<target dev='vdb' bus='virtio'/>
</disk>
3 啓動虛機,然後在虛機系統內部對磁盤進行格式化,分區等操作。
報錯問題:
打開後,有報錯!看來在上面使用ssh連接的配置環節有誤所致!
解決措施:
1)在webvirtmgr服務器(服務端)上(這裏kvm和WebVirtMgr部署在同一臺機器上)創建nginx用戶家目錄(默認nginx服務安裝時是沒有nginx家目錄的),生成nginx的公私鑰
[root@openstack ops]# cd /home/
[root@openstack home]# mkdir nginx
[root@openstack home]# chown nginx.nginx nginx/
[root@openstack home]# chmod 700 nginx/ -R
[root@openstack home]# su - nginx -s /bin/bash
-bash-4.1$ ssh-keygen #期間輸入yes後直接回車,回車
-bash-4.1$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.1$ chmod 0600 ~/.ssh/config
2)在webvirtmgr服務器(服務端)上(這裏kvm和WebVirtMgr部署在同一臺機器上),將nginx用戶的ssh-key上傳到kvm服務器上(這裏kvm和WebVirtMgr部署在同一臺機器上)
[root@openstack ops]# su - nginx -s /bin/bash
-bash-4.1$ ssh-copy-id [email protected]
Warning: Permanently added '192.168.1.17' (RSA) to the list of known hosts.
[email protected]'s password: #輸入192.168.1.17即本機的root賬號
Now try logging into the machine, with "ssh '[email protected]'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
---------------------------------------------------------------------------------------------------------------------
這裏採用的是root用戶,如果採用其他用戶,比如上面假設的webvirtmgr用戶,操作如下:
[root@openstack ops]#su - nginx -s /bin/bash
-bash-4.1$ssh-copy-id [email protected]