KVM介绍
Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块,自Linux 2.6.20之后集成在Linux的各个主要发行版本中。它使用Linux自身的调度器进行管理,所以相对于Xen,其核心源码很少。KVM目前已成为学术界的主流VMM之一。
KVM的虚拟化需要硬件支持(如Intel VT技术或者AMD V技术)。是基于硬件的完全虚拟化。而Xen早期则是基于软件模拟的Para-Virtualization,新版本则是基于硬件支持的完全虚拟化。但Xen本身有自己的进程调度器,存储管理模块等,所以代码较为庞大。广为流传的商业系统虚拟化软件VMware ESX系列是基于软件模拟的Full-Virtualization。
1、环境准备
2、规划说明
开启之后修改IP地址:
本次规划IP地址
IP地址 | 硬盘大小 | 内存大小 |
10.0.0.88 | 8G | 2G |
3、虚拟化概念
1.软件模拟
优点:能够模拟任何硬件,包括不存在的
缺点:功能非常低效,一般用于研究,生产环境不同。
代表:QEM
2.虚拟化层翻译
2.1 软件全虚拟化,通过虚拟化层翻译 ===> 你和美国人说话 —需要通过翻译=VMware
2.2 半虚拟化 改动虚拟机的内核(linux)加上虚拟化层翻译
======>学了英语在和美国人交流 xen(被淘汰)
2.3 硬件支持的全虚拟化 ===>你重生到美国和美国人交流 KVM
3.容器虚拟化 docker
安装
KVM已经集成了,我们需要安装管理软件
[root@kvm /]# egrep '(vmx|svm)' /proc/cpuinfo
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 tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb pln pts dts tpr_shadow vnmi ept vpid fsgsbase smep
[root@kvm /]# yum -y install qemu-kvm qemu-kvm-tools virt-manager libvirt python-virtinst libvirt-python
[root@kvm yum.repos.d]# rpm -qa qemu-kvm qemu-kvm-tools virt-manager libvirt python-virtinst libvirt-python
libvirt-python-0.10.2-60.el6.x86_64
python-virtinst-0.600.0-29.el6.noarch
virt-manager-0.9.0-31.el6.x86_64
qemu-kvm-0.12.1.2-2.491.el6_8.1.x86_64
libvirt-0.10.2-60.el6.x86_64
qemu-kvm-tools-0.12.1.2-2.491.el6_8.1.x86_64
[root@kvm yum.repos.d]# lsmod|grep kvm
kvm_intel 55624 0
kvm 341551 1 kvm_intel
查看我们的管理工具
[root@kvm yum.repos.d]# rpm -ql qemu-kvm
/etc/ksmtuned.conf
/etc/modprobe.d/blacklist-kvm.conf
/etc/rc.d/init.d/ksm
/etc/rc.d/init.d/ksmtuned
/etc/sasl2/qemu-kvm.conf
/etc/sysconfig/ksm
/etc/sysconfig/modules/kvm.modules
/etc/udev/rules.d/80-kvm.rules
/usr/libexec/qemu-kvm
/usr/sbin/ksmtuned
/usr/share/doc/qemu-kvm-0.12.1.2/COPYING
/usr/share/doc/qemu-kvm-0.12.1.2/COPYING.LIB
/usr/share/doc/qemu-kvm-0.12.1.2/Changelog
/usr/share/doc/qemu-kvm-0.12.1.2/LICENSE
/usr/share/doc/qemu-kvm-0.12.1.2/README
/usr/share/doc/qemu-kvm-0.12.1.2/README.systemtap
/usr/share/doc/qemu-kvm-0.12.1.2/TODO
/usr/share/doc/qemu-kvm-0.12.1.2/qemu-doc.html
/usr/share/doc/qemu-kvm-0.12.1.2/qemu-tech.html
/usr/share/man/man1/qemu-kvm.1.gz
/usr/share/qemu-kvm
/usr/share/qemu-kvm/bios.bin
/usr/share/qemu-kvm/dump-guest-memory.py
/usr/share/qemu-kvm/dump-guest-memory.pyc
/usr/share/qemu-kvm/dump-guest-memory.pyo
/usr/share/qemu-kvm/extboot.bin
/usr/share/qemu-kvm/keymaps
/usr/share/qemu-kvm/keymaps/ar
/usr/share/qemu-kvm/keymaps/common
/usr/share/qemu-kvm/keymaps/da
/usr/share/qemu-kvm/keymaps/de
/usr/share/qemu-kvm/keymaps/de-ch
/usr/share/qemu-kvm/keymaps/en-gb
/usr/share/qemu-kvm/keymaps/en-us
/usr/share/qemu-kvm/keymaps/es
/usr/share/qemu-kvm/keymaps/et
/usr/share/qemu-kvm/keymaps/fi
/usr/share/qemu-kvm/keymaps/fo
/usr/share/qemu-kvm/keymaps/fr
/usr/share/qemu-kvm/keymaps/fr-be
/usr/share/qemu-kvm/keymaps/fr-ca
/usr/share/qemu-kvm/keymaps/fr-ch
/usr/share/qemu-kvm/keymaps/hr
/usr/share/qemu-kvm/keymaps/hu
/usr/share/qemu-kvm/keymaps/is
/usr/share/qemu-kvm/keymaps/it
/usr/share/qemu-kvm/keymaps/ja
/usr/share/qemu-kvm/keymaps/lt
/usr/share/qemu-kvm/keymaps/lv
/usr/share/qemu-kvm/keymaps/mk
/usr/share/qemu-kvm/keymaps/modifiers
/usr/share/qemu-kvm/keymaps/nl
/usr/share/qemu-kvm/keymaps/nl-be
/usr/share/qemu-kvm/keymaps/no
/usr/share/qemu-kvm/keymaps/pl
/usr/share/qemu-kvm/keymaps/pt
/usr/share/qemu-kvm/keymaps/pt-br
/usr/share/qemu-kvm/keymaps/ru
/usr/share/qemu-kvm/keymaps/sl
/usr/share/qemu-kvm/keymaps/sv
/usr/share/qemu-kvm/keymaps/th
/usr/share/qemu-kvm/keymaps/tr
/usr/share/qemu-kvm/linuxboot.bin
/usr/share/qemu-kvm/multiboot.bin
/usr/share/qemu-kvm/pxe-e1000.bin
/usr/share/qemu-kvm/pxe-ne2k_pci.bin
/usr/share/qemu-kvm/pxe-pcnet.bin
/usr/share/qemu-kvm/pxe-rtl8139.bin
/usr/share/qemu-kvm/pxe-virtio.bin
/usr/share/qemu-kvm/sgabios.bin
/usr/share/qemu-kvm/simpletrace.py
/usr/share/qemu-kvm/simpletrace.pyc
/usr/share/qemu-kvm/simpletrace.pyo
/usr/share/qemu-kvm/systemtap/conf.d/qemu_kvm.conf
/usr/share/qemu-kvm/systemtap/script.d/qemu_kvm.stp
/usr/share/qemu-kvm/trace-events
/usr/share/qemu-kvm/vapic.bin
/usr/share/qemu-kvm/vgabios-cirrus.bin
/usr/share/qemu-kvm/vgabios-qxl.bin
/usr/share/qemu-kvm/vgabios-stdvga.bin
/usr/share/qemu-kvm/vgabios-vmware.bin
/usr/share/qemu-kvm/vgabios.bin
/usr/share/systemtap/tapset/qemu-kvm-simpletrace.stp
/usr/share/systemtap/tapset/qemu-kvm.stp
软件包解释
[root@m01 kvm]# rpm -qa|grep -E 'qemu|libvirt|virt'
libvirt-python-0.10.2-60.el6.x86_64 #libvirt的图形化虚拟机管理软件,需要图形界面操作系统
virt-what-1.11-1.2.el6.x86_64 #基于Libvirt的图像化虚拟机管理软件,需要图形界面操作系统
qemu-img-0.12.1.2-2.491.el6_8.1.x86_64 #用于操作虚拟机硬盘镜像的创建、查看和格式化转化
gpxe-roms-qemu-0.9.7-6.15.el6.noarch #虚拟机IPXE的启动固件,支持虚拟机从网络启动
libvirt-client-0.10.2-60.el6.x86_64 #Libvirt的客户端,最重要的功能之一就是在宿主机关机时可以通过虚拟机也关机,使虚拟机系统正常关机,而不是被强制关机,造成数据丢失
python-virtinst-0.600.0-29.el6.noarch #一套Python的虚拟机安装工具
virt-manager-0.9.0-31.el6.x86_64 #基于Libvirt的图像化虚拟机管理软件,需要图形界面操作系统
qemu-kvm-0.12.1.2-2.491.el6_8.1.x86_64 #KVM在用户运行的程序
libvirt-0.10.2-60.el6.x86_64 #用于管理虚拟机,它提供了一套虚拟机操作API
qemu-kvm-tools-0.12.1.2-2.491.el6_8.1.x86_64
加入host 解析
vim /etc/host
10..0.88 kmv
启动服务(libvirt)管理kvm
[root@kvm yum.repos.d]# /etc/init.d/libvirtd start
Starting libvirtd daemon: [ OK ]
生成了几个网卡
[root@kvm yum.repos.d]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:30:e9:bc brd ff:ff:ff:ff:ff:ff
inet 10.0.0.88/24 brd 10.0.0.255 scope global eth0
inet6 fe80::20c:29ff:fe30:e9bc/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:30:e9:c6 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.88/24 brd 172.16.1.255 scope global eth1
inet6 fe80::20c:29ff:fe30:e9c6/64 scope link
valid_lft forever preferred_lft forever
4: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
link/ether 52:54:00:57:95:13 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
link/ether 52:54:00:57:95:13 brd ff:ff:ff:ff:ff:ff
DNS小工具
[root@kvm yum.repos.d]# ps -ef|grep dns
nobody 2735 1 0 11:31 ? 00:00:00 /usr/sbin/dnsmasq --strict-order --pid-file=/var/run/libvirt/network/default.pid --conf-file= --except-interface lo --bind-interfaces --listen-address 192.168.122.1 --dhcp-range 192.168.122.2,192.168.122.254 --dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases --dhcp-lease-max=253 --dhcp-no-override --dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile --addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
root 2793 2272 0 11:32 pts/1 00:00:00 grep dns
管理dhcp的一些功能。
1.创建虚拟磁盘
[root@kvm yum.repos.d]# qemu-img create -f raw /opt/kvm.raw 10G
Formatting '/opt/kvm.raw', fmt=raw size=10737418240
================================================================
[root@kvm yum.repos.d]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 6.6G 1.9G 4.4G 31% /
tmpfs 932M 0 932M 0% /dev/shm
/dev/sda1 190M 36M 145M 20% /boot
2.挂载镜像光盘
挂载(此处不能使用mount)
[root@kvm ~]# dd if=/dev/cdrom of=CentOS6.7.ios
[root@kvm ~]# dd if=/dev/cdrom of=CentOS6.7.ios
7608320+0 records in
7608320+0 records out
3895459840 bytes (3.9 GB) copied, 164.972 s, 23.6 MB/s
下载软件包
TightVNC_2.7.10
最后界面
创建虚拟机
[root@kvm ~]#virt-install --virt-type=kvm --name=kvm-demo -r 1024 -c /root/CentOS6.7.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel6 --disk path=/opt/kvm.raw
[root@kvm ~]# mv CentOS6.7.ios /opt/
[root@kvm ~]# virt-install --virt-type=kvm --name=kvm-demo -r 1024 -c /opt/CentOS6.7.ios --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel6 --disk path=/opt/kvm.raw
Starting install...
Creating domain... | 0 B 00:01
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
和安装操作系统一样
选包文章:http://oldboy.blog.51cto.com/2561410/1564620
[root@kvm ~]# virsh list –all 查看所有虚拟机
[root@kvm ~]# virsh start kvm-demo 关闭虚拟机,接虚拟机名字即可
Domain kvm-demo started
开启接上即可
[root@kvm opt]# virsh destroy kvm-demo 停止
[root@kvm ~]# netstat -tunlp|grep qemu-kvm 查看启动的进程
[root@kvm opt] virsh undefine kvm-demo 强制关闭
[root@kvm ~]# virsh resume kvm-demo 暂停
生成kvm虚拟机:virt-install
查看在运行的虚拟机:virsh list
查看所有虚拟机:virsh list –all
查看kvm虚拟机配置文件:virsh dumpxml name
启动kvm虚拟机:virsh start name
正常关机:virsh shutdown name
非正常关机(相当于物理机直接拔掉电源):virsh destroy name
删除:virsh undefine name(彻底删除,找不回来了,如果想找回来,需要备份/etc/libvirt/qemu的xml文件)
根据配置文件定义虚拟机:virsh define file-name.xml
挂起,终止:virsh suspend name
恢复挂起状态:virsh resume name
查看所有虚拟机:virsh list –all
查看kvm虚拟机配置文件:virsh dumpxml name
启动kvm虚拟机:virsh start name
正常关机:virsh shutdown name
非正常关机(相当于物理机直接拔掉电源):virsh destroy name
删除:virsh undefine name(彻底删除,找不回来了,如果想找回来,需要备份/etc/libvirt/qemu的xml文件)
根据配置文件定义虚拟机:virsh define file-name.xml
挂起,终止:virsh suspend name
恢复挂起状态:virsh resume name
=====================================
配置网络,连接! ok~
[root@kvm ~]# virt-install –virt-type=kvm –name=kvm-demo -r 1024 -c /opt/CentOS6.7.ios –network network=default –graphics vnc,listen=0.0.0.0,port=5901 –noautoconsole –os-type=linux –os-variant=rhel6 –disk path=/opt/kvm.raw
在添加一台需要指定端口port
http://blog.csdn.net/starshine/article/details/8179483
使用undefined 删除只是删除这下面的文件
[root@m01 oldboy]# cd /etc/libvirt/qemu
[root@m01 qemu]# ls
kvm-demo.xml networks
制作快照
raw 和qcow2简单区别
raw不支持快照
qcow2支持快照
[root@m01 kvm]# qemu-img convert -c -f raw -O qcow2 kvm.raw kvm.qcow2
[root@m01 kvm]# ll -h
total 6.7G
-rw-r–r– 1 qemu qemu 3.7G Jul 5 12:49 CentOS6.7.ios
-rw-r–r– 1 root root 756M Jul 5 16:00 kvm.qcow2
-rw-r–r– 1 qemu qemu 18G Jul 5 16:01 kvm.raw
drwx—— 2 root root 16K Jul 5 12:43 lost+found
virsh edit kvm-demo
提示raw类型分区无法进行制作快照,所以我们修改分区类型
23行
<driver name='qemu' type='raw' cache='none'/>
<driver name='qemu' type='qcow2' cache='none'/>
24行
<source file='/kvm/kvm.raw'/>
<source file='/kvm/kvm.qcow2'/>
[root@m01 kvm]# ps -ef|grep –color=auto qcow 检查
[root@m01 kvm]# virsh snapshot-create-as kvm-demo kvm-demo-shot1 创建快照
kvm-demo=虚拟机名
kvm-demo-shot1 =快照名
查看所有虚拟机快照
[root@m01 kvm]# virsh snapshot-list kvm-demo 指定虚拟机快照
Name Creation Time State
————————————————————
kvm-demo-shot1 2016-07-05 16:20:50 +0800 running
查看某一个虚拟机快照的信息
[root@m01 kvm]# virsh snapshot-info kvm-demo kvm-demo-shot1
kvm-demo=虚拟机
kvm-demo-shot1 =快照名
Name: kvm-demo-shot1
Domain: kvm-demo
Current: yes
State: running
Location: internal
Parent: -
Children: 0
Descendants: 0
Metadata: yes
存放虚拟机的配置路径
[root@m01 kvm]# ll /var/lib/libvirt/qemu/虚拟机配置文件
[root@m01 kvm]# ls /etc/libvirt/qemu/ 网络配置文件(服务)
快照存放路径
[root@m01 kvm]# ll /var/lib/libvirt/qemu/snapshot/kvm-demo/
total 4
-rw——- 1 root root 3475 Jul 5 16:20 kvm-demo-shot1.xml
还原快照
[root@m01 kvm]# virsh snapshot-revert kvm-demo kvm-demo-shot1
删除快照
[root@m01 kvm]# virsh snapshot-delete kvm-demo kvm-demo-shot1
克隆虚拟机
1.设置网卡,关闭selinux和iptables
2.清空/etc/udev
导出xml文件
[root@m01 kvm]# virsh dumpxml kvm-demo >kvm-clone.xml
复制kvm.qcow2(导出磁盘文件)
[root@m01 kvm]# cp kvm.qcow2 kvm-clone.qcow2
[root@m01 kvm]# ll -h
total 7.8G
-rw-r–r– 1 qemu qemu 3.7G Jul 5 12:49 CentOS6.7.ios
-rw-r–r– 1 root root 981M Jul 5 17:13 kvm-clone.qcow2
-rw-r–r– 1 root root 2.8K Jul 5 17:12 kvm-clone.xml
-rw-r–r– 1 root root 981M Jul 5 17:11 kvm.qcow2
-rw-r–r– 1 root root 18G Jul 5 16:12 kvm.raw
drwx—— 2 root root 16K Jul 5 12:43 lost+found
修改xml配置文件
[root@m01 kvm]# vim kvm-clone.xml
<name>kvm-clone</name>
<uuid>0bb17855-95db-442b-ac9f-d855d63badd2</uuid>
<source file='/kvm/kvm-clone.qcow2'/>
<mac address='52:54:00:ae:49:b1'/>
===============================================
可以随机生成uuid
[root@m01 oldboy]# cat /proc/sys/kernel/random/uuid
0bb17855-95db-442b-ac9f-d855d63badd2
修改了以下结果
[root@kvm opt]# diff kvm-clone.xml*
2,3c2,3
< <name>kvm-clone</name>
< <uuid>0a515a20-ea43-4b4b-9683-688c4792800f</uuid>
---
> <name>kvm-demo</name>
> <uuid>45fd0d59-268f-0968-7101-e27d633286b7</uuid>
24c24
< <source file='/opt/kvm-clone.qcow2'/>
---
> <source file='/opt/kvm.qcow2'/>
53c53
< <mac address='52:54:00:9a:7a:1d'/>
---
> <mac address='52:54:00:9a:7a:bd'/>
导入进去
[root@m01 kvm]# virsh define kvm-clone.xml
Domain kvm-clone defined from kvm-clone.xml
[root@m01 kvm]# virsh list --all
Id Name State
----------------------------------------------------
- kvm-clone shut off
- kvm-demo shut off
启动虚拟机
[root@m01 kvm]# virsh start kvm-clone
Domain kvm-clone started
[root@m01 kvm]# virsh list --all
Id Name State
----------------------------------------------------
7 kvm-clone running
- kvm-demo shut off
[root@m01 kvm]# virsh start kvm-demo
Domain kvm-demo started
[root@m01 kvm]# virsh list --all
Id Name State
----------------------------------------------------
7 kvm-clone running
8 kvm-demo running
更多介绍:
openstack
cloudstack
ovirt
RHEV,即Red Hat Enterprise virtualization的缩写,中文全称红帽企业虚拟化