KVM虚拟化的特性
嵌入到linux正式kernel,提供兼容性
代理级资源调用,×××能
虚拟机就是一个进程,内存易于管理
可以实现服务器虚拟化 桌面虚拟化 应用虚拟化
下面开始安装和配置管理KVM
查看系统版本、内核版本
[root@kvm ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@kvm ~]# uname -r
3.10.0-327.el7.x86_64
查看系统是否支持KVM虚拟化
开启cpu虚拟化才可以查看上面支持虚拟化的标志
kvm安装
[root@kvm ~]# yum install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install -y
qemu-kvm是用户态的一个程序
创建一台虚拟机--磁盘
[root@kvm ~]# qemu-img create -f raw /opt/CentOS-7.2-x86_64.raw 10G
Formatting '/opt/CentOS-7.2-x86_64.raw', fmt=raw size=10737418240
[root@kvm ~]# lsmod |grep kvm
kvm_intel 162153 0
kvm 525259 1 kvm_intel
添加到开机启动
[root@kvm ~]# systemctl enable libvirtd.service
启动
[root@kvm ~]# systemctl start libvirtd.service
[root@kvm ~]# systemctl status libvirtd.service
a— libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running)
安装一台虚拟机
创建虚拟机的镜像--iso
[root@kvm ~]# dd if=/dev/cdrom of=/opt/CentOS-7.2.iso
8456192+0 records in
8456192+0 records out
4329570304 bytes (4.3 GB) copied, 222.565 s, 19.5 MB/s
开始安装
[root@kvm ~]# virt-install --help
[root@kvm ~]# virt-install --name CentOS-7.2-x86_64 --virt-type kvm --ram 1024 --cdrom=/opt/CentOS-7.2.iso --disk path=/opt/CentOS-7.2-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
[root@kvm ~]# ps -aux |grep kvm
root 802 0.0 0.0 0 0 ? S< 07:16 0:00 [kvm-irqfd-clean]
qemu 3349 17.4 80.2 1490228 702752 ? Sl 07:46 5:53 /usr/libexec/qemu-kvm
[root@kvm ~]# netstat -lnptu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 3349/qemu-kvm
创建虚拟机后,使用vnc链接 开始安装KVM虚拟机
192.168.1.111:5900
然后启动kvm
[root@kvm ~]# virsh start CentOS-7.2-x86_64
Domain CentOS-7.2-x86_64 started
[root@kvm ~]# virsh list
Id Name State
----------------------------------------------------
2 CentOS-7.2-x86_64 running
KVM使用
[root@kvm ~]# cd /etc/libvirt/qemu/
[root@kvm qemu]# ls
CentOS-7.2-x86_64.xml networks
CentOS-7.2-x86_64.xml #虚拟机的配置文件
[root@kvm ~]# virsh list
Id Name State
----------------------------------------------------
[root@kvm ~]# virsh list --all #查看所有的虚拟机
Id Name State
----------------------------------------------------
- CentOS-7.2-x86_64 shut off
[root@kvm ~]# ifconfig virbr0 安装kvm后会产生一个virbr0的网卡
virbr0: flags=4099
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 00:00:00:00:00:00 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1 bytes 42 (42.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@kvm ~]# iptables -t nat -nvL 这里可以看到这个虚拟机安装完后是可以通信的,通过nat上网
Chain POSTROUTING (policy ACCEPT 5 packets, 268 bytes)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- * * 192.168.122.0/24 224.0.0.0/24
0 0 RETURN all -- * * 192.168.122.0/24 255.255.255.255
0 0 MASQUERADE tcp -- * * 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
0 0 MASQUERADE udp -- * * 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535
0 0 MASQUERADE all -- * * 192.168.122.0/24 !192.168.122.0/24
生产是将kvm的网卡改成物理机的桥接网卡上网,跟物理机同一个网段
vnc连接进去后把他的网卡设置为开机启动,然后重启网卡,此时ping百度也是通的
ONBOOT=yes #这样子kvm网卡就可以开机启动dhcp获取到一个ip地址
[root@kvm ~]# ps -aux |grep dns #kvm是通过dnsmasq分配的一个ip地址
nobody 2820 0.0 0.0 15544 344 ? S 07:16 0:00 /sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root 2822 0.0 0.0 15516 4 ? S 07:16 0:00 /sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root 7913 0.3 0.1 112648 952 pts/0 S+ 09:57 0:00 grep --color=auto dns
打开dnsmasq的配置文件,可以看到他分配的ip地址
[root@kvm ~]# cat /var/lib/libvirt/dnsmasq/default.conf
## dnsmasq conf file created by libvirt
strict-order
pid-file=/var/run/libvirt/network/default.pid
except-interface=lo
bind-dynamic
interface=virbr0
dhcp-range=192.168.122.2,192.168.122.254
dhcp-no-override
dhcp-lease-max=253
dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile
addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
管理KVM的网卡
#网卡,手动设置为桥接(生产用最多)
brctl命令需要bridge-utils安装这个包
[root@kvm ~]# ifconfig
eth0: flags=4163
inet 192.168.1.111 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe8a:4320 prefixlen 64 scopeid 0x20
ether 00:0c:29:8a:43:20 txqueuelen 1000 (Ethernet)
RX packets 72557 bytes 13501818 (12.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 83261 bytes 28375563 (27.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4163
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether fe:54:00:5d:62:67 txqueuelen 0 (Ethernet)
RX packets 2263 bytes 124594 (121.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3480 bytes 9022739 (8.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@kvm ~]# brctl addbr br0 #添加一个网桥
[root@kvm ~]# brctl show #查看添加的网卡
bridge name bridge id STP enabled interfaces
br0 8000.000000000000 no
virbr0 8000.fe54005d6267 yes vnet0
[root@kvm ~]# brctl addif br0 eth0 #把br0加入到eth0,这样子我连接的物理本机的网络就断了如果单独执行这个操作的话
[root@kvm ~]# brctl addif br0 eth0 && ip addr del dev eth0 192.168.1.111/24 && ifconfig br0 192.168.1.111/24 up && route add default gw 192.168.1.1 && iptables -F
##注释
ip addr del dev eth0 192.168.1.111/24 #删除eth0的ip
ifconfig br0 192.168.1.111/24 up #这样子就可以连接br0这个网卡的ip了,下面需要添加路由才可以连接
route default gw 192.168.1.1 添加路由才可以连接
设置为桥接后变成
[root@kvm ~]# ifconfig
br0: flags=4163
inet 192.168.1.111 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe8a:4320 prefixlen 64 scopeid 0x20
ether 00:0c:29:8a:43:20 txqueuelen 0 (Ethernet)
RX packets 50 bytes 3203 (3.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 75 bytes 5699 (5.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163
inet6 fe80::20c:29ff:fe8a:4320 prefixlen 64 scopeid 0x20
ether 00:0c:29:8a:43:20 txqueuelen 1000 (Ethernet)
RX packets 73572 bytes 13571497 (12.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 85141 bytes 28514610 (27.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4163
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether fe:54:00:5d:62:67 txqueuelen 0 (Ethernet)
RX packets 2263 bytes 124594 (121.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3480 bytes 9022739 (8.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
kvm常用操作
virsh edit CentOS-7.2-x86_64 #编辑虚拟机配置文件
virsh start CentOS-7.2-x86_64 #启动
virsh shutdown CentOS-7.2-x86_64 #关机
virsh destroy CentOS-7.2-x86_64 #直接关闭电源
virsh suspend CentOS-7.2-x86_64 #挂起虚拟机
virsh resume CentOS-7.2-x86_64 #恢复
virsh list #查看
virsh undefine CentOS-7.2-x86_64 #永久删除虚拟机