码迷,mamicode.com
首页 > 其他好文 > 详细

KVM 基本介绍

时间:2018-08-10 15:58:05      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:tcp   动态扩容   10g   monitor   function   fun   too   spec   enable   

[toc]

一、安装KVM

grep  -E  ‘vmx|svm‘  /proc/cpuinfo  #查看CPU是否支持KVM,只有支持才能做
lsmod | grep kvm  #查看CPU是否支持KVM,只有支持才能做
yum install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install -y

安装完毕通过下面命令都可以看到多出一个新的网络设备virbr0

brctl show
ifconfig

设置libvirtd服务开机启动,libvirt对虚拟机不产生任何影响,只是用来管理的

systemctl enable libvirtd.service
systemctl start libvirtd.service
systemctl status libvirtd.service

qemu-img工具创建磁盘

qemu-img create -f raw /opt/Centos-7.2-x86_64.raw 10G

创建虚拟机

需要提前准备ISO镜像

virt-install --virt-type kvm --name  Centos-7.2-x86_64 --ram 1024  --cdrom=/tmp/CentOS-7-x86_64-DVD-1511.iso  --disk /opt/Centos-7.2-x86_64.raw  --network network=default  --graphics vnc,listen=0.0.0.0 --noautoconsole 

紧接着用vncviewer工具,连接过去,然后安装虚拟机,注意关闭宿主机的防火墙和selinux
virsh vncdisplay Centos-7.2-x86_64
iptables -F
connect:宿主机:5900

选中Install CentOS7 ,按tab键,输入net.ifnames=0 biosdevname=0
分区那里不用设置swap分区。本身就是虚拟机了,再设置swap分区,效果很差.
最后一步要注意,点击reboot,它其实是关机。需要手动使用virsh命令启动

virsh常用命令

virsh --help
virsh list
virsh list --all
virsh shudown Centos-7.2-x86_64(主机名)
virsh destroy Centos-7.2-x86_64(主机名) 类似kill -9 进程号
virsh start Centos-7.2-x86_64
virsh undefine Centos-7.2-x86_64 #会删除/etc/libvirt/qemu的xml文件,可以通过dumpxml的xml恢复,前提是磁盘文件没有问题
virsh edit Centos-7.2-x86_64
virsh suspended Centos-7.2-x86_64
virsh resume Centos-7.2-x86_64
virsh dumpxml  Centos-7.2-x86_64  > Centos-7.2-x86_64.backup
virsh define Centos-7.2-x86_64.backup
virsh autostart Centos-7.2-x86_64
ps -aux |grep kvm

KVM虚拟机配置文件说明

cat /etc/libvirt/qemu/Centos-7.2-x86_64.xml 

vnc的端口是-1 表示监听再5900端口上
<graphics type=‘vnc‘ port=‘-1‘ autoport=‘yes‘ listen=‘0.0.0.0‘>

这里是最大内存和当前内存,以及cpu数量

<memory unit=‘KiB‘>4096000</memory>
<currentMemory unit=‘KiB‘>1048576</currentMemory>
<vcpu placement=‘auto‘ current=‘1‘>4</vcpu>

下面是硬盘路径和格式
<driver name=‘qemu‘ type=‘raw‘/>
<source file=‘/opt/CentOS-7.1-x86_64.raw‘/>
 

hvm表示硬件虚拟化
<type arch=‘x86_64‘ machine=‘pc-i440fx-rhel7.0.0‘>hvm</type>

二、快照

快照配置文件在/var/lib/libvirt/qemu/snapshot/虚拟机名称/下

ls /var/lib/libvirt/qemu/snapshot/Centos-7.2-x86_64/

快照命令

 virsh --help | grep snapshot
    snapshot-create                Create a snapshot from XML
    snapshot-create-as             Create a snapshot from a set of args
    snapshot-current               Get or set the current snapshot
    snapshot-delete                Delete a domain snapshot
    snapshot-dumpxml               Dump XML for a domain snapshot
    snapshot-edit                  edit XML for a snapshot
    snapshot-info                  snapshot information
    snapshot-list                  List snapshots for a domain
    snapshot-parent                Get the name of the parent of a snapshot
    snapshot-revert                Revert a domain to a snapshot

转换磁盘的格式

raw磁盘格式的虚拟机不支持快照功能,要使用镜像功能,磁盘格式必须为qcow2
转换格式时虚拟机必须先关机

qemu-img  convert -f raw  -O qcow2  /opt/Centos-7.2-x86_64.raw /opt/Centos-7.2-x86_64.qcow2
qemu-img info /opt/Centos-7.2-x86_64.qcow2

修改配置文件的disk模块,把磁盘改成最新的qcow2的这个文件,同时格式也改成qcow2的

virsh edit Centos-7.2-x86_64

 <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘/>
      <source file=‘/opt/Centos-7.2-x86_64.qcow2‘/>
      <target dev=‘vda‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x06‘ function=‘0x0‘/>
    </disk>

改好之后,检查启动是否正常

virsh start Centos-7.2-x86_64
virsh shutdown Centos-7.2-x86_64

快照备份,启动kvm和关闭的情况都可以快照

virsh snapshot-create Centos-7.2-x86_64  #每次创建一个快照之前,都可以在KVM虚拟机上touch一个文件,下面恢复快照的时候,可以检查该文件是否存在
virsh snapshot-list Centos-7.2-x86_64
virsh snapshot-create Centos-7.2-x86_64 
virsh snapshot-list Centos-7.2-x86_64
virsh snapshot-current  Centos-7.2-x86_64    # 查看当前快照

