虚拟化(KVM)
虚拟化介绍:
VMware Workstation就是虚拟化虚拟化简单讲,就是把一台物理计算机虚拟成多台逻辑计算机,每个逻辑计算机里面可以运行不同的操作系统,相互不受影响,这样就可以充分利用硬件资源
关键词Hypervisor(VMM) 虚拟机管理器
虚拟化技术分为全虚拟化和半虚拟化
早期的CPU硬件不支持虚拟化,虚拟机上的操作系统(guestos)要想使用cpu资源,需要通过VMM来翻译指令,这个过程比较耗费资源,这种虚拟化技术叫做全虚拟化(VMware Workstation)
半虚拟化技术通过修改guestos内核,让guestos可以直接使用CPU资源,而不需要翻译指令了,从而节省了资源,但修改内核比较鸡肋(XEN)
后续cpu厂商直接支持虚拟化,不需要通过VMM翻译指令了,无所谓半虚拟化和全虚拟化
Centos7上安装KVM
前提是CPU支持虚拟化
虚拟机设置要求:
虚拟机设置内存2G
,增加一个50G的虚拟磁盘
,处理器2核
,虚拟化引擎两个虚拟化相关的选项打上对勾
先下载一个centos7的镜像文件
查看空闲内存
[root@kvm ~]# free
total used free shared buff/cache available
Mem: 1868684 104824 1600708 8752 163152 1603176
Swap: 2097148 0 2097148
检查cpu参数是否支持虚拟化
[root@kvm ~]# 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 syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good 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 erms invpcid avx512f rdseed adx smap clflushopt avx512cd xsaveopt xsavec xgetbv1 xsaves
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good 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 erms invpcid avx512f rdseed adx smap clflushopt avx512cd xsaveopt xsavec xgetbv1 xsaves
[root@kvm ~]# cat /proc/cpuinfo |grep svm
[root@kvm ~]#
在格式化新磁盘前先看是否添加磁盘成功
注意:添加完磁盘后要重启
查看磁盘情况
[root@kvm ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 19.5G 0 part
├─centos-root 253:0 0 17.5G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 50G 0 disk
sr0 11:0 1 1024M 0 rom
由此可知,sdb这块新磁盘已添加
格式化新磁盘
[root@kvm ~]# mkfs.ext4 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13107200 blocks
655360 blocks (5.00%) reserved for the super user
First data block=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: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@kvm ~]# blkid
/dev/sdb: UUID="100ccced-54e3-4d45-8061-e8769c22294f" TYPE="ext4"
/dev/sda1: UUID="b8a1c1b7-954b-408b-b6e4-24c330b0ca73" TYPE="xfs"
/dev/sda2: UUID="Cu8T2o-9lgu-hX2v-M2vp-Hwbf-N64t-d3LFYH" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="9c16a4ed-bb0b-4042-811b-1256636d7c72" TYPE="xfs"
/dev/mapper/centos-swap: UUID="1a91125f-a432-4c62-b44f-e6af58b2ea56" TYPE="swap"
[root@kvm ~]# blkid /dev/sdb
/dev/sdb: UUID="100ccced-54e3-4d45-8061-e8769c22294f" TYPE="ext4"
挂载
(挂载到/kvm_data目录下)
[root@kvm ~]# mkdir /kvm_data
[root@kvm ~]# mount /dev/sdb /kvm_data
[root@kvm ~]# vi /etc/fstab //永久挂载
/dev/sdb /kvm_data ext4 defaults 0 0
关闭一系列的防火墙
[root@kvm ~]# systemctl stop firewalld
[root@kvm ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@kvm ~]# iptables -nvL
Chain INPUT (policy ACCEPT 1330 packets, 97508 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 865 packets, 97027 bytes)
pkts bytes target prot opt in out source destination
[root@kvm ~]# setenforce 0
[root@kvm ~]# getenforce
Permissive
安装kvm
[root@kvm ~]# yum install -y virt-* libvirt bridge-utils qemu-img
配置网卡
配置网卡,增加桥接网卡
[root@kvm ~]# cd /etc/sysconfig/net
netconsole network network-scripts/
[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# ls
ifcfg-eno16777736 ifdown-Team ifup-post
ifcfg-lo ifdown-TeamPort ifup-ppp
ifdown ifdown-tunnel ifup-routes
ifdown-bnep ifup ifup-sit
ifdown-eth ifup-aliases ifup-Team
ifdown-ib ifup-bnep ifup-TeamPort
ifdown-ippp ifup-eth ifup-tunnel
ifdown-ipv6 ifup-ib ifup-wireless
ifdown-isdn ifup-ippp init.ipv6-global
ifdown-post ifup-ipv6 network-functions
ifdown-ppp ifup-isdn network-functions-ipv6
ifdown-routes ifup-plip
ifdown-sit ifup-plusb
设置一个br0网卡,TYPE为Bridge,IP地址设置为eno16777736的ip
eno16777736配置文件里把IP地址去掉,增加BRIDGE=br0
**增加桥接网卡br0**
[root@kvm network-scripts]# cp ifcfg-eno16777736 ifcfg-br0
[root@kvm network-scripts]# vi ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.153.20
GATEWAY=192.168.153.2
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8
[root@kvm network-scripts]# vi ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777736
DEVICE=eno16777736
ONBOOT=yes
BRIDGE=br0
[root@kvm network-scripts]# service network restart
Restarting network (via systemctl): [OK ]
检查KVM模块是否加载
[root@kvm network-scripts]# lsmod|grep kvm
kvm_intel 162153 0
kvm 525259 1 kvm_intel
[root@kvm network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000
link/ether 00:0c:29:69:61:1c brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 00:0c:29:69:61:1c brd ff:ff:ff:ff:ff:ff
inet 192.168.153.20/24 brd 192.168.153.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::7cf3:2ff:fe70:e5db/64 scope link
valid_lft forever preferred_lft forever
[root@kvm network-scripts]# systemctl restart libvirtd
[root@kvm network-scripts]# ps -ef |grep libvirtd //查看libvirtd进程
root 22738 1 0 19:47 ? 00:00:00 /usr/sbin/libvirtd
root 22896 2774 0 19:57 pts/0 00:00:00 grep --color=auto libvirtd
查看网卡详细信息
[root@kvm network-scripts]# brctl show // brctl show可以看到两个网卡br0和virbr0
bridge name bridge id STP enabled interfaces
br0 8000.000c2969611c no eno16777736
virbr0 8000.000000000000 yes
到此网卡配置完成