KVM是开源软件,全称是kernel-based virtual machine(基于内核的虚拟机),是一个开源的系统虚拟化模块,基于硬件的完全虚拟化,
不过需要硬件支持(如Intel VT技术或者AMD V技术)。自Linux 2.6.20之后集成在Linux的各个主要发行版本中。
它使用Linux自身的调度器进行管理,所以相对于Xen,其核心源码很少。KVM目前已成为学术界的主流VMM之一。
OS:Centos6.4
网卡:ifcfg-p3p1
[root@ckcest04 ~]# service iptables stop
[root@ckcest04 ~]# cat /proc/cpuinfo |grep -E 'vmx|svm' #查看是否支持cpu虚拟化,有类似内容弹出来说明支持,否则需要在BIOS中设置
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht
tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64
monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c
rdrand lahf_lm abm ida arat epb xsaveopt pln pts dts tpr_shadow vnmi flexpriority ept vpid fsgsbase bmi1 avx2 smep bmi2 erms invpcid
[root@ckcest04 ~]# yum install kvm virt-* libvirts bridge-utils qemu-img
[root@ckcest04 ~]# yum upgrade device-mapper-libs
kvm:软件包中含有KVM内核模块,它在默认linux内核中提供kvm管理程序
libvirts:安装虚拟机管理工具,使用virsh等命令来管理和控制虚拟机
bridge-utils:设置网络网卡桥接
virt-*:创建、克隆虚拟机命令,使用qemu命令来创建磁盘等。
qemu-img:安装qemu组件,使用qemu命令来创建磁盘等。
device-mapper-libs:更新下系统依赖库,否则kvm无法运行。
[root@ckcest04 ~]# modprobe kvm-intel #加载kvm模块
[root@ckcest04 ~]# lsmod |grep kvm #是否加载成功,
kvm_intel 53484 4
kvm 316506 1 kvm_intel
[root@ckcest04 ~]# reboot
需要将NAT方式改为桥接方式。
[root@ckcest04 network-scripts]# vim ifcfg-br0
DEVICE="br0" #设备名
IPADDR=172.16.20.104 #网桥地址
NETMASK=255.255.255.0 #子网掩码
GATEWAY=172.16.20.1 #网关
DNS1=221.12.1.227 #DNS
#HWADDR=F4:8E:38:77:64:15
TYPE=Bridge #这个很重要,类型是网桥
#UUID=7e34e95e-dbfc-449a-a885-b50f7b717798
ONBOOT=yes #开机自启动
NM_CONTROLLED=yes
BOOTPROTO=static #服务器肯定要静态地址
[root@ckcest04 network-scripts]# vim ifcfg-eth0
DEVICE=p3p1 #网卡名
NAME=eth0
#IPADDR=172.16.20.104
#NETMASK=255.255.255.0
#GATEWAY=172.16.20.1
#DNS1=221.12.1.227
HWADDR=F4:8E:38:77:64:15 #网卡的硬件地址
TYPE=Ethernet #类型是网口
#UUID=7e34e95e-dbfc-449a-a885-b50f7b717798
ONBOOT=yes
BRIDGE=br0 #网卡连接的网桥名
NM_CONTROLLED=yes
BOOTPROTO=static
[root@ckcest04 network-scripts]# /etc/init.d/NetworkManager stop #如果是桌面版的需要把这个服务停了,要么会冲突
[root@ckcest04 network-scripts]# /etc/init.d/network restart
[root@ckcest04 network-scripts]# brctl show #查看网卡桥接
bridge name bridge id STP enabled interfaces
br0 8000.f48e38776415 no p3p1
vnet0
virbr0 8000.525400b2de6e yes virbr0-nic
[root@ckcest04 network-scripts]# route #查看路由
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.16.20.0 * 255.255.255.0 U 0 0 0 br0
192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0
link-local * 255.255.0.0 U 1004 0 0 br0
default localhost 0.0.0.0 UG 0 0 0 br0
[root@ckcest04 network-scripts]# ifconfig
br0 Link encap:Ethernet HWaddr F4:8E:38:77:64:15
inet addr:172.16.20.104 Bcast:172.16.20.255 Mask:255.255.255.0
inet6 addr: fe80::f68e:38ff:fe77:6415/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:132044 errors:0 dropped:0 overruns:0 frame:0
TX packets:29839 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:31268541 (29.8 MiB) TX bytes:6386974 (6.0 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:43424 errors:0 dropped:0 overruns:0 frame:0
TX packets:43424 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:36155279 (34.4 MiB) TX bytes:36155279 (34.4 MiB)
p3p1 Link encap:Ethernet HWaddr F4:8E:38:77:64:15
inet6 addr: fe80::f68e:38ff:fe77:6415/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:300768 errors:0 dropped:0 overruns:0 frame:0
TX packets:118897 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:219757780 (209.5 MiB) TX bytes:14099593 (13.4 MiB)
virbr0 Link encap:Ethernet HWaddr 52:54:00:B2:DE:6E
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
vnet0 Link encap:Ethernet HWaddr FE:54:00:92:25:D5
inet6 addr: fe80::fc54:ff:fe92:25d5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:28419 errors:0 dropped:0 overruns:0 frame:0
TX packets:123341 errors:0 dropped:0 overruns:2 carrier:0
collisions:0 txqueuelen:500
RX bytes:3640634 (3.4 MiB) TX bytes:47763451 (45.5 MiB)
#配置完成后就将p3p1这个网卡设置成了桥接网卡,vm到时候就可以选择这个桥接的配置了。接下来在KVM上
#修改虚拟机的网卡模式。
[root@ckcest04 network-scripts]# /etc/init.d/libvirtd start
安装好虚拟机后,然后关机,然后参照下图:点击网络选项然后将桥接的网卡名设置成之前配置的"br0"。
[root@ckcest05 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 #VM的网卡配置
DEVICE="eth0"
NAME=eth0
HWADDR=52:54:00:92:25:D5
TYPE=Ethernet
UUID=66e81ab6-8ad0-4d74-acd7-003ad86e924d
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=172.16.20.105
NETMASK=255.255.255.0
GATEWAY=172.16.20.1
[root@ckcest05 data01]# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:92:25:D5
inet addr:172.16.20.105 Bcast:172.16.20.255 Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fe92:25d5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:126388 errors:0 dropped:0 overruns:0 frame:0
TX packets:28638 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:48188196 (45.9 MiB) TX bytes:3677161 (3.5 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:672 (672.0 b) TX bytes:672 (672.0 b)
[root@ckcest05 data01]# ping 172.16.20.104
PING 172.16.20.104 (172.16.20.104) 56(84) bytes of data.
64 bytes from 172.16.20.104: icmp_seq=1 ttl=64 time=0.181 ms
[root@ckcest05 data01]# ping 172.16.20.102
PING 172.16.20.102 (172.16.20.102) 56(84) bytes of data.
64 bytes from 172.16.20.102: icmp_seq=1 ttl=64 time=1.28 ms
#上面那个桥接名写好之后开启虚拟机根据上述配置一下,局域网就可以互通了、、