28.4 kvm介绍

28.5 Centos7上安装KVM

28.6 配置网卡

28.7 创建虚拟机安装CentOS7

 

 

 

28.4 kvm介绍

 

 

KVM是以色列初创公司Qumranet开发,2008年9月RedHat公司收购了Qumranet

KVM是Linux内核的一个模块,它把Linux内核变成了一个Hypervisor

#linux内核就是一个一个的模块组成,很多功能

KVM是完全开源的,RedHat基于KVM的虚拟化解决方案叫做RHEV

KVM在Linux操作系统里面以进程的形式出现,由标准的Linux调度程序进行调度,这使得KVM能够使用Linux内核的已有功能

只有一个KVM内核模块还不能实现虚拟化的全部功能,就好比操作系统只有内核还不能成为一个完整的操作系统一样

QEMU是一个开源的虚拟化软件,纯软件,可以虚拟化所以的硬件,性能不强

KVM基于QEMU开发了一个能够运行在用户空间的工具QEMU-KVM

磁盘、网络设备等都是通过QEMU-KVM这个工具模拟出来的

KVM和QEMU-KVM通信是通过/dev/kvm实现的

libvirt是用来管理KVM虚拟机的API,其命令为virsh

 

 

 

 

 

kvm作为linux内核的模块在内核空间,利用/dev/kvm与用户空间通信。Qemu-kvm虚拟出虚拟硬件,在这基础上,我们安装GuestOS(虚拟机上的操作系统)

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

28.5 Centos7上安装KVM

 

 

 

实验是在vmware虚拟机里的,相当于在虚拟机里(vmware)安装虚拟机(kvm)

前提是CPU支持虚拟化

1.虚拟机设置内存2G,增加一个50G的虚拟磁盘,处理器2核,虚拟化引擎两个虚拟化相关的选项打上对勾

2.关闭iptables或者firewalld

3.关闭selinux

4.格式化新磁盘,挂载到/kvm_data

5.下载一个centos7的镜像文件

6.检查cpu参数是否支持虚拟化

grep -Ei 'vmx|svm' /proc/cpuinfo

7.安装kvm

yum install -y virt-* libvirt bridge-utils qemu-img

 

 

实例:

先将虚拟机关机,调整硬件资源

 

 

 

 

 

 

[root@axinlinux-01 ~]# free -h

total        used        free      shared  buff/cache   available
Mem:           1.8G        1.4G        214M        9.8M        207M        236M
Swap:          4.0G        264K        4.0G

[root@axinlinux-01 ~]# cat /proc/cpuinfo

processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 78
model name	: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
stepping	: 3
microcode	: 0x9e
cpu MHz		: 2400.250
cache size	: 3072 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
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 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 aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp
bogomips	: 4800.50
clflush size	: 64
cache_alignment	: 64
address sizes	: 42 bits physical, 48 bits virtual
power management:

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 78
model name	: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
stepping	: 3
microcode	: 0x9e
cpu MHz		: 2400.250
cache size	: 3072 KB
physical id	: 2
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 2
initial apicid	: 2
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
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 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 aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp
bogomips	: 4800.50
clflush size	: 64
cache_alignment	: 64
address sizes	: 42 bits physical, 48 bits virtual
power management:

[root@axinlinux-01 ~]# grep -Ei 'vmx|svm' /proc/cpuinfo #有无vmx的字样,代表cpu是否支持虚拟化。vmx和svm分别代表因特尔芯片或arm芯片

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 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 aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp
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 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 aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb tpr_shadow vnmi ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp

[root@axinlinux-01 ~]# fdisk -l #查看一下我们新配置的虚拟磁盘

磁盘 /dev/sda:85.9 GB, 85899345920 字节,167772160 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000e482f

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     8800255     4194304   82  Linux swap / Solaris
/dev/sda3         8800256   167772159    79485952   83  Linux

磁盘 /dev/sdb:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

 

[root@axinlinux-01 ~]# mkfs.ext4 /dev/sdb #直接格式化成ext4,可以不必分区

mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13107200 blocks
655360 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2162163712
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

[root@axinlinux-01 ~]# blkid /dev/sdb #查看这块磁盘

/dev/sdb: UUID="ea87b8ad-8973-4a8b-b686-11901a9637cc" TYPE="ext4"

 

[root@axinlinux-01 ~]# mkdir /kvm_data #创建目录
[root@axinlinux-01 ~]# mount /dev/sdb /kvm_data/ #挂载磁盘到这个目录
[root@axinlinux-01 ~]# vim /etc/fstab #编辑/etc/fstab,开机直接挂载新增加的硬盘
/dev/sdb /kvm_data ext4 defaults 0 0
[root@axinlinux-01 ~]# systemctl stop firewalld
[root@axinlinux-01 ~]# systemctl stop iptables
[root@axinlinux-01 ~]# systemctl disable firewalld
[root@axinlinux-01 ~]# systemctl disable iptables
[root@axinlinux-01 ~]# iptables -F #如果还有的话,就清空掉
[root@axinlinux-01 ~]# iptables -nvL #随着kvm的使用或启动,会产生kvm的规则
Chain INPUT (policy ACCEPT 28 packets, 2148 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 8 packets, 660 bytes)
 pkts bytes target     prot opt in     out     source               destination

