KVM虚拟化安装步骤:
准备服务器:
192.168.1.X 安装路径:
1、程序:路径/var/www
数据库:路径/usr/local/application
虚拟机存放:路径/data
一、首先要安装KVM虚拟化环境
1、安装虚拟机:
1)修改内核模式为兼容内核启动
[[email protected] ~]# 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
[[email protected] ~]#vim /boot/grub/grub.conf
......
default=1 #由默认的0改为1,内核兼容模式
2)关闭selinux,重启后生效
[[email protected] ~]#vim /etc/sysconfig/selinux
......
SELINUX=disabled
[[email protected] ~]#setenforce 0
setenforce: SELinux is disabled
[[email protected] ~]#getenforce
Disabled
3)关闭防火墙
[[email protected] ~]#chkconfig ip6tables off
[[email protected] ~]#chkconfig iptables off
4)重启
[[email protected] ~]#reboot
虚拟化环境配置
1)查看是否支持虚拟机
说明1:半虚拟化是不能运行与安装KVM虚拟机的。
[[email protected] ~]#egrep '(vmx|svm)' --color=always /proc/cpuinfo
2)配置yum环境,确认yum源是好使的。(这一步骤省略,可以用默认yum源,也可以换用163或阿里的yum源,只要能正常使用就行)
3)安装kvm软件包
[[email protected] ~]#yum -y install kvm python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v
补充安装kvm虚拟化一些管理工具包
[[email protected] ~]#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)查看虚拟机环境
[[email protected] ~]# /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
----------------------------------------------------------------------------------------------------------------------------------------------
[[email protected] ~]# 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模块支持
[[email protected] ~]# lsmod |grep kvm
kvm_intel 54285 0
kvm 333172 1 kvm_intel
3)查看虚拟工具版本
[[email protected] ~]# virsh --version
0.10.2
[[email protected] ~]# virt-install --version
0.600.0
[[email protected] ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
手动配置虚拟网桥
1)关闭networkmanager服务
[[email protected] ~]# chkconfig NetWorkManager off
[[email protected] ~]# /etc/init.d/NetWorkManager stop
2)创建br0网桥
[[email protected] ~]#cd /etc/sysconfig/network-scripts/
[[email protected] ~]#cp ifcfg-enp2s0f0 ifcfg-br0
[[email protected] ~]# 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
[[email protected] ~]# 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服务之后,才能通过重启网卡服务管理网络。
[[email protected] ~]# service network restart
4)查看网桥br0信息
[[email protected] ~]# ifconfig
5)查看网桥
[[email protected] ~]# 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地址
例:[[email protected]~]# 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的公私钥
[[email protected] ops]# cd /home/
[[email protected] home]# mkdir nginx
[[email protected] home]# chown nginx.nginx nginx/
[[email protected] home]# chmod 700 nginx/ -R
[[email protected] home]# su - nginx -s /bin/bash
-bash-4.1$ ssh-****** #期间输入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部署在同一台机器上)
[[email protected] 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用户,操作如下:
[[email protected] ops]#su - nginx -s /bin/bash
-bash-4.1$ssh-copy-id [email protected]