设为首页 收藏本站
查看: 1759|回复: 0

[经验分享] Master KVM(一)

[复制链接]
累计签到:161 天
连续签到:1 天
发表于 2017-5-5 16:09:57 | 显示全部楼层 |阅读模式
1.KVM is a set of kernel modules for an x86 architecture hardware with virtualization extensions(KVM是一组用于实现虚拟化扩展的内核模块)
  内核模块组件:kvm-intel.ko, kvm-amd.ko
2.使用kvm能对X86的内核架构虚拟化扩展,但仍需要QEMU来真实虚拟出来的硬件外围设备
3.QEMU(Quick Emulator):QEMU使用动态翻译技术在宿主机上执行虚拟机命令,然而这种翻译方式很慢
4.qemu-kvm直接与kvm模块交互,并能够通过VM直接在CPU上执行指令不再需要动态翻译,因此我们使用的都是qemu-kvm
5.libvirt用来管理qemu-kvm,而qemu-kvm模拟运行KVM虚拟机
6.libvirt是一组在终端用户与监视器之间的一组API库,libvirt通过接收用户命令对虚拟机进行修改或是操作监视器(hypervisor),libvirt是后端管理层。
  前端管理的工具有oVirt ,RHEV(Red Hat Enterprise Virtualization),OpenStack等等。libvirt可以用来管理存储,网络和管理虚拟硬件。
7.virsh start TestVM:启动虚拟机(virsh是libvirt用来libvirt服务交互的前端命令行,virt-manager是全段管理工具)
8.虚拟机安装前要求:
  grep --color -Ew 'svm|vmx|lm' /proc/cpuinfo
(The svm flag means that the CPU has AMD-V, vmx flag means that the CPU has VT-x,
and lm means a 64-bit support)
9.lsmod|grep kvm #检查kvm模块是否加载
10.modprobe kvm kvm_intel(modprobe kvm kvm_amd)
11.安装kvm:
  yum install qemu-kvm libvirt virt-install virt-manager -y(yum groupinstall "virtualization" -y)
  yum groupinfo "virtualization"
12.启动libvirt服务,libvirt提供API接口与qemu-kvm交互,客户端可通过virsh或是virt-manager来对虚拟机的生命周期进行管理。
  systemctl enable libvirtd && systemctl start libvirtd
13.检查KVM的配置信息:
  virt-host-validate
14.硬件虚拟化使得KVM虚拟机可以直接访问硬件CPU,提高系统的性能。
        [iyunv@localhost-localdomain ~]# virt-host-validate
          QEMU: Checking for hardware virtualization                                 : PASS
          QEMU: Checking for device /dev/kvm                                         : PASS
          QEMU: Checking for device /dev/vhost-net                                   : PASS
          QEMU: Checking for device /dev/net/tun                                     : PASS
           LXC: Checking for Linux >= 2.6.26                                         : PASS
15.dev/kvm:没有该设备将不能访问硬件
   /dev/vhost-net:没有该设备将会降低虚拟网络的性能
   /dev/net/tun:创建/tun/tap设备加速网络连通性
16.virsh(virtualization shell):虚拟shell,用来管理VM和监视器
17.virsh管理命令分类:
        Guest management commands (for example start, stop)
        Guest monitoring commands (for example memstat, cpustat)
        Host and hypervisors commands (for example capabilities, nodeinfo)
        Virtual networking commands (for example net-list, net-define)
        Storage management commands (for example pool-list, pool-define)
        Snapshot commands (create-snapshot-as)
18.virsh nodeinfo                #显示节点的系统资源信息
        CPU model: x86_64
        CPU(s): 4
        CPU frequency: 2534 MHz
        CPU socket(s): 1
        Core(s) per socket: 2
        Thread(s) per core: 2
        NUMA cell(s): 1
        Memory size: 7967796 KiB
19.The virt-manager application is a Python-based desktop user interface for managing virtual machines through libvirt
20.Alt + F2 and it will then display the dialog box of virt-manager.
   polkit是用来验证非root用户访问virt-manager的系统框架(rule file: /etc/polkit-1/rules.d)
