KVM是Kernel-based Virtual Machine的简称,是一个开源的虚拟化模块,今天我将在CentOS7的操作系统上安装KVM,以下是我的安装步骤.
一.环境信息
系统: CentOS 7.2 IP: 10.0.0.12/24
在虚拟机安装时,需要启用CPU的虚拟化功能
二. KVM安装步骤
1. 安装qemu-kvm和libvirt
[root@kvm-node1 ~]# yum install -y qemu-kvm libvirt ###qemu-kvm用来创建虚拟机硬盘,libvirt用来管理虚拟机
2. 安装virt-install
[root@kvm-node1 ~]# yum install -y virt-install ###用来创建虚拟机
3. 启动libvirtd,并将它设为开机启动,启动后使用ifconfig查看,发现会多出来一块virbr0的网卡,ip默认为192.168.122.1/24,说明libvirtd启动成功,如果默认没有ifconfig命令,使用yum install -y net-tools安装
[root@kvm-node1 ~]# systemctl start libvirtd && systemctl enable libvirtd [root@kvm-node1 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.12 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::5505:5aa:3f96:f3f8 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:51:09:70 txqueuelen 1000 (Ethernet) RX packets 22459 bytes 29147955 (27.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5161 bytes 587757 (573.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 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:fb:b1:55 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
经过以上三步,KVM安装成功,下面开始使用KVM创建虚拟机.
三. 使用KVM创建虚拟机(CentOS7)
1. 使用qemu命令创建一个10G的硬盘(最小10,G,可以更多),硬盘的名称为: CentOS-7-x86_64.raw
[root@kvm-node1 ~]# qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G Formatting '/opt/CentOS-7-x86_64.raw', fmt=raw size=10737418240 [root@kvm-node1 ~]# ll -h /opt total 0 -rw-r--r-- 1 root root 10G Aug 22 07:59 CentOS-7-x86_64.raw
2. 使用virt-install创建名称为CentOS-7-x86_64的虚拟机,在创建之前,先上传一个CentOS7的ISO镜像,最好在根目录下创建一个ISO的目录文件夹
[root@kvm-node1 ~]# virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/ISO/CentOS-7-x86_64-Everything-1611.iso --disk path=/opt/CentOS-7-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole Starting install... Creating domain... | 0 B 00:03 Domain installation still in progress. You can reconnect to the console to complete the installation process.
这时候使用TightVNC工具,连接主机IP 10.0.0.12,设置安装操作系统的网卡名称为eth0,如图所示
安装步骤和我们平时安装操作系统的方法一样,安装完成后,可以使用virsh list --all显示KVM上所有的虚拟机
[root@kvm-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - CentOS-7-x86_64 shut off ### 可以看到虚拟机的名称和状态
四. 管理KVM
1. 使用virsh启动KVM中的虚拟机
[root@kvm-node1 ~]# virsh start CentOS-7-x86_64 Domain CentOS-7-x86_64 started
启动后,使用VNC连接工具,登录后可以看到网卡的名称为eth0,默认没有ip地址,是因为网卡没有激活,将ONBOOT改为yes,重启网卡,可以看到ip地址为192.168.122.0网段的
当KVM中的虚拟机启动后,可以在/etc/libvirt/qemu目录下,看到启动的虚拟机的信息
[root@kvm-node1 ~]# cd /etc/libvirt/qemu [root@kvm-node1 qemu]# pwd /etc/libvirt/qemu [root@kvm-node1 qemu]# ls -lh total 4.0K -rw------- 1 root root 3.8K Aug 22 08:18 CentOS-7-x86_64.xml drwx------ 3 root root 42 Aug 22 07:51 networks
CentOS-7-x86_64.xml就是启动的虚拟机的信息,当我们使用vim编辑器查看时,会提示如下信息:
<!-- WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh edit CentOS-7-x86_64 or other application using the libvirt API. -->
看到提示需要修改虚拟机的信息,需要使用命令 virsh edit CentOS-7-x86_64,是用这条命令后,我们可以看到很多虚拟机的信息,包括vcpu,memory等
2. CPU热添加(KVM支持CPU的热添加,不过局限是宿主机和虚拟机的系统相同才行,这里宿主机是CentOS7,虚拟机也是CentOS7,所以可以模拟CPU热添加,不过真实生产环境不建议这样操作)
[root@kvm-node1 ~]# virsh edit CentOS-7-x86_64 6 <vcpu placement='static'>1</vcpu> 修改为: <vcpu placement='auto' current="1">4</vcpu> ##意思是当前cpu为1核,最多可以有4核,修改完虚拟机的配置文件,必须关闭虚拟机再启动,配置才能生效
[root@kvm-node1 ~]# virsh shutdown CentOS-7-x86_64 ##关闭虚拟机
Domain CentOS-7-x86_64 is being shutdown
[root@kvm-node1 ~]# virsh list --all ##查看虚拟机状态
Id Name State
----------------------------------------------------
- CentOS-7-x86_64 shut off
[root@kvm-node1 ~]# virsh start CentOS-7-x86_64 ##启动虚拟机
Domain CentOS-7-x86_64 started
[root@kvm-node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
3 CentOS-7-x86_64 running
先通过VNC工具查看虚拟机的cpu信息
在宿主机上通过命令添加核数
[root@kvm-node1 ~]# virsh setvcpus CentOS-7-x86_64 2 --live ##虚拟机的cpu添加到2core
再通过VNC工具查看虚拟机CPU信息
3. 内存热添加(还是不建议生产中使用,知道功能就行了)
通过VNC连接虚拟机,查看虚拟机内存
[root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon ## 查看当前虚拟机的内存 balloon: actual=1024 [root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd balloon 512 ## 修改当前虚拟机的内存为512 [root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon ## 查看修改的内存信息已经改变成了512 balloon: actual=512
使用VNC工具连接虚拟机,查看内存是否有变:
4. 网卡配置(如何将KVM虚拟机中的网卡改为桥接模式,让局域网的其他用户可以正常访问)
[root@kvm-node1 ~]# brctl show ##查看已有网桥 bridge name bridge id STP enabled interfaces virbr0 8000.525400fbb155 yes virbr0-nic vnet0
创建一个脚本,删除ens33网卡的ip信息,新建一个桥接网卡,并且将ens33网卡的ip信息富裕新建的桥接网卡
[root@kvm-node1 ~]# vim br0.sh #!/bin/bash brctl addbr br0 #创建一个桥接网卡br0 brctl addif br0 ens33 #将ens33网卡的桥接信息转到br0网卡 ip addr del dev ens33 10.0.0.12/24 #将ens33网卡的ip信息删除 ifconfig br0 10.0.0.12/24 up #将ens33网卡的ip信息这只到br0网卡上 route add default gw 10.0.0.2 #添加网关
[root@kvm-node1 ~]# chmod u+x br0.sh
[root@kvm-node1 ~]# ./br0.sh
使用ifocnfig查看网卡的ip信息,可以看到br0的地址已经变为了10.0.0.12/24
[root@kvm-node1 ~]# ifconfig br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.12 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::20c:29ff:fe51:970 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:51:09:70 txqueuelen 1000 (Ethernet) RX packets 553 bytes 41723 (40.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 680 bytes 63511 (62.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::5505:5aa:3f96:f3f8 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:51:09:70 txqueuelen 1000 (Ethernet) RX packets 6107998 bytes 8708309251 (8.1 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 534420 bytes 68906654 (65.7 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=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:fb:b1:55 txqueuelen 1000 (Ethernet) RX packets 6685 bytes 278745 (272.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6981 bytes 34351624 (32.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::fc54:ff:fe6b:5fee prefixlen 64 scopeid 0x20<link> ether fe:54:00:6b:5f:ee txqueuelen 1000 (Ethernet) RX packets 16 bytes 1650 (1.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 987 bytes 52236 (51.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用命令修改KVM虚拟机的信息
[root@kvm-node1 ~]# virsh edit CentOS-7-x86_64 69 <interface type='network'> 修改为: <interface type='bridge'> ##桥接 71 <source network='default'/> 修改为: <source bridge='br0'/>
修改完成后关闭虚拟机,启动虚拟机,通过VNC工具修改ip为10.0.0.14/24,尝试ping宿主机是否可以ping通,并通过ssh连接
[root@kvm-node1 ~]# virsh shutdown CentOS-7-x86_64 Domain CentOS-7-x86_64 is being shutdown [root@kvm-node1 ~]# virsh start CentOS-7-x86_64 Domain CentOS-7-x86_64 started
通过本地的ssh软件,尝试连接,可以连接,并且可以看到IP信息
以上就是CentOS7中KVM的安装和简单管理.