[root@axinlinux-01 ~]# yum install -y virt-* libvirt bridge-utils qemu-img #安装kvm

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

 

28.6 配置网卡

 

 

可选择桥接或NAT,亦或者其他

1.配置网卡,增加桥接网卡

先要虚拟一个桥接网卡出来,这个桥接网卡就是提供你的虚拟机和宿主机(物理机),相互连接起来的中间桥梁

2.设置一个br0网卡,TYPE为Bridge,IP地址设置为ens33的ip

ens33配置文件里把IP地址去掉,增加BRIDGE=br0

3.检查KVM模块是否加载

lsmod|grep kvm

4.启动libvirtd服务

systemctl start libvirtd

brctl show可以看到两个网卡br0和virbr0

 

 

 

实例:

[root@axinlinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@axinlinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@axinlinux-01 network-scripts]# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.193.128
GATEWAY=192.168.193.2
DNS1=114.114.114.114
DNS2=8.8.8.8
PREFIX=24
[root@axinlinux-01 network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
[root@axinlinux-01 network-scripts]# systemctl restart network.service
[root@axinlinux-01 network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.193.128  netmask 255.255.255.0  broadcast 192.168.193.255
        inet6 fe80::20c:29ff:fe32:cee6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:32:ce:e6  txqueuelen 1000  (Ethernet)
        RX packets 204  bytes 17222 (16.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 63  bytes 11182 (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:32:ce:e6  txqueuelen 1000  (Ethernet)
        RX packets 51103  bytes 60556873 (57.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19889  bytes 1819897 (1.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 1000  (Local Loopback)
        RX packets 34  bytes 2768 (2.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 34  bytes 2768 (2.7 KiB)
        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:8d:04: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

[root@axinlinux-02 network-scripts]# systemctl start libvirtd #axin到这一步报错,libvirtd服务起不来,换了台虚拟机就好了。报错信息是这样的“Job for libvirtd.service failed because the control process exited with error code. See "systemctl status libvirtd.service" and "journalctl -xe" for details.”

[root@axinlinux-02 ~]# ps aux |grep libvirt

nobody     1708  0.0  0.0  53852  1108 ?        S    13:49   0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root       1709  0.0  0.0  53824   376 ?        S    13:49   0:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root       2699  0.0  0.8 943392 16304 ?        Ssl  14:29   0:00 /usr/sbin/libvirtd
root       4918  0.0  0.0 112720   984 pts/0    S+   15:44   0:00 grep --color=auto libvirt

[root@axinlinux-02 ~]# brctl show #查看桥接网卡的

bridge name	bridge id		STP enabled	interfaces
br0		8000.000c2932cee6	no		ens33
virbr0		8000.5254008d0455	yes		virbr0-nic

virbro是libvirtd服务所启动的桥接网卡,类似于VM ware的vmnet8的nat的桥接网卡

在kvm没法用鼠标点选择桥接还是nat,因为是命令行的。他的网络类型是哪一个,取决于目标桥接网卡是哪一个。如果是br0就是桥接模式,virbr0就是NAT模式

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

 

28.7 创建虚拟机安装CentOS7

 

 

 

 

首先要有centos7的镜像文件

命令行安装centos7

virt-install --name=aminglinux01 --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-17081.iso --disk path=/kvm_data/aminglinux01.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"

--name 定义你虚拟机的名字

--memory 定义它的内存大小,就是安装完就是512

maxmemory 定义它的最大内存多大,就是可调整到1024(支持热调整)

--vcpus 定义cpu核数

maxvcpus 最大cpu为2核

--os-type 操作类型是linux

--os-variant 版本是rhel7(centos7)

--location 指定ISO文件的路径(可以是http:xxxxx,也就是可以在线安装)

--disk path 虚拟硬盘所在路径,包括名字、大小

--bridge 桥接网卡名字

--graphics 是否开启图形

--console 定义开启的终端、属性

--extra-args 也是终端的属性

 

 

实例:

 

 

Installation
 
1) [x] Language settings 2) [!] Time settings
(English (United States)) (Timezone is not set.)
3) [!] Installation source 4) [!] Software selection
(Processing...) (Processing...)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:
#按1 语言选择 回车 68 回车 1回车
#按2 时区 回车 1 回车 64(shanghai) 回车
#按5 分区 回车 1 回车 c(继续) 回车 c 回车 2(后面x代表默认) 回车 c 回车 1(标准分区) 回车 c 回车
#按8 root密码 wangxin789 回车 两遍
完成设置后,按b+回车
Installation
 
1) [x] Language settings 2) [x] Time settings
(Simplified Chinese (China)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [x] Installation Destination 6) [x] Kdump
(Automatic partitioning (Kdump is enabled)
selected) 8) [x] Root password
7) [ ] Network configuration (Password is set.)
(Not connected)
9) [ ] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:

以上为设置完成后的,x代表为已设置的

最后按ctrl+],退出终端就可以了。到这一步,阿铭的宿主机(vmware虚拟机)也跟着重启了,阿鑫做的时候没有出现重启的现象。所以,在上面的设置完成以后,不要按回车退出,而是按ctrl+]退出终端,然后再针对axinlinux-02这一台虚拟机重启