1.KVM is a set of kernel modules for an x86 architecture hardware with virtualization extensions(KVM是一组用于实现虚拟化扩展的内核模块)
内核模块组件:kvm-intel.ko, kvm-amd.ko
2.使用kvm能对X86的内核架构虚拟化扩展,但仍需要QEMU来真实虚拟出来的硬件外围设备
3.QEMU(Quick Emulator):QEMU使用动态翻译技术在宿主机上执行虚拟机命令,然而这种翻译方式很慢
4.qemu-kvm直接与kvm模块交互,并能够通过VM直接在CPU上执行指令不再需要动态翻译,因此我们使用的都是qemu-kvm
5.libvirt用来管理qemu-kvm,而qemu-kvm模拟运行KVM虚拟机
6.libvirt是一组在终端用户与监视器之间的一组API库,libvirt通过接收用户命令对虚拟机进行修改或是操作监视器(hypervisor),libvirt是后端管理层。
前端管理的工具有oVirt ,RHEV(Red Hat Enterprise Virtualization),OpenStack等等。libvirt可以用来管理存储,网络和管理虚拟硬件。
7.virsh start TestVM:启动虚拟机(virsh是libvirt用来libvirt服务交互的前端命令行,virt-manager是全段管理工具)
8.虚拟机安装前要求:
grep --color -Ew 'svm|vmx|lm' /proc/cpuinfo
(The svm flag means that the CPU has AMD-V, vmx flag means that the CPU has VT-x,
and lm means a 64-bit support)
9.lsmod|grep kvm #检查kvm模块是否加载
10.modprobe kvm kvm_intel(modprobe kvm kvm_amd)
11.安装kvm:
yum install qemu-kvm libvirt virt-install virt-manager -y(yum groupinstall "virtualization" -y)
yum groupinfo "virtualization"
12.启动libvirt服务,libvirt提供API接口与qemu-kvm交互,客户端可通过virsh或是virt-manager来对虚拟机的生命周期进行管理。
systemctl enable libvirtd && systemctl start libvirtd
13.检查KVM的配置信息:
virt-host-validate
14.硬件虚拟化使得KVM虚拟机可以直接访问硬件CPU,提高系统的性能。
[iyunv@localhost-localdomain ~]# virt-host-validate
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking for device /dev/kvm : PASS
QEMU: Checking for device /dev/vhost-net : PASS
QEMU: Checking for device /dev/net/tun : PASS
LXC: Checking for Linux >= 2.6.26 : PASS
15.dev/kvm:没有该设备将不能访问硬件
/dev/vhost-net:没有该设备将会降低虚拟网络的性能
/dev/net/tun:创建/tun/tap设备加速网络连通性
16.virsh(virtualization shell):虚拟shell,用来管理VM和监视器
17.virsh管理命令分类:
Guest management commands (for example start, stop)
Guest monitoring commands (for example memstat, cpustat)
Host and hypervisors commands (for example capabilities, nodeinfo)
Virtual networking commands (for example net-list, net-define)
Storage management commands (for example pool-list, pool-define)
Snapshot commands (create-snapshot-as)
18.virsh nodeinfo #显示节点的系统资源信息
CPU model: x86_64
CPU(s): 4
CPU frequency: 2534 MHz
CPU socket(s): 1
Core(s) per socket: 2
Thread(s) per core: 2
NUMA cell(s): 1
Memory size: 7967796 KiB
19.The virt-manager application is a Python-based desktop user interface for managing virtual machines through libvirt
20.Alt + F2 and it will then display the dialog box of virt-manager.
polkit是用来验证非root用户访问virt-manager的系统框架(rule file: /etc/polkit-1/rules.d)
21.Virtual Networks:
NATed:
That means the VMs can communicate with the outside network
based on the network connectivity available on the host but none of the outside
entities will be able to communicate with the VMs
Routed:
A routed virtual network allows the connection of virtual machines directly to the
physical network. Here VMs will send out packets to the outside network based on
the routing rules set on the hypervisor.
Isolated:
provides a private network between the hypervisor and
the virtual machines.
1.virsh net-list --all #list the virtual networks. --all is used to list both active and inactive virtual networks.
Autostart: shows whether the virtual network should be started when you activate the libvirtd service.
2.virsh net-info default #显示网络的详细信息
[iyunv@localhost-localdomain rules.d]# virsh net-info default
Name default
UUID 5903e242-6b1f-411c-b352-85957c7ace03
Active: yes
Persistent: yes
Autostart: yes
Bridge: virbr0
3.virsh net-dumpxml default #显示网络的配置文件信息
[iyunv@localhost-localdomain rules.d]# virsh net-dumpxml default (显示虚拟网络配置文件的xml信息(/etc/libvirt/qemu/networks/default.xml))
<network>
<name>default</name>
<uuid>5903e242-6b1f-411c-b352-85957c7ace03</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0' />
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254' />
</dhcp>
</ip>
</network>
4.虚拟网络配置文件的路径: /etc/libvirt/qemu/networks/default.xml
5.停止/开启虚拟网络 virsh net-destroy default(virsh net-start default)
6.存储池是用来保存虚拟机磁盘映像
7.存储池的文件所在路径:/var/lib/libvirt/images
8.Allocate entire disk now, to choose a disk allocation method; deselecting this button will result in a thin-provisioned disk and selecting it will result in
a thick-provisioned disk
22.虚拟网络
可以把bridge看成是虚拟交换机,bridge维护和存储MAC表。(IP与接口映射关系的MAC表)
接口通常是brigde的网络接口,这些特殊的网络接口被称为TAP设备
可以把TAP设备看做是网络电缆在虚拟机与bridge之间传输分组
TUN(tunnel) layer 3 IP
TAP(network tap) layer 2 Enternet frames
TUN is used with routing, while TAP is used to create a network bridge
23.创建bridge并绑定TAP设备
1.lsmod |grep bridge #检查bridge组件是否安装
2.brctl addbr tester #添加bridge
3.brctl show #查看bridge
4.ip link show tester #查看桥设备
5.lsmod |greptun #查看TUN/TAP设备模块是否加载至内核
6.ip tuntap add dev vm-vnic mode tap #添加tap设备
7.ip link show vm-vnic #显示vm-vnic设备信息
8.brctl addif tester vm-vnic #添加tap设备至bridge
9.brctl show #查看桥设备
********************************************************
1.brctl delif tester vm-vnic
2.brctl show tester
3.ip tuntap del vm-vnic mode tap
4.brctl delbr tester
brctl --help #查看帮助
********************************************************
24.Isolated virtual network(只有虚拟机之间可互通)
1.virt-manager 创建isolated网络,该网络可看做虚拟机之间的交换机
2.为isolated中的虚拟机添加虚拟网卡
virsh创建isolated网络:
1.创建isolated.xml
cat isolated.xml
<network>
<name>isolated</name>
</network>
2.virsh net-define isolated.xml
3.virsh net-list --all #查看可用网络
4.virsh net-dumpxml isolated #查看生成的配置文件
[iyunv@localhost-localdomain zhanglei]# virsh net-dumpxml isolated
<network connections='4'>
<name>isolated</name>
<uuid>2bd84ec3-2993-4dca-9900-61ea2b3d493f</uuid>
<bridge name='virbr1' stp='on' delay='0' />
<mac address='52:54:00:fa:c5:b4'/>
<domain name='isolated'/>
</network>
5.virsh net-autostart isolated #设定libvirtd服务启动之后自启动网络
6.vrish net-start isolated #手动启动isolated网络服务
7.virsh domiflist node2 #查看某一节点网卡的信息
[iyunv@localhost-localdomain zhanglei]# virsh domiflist node1
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 network isolated virtio 52:54:00:fe:68:96
vnet2 network isolated virtio 52:54:00:ac:2c:f9
8.virsh attah-interface --domain node2 --source isolated --type network --model virtio --config --live
(--config: This will make the change persistent in the next startup of the VM.
--live: This will inform libvirt that you are attaching the NIC to a live
virtual machine. Remove --live if the virtual machine is not running)
9.brctl show virbr1
10.virsh detach-interface --domain node2 --type network --mac 52:54:00***** --config --live