KVM虚拟化
一、安装所需软件
1.yum安装软件
[root@localhost ~]# yum -y install qemu-kvm qemu-kvm-tools virt-install qemu-img bridge-utils libvirt virt-manager
重新启动
[root@localhost ~]# reboot
2、查看CPU是否支持虚拟化(Intel的服务器)
AMD服务器可用cat /proc/cpumfo | grep smv命令查看
[root@localhost ~]# cat /proc/cpuinfo | grep vmx
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec
检查KVM模块是否安装
[root@localhost ~]# lsmod | grep kvm
kvm_intel 170181 0
kvm 554609 1 kvm_intel
irqbypass 13503 1 kvm
开启服务
[root@localhost ~]# systemctl start libvirtd
[root@localhost ~]# systemctl enable libvirtd
2.设置KVM网络
用户模式:即NAT方式,这种方式是默认网络,数据包由NAT方式通过主机的接口进行传送,可以访问外网,但是无法从外部访问虚拟机网络。
桥接模式:这种模式允许虚拟机像一台独立的主机一样拥有网络,外部的机器可以直接访冋到虚拟机内部,但需要网卡支持,一般有线网卡都支持。
1、设置网络桥接模式
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@localhost network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
[root@localhost network-scripts]# vim ifcfg-br0
TYPE=Bridge
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADDR=192.168.1.30
GATEWAY=192.168.1.2
PREFIX=24
DNS1=223.5.5.5
DNS2=223.6.6.6
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=br0
DEVICE=br0
ONBOOT=yes
2.重启network服务
[root@localhost ~]# /etc/init.d/network restart
Restarting network (via systemctl): [ 确定 ]
3.确认IP地址信息
[root@localhost ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.30 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::cd0f:b59d:68e1:5ffb prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:dd:1a:df txqueuelen 1000 (Ethernet)
RX packets 83 bytes 7042 (6.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 59 bytes 11171 (10.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:dd:1a:df txqueuelen 1000 (Ethernet)
RX packets 1292 bytes 113187 (110.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 690 bytes 105416 (102.9 KiB)
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 4 bytes 340 (340.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 340 (340.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:aa:1d:a1 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
3.KVM管理
1.创建存储目录
[root@localhost ~]# mkdir -p /data_kvm/store /data_kvm/iso
[root@localhost ~]# cd /data_kvm/
[root@localhost data_kvm]# ls
iso store
2、把镜像上传到服务器目录/data_kvm/iso
[root@localhost ~]# cp /dev/cdrom /data_kvm/iso/
3、启动virt-manager工具
[root@localhost ~]# virt-manager
二、使用KVM命令集管理虚拟机
1、KVM基本功能管理
1、查看KVM的配置文件存放目录
[root@localhost ~]# ls /etc/libvirt/qemu
autostart centos7.0.xml networks
2、查看虚拟机状态
[root@localhost ~]# virsh list --all
Id 名称 状态
----------------------------------------------------
1 centos7.0 running
3、强制实例系统关闭电源
[root@localhost ~]# virsh destroy centos7.0
域 centos7.0 被删除
4、通过配置文件启动虚拟机系统实例
[root@localhost ~]# virsh create /etc/libvirt/qemu/centos7.0.xml
域 centos7.0 被创建(从 /etc/libvirt/qemu/centos7.0.xml)
[root@localhost ~]# virsh list --all
Id 名称 状态
----------------------------------------------------
3 centos7.0 running
5、挂起虚拟机
[root@localhost ~]# virsh suspend centos7.0
域 centos7.0 被挂起
6、恢复虚拟机
[root@localhost ~]# virsh resume centos7.0
域 centos7.0 被重新恢复
[root@localhost ~]# virsh list --all
Id 名称 状态
----------------------------------------------------
3 centos7.0 running
7、配置虚拟机实例伴随宿主机自动启动
[root@localhost ~]# virsh autostart centos7.0
域 centos7.0标记为自动开始
8、导出虚拟机配置
[root@localhost ~]# virsh dumpxml centos7.0 > /etc/libvirt/qemu/centos7.0.xml
9、删除虚拟机
[root@localhost ~]# virsh shutdown centos7.0
域 centos7.0 被关闭
[root@localhost ~]# virsh undefine centos7.0
域 centos7.0 已经被取消定义
10、通过备份的配置文件重新定义虚拟机
[root@localhost ~]# cd /etc/libvirt/qemu/
[root@localhost qemu]# mv centos7.3.xml centos7.0.xml
[root@localhost qemu]# virsh define centos7.0.xml
定义域 centos7.0(从 centos7.0.xml)
[root@localhost qemu]# virsh list --all
Id 名称 状态
----------------------------------------------------
- centos7.0 关闭
11、修改虚拟机配置信息
通过vim命令修改
[root@localhost qemu]# vim /etc/libvirt/qemu/centos7.0.xml
通过virsh命令修改
[root@localhost qemu]# virsh edit centos7.0
2、KVM文件管理
1、查看当前磁盘格式
[root@localhost ~]# qemu-img info /data_kvm/store/admin.kvm
image: /data_kvm/store/admin.kvm
file format: qcow2
virtual size: 9.0G (9663676416 bytes)
disk size: 4.1G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
2、虚拟机克隆
[root@localhost ~]# virt-clone -o centos7.0 -n centos7.3 -f /data_kvm/store/centos7.3.qcow2
正在分配 'centos7.3.qcow2' | 9.0 GB 00:01:50
成功克隆 'centos7.3'。
[root@localhost ~]# virsh list --all
Id 名称 状态
----------------------------------------------------
- centos7.0 关闭
- centos7.3 关闭
3、虚拟机快照
[root@localhost ~]# virsh snapshot-create centos7.0
已生成域快照 1590591281
查看快照信息
[root@localhost ~]# virsh snapshot-list centos7.0
名称 生成时间 状态
------------------------------------------------------------
1590591281 2020-05-27 22:54:41 +0800 running
恢复虚拟机状态,查看虚拟机快照版本信息
[root@localhost data_kvm]# virsh snapshot-revert centos7.0 1590591726
[root@localhost data_kvm]# virsh snapshot-current centos7.0
<domainsnapshot>
<name>1590591726</name>
<state>running</state>
<parent>
<name>1590591281</name>
</parent>
删除快照
[root@localhost data_kvm]# virsh snapshot-delete centos7.0 1590591726
已删除域快照 1590591726