21.Virtual Networks:
   NATed:
        That means the VMs can communicate with the outside network
        based on the network connectivity available on the host but none of the outside
        entities will be able to communicate with the VMs
   Routed:
        A routed virtual network allows the connection of virtual machines directly to the
        physical network. Here VMs will send out packets to the outside network based on
        the routing rules set on the hypervisor.
   Isolated:
        provides a private network between the hypervisor and
        the virtual machines.

   1.virsh net-list --all #list the virtual networks. --all is used to list both active and inactive virtual networks.
     Autostart: shows whether the virtual network should be started when you activate the libvirtd service.
   2.virsh net-info default #显示网络的详细信息
    [iyunv@localhost-localdomain rules.d]# virsh net-info default
        Name            default
        UUID            5903e242-6b1f-411c-b352-85957c7ace03
        Active:         yes
        Persistent:     yes
        Autostart:      yes
        Bridge:         virbr0
  3.virsh net-dumpxml default #显示网络的配置文件信息
                [iyunv@localhost-localdomain rules.d]# virsh net-dumpxml default  (显示虚拟网络配置文件的xml信息(/etc/libvirt/qemu/networks/default.xml))
                <network>
                  <name>default</name>
                  <uuid>5903e242-6b1f-411c-b352-85957c7ace03</uuid>
                  <forward mode='nat'>
                        <nat>
                          <port start='1024' end='65535'/>
                        </nat>
                  </forward>
                  <bridge name='virbr0' stp='on' delay='0' />
                  <ip address='192.168.122.1' netmask='255.255.255.0'>
                        <dhcp>
                          <range start='192.168.122.2' end='192.168.122.254' />
                        </dhcp>
                  </ip>
                </network>
  4.虚拟网络配置文件的路径: /etc/libvirt/qemu/networks/default.xml
  5.停止/开启虚拟网络 virsh net-destroy default(virsh net-start default)
  6.存储池是用来保存虚拟机磁盘映像
  7.存储池的文件所在路径:/var/lib/libvirt/images
  8.Allocate entire disk now, to choose a disk allocation method; deselecting this button will result in a thin-provisioned disk and selecting it will result in
        a thick-provisioned disk
22.虚拟网络
   可以把bridge看成是虚拟交换机,bridge维护和存储MAC表。(IP与接口映射关系的MAC表)
   接口通常是brigde的网络接口,这些特殊的网络接口被称为TAP设备
   可以把TAP设备看做是网络电缆在虚拟机与bridge之间传输分组
   TUN(tunnel) layer 3 IP
   TAP(network tap) layer 2 Enternet frames
   TUN is used with routing, while TAP is used to create a network bridge
23.创建bridge并绑定TAP设备
        1.lsmod |grep bridge   #检查bridge组件是否安装
        2.brctl addbr tester   #添加bridge
        3.brctl show      #查看bridge
        4.ip link show tester #查看桥设备
        5.lsmod |greptun #查看TUN/TAP设备模块是否加载至内核
        6.ip tuntap add dev vm-vnic mode tap   #添加tap设备
        7.ip link show vm-vnic #显示vm-vnic设备信息
        8.brctl addif tester vm-vnic #添加tap设备至bridge
        9.brctl show    #查看桥设备
    ********************************************************
        1.brctl delif tester vm-vnic
        2.brctl show tester
        3.ip tuntap del vm-vnic mode tap
        4.brctl delbr tester
        brctl --help #查看帮助
        ********************************************************
24.Isolated virtual network(只有虚拟机之间可互通)
   1.virt-manager 创建isolated网络,该网络可看做虚拟机之间的交换机
   2.为isolated中的虚拟机添加虚拟网卡

   virsh创建isolated网络:
        1.创建isolated.xml
                cat isolated.xml
                <network>
                        <name>isolated</name>
                </network>
        2.virsh net-define isolated.xml
        3.virsh net-list --all #查看可用网络
        4.virsh net-dumpxml isolated #查看生成的配置文件
                [iyunv@localhost-localdomain zhanglei]# virsh net-dumpxml isolated
                <network connections='4'>
                  <name>isolated</name>
                  <uuid>2bd84ec3-2993-4dca-9900-61ea2b3d493f</uuid>
                  <bridge name='virbr1' stp='on' delay='0' />
                  <mac address='52:54:00:fa:c5:b4'/>
                  <domain name='isolated'/>
                </network>
        5.virsh net-autostart isolated #设定libvirtd服务启动之后自启动网络
        6.vrish net-start isolated        #手动启动isolated网络服务
        7.virsh domiflist node2    #查看某一节点网卡的信息
                [iyunv@localhost-localdomain zhanglei]# virsh domiflist node1
                Interface  Type       Source     Model       MAC
                -------------------------------------------------------
                vnet0      network    isolated   virtio      52:54:00:fe:68:96
                vnet2      network    isolated   virtio      52:54:00:ac:2c:f9
        8.virsh attah-interface --domain node2 --source isolated --type network --model virtio --config --live
        (--config: This will make the change persistent in the next startup of the VM.
        --live: This will inform libvirt that you are attaching the NIC to a live
        virtual machine. Remove --live if the virtual machine is not running)
        9.brctl show virbr1
        10.virsh detach-interface --domain node2 --type network --mac 52:54:00***** --config --live


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373513-1-1.html 上篇帖子: KVM官方文档 内核级虚拟化技术 下篇帖子: KVM虚拟化(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表