一、关于虚拟化
目前市场主要虚拟化厂商及产品
• VMware vSphere
• 微软 Hyper-V
• Citrix XenServer
• IBM PowerVM
• Red Hat Enterprise Virtulization
• 开源的KVM、 Xen 、VirtualBSD等。
• KVM (Kernel-Based Virtual Machine)
• 2007年10月份发布Linux 2.6.20内核第一次包含KVM
二、实验环境:
[root@kvm-server-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@kvm-server-01 ~]# uname -r
3.10.0-693.el7.x86_64
ip:192.168.198.24
[root@kvm-server02 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@kvm-server02 ~]# uname -r
3.10.0-693.el7.x86_64
ip:192.168.198.25
实验前先禁止防火墙,NetworkManager,和关闭selinux
[root@kvm-server-01 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@kvm-server-01 ~]# systemctl disable NetworkManager
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
[root@kvm-server-01 ~]# systemctl stop firewalld
[root@kvm-server-01 ~]# systemctl stop NetworkManager
编辑selinux的配置文件,把SELINUX=enforcing 的值改成 disabled 保存退出。
[root@kvm-server-01 ~]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
重启机器配置生效
[root@kvm-server-01 ~]# reboot
三、安装KVM相关软件:
1) 查看CPU是否支持KVM虚拟化。
[root@kvm-server-01 ~]# grep -E "svm | vmx " /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 ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt dtherm arat pln pts
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 ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt dtherm arat pln pts
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 ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt dtherm arat pln pts
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 ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer xsave avx f16c rdrand hypervisor lahf_lm abm epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt dtherm arat pln pts
如果无输出信息,可以进去机器BIOS里面改 Intel(R) Virtualization Technology(Disable &&Enable)选项卡的值改成Enable
2)安装相关KVM软件
[root@kvm-server-01 ~]# yum -y install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install
3)查看KVM模块是否加载
[root@kvm-server-01 ~]# lsmod | grep kvm
kvm_intel 170086 0
kvm 566340 1 kvm_intel
irqbypass 13503 1 kvm
4)查看网卡配置
[root@kvm-server-01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.198.24 netmask 255.255.255.0 broadcast 192.168.198.255
inet6 fe80::20c:29ff:fecb:f3df prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:cb:f3:df txqueuelen 1000 (Ethernet)
RX packets 74536 bytes 106116373 (101.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 30060 bytes 2494387 (2.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
5)启用并启动ibvirtd.service服务
[root@kvm-server-01 ~]# systemctl enable libvirtd.service
[root@kvm-server-01 ~]# systemctl start libvirtd.service
[root@kvm-server-01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.198.24 netmask 255.255.255.0 broadcast 192.168.198.255
inet6 fe80::20c:29ff:fecb:f3df prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:cb:f3:df txqueuelen 1000 (Ethernet)
RX packets 74734 bytes 106133307 (101.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 30169 bytes 2507305 (2.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:3b:2f:6d txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
6)我把以后要创建的虚拟放在磁盘sdb1,对应/home/KVM_data目录
[root@kvm-server-01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 160G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 159G 0 part
├─centos_pxe--server--centos7-root 253:0 0 50G 0 lvm /
├─centos_pxe--server--centos7-swap 253:1 0 2G 0 lvm [SWAP]
└─centos_pxe--server--centos7-home 253:2 0 107G 0 lvm /home
sdb 8:16 0 160G 0 disk
└─sdb1 8:17 0 160G 0 part /home/KVM_data
sr0 11:0 1 4.2G 0 rom
7)虚拟机磁盘格式,raw 、qcow2
• raw
格式是最简单,什么都没有,所以叫raw格式。连头文件都没有,就是一个直接给虚拟机进行读写的文件。raw不支持动态增长空间,必须一开始就指定空间大小。所以相当的耗费磁盘空间。
• qcow2
qcow2是集各种技术为一体的超级镜像格式,支持内部快照,加密,压缩等一系列功能,访问性能也在不断提高。但qcow2的问题就是过于臃肿,把什么功能都集于一身。
8)创建虚拟机磁盘文件
[root@kvm-server-01 ~]# cd /home/KVM_data/
[root@kvm-server-01 KVM_data]# ll
总用量 16
drwx------ 2 root root 16384 12月 14 13:44 lost+found
[root@kvm-server-01 KVM_data]# qemu-img create -f qcow2 CentOS-7.4-x86_64.qcow2 10G
Formatting 'CentOS-7.4-x86_64.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off
[root@kvm-server-01 KVM_data]# ll
总用量 216
-rw-r--r-- 1 root root 197120 12月 14 15:10 CentOS-7.4-x86_64.qcow2
drwx------ 2 root root 16384 12月 14 13:44 lost+found
9)安装centos7虚拟机的需要用到镜像,先把镜像临时挂载到/mnt/cdrom 目录
[root@kvm-server-01 ~]# mkdir /mnt/cdrom
[root@kvm-server-01 ~]# mount -t iso9660 /dev/cdrom /mnt/cdrom
10)把镜像,转换成安装虚拟机用到的iso文件
[root@kvm-server-01 KVM_data]# dd if=/dev/cdrom of=/home/KVM_data/CentOS-7.4-x86_64.iso
11)安装centos7虚拟机
[root@kvm-server-01 KVM_data]# virt-install --name CentOS-7-x86-64 --virt-type kvm --ram 1024--cdrom=/home/KVM_data/CentOS-7.4-x86_64.iso --disk path=/home/KVM_data/CentOS-7.4-x86_64.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
12)使用vnc客户端连接安装的虚拟机
13)查看虚拟机,和虚拟机使用的端口
[root@kvm-server-01 ~]# virsh list --all
Id 名称 状态
----------------------------------------------------
1 CentOS-7-x86-64 running
[root@kvm-server-01 ~]# netstat -anlp| grep 5900
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 12152/qemu-kvm
14)常用命令
a) 虚拟机配置文件,就是xml文件所在的目录是
/etc/libvirt/qemu/
b) 查看运行的虚拟机
virsh list
c) 查看所有的虚拟机(运行和关闭的虚拟机)
virsh list --all
d) 关闭虚拟机(强制关机)
virsh destroy +domain
e) 挂起虚拟机
virsh suspend +domain
f) 恢复被挂起的虚拟机
virsh resume +domain
g) 子机随宿主主机启动而启动
virsh autostart +domain
h) 取消自动启动
virsh autostart --disable +domain
i) 删除创建的虚拟机
virsh undefine +domain
j) 启动虚拟机
virsh start +domain
k) 关闭虚拟机
virsh shutdown +domain
内容太多,下节继续。~~~~