一、简介
1.1 介绍
基于内核的虚拟机 Kernel-based Virtual Machine(KVM)是内建的一种 Linux? 开源虚拟化技术。具体来说,KVM 可帮助您将 Linux 转化为虚拟机监控程序,使主机计算机能够运行多个隔离的虚拟环境,即虚拟客户机或虚拟机(VM)。
1.2 主流虚拟化架构
图1比较了几种主流虚拟化技术架构:ESXi、Xen与KVM
,它的主要区别在于它与每个组件的主要区别(CPU、内存、磁盘和网络IO)实现组件的虚拟化不同于调度管理。
- ESXi内核实现了所有虚拟化功能。
- Xen内核仅实现CPU与内存虚拟化, IO虚拟化和调度管理原因Domain0(主机上启动的第一管理VM)实现。
- KVM内核实现CPU与内存虚拟化,QEMU实现IO虚拟化,通过Linux实现进程调度器VM管理。
二、 KVM需求说明
-
假如你在虚拟机中使用它KVM,请在VMware打开虚拟化设置
VMware中宿主机建议配置2核2G以上。
-
请打开物理机器中的虚拟化BIOS每个主板的开启方式可能不同,BIOS开启方法虚拟化自己百度
注:安装在虚拟机中的注:Ubuntu系统,以下总称ubuntu20_01,
apt update # libvirt-bin 这个包分成两个包。 => libvirt-daemon-system libvirt-clients apt install qemu qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
-
service libvirtd start
三、创建KVM 虚拟机
3.1 安装Linux系统
3.1.1 传入镜像
可以使用shell也可以使用工具lrzsz这个工具
我这边使用shell起初,我想安装工具Ubuntu22,但发现自己的系统是20,发现最高支持20版的系统。
安装系统前注意静态配置IP,而且宿主机可以访问虚拟机(192).168.1.101)系统,虚拟机可以访问百度。
3.1.2 创建虚拟网卡
为什么要创建虚拟网卡,因为我们创建的虚拟机需要桥接网络NAT网络,所以宿主机不能与虚拟机中的虚拟机相匹配(Ubuntu20_01)访问。
创建的虚拟网卡需要通过buntu20_01 网络转发数据,因此网卡配置如下:
root@rion:~/tools# cat /etc/netplan/00-installer-config.yaml # This is the network config written by 'subiquity' network: ethernets: # ens33 各系统的网卡名称不同,通过ifconfig查看系统的网卡名称 ens33: dhcp4: false bridges: br0: interfaces: [ens33] dhcp4: false addresses: [192.168.1.101/24] gateway4: 192.168.1.2 nameservers: addresses: [192.168.1.2,114.114.114.114] version: 2
-
netplan apply
-
发现虚拟网卡已成功添加并设置有效IP地址,可以ping通百度。
root@rion:~/tools# ifconfig br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::1408:35ff:fe6b:956c prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ad:3e:d7 txqueuelen 1000 (Ethernet) RX packets 19 bytes 1252 (1.2 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 21 bytes 1918 (1.9 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:ad:3e:d7 txqueuelen 1000 (Ethernet) RX packets 2241784 bytes 3223246745 (3.2 GB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 380027 bytes 24712333 (24.7 MB) 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 1000 (Local Loopback) RX packets 188 bytes 15876 (15.8 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 188 bytes 15876 (15.8 KB) 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:16:f4:73 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 root@rion:~/tools# ping baidu.com PING baidu.com (220.181.38.148) 56(84) bytes of data. 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=108 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=121 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=145 ms
3.1.3 创建虚拟机
virt-install -n ubuntu20_01\ -r 2048 --vcpus=2 \ --os-variant=ubutu20.04 \
--accelerate \
-c /root/tools/ubuntu20.04-live.iso \
--disk path=/home/vmsets/ubuntu20_01.img,format=qcow2,bus=virtio,size=30 \
--network bridge=br0 \
--vnc --vncport=5996 --vnclisten=0.0.0.0
-n:指定生成的虚拟机名称,全局唯一
--os-type=linux :不需要添加了,已删除配置
-os-variant:参数支持的os # osinfo-query os 查看系统支持的系统
-c:引用的iso文件
-r:内存大小
--vcpus:系统核心数
--disk:生成的磁盘文件存储目录
--format:磁盘格式
--network:指定网络模式,及其网卡
--size:磁盘空间大小
--vnc:指定宿主机开放的监控端口和监听地址(0.0.0.0 表示监听所有的IP),可以使用vnc连接系统
--accelerate 快速生成
-
如果出现以下报错信息
ERROR internal error: process exited while connecting to monitor: 2022-07-02T06:46:48.302110Z qemu-system-x86_64: -blockdev {"driver":"file","filename":"/root/tools/ubuntu20.04-live.iso","node-name":"libvirt-1-storage","auto-read-only":true,"discard":"unmap"}: Could not open '/root/tools/ubuntu20.04-live.iso': Permission denied
修改
/etc/libvirt/qemu.conf
,去掉 user 和 group 两行前的注释
然后重启 libvirtd
service libvirtd restart
3.1.4 安装Ubuntu系统
此时,这是在Ubuntu中使用KVM创建的ubuntu20_01 系统,如果上一小节配置无误则会弹出一个系统安装界面
安装即可,记得开启ssh服务
3.1.5 修改IP地址
-
查看宿主机中的所有虚拟机情况,执行后会出现图形管理界面
virt-manager
-
开始ping 百度,可能会不同,过一下下就可以了。
四、Virsh 管理工具
在安装之后,可以通过virsh来管理我们的虚拟机,常用命令如下
-
virsh list # 查看所有的虚拟机 virsh start ubuntu20_01 # 启动虚拟机 virsh shutdown ubuntu20_01 # 关闭虚拟机 virsh destroy ubuntu20_01 # 清除Domain中的记录,同时也会强制关机 virsh edit ubuntu20_01 # 编辑虚拟机的配置文件,虚拟机中的参数是以配置文件的形式来控制的 virsh define ubuntu20_01 # 重载配置文件 virsh undefine ubuntu20_01 # 删除配置文件
-
cat /etc/libvirt/qemu/ubuntu20_01.xml
### 配置文件:cat /etc/libvirt/qemu/ubuntu20_01.xml ###
<domain type='qemu'>
<name>ubuntu20_01</name>
<uuid>3da6f3cd-4f8b-438a-b067-27b12e11f3d5</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://ubuntu.com/ubuntu/20.04"/>
</libosinfo:libosinfo>
</metadata>
##### 内存配置 ####
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
#### CPU核心数 ####
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-q35-4.2'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='none'>
<model fallback='forbid'>qemu64</model>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
#### 磁盘配置 ####
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/home/vmsets/ubuntu20_01.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='sda' bus='sata'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
......
#### 网络配置 ####
<interface type='bridge'> # NAT网络: type='network'
<mac address='52:54:00:c9:8a:62'/>
<source bridge='br0'/> # NAT网络: network='br0'
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
</interface>
.....
</devices>
</domain>
五、VNC
VNC(Virtual Network Computing)是一套实现远程网络操控的软件
那么我们为什么要使用VNC呢,细心的小伙伴应该注意到我们在安装系统时,通过--vnc --vncport=5996 --vnclisten=0.0.0.0
来开启了系统的5996端口用来被外部监听。
VNC 可以简单的理解为远程连接工具,可以实现对多台设备之间的管理,特别是对于Windows系统时,ssh无法连接,就可以使用VNC来连接Windows虚拟机。
KVM虚拟机安装后面看看有时间再出个详细教程,主要注意2个地方
-
一个是创建镜像时,指定的系统它的
--os-variant=win2k8
,通过前面的指令可以查看 -
另一个就是在安装时需要配置额外的驱动程序
-
大家百度下载VNC Viewer软件,
-
填写好宿主机的IP地址,和前面写的监听端口号
连接成功后,进入管理系统界面
参考链接:https://zhuanlan.zhihu.com/p/105499858 https://zhuanlan.zhihu.com/p/105499858