随着IT产业的不断发展,IT技术的不断革新,近几年虚拟化、云计算技术火热,那我们今天来研究一下虚拟化技术,到底什么是虚拟化技术呢?我们为什么要使用虚拟化呢?
虚拟化是指计算元件在虚拟的基础上而不是真实的基础上运行,是一个为了简化管理,优化资源的解决方案。
虚拟化技术可以扩大硬件的容量,简化软件的重新配置过程。CPU的虚拟化技术可以单CPU模拟多CPU并行,允许一个平台同时运行多个操作系统,并且应用程序都可以在相互独立的空间内运行而互不影响,从而显著提高计算机的工作效率。
目前主流的虚拟化技术主要有:KVM、Xen、VMware、VirtualBox,虚拟化技术也越来越广泛的应用在企业中,今天我们来研究和复习一下KVM虚拟化集群的搭建。
1、系统版本:
CentOS 6.4 x86_64
2、处理器需求:
需要一台可以运行最新linux内核的Intel处理器(含VT虚拟化技术)或AMD处理器(含SVM安全虚拟机技术的AMD处理器,也叫AMD-V)
[root@kvm~]# cat /etc/redhat-release
CentOSrelease 6.4 (Final)
3、CPU 支持需求:
[root@localhost~]# egrep 'vmx|svm' /proc/cpuinfo
如果输出的结果包含VMX,它是Intel处理器虚拟机技术标志:如果包含SVM,它是AMD处理器虚拟机技术的标志,如果你什么都没有得到,那应你的系统并没有支持虚拟化的处理,不能使用KVM,另处linux发行版本必须在64bit环境中才能使用KVM。
4、KVM正式安装:
这里直接使用脚本,比较方便快捷,了解更多可以参官方网站。(仅供参考,后期不断优化)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#!/bin/sh
#Auto Make KVM Virtualization
#Author wugk 2013-12-06
#Defined Path
cat
<<EOF
++++++++++++++++Welcome To Use Auto Install KVM Scripts ++++++++++++++++++
+++++++++++++++++++++++++This KVM Install Virtual ++++++++++++++++++++++++
+++++++++++++++++++++++++2013-12-06 Author wugk ++++++++++++++++++++++++++
EOF
KVM_SOFT=(
kvm python-virtinst libvirt bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v libguestfs-tools
)
NETWORK=(
HWADDR=`
ifconfig
eth0 |
egrep
"HWaddr|Bcast"
|
tr
"\n"
" "
|
awk
'{print $5,$7,$NF}'
|
sed
-e
's/addr://g'
-e
's/Mask://g'
|
awk
'{print $1}'
`
IPADDR=`
ifconfig
eth0 |
egrep
"HWaddr|Bcast"
|
tr
"\n"
" "
|
awk
'{print $5,$7,$NF}'
|
sed
-e
's/addr://g'
-e
's/Mask://g'
|
awk
'{print $2}'
`
NETMASK=`
ifconfig
eth0 |
egrep
"HWaddr|Bcast"
|
tr
"\n"
" "
|
awk
'{print $5,$7,$NF}'
|
sed
-e
's/addr://g'
-e
's/Mask://g'
|
awk
'{print $3}'
`
GATEWAY=`route -n|
grep
"UG"
|
awk
'{print $2}'
`
)
#Check whether the system supports virtualization
egrep
'vmx|svm'
/proc/cpuinfo
>>
/dev/null
if
[
"$?"
-
eq
"0"
];
then
echo
'Congratulations, your system success supports virtualization !'
else
echo
-e
'OH,your system does not support virtualization !\nPlease modify the BIOS virtualization options (Virtualization Technology)'
exit
0
fi
if
[ -e
/usr/bin/virsh
];
then
echo
"Virtualization is already installed ,Please exit ...."
;
exit
0
fi
yum -y
install
${KVM_SOFT[@]}
/sbin/modprobe
kvm
ln
-s
/usr/libexec/qemu-kvm
/usr/bin/qemu-kvm
lsmod |
grep
kvm >>
/dev/null
if
[
"$?"
-
eq
"0"
];
then
echo
'KVM installation is successful !'
else
echo
'KVM installation is falis,Please check ......'
exit
1
fi
cd
/etc/sysconfig/network-scripts/
mkdir
-p
/data/backup/
`
date
+%Y%m%d-%H:%M`
yes
|
cp
ifcfg-eth*
/data/backup/
`
date
+%Y%m%d-%H:%M`/
if
[ -e
/etc/sysconfig/network-scripts/ifcfg-br0
];
then
echo
"The ifcfg-br0 already exist ,Please wait exit ......"
exit
2
else
cat
>ifcfg-eth0 <<EOF
DEVICE=eth0
BOOTPROTO=none
${NETWORK[0]}
NM_CONTROLLED=no
ONBOOT=
yes
TYPE=Ethernet
BRIDGE=
"br0"
${NETWORK[1]}
${NETWORK[2]}
${NETWORK[3]}
USERCTL=no
EOF
cat
>ifcfg-br0 <<EOF
DEVICE=
"br0"
BOOTPROTO=none
${NETWORK[0]}
IPV6INIT=no
NM_CONTROLLED=no
ONBOOT=
yes
TYPE=
"Bridge"
${NETWORK[1]}
${NETWORK[2]}
${NETWORK[3]}
USERCTL=no
EOF
fi
echo
'Your can restart Ethernet Service: /etc/init.d/network restart !'
echo
'---------------------------------------------------------'
sleep
1
echo
'Your can restart KVM Service : /etc/init.d/libvirtd restart !'
echo
echo
-e
"You can create a KVM virtual machine: \nvirt-install --name=centos01 --ram 512 --vcpus=1 --disk path=/data/kvm/centos01.img,size=7,bus=virtio --accelerate --cdrom /data/iso/centos58.iso --vnc --vncport=5910 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole"
|
自此KVM简单安装到此结束,安装完毕,怎样来创建一个虚拟机并且安装呢,首先我们把需要安装的系统ISO镜像上传到/data/iso目录,然后新建/data/kvm虚拟机安装目录,然后启动如下脚本:
1
|
virt-install --name=centos01 --ram
512
--vcpus=
1
--disk path=/data/kvm/centos01.img,size=
7
,bus=virtio --accelerate --cdrom /data/iso/centos58.iso --vnc --vncport=
5910
--vnclisten=
0.0
.
0.0
--network bridge=br0,model=virtio
|
然后在客户端(PC机)使用VNC客户端连接KVM服务端5910端口,进行安装系统即可。
当安装完一台后,如何快速启动第二台呢,这里只需要复制我们刚安装的第一台的两个文件(/data/kvm/centos01.img /etc/libvirt/qemu/centos01.xml),然后做相应修改即可。
还可以使用命令克隆:
nohup virt-clone -o centos01 -n centos02 -f /data/kvm/centos02.img &
这样就配置完毕,怎样重启两台虚拟机呢?命令如下:
(参数主要有reboot、start、shutdown)
1
2
3
4
|
virsh reboot centos01 (重启)
virsh reboot centos02 (重启)
virsh start centos01 (启动)
virsh start centos02 (启动)
|
当我们启动新克隆的两台虚拟机,我们会发现网卡无法启动,原因是因为克隆改变新虚拟机的MAC地址,故eth0无法启动,新的虚拟机后网卡变成了eth1,那我们怎样修改为eth0呢,使用如下脚本,执行,然后重启网卡(或者重启系统)即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/bin/sh
#Auto Set Centos6.x eth1 MAC to eth0
NET=
/etc/udev/rules
.d
/70-persistent-net
.rules
ETH0=
/etc/sysconfig/network-scripts/ifcfg-eth0
sed
-i
'/eth0/s/SUBSYSTEM/#SUBSYSTEM/g'
$NET
HWADDR=`
grep
eth1 $NET|
awk
'{print $4}'
|
awk
-F
'=='
'{print $2}'
|
sed
's/\,//g'
`
sed
-i
"s/HWADDR.*/HWADDR=$HWADDR/g"
$ETH0
sed
-i -e
'/^SUBSYSTEM/s/eth1/eth0/'
-e
'/#SUBSYSTEM/s/eth0/eth1/'
-e
's/#SUBSYSTEM/SUBSYSTEM/'
$NET
#This script applies only two NIC configuration
COUNT=`
grep
"SUBSYSTEM"
$NET|
awk
'{print $NF}'
|
sed
's/[^0-9]//g'
|
awk
'NR==2'
`
if
[ $COUNT == 0 ];
then
echo
'The IP MAC Change Successfully !'
else
echo
"The IP MAC Change Fails,Please rerun the Shell Scripts"
fi
|
2013-12-17更新,添加一个KVM日常管理,包括批量添加、克隆、删除功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#!/bin/sh
#Auto Batch Add KVM Virtual
#Author wugk 2013-12-17
#Define Variables
XML_DIR=
/etc/libvirt/qemu/
KVM_DIR=
/data/kvm/
COUNT1=`
ls
$XML_DIR|
grep
xml|
tail
-1 |
sed
's/[^0-9]//g'
`
COUNT2=`
expr
$COUNT1 + 1`
add_kvm()
{
cat
<<eof
#VIR_NAME RAM{M} SIZE{g}
centos01 1024 2
centos02 512 3
centos03 1024 4
centos04 1024 5
centos05 1024 6
eof
echo
"=================================="
if
[ ! -e kvm.txt ];
then
echo
"The kvm.txt File does not exist,Please create ......"
exit
fi
#Auto Create machines
if
[ ! -s kvm.txt ];
then
echo
"The kvm.txt is empty file ,Please Refer Above Content ......"
exit
fi
while
read
line
do
NAME=`
echo
$line |
grep
-
v
"^#"
|
awk
'{print $1}'
`
RAM=`
echo
$line |
grep
-
v
"^#"
|
awk
'{print $2}'
`
SIZE=`
echo
$line |
grep
-
v
"^#"
|
awk
'{print $3}'
`
/usr/bin/virt-install
--name=${NAME} --
ram
${RAM} --vcpus=1 --disk path=
/data/kvm/
${NAME}.img,size=${SIZE},bus=virtio --accelerate --cdrom
/data/iso/centos58
.iso --vnc --vncport=-1 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole
done
<kvm.txt
}
clone_kvm()
{
read
-p
"Please Enter you want create virtual machines count: "
COUNT3
COUNT4=`
expr
${COUNT1} + ${COUNT3}`
for
i
in
`
seq
${COUNT2} ${COUNT4}`
do
UUID=`
/usr/bin/uuidgen
`
MAC=52\\:54\\:$(
dd
if
=
/dev/urandom
count=1 2>
/dev/null
| md5sum |
sed
's/^\(..\)\(..\)\(..\)\(..\).*$/\1\\:\2\\:\3\\:\4/'
)
cd
$XML_DIR ;
cp
centos01.xml centos${i}.xml
cd
$XML_DIR ;
sed
-i -e
"/uuid/s:.*:<uuid>$UUID</uuid>:g"
-e
"s/centos01/centos${i}/g"
-e
"/<mac/s:.*:<mac address='$MAC'/>:g"
centos${i}.xml
cd
$KVM_DIR ;
nohup
cp
centos01.img centos${i}.img &
/usr/bin/virsh
define $XML_DIR
/centos
${i}.xml
echo
"The Virtual machines Info:"
echo
"The centos${i} Virtual machines created successfull"
done
}
delete_kvm()
{
read
-p
"Please Enter you want deleted virtual machines :"
name
for
i
in
`
echo
${name} |
sed
's/ /\n/g'
`
do
/usr/bin/virsh
destroy $i
/usr/bin/virsh
undefine $i ;
echo
"The $i Virtual machines Deleted Successfully"
done
}
case
$1
in
add_kvm )
add_kvm
;;
clone_kvm )
clone_kvm
;;
delete_kvm )
delete_kvm
;;
* )
echo
Usage: $0
"{add_kvm|clone_kvm|delete_kvm|help}"
;;
esac
|
如上我们知道如何创建虚拟机已经批量管理虚拟机,那我们如何来创建虚拟机快照呢,如下:
我们创建的虚拟机默认格式为raw,要想使用快照功能,需要先将虚拟机格式转换为qcow2格式,转换前需要先关闭虚拟机。
(使用qemu-img info centos01.img 可以看到虚拟机默认使用的格式)
1
2
3
4
5
6
7
8
9
|
raw转qcow2命令:
qemu-img convert -f raw -O qcow2 centos01.img centos01.qcow2 (大写-o)
然后创建快照:
qemu-img snapshot -c centos01.img centos01.qcow2
[root@localhost kvm]# qemu-img snapshot -l centos01.qcow2
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1centos01.img 02013-12-2020:09:2000:00:00.000
[root@localhost kvm]#
|
恢复快照:
1
|
qemu-img snapshot -a centos01.img centos01.qcow2
|
KVM简单操作就先介绍到这里,更多功能和维护在后期会更新。
本文参考如下文章,非常感谢:
http://koumm.blog.51cto.com/703525/1290191
http://g.51cto.com/netengineer/216414
本文转自 wgkgood 51CTO博客,原文链接:http://blog.51cto.com/wgkgood/1337628