kvm虚拟化

1、介绍部署虚拟化环境。
2、创建虚拟机实例。
3、虚拟机的基本管理。
KVM 2.6.20 之后 CPU虚拟化指令集(Intel-VT | AMD-V)
=============================================================
Centos 6.5 X86_64 :
40G硬盘
CPU支持虚拟化指令集(Intel-VT | AMD-V)
4G内存
在虚拟机中必须勾选两项:虚拟哈 Intel VT-x/EPT或AMD-V/RVI(V)
Virtualise CPU ...
-----------------------------------------------------------
setup1:
setup1.1: 在已有系统基础上,安装KVM所需软件
[root@localhost 桌面]# yum -y install qemu-kvm.x86_64 qemu-kvm-tools.x86_64 python-virtinst.noarch qemu-img.x86_64 bridge-utils.x86_64 libvirt virt-manager
1.2 验证。重新启动系统,查看CPU是否支持虚拟化
[root@localhost 桌面]# cat /proc/cpuinfo |grep vmx
flags  : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb xsaveopt pln pts dts tpr_shadow vnmi ept vpid fsgsbase smep
检查KVM模块是否安装:
[root@localhost 桌面]# lsmod |grep kvm
kvm_intel              54285  0
kvm                   333172  1 kvm_intel
============================================================
Setup2:
2.1
在libvirt中运行KVM网络有两种方法:NAT和bridge,默认是NAT.
将以bridge(桥接模式)为例。
[root@localhost 桌面]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0

HWADDR=00:0C:29:BB:CA:36
TYPE=Ethernet
UUID=5697689c-b71a-47b9-8a4b-de1c3d4c0a03
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
BRIDGE="br0"
---------------------------------------------------------
[root@localhost 桌面]# vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes

NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=192.168.10.1
NETMASK=255.255.255.0
--------------------------------------------------------------------
[root@localhost 桌面]# service network restart
关闭环回接口:                                             [确定]
弹出环回接口:                                             [确定]
弹出界面 eth0:                                            [确定]
弹出界面 br0: Determining if ip address 192.168.10.1 is already in use for device br0...
                                                          [确定]
-----------------------------------------------------
确定IP地址信息
[root@localhost 桌面]# ifconfig
br0       Link encap:Ethernet  HWaddr 00:0C:29:BB:CA:36  
         inet addr:192.168.10.1  Bcast:192.168.10.255  Mask:255.255.255.0
         inet6 addr: fe80::20c:29ff:febb:ca36/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:33 errors:0 dropped:0 overruns:0 frame:0
         TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0
         RX bytes:1772 (1.7 KiB)  TX bytes:636 (636.0 b)


eth0      Link encap:Ethernet  HWaddr 00:0C:29:BB:CA:36  
         inet6 addr: fe80::20c:29ff:febb:ca36/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:3091 errors:0 dropped:0 overruns:0 frame:0
         TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000
         RX bytes:797555 (778.8 KiB)  TX bytes:1182 (1.1 KiB)
-------------------------------------------------------------------
Setup3 :KVM管理
[root@localhost 桌面]# virt-manager (在执行该命令后,弹出图形管理器界面报错,需重启
系统,问题自然解决)
----------------------------------------------------------------------
[root@localhost 桌面]# mkdir -p /data_kvm/{store,iso}
1、创建存储池:双击localhost(QEMU)
2、创建镜像存储池:
3、创建存储卷:
4、新建虚拟机,将虚拟机名称命名为Centos6.5
===============================================================
Setup4:使用KVM命令集管理虚拟机
4.1 查看命令帮助
[root@localhost ~]# virsh -h
4.2 查看KVM的配置文件存放目录
[root@localhost ~]# ls /etc/libvirt/qemu
autostart  Centos6.5.xml  networks
4.3 查看虚拟机状态
[root@localhost ~]# virsh list --all
Id    名称                         状态
----------------------------------------------------
2     Centos6.5                      running
4.4 虚拟机的关机与开机
首先需要确认acpid服务安装并运行
[root@localhost ~]# service acpid status
acpid (pid  1625) 正在运行...
[root@localhost ~]# virsh shutdown Centos6.5
[root@localhost ~]# virsh start Centos6.5
4.5 强制实例系统关闭电源
[root@localhost ~]# virsh destroy Centos6.5
4.6 通过配置文件启动虚拟机系统实例
[root@localhost ~]# virsh create /etc/libvirt/qemu/Centos6.5.xml
4.7 挂起虚拟机
[root@localhost ~]# virsh suspend Centos6.5
域 Centos6.5 被挂起
[root@localhost ~]# virsh list --all
Id    名称                         状态
----------------------------------------------------
2     Centos6.5                      暂停
4.8 回复挂起

[root@localhost ~]# virsh resume Centos6.5
域 Centos6.5 被重新恢复
[root@localhost ~]# virsh list --all
Id    名称                         状态
----------------------------------------------------
2     Centos6.5                      running
 4.9 配置虚拟机实例伴随宿主机自动启动。
[root@localhost ~]# virsh autostart Centos6.5
域 Centos6.5标记为自动开始
4.10 导出虚拟机配置
[root@localhost ~]# virsh dumpxml Centos6.5 > /etc/libvirt/qemu/Centos6.5_bak.xml
 4.11 虚拟机的删除与添加
[root@localhost ~]# virsh shutdown Centos6.5
[root@localhost ~]# virsh undefine Centos6.5
 4.12 修改虚拟机的配置信息
[root@localhost qemu]# virsh edit Centos6.5
[root@localhost qemu]# vim /etc/libvirt/qemu/Centos6.5.xml (效果同上)


Setup5:KVM文件管理
 虚拟机磁盘文件有raw与qcow2格式,KVM 默认格式是:raw;
raw:好处:性能好,速度最快。缺点:不支持一些新的功能。如:镜像、Zlib磁盘压缩,AES加密等。
libgestfs-tools
 5.1 转换raw格式磁盘至qcow2的格式
查看当前磁盘格式
[root@localhost qemu]# qemu-img info /data_kvm/store/xinwen_kvm.img
image: /data_kvm/store/xinwen_kvm.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 10G
关闭虚拟机:
[root@localhost qemu]# virsh shutdown Centos6.5
域 Centos6.5 被关闭
 转换磁盘文件格式:
[root@localhost qemu]# qemu-img convert -f raw -O qcow2 /data_kvm/store/xinwen_kvm.img /data_kvm/store/xinwen_kvm.qcow2
 5.2 修改Centos6.5的xml配置文件
[root@localhost qemu]# virsh edit Centos6.5
...省略
23
24
...省略
----------------------------------------------------
5.3 virt-cat命令,类似于cat
[root@localhost qemu]# virt-cat -a /data_kvm/store/xinwen_kvm.qcow2 /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain
 5.4 virt-edit命令,用于编辑文件,用法与vim基本相同
[root@localhost qemu]# virt-edit -a /data_kvm/store/xinwen_kvm.qcow2 /etc/resolv.conf
 5.5 virt-df 命令用查看虚拟机磁盘信息
[root@localhost qemu]# virt-df -h Centos6.5
Filesystem                                Size       Used  Available  Use%
Centos6.5:/dev/sda1                       484M        34M       425M    8%
Centos6.5:/dev/VolGroup/lv_root           8.4G       3.3G       4.6G   40%
======================================================================================


6 虚拟机克隆
6.1)查看虚拟机的状态:
[root@localhost ~]# virsh list --all
Id    名称                         状态
----------------------------------------------------
1     Centos6.5                      running
----------------------------------------------------
[root@localhost ~]# virsh shutdown Centos6.5
域 Centos6.5 被关闭
----------------------------------------------------
[root@localhost ~]# virsh list --all
Id    名称                         状态
----------------------------------------------------
-     Centos6.5                      关闭
6.2 从Centos6.5 克隆为 test
[root@localhost ~]# virt-clone -o Centos6.5 -n test -f /data_kvm/store/test.qcow2
正在分配 'test.qcow2'                                        |  10 GB     00:00    
Clone 'test' created successfully.
 6.3 再查看虚拟机的状态
[root@localhost ~]# virsh list --all
Id    名称                         状态
----------------------------------------------------
-     Centos6.5                      关闭
-     test                           关闭
6.4 启动虚拟机
[root@localhost ~]# virsh start test
域 test 已开始
[root@localhost ~]# virsh list --all
Id    名称                         状态
----------------------------------------------------
2     test                           running
-     Centos6.5                      关闭


7 虚拟机的快照
KVM虚拟机要使用镜像功能,磁盘的格式必须为qcow2
 7.1 对Centos6.5创建快照
[root@localhost ~]# virsh snapshot-create Centos6.5
Domain snapshot 1420636959 created
7.2 查看虚拟机快照版本信息
[root@localhost ~]# virsh snapshot-current Centos6.5
1420636959//快照的版本号
shutoff
.........
 7.3 查看快照信息
[root@localhost ~]# virsh snapshot-list Centos6.5
名称               Creation Time             状态
------------------------------------------------------------
1420636959           2015-01-07 21:22:39 +0800 shutoff
 7.4 创建新快照
[root@localhost ~]# virsh snapshot-create Centos6.5
Domain snapshot 1420637149 created
 [root@localhost ~]# virsh snapshot-list Centos6.5
名称               Creation Time             状态
------------------------------------------------------------
1420636959           2015-01-07 21:22:39 +0800 shutoff
1420637149           2015-01-07 21:25:49 +0800 shutoff
----------------------------------------------------------------
7.5 恢复快照状态至1420636959
[root@localhost ~]# virsh snapshot-revert Centos6.5 1420636959
[root@localhost ~]# virsh snapshot-current Centos6.5
1420636959
shutoff
 7.6 删除快照
[root@localhost ~]# virsh snapshot-list Centos6.5
名称               Creation Time             状态
------------------------------------------------------------
 1420636959           2015-01-07 21:22:39 +0800 shutoff
1420637149           2015-01-07 21:25:49 +0800 shutoff
 [root@localhost ~]# virsh snapshot-delete Centos6.5 1420637149
Domain snapshot 1420637149 deleted
 [root@localhost ~]# virsh snapshot-list Centos6.5
名称               Creation Time             状态

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章