这里我们用vmware模拟操作系统,然在VMware上搭建KVM
KVM安装
一般企业的机器安装的系统直接是Centos,我们电脑安装的是Windows,所以我们用VMware模拟Linux操作系统
安装一个Centos机器,内存和磁盘空间要给大一点
我们安装的时候不要最小化安装,选择gnome桌面安装,开启机器,打开终端
下载安装我们用到的软件
在所谓的kvm技术中,应用到的其实有2个东西:qemu+kvm
kvm负责cpu虚拟化+内存虚拟化,实现了cpu和内存的虚拟化,但kvm不能模拟其他设备;
qemu是模拟IO设备(网卡,磁盘),kvm加上qemu之后就能实现真正意义上服务器虚拟化。
因为用到了上面两个东西,所以一般都称之为qemu-kvm。
libvirt则是调用kvm虚拟化技术的接口用于管理的,用libvirt管理方便,直接用qemu-kvm的接口太繁琐。
[root@kvm ~]# yum install qemu-kvm libvirt virt-manager librbd1-devel -y
刚打开机器,它会默认在下载一下初始化软件,导致会出现以下情况
这种情况我们可以等待安装完即可,也可以直接根据进程ID直接杀死进程不影响后面操作
启动服务
[root@kvm ~]# systemctl start libvirtd
[root@kvm ~]# systemctl enable libvirtd
查看kvm模块加载
[root@kvm ~]# lsmod | grep kvm
kvm_intel 170086 0
kvm 566340 1 kvm_intel
一般看到这两行说明支持kvm模块
KVM安装一般有三种方式这里我们分别讲解
一、完全文本方式安转虚拟机
下载vsftp
[root@kvm ~]# yum -y install vsftpd
上传镜像到虚拟机上,直接找到镜像,把他拖到VM上就会自动上传
会显示一个路径,我们先把镜像拷贝到/root目录下
我们要创建一个镜像的挂载目录,然后把镜像挂载上去,我们是挂载在ftp下的
[root@kvm ~]# mkdir /var/ftp/centos7u4
[root@kvm ~]# mount CentOS-7-x86_64-DVD-1708.iso /var/ftp/centos7u4
mount: /dev/loop0 写保护,将以只读方式挂载
一定要关闭防火墙,然后开启vsftp
[root@kvm ~]# systemctl stop firewalld
[root@kvm ~]# setenforce 0
[root@kvm ~]# systemctl start vsftpd
下载virt-install
[root@kvm ~]# yum -y install virt-install
开始安装,敲回车之后不要动,等待即可
[root@kvm ~]# virt-install --connect qemu:///system -n test1 -r 2050 --disk path=/var/lib/libvirt/images/test1.qcow2,size=3 --os-type=linux --os-variant=centos7.0 --vcpus=1 --location=ftp://192.168.126.170/centos7u4 -x console=ttyS0 --nographics
qemu:///system 系统如果在裸露的金属内核上运行为根(需要KVM安装)
-n name
-r 以M为单位指定分配给虚拟机的内存大小
--disk 指定作为客户机存储的媒介 size以G为单位的存储
--os-type 针对一类操作系统优化虚拟机配置
--os-variant 针对特定操作系统变体进一步优化虚拟机配置
--vcpus 指定核数
--location 客户虚拟机kernel+initrd 安装源,必须为镜像挂载在ftp目录下
-x console=ttyS0 执行终端
--nographics 无图形,文本模式
ftp的ip一定是要自己的ip
完成之后是这样的
输入2,进入文本安装模式
我们一定要保证每个数字后面都是感叹号 ,9可以不是
1.改语言 输入1 ,回车,在回车,输入68,回车
输入1,简体中文
2. 输入2,回车,输入1,回车,输入2选亚洲时区,输入64,shanghai,回车
输入5,一直输入c 最小化安装
输入8,给root设置密码
输入b开始安装
按回车继续
安装完成
退出之后我们使用命令打开虚拟机管理,在宿主机终端输入命令virt-manger
二、KVM gustos图形方式部署安装虚拟机
图形化安装跟我们VMware安装一样
先打开虚拟机管理器
分配内存,存储空间,cpu
后面正常安装即可
三、镜像模版+配置文件方式安装虚拟机
记住两个目录:
/etc/libvirt/qemu 虚拟机配置文件存放目录
/var/lib/libvirt/images/ 虚拟机磁盘镜像(磁盘文件)存放目录
虚拟机创建好之后会在宿主机的目录内产生两个文件,我们就是拷贝这两个文件,修改内容,实现安装新的虚拟机。
1.拷贝模版镜像和配置文件
[root@kvm ~]# cp /var/lib/libvirt/images/test1.qcow2 /var/lib/libvirt/images/test3.qcow2
[root@kvm ~]# cp /etc/libvirt/qemu/test1.xml /etc/libvirt/qemu/test3.xml
[root@kvm ~]# ls /etc/libvirt/qemu/
networks test1.xml test2.xml test3.xml
[root@kvm ~]# ls /var/lib/libvirt/images/
test1.qcow2 test2.img test3.qcow2
2.修改配置文件
[root@kvm ~]# vim /etc/libvirt/qemu/test3.xml
<domain type='kvm'>
<name>test3</name> #此处要修改
<uuid>87ea370e-3be1-477d-bbed-269d2a033088</uuid> #此处要修改,改个数字即可
<memory unit='KiB'>2099200</memory>
<currentMemory unit='KiB'>2099200</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Broadwell-noTSX-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</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/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/test3.qcow2'/> #磁盘名称要修改
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:7f:7a:bf'/> #mac地址要修改,修改一个字母即可
<source network='default'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
define一下配置文件
[root@kvm ~]# virsh define /etc/libvirt/qemu/test3.xml
打开虚拟机管理器可以看到创建成功