virsh snapshot-list Centos-7.2-x86_64 
 Name                 Creation Time             State
------------------------------------------------------------
 1533869463           2018-08-10 10:51:03 +0800 shutoff
 1533869964           2018-08-10 10:59:24 +0800 running
 1533869988           2018-08-10 10:59:48 +0800 running
 1533870006           2018-08-10 11:00:06 +0800 running

virsh snapshot-revert Centos-7.2-x86_64 1533870006  #恢复到某个快照
virsh snapshot-revert Centos-7.2-x86_64 1533869964
virsh snapshot-delete  Centos-7.2-x86_64 1533869502 #删除到某个快照

qemu-img info /opt/Centos-7.2-x86_64.qcow2   #qemu-img info也可以查看快照 
image: /opt/Centos-7.2-x86_64.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 2.2G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         1533869463                0 2018-08-10 10:51:03   00:00:00.000
2         1533869964             380M 2018-08-10 10:59:24   00:05:47.239
3         1533869988             380M 2018-08-10 10:59:48   00:06:07.406
4         1533870006             380M 2018-08-10 11:00:06   00:06:21.841
Format specific information:
    compat: 1.1
    lazy refcounts: false

CPU和内存动态扩容

CPU和内存的动态扩容可以通过virt-install 命令实现

virt-install --help | grep cpu

  --vcpus VCPUS         Number of vcpus to configure for your guest. Ex:
                        --vcpus 5
                        --vcpus 5,maxcpus=10,cpuset=1-4,6,8
                        --vcpus sockets=2,cores=4,threads=2,
  --cpu CPU             CPU model and features. Ex:
                        --cpu coreduo,+x2apic
                        --cpu host-passthrough
                        --cpu host

virt-install --help | grep memory
usage: virt-install --name NAME --memory MB STORAGE INSTALL [options]
  --memory MEMORY       Configure guest memory allocation. Ex:
                        --memory 1024 (in MiB)
                        --memory 512,maxmemory=1024
                        --memory 512,maxmemory=1024,hotplugmemorymax=2048,hotplugmemoryslots=2
  --memdev MEMDEV       Configure a guest memory device. Ex:
  --memtune MEMTUNE     Tune memory policy for the domain process.
  --memorybacking MEMORYBACKING
                        Set memory backing policy for the domain process. Ex:
                        --memorybacking hugepages=on

也通过修改配置文件实现,首先需要修改支持动态更改cpu和内存

<memory unit=‘KiB‘>4096000</memory>
<currentMemory unit=‘KiB‘>2097152</currentMemory>
<vcpu placement=‘auto‘ current=‘1‘>4</vcpu>

动态修改cpu

virsh setvcpus Centos-7.2-x86_64 2 --live

KVM虚拟机查看 cat /proc/cpuinfo | grep processor | wc -l
另外动态修改CPU,只能动态的添加,不能动态的减少,如果要减少可以通过关闭kvm,修改配置文件操作

virsh dominfo Centos-7.2-x86_64 | grep CPU

动态修改内存

virsh qemu-monistor-command Centos-7.2-x86_64  --hmp --cmd info balloon
virsh qemu-monitor-command Centos-7.2-x86_64  --hmp --cmd  balloon 2048
virsh dominfo Centos-7.2-x86_64| grep memory

另一种修改kvm内存的方式,另外修改内存不能超过最大内存

virsh setmem Centos-7.2-x86_64 748288

在规划是考虑到想要热添加,当时装虚拟机的时候,就要考虑这个问题,设置max内存和cpu  
关于硬盘扩容部分,虽然可以resize增大,但是有丢失数据的风险,生产中不推荐使用

KVM的网络部分

启动kvm虚拟机,会多出一个vnet0网络设备,这是虚拟启动后生成的。

ifconfig

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:2f:b1:73  txqueuelen 0  (Ethernet)
        RX packets 13  bytes 1042 (1.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5  bytes 670 (670.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

kvm虚拟机启动后,vnet0默认桥接到了virbr0上,关闭后消失
此时的kvm虚拟机出去的流量是接到virbr0上,然后经过宿主机的iptables的nat,再经过eth0出去
这就有网络瓶颈了,而且依靠iptables,如果你把iptables关闭无法上网了。同时ip地址是地址池中分配的内网地址

iptables -t nat -vnL

Chain POSTROUTING (policy ACCEPT 5 packets, 585 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    2   269 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    
    7   855 POSTROUTING_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    7   855 POSTROUTING_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    7   855 POSTROUTING_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0   

#brctl show

bridge name bridge id       STP enabled interfaces
virbr0      8000.5254002fb173   yes     vnet0

virsh net-list #查看default网络

less /etc/libvirt/qemu/networks/default.xml #查看defaut 网络配置

kvm获取的地址来源于下面

ps aux | grep dns #DNS服务
less /var/lib/libvirt/dnsmasq/default.conf #DNS配置文件

参考:https://www.cnblogs.com/nmap/p/6369180.html

KVM 基本介绍

标签:tcp   动态扩容   10g   monitor   function   fun   too   spec   enable   

原文地址:http://blog.51cto.com/395469372/2157227

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!