KVM杩�绉�

����杩�绉伙��疯�绉伙�
瀵逛�����杩�绉伙�浣���浠ュ�ㄥ�夸富�轰�淇�海洋�涓�涓�疏浚�����疏浚㈡�锋�洪����蹇��э��跺���ㄥ�夸富�轰腑�抽����������璇ュ�㈡�锋�虎��跺��广大�疏浚㈡�锋�雾��������浠跺��跺�板��涓��板�夸富�轰腑锛�浣跨�ㄥ�ㄦ�涓绘�轰腑���ㄨ�ュ�㈡�锋�烘�剁���戒护�ュ���ㄥ��惰��ョ��������

�ㄦ��杩�绉伙���杩�绉伙�
濡���疏浚夸富�哄������疏浚夸富�哄�变韩瀛��ㄧ郴缁�锛�������瑕���杩�缃�缁�����疏浚㈡�锋�雾��vCPU�ц��舵��锛���海洋�涓�����疏浚癸����鸿�惧����舵���扮演����涓绘�轰����变韩瀛�绯荤������舵����������雾��������浠剁��褰����ㄤ�涓��变韩��海洋��ㄤ�����

�ㄥ�轰��变韩瀛��ㄧ郴缁��讹�KVM�ㄦ��杩�绉荤���蜂�杩�绋�涓猴�

  • 1��杩�绉诲�彭��讹�疏浚㈡�锋�轰��跺�ㄥ�夸富�轰�杩�琛�锛�涓�姝ゅ���讹�疏浚㈡�锋�雾����海洋�椤佃�浼�杈��扮演����涓绘�轰���
  • 2��QEMU/KVM 浼����у苟璁板�涓�杩�绉昏�绋�涓�����宸茶�浼�杈�����海洋�椤电��浠讳�淇��戊寅�骞跺�ㄦ������海洋�椤甸�戒�杈�疏浚������冲�彭�浼�杈��ㄥ���㈣�绋�涓���海洋�椤电���存�瑰��疏浚广��
  • 3��QEMU/KVM 浼�浼拌�¤�绉昏�绋�涓���浼�杈���搴�锛�褰��╀�����海洋��版�����藉��ㄤ�涓���浠ヨ�惧�����堕�村�ㄦ��锛�榛�璁� 30 姣�绉�锛���浼�杈�疏浚����讹�QEMU/KVM 浼��抽��婧�疏浚夸富�轰���疏浚㈡�锋�虎���广大��╀����版����浼�杈��扮演����涓绘�轰�锛�����浼�杈�杩��ョ����海洋���疏浚瑰�ㄧ����疏浚夸富�轰��㈠�疏浚㈡�锋�雾��杩�琛��舵����
  • 4���虫�わ�KVM ���ㄦ��杩�绉绘��浣�广大卞����河渠���杩�绉诲����疏浚㈡�锋�哄敖���戒�杩�绉诲��涓��达��ら������涓绘�轰�缂哄�涓�河渠���缃�锛�姣�濡�缃�妗ョ����

    娉ㄦ��锛�褰�疏浚㈡�锋�轰腑��海洋�浣跨�ㄧ����甯稿ぇ��涓�淇��归�绻��讹���海洋�涓��版��涓���琚�淇��圭����搴�澶т�KVM�藉�浼�杈�����海洋���搴��讹��ㄦ��杩�绉荤��杩�绋���疏浚���涓�河渠���锛�杩��跺�����介����杩�绉汇��

  • 杩�绉绘敞��河渠�椤癸�
    1����濂借�绉荤�����″��cpu����涓���
    2��64浣����藉��64浣�疏浚夸富�洪�磋�绉伙�32浣���浠ヨ�绉�32浣���64浣�疏浚夸富��
    3��疏浚夸富�轰腑�������哄��海洋�涓��藉�茬��
    4������疏浚夸富�哄��婧�疏浚夸富�鸿蒋浠堕��缃�广大藉���界���稿��锛�濡� ���稿����妗ユ�ョ��★�璧�婧�姹�绛���
    5��涓ゅ�拌�绉荤��涓绘�� cat /proc/cpuinfo |grep nx ��璁剧疆���稿����.

    涓�锛�����杩�绉�

    ��澧���杩股�

    涓绘�哄�� IP�板�� ��浣�绯荤�
    kvm01
    172.16.1.30
    centos7
    kvm02
    172.16.1.40
    centos7

    1锛���澶�宸ヤ�
    1锛���kvm01��kvm02涓绘�轰�疏浚�瑁�kvm

    ��澧�瑕�姹�锛�
    1��濡������╃�����″��锛���瑕���BIOS涓���寮����������斤�Virtualization Technology锛�锛�涓��������″�ㄩ�璁ゆ����寮�杩�涓����界����
    2��濡�������VMware Workstation��疏浚�楠�锛�寤虹���������雾��CPU瑕��鹃�����������斤�杩��疯�����烘��浼�����KVM������

    #楠�璇�cpu��������������锛�
    kvm�风���舵��杩�绉�

    #疏浚�瑁�������潘�浠讹�
    [root@kvm01 ~]# yum -y install qemu-kvm qemu-kvm-tools qemu-img libvirt virt-install virt-manager  bridge-utils

    ��宸ュ�山����浣���锛�

    qemu-kvm  //KVM�稿�妯″��
    qemu-kvm-tools //KVM璋�璇�宸ュ�凤������╂�у��瑁�
    qemu-img  //��寤猴�潘��㈠��淇��归����锛��藉���琚�qemu���������������煎�
    libvirt //绠$�������雾��宸ュ�山��
    virt-install  //libvirt搴���寤烘�拌�����雾���戒护琛�宸ュ��
    virt-manager //�惧舰�����㈢�$��������
     bridge-utils //��缃�linux浠ュお缃�妗�

    #濡�������广大���疏浚�瑁�锛���杩���瑕�疏浚�瑁�GNOME妗��㈢��澧� yum -y groupinstall "GNOME 妗���" .

     #����libvirtd宸ュ�凤�
     [root@kvm01 ~]# systemctl start libvirtd
    [root@kvm01 ~]# systemctl status libvirtd

    kvm�风���舵��杩�绉�
    kvm02涓绘�烘��浣�涓�涓�杈圭�稿����

    2锛���寤轰�涓������虎��e氨��寤�kvm01涓绘�轰�锛�

    #��寤哄���剧�����浠跺��centos��������褰�锛�
    [root@kvm01 ~]# mkdir /kvm-vm
    [root@kvm01 ~]# mkdir /iso
    [root@kvm01 ~]# ls /iso/
    CentOS-7-x86_64-DVD-1708.iso    #涓�浼�centos����

    #kvm�戒护琛�疏浚�瑁�锛�

    [root@kvm01 ~]# virt-install --os-type=linux --os-variant centos7.0 --name web01 --ram 1024 --vcpus 1 --disk /kvm-vm/web01.qcow2,format=qcow2,size=10 --location /iso/CentOS-7-x86_64-DVD-1708.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
    
    Starting install...
    Retrieving file .treeinfo...                                                       |  354 B  00:00:00     
    Retrieving file vmlinuz...                                                         | 5.6 MB  00:00:00     
    Retrieving file initrd.img...                                                      |  46 MB  00:00:00     
    Allocating 'web01.qcow2'                                                           |  10 GB  00:00:00     
    Domain installation still in progress. You can reconnect to 
    the console to complete the installation process.

    ���拌В��锛�

    ��杩�virt-install�戒护��寤烘�拌�����哄苟杩�琛�疏浚�瑁�
     --os-type= linux  #绯荤�绫诲��涓�linux
     --os-variant=  #��疏浚���浣�绯荤�����   
     --name  #��疏浚������哄��绉�
     --ram   #浠�MB涓哄��浣�涓哄�㈡�风��河渠�浠跺��������海洋�
     --vcpus  #��缃������雾������CPU(vcpu)�伴��
     --disk   #��疏浚������雾��海洋��ㄤ�缃�锛�纾�����浠剁���煎���澶у�锛���浣�GB锛�
     --location  #���╁��瑁�婧�
     --network   #��缃�疏浚㈡�风�缁��ュ��
     --graphics #��缃������烘�剧ず璁剧疆锛�vnc锛�
     listen=0.0.0.0  #琛ㄧず����缃�娈甸�藉��浠ヨ��ヨ�ヨ������
     --noautoconsole #涓�绠℃�����ㄧ讲����锛��ц�疏浚��戒����虎�濡���涓�����璇�锛�广大�浼�涓��村�″�ㄧ�绔�

    疏浚�瑁�������锛��ㄥ�戒护琛�杈���virt-manager��寮�kvm�惧舰��绠$������锛�濡�涓���绀猴�
    kvm�风���舵��杩�绉�
    �瑰�昏��ュ��瑁�centos绯荤���疏浚�瑁�疏浚�����锛��ョ�������雾��涓�涓��舵��:

    [root@kvm01 ~]# virsh list --all
     Id    Name                           State
    ---------------------------------------------------- 
     -     web01                          shut off     #疏浚�瑁�疏浚�����锛�榛�璁や����抽���舵��

    ��缃�kvm�����轰负妗ユ�ョ�缁�锛�浣垮�惰�藉�璁块��澶�缃�锛�锛�

    #�抽��疏浚夸富�雾��缃�缁��у�跺�起�
    [root@kvm01 ~]# systemctl stop NetworkManager
    #�ㄥ�夸富�轰���寤�br0锛�妗ユ�ョ��★�锛�
    [root@kvm01 ~]# virsh iface-bridge ens33 br0
    Created bridge br0 with attached device ens33
    Bridge interface br0 started
    #淇��瑰��岛�����缃���浠讹�
    [root@kvm01 ~]# virsh edit web01 
         69     <interface type='bridge'>    #淇��圭��$把��涓烘ˉ��
         70       <mac address='52:54:00:dc:0f:d0'/>
         71       <source bridge='br0'/>   #��疏浚�妗ユ�ョ���
    
    #���ㄥ��岛�锛�
    [root@kvm01 ~]# virsh start web01 
    Domain web01 started
    
    [root@kvm01 ~]# virsh list 
     Id    Name                           State
    ----------------------------------------------------
     2     web01                          running

    ��缃�web01涓绘�雾��缃�缁���缃�淇℃��锛�骞舵�璇�ping��澶�缃�锛�
    kvm�风���舵��杩�绉�

    2锛�����杩�绉�
    ����杩�绉讳富瑕���涓轰袱涓�姝ラ��:
    1锛��疯�������浠跺�������洪��缃���浠�
    2锛����板��涔�姝よ������

    1锛��抽������璧疯�����虎�
    [root@kvm01 ~]# virsh shutdown web01 
    Domain web01 is being shutdown
    [root@kvm01 ~]# virsh list 
     Id    Name                           State
    ----------------------------------------------------

    2锛��疯�������浠跺����缃���浠惰��kvm02涓绘�虎�
    #�ㄦ�疯�涔�������澶�濂�kvm02涓绘�轰�����澧�锛���寰�寰�浼����讳慨�归��缃���浠跺�瑰������起�杆塔荤��锛���

    [root@kvm02 ~]# mkdir /kvm-vm   //��寤哄�瑰���������浠剁��褰�
    [root@kvm02 ~]# systemctl stop NetworkManager  
    [root@kvm02 ~]# virsh iface-bridge ens33 br0   //��寤烘ˉ�ョ���
    Created bridge br0 with attached device ens33
    Bridge interface br0 started
    #�疯�锛�
    [root@kvm01 ~]# scp /kvm-vm/web01.qcow2 172.16.1.40:/kvm-vm/
    [root@kvm01 ~]# scp /etc/libvirt/qemu/web01.xml  172.16.1.40:/etc/libvirt/qemu/

    3锛���kvm02涓绘�轰�疏浚�涔�璇ヨ�����虎�

    [root@kvm02 ~]# virsh define /etc/libvirt/qemu/web01.xml 
    Domain web01 defined from /etc/libvirt/qemu/web01.xml
    
    #���ㄨ�����虎�
    [root@kvm02 ~]# virsh start web01 
    Domain web01 started
    
    [root@kvm02 ~]# virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     1     web01                          running
    
    [root@kvm02 ~]# 

    ����疏浚�����杩�绉汇������

    河渠�锛��ㄦ��杩�绉伙���杩�绉伙�

    �ㄦ��杩�绉讳富瑕���涓�5涓�姝ラ�わ�
    1锛���寤哄�变韩瀛���
    2锛�涓ゅ�版�哄�ㄦ��潘藉�变韩瀛���锛����ㄦ��潘斤�浣跨�ㄨ�婧�姹�锛�
    3锛����ㄥ�ㄦ��杩�绉�
    4锛���寤鸿�绉诲���������洪��缃���浠�
    5锛����板��涔�������

    ��澧���杩股�

    涓绘�哄�� IP�板�� ��浣�绯荤�
    nfs
    172.16.1. 10
    centos7
    kvm01
    172.16.1.30
    centos7
    kvm02
    172.16.1.40
    centos7
    1锛�绗�涓�姝ワ��抽������涓绘�雾��selinux锛�涓�疏浚�瑕��规划����茬��澧�锛�
    [root@kvm01 ~]# vim /etc/selinux/config
    SELINUX=disabled
    [root@kvm01 ~]# reboot  #��������
    
    [root@kvm01 ~]# systemctl stop firewalld     

    娉ㄦ��锛�涓�瑕�浣跨��setenforce 0锛�涓存�跺�抽��锛�锛�����杩�绉绘�惰�浼��ラ����

    2锛��ㄧ讲nfs锛���nfs���″�ㄤ�锛�

    [root@nfs ~]# yum -y install nfs-utils
    [root@nfs ~]# vim /etc/exports
    /nfs-share 172.16.1.0/24(rw,sync,no_root_squash)
    [root@nfs ~]# mkdir /nfs-share
    [root@nfs ~]# systemctl start rpcbind
    [root@nfs ~]# systemctl start nfs-server
    [root@nfs ~]# systemctl enable nfs-server

    #kvm01��kvm02娴�璇������藉�杩�琛�杩�绋���潘�

    [root@kvm01 ~]# showmount -e 172.16.1.10
    Export list for 172.16.1.10:
    /nfs-share 172.16.1.0/24
    [root@kvm02 ~]# showmount -e 172.16.1.10
    Export list for 172.16.1.10:
    /nfs-share 172.16.1.0/24

    3锛�璁剧疆涓绘�哄��锛�/etc/hosts 河渠��歌В��锛�浠ュ��涓ゅ��kvm涓绘�洪��缃���瀵��诲�

    [root@kvm01 ~]# vim /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    172.16.1.10 nfs
    172.16.1.30 kvm01
    172.16.1.40 kvm02
    #�疯�缁��朵�涓绘�虎�
    [root@kvm01 ~]# for i in 10 40; do scp /etc/hosts 172.16.1.$i:/etc/; done
    #��缃���瀵��诲�锛�
    [root@kvm01 ~]# ssh-keygen -t rsa
    [root@kvm01 ~]# ssh-copy-id kvm02
    [root@kvm01 ~]# ssh kvm02 hostname
    kvm02

    4锛�涓や锅kvm涓绘�鸿�琛���潘藉�变韩瀛���锛�

    #��瑕�广大�kvm01涓���������浠讹��疯���nfs���变韩��褰�涓�锛�
    [root@kvm01 ~]# scp /kvm-vm/web01.qcow2  nfs:/nfs-share
    [root@nfs ~]# ls /nfs-share/
    web01.qcow2
    kvm01锛�  
    [root@kvm01 ~]# vim /etc/fstab    
    //娣诲��浠ヤ���疏浚癸�
    172.16.1.10:/nfs-share          /kvm-vm                 nfs     defaults        0 0
    [root@kvm01 ~]# mount -a     //���板��潘�
    [root@kvm01 ~]# ls /kvm-vm/
    web01.qcow2
    
    kvm02锛�
    [root@kvm02 ~]# mkdir /kvm-vm
    [root@kvm02 ~]# vim /etc/fstab
    172.16.1.10:/nfs-share          /kvm-vm                 nfs     defaults        0 0
    [root@kvm02 ~]# mount -a
    [root@kvm02 ~]# ls /kvm-vm/
    web01.qcow2
    #��寤烘ˉ�ョ�缁�锛�
    [root@kvm02 ~]# systemctl stop NetworkManager
    [root@kvm02 ~]# virsh iface-bridge ens33 br0 
    Created bridge br0 with attached device ens33
    Bridge interface br0 started

    4锛�杩�琛��ㄦ��杩�绉伙�
    #�ㄥ�ㄦ��杩�绉讳���锛�妯℃��web01�����烘�e父宸ヤ�涓�锛�
    kvm�风���舵��杩�绉�

    kvm��岛�河渠�杩�绉诲伐��migrate锛�
    [root@kvm01 ~]# virsh migrate --live --unsafe --verbose web01 qemu+ssh://172.16.1.40/system
    Migration: [100 %]     #杩�绉绘����

    ���拌В��:

    --live #��杩�绉�
    --unsafe #�充娇涓�疏浚��ㄤ�瑕�寮哄�惰�绉�
    --verbose #�剧ず杩�绉昏�绋�
    web01��琛ㄧず褰���������锛������虎�锛���杈瑰����������涓绘�雾���板����

    #杩�绉绘������锛���kvm02涓绘�轰��ョ�������雾�舵��锛�

    [root@kvm02 ~]# virsh list 
     Id    Name                           State
    ----------------------------------------------------
     2     web01                          running

    ��浠ョ���拌�����雾���舵��涓鸿�琛��舵����

    #�诲������虎��ョ��宸ヤ��舵������涓��存��锛�
    kvm�风���舵��杩�绉�

    ��浠ョ���伴��杩���杩�绉伙������轰�涓��翠���宸ヤ�锛�骞朵�浼�涓㈠け�版��锛�骞朵�QEMU/KVM 浼��抽��婧�疏浚夸富�轰���疏浚㈡�锋�虎���广大��╀����版����浼�杈��扮演����涓绘�轰����虫�わ�杩�绉荤����宸茬�疏浚��颁���

    �ㄧ��河渠х��澧�涓�锛��ヤ��ユ��浠���浠ラ���╀�涓��������堕�存�ラ���板��涔������虎�

    #广大������雾����缃���浠舵�疯��虫�版�轰�锛�����宸卞��寤洪��缃���浠讹�锛�
    [root@kvm01 ~]# scp /etc/libvirt/qemu/web01.xml kvm02:/etc/libvirt/qemu/
    web01.xml                                                               100% 4047     4.0KB/s   00:00   
    [root@kvm02 ~]# ls /etc/libvirt/qemu  #��web02涓��ョ��
    networks  web01.xml
    #���板��涔������虎�
    [root@kvm02 ~]# virsh shutdown web01 
    Domain web01 is being shutdown
    
    [root@kvm02 ~]# virsh define /etc/libvirt/qemu/web01.xml 
    Domain web01 defined from /etc/libvirt/qemu/web01.xml
    
    [root@kvm02 ~]# virsh start web01   #���ㄨ������
    Domain web01 started
    
    [root@kvm02 ~]# virsh list --all
     Id    Name                           State
    ----------------------------------------------------
     3     web01                          running    
    
    [root@kvm02 ~]# 

    �虫�わ���杩�绉诲伐浣�缁�������

    甯歌���璇�锛�

    1��杩�绉绘�堕���扮演����璇���杩股�
    # virsh migrate centos --live qemu+ssh://192.168.30.132/system
    error: unable to connect to server at 'KVM-2:49152': No route to host��������
    ����锛�浣�����瀵��诲�娌℃������
    瑙e�虫�规�锛����板����瀵��诲��冲��
    2��杩�绉绘�剁��海洋��ㄩ��璇�锛�
    # virsh migrate centos --live qemu+ssh://192.168.30.132/system
     error: Failed to open file '/mnt/CentOS6.8.qcow2': Input/output error
    ��������锛�海洋��ㄦ病魔����潘芥����
    ����瑙e�虫�规�锛�mount -o remount /dev/sdb /mnt
    3��杩�绉绘��FQDN��璇�锛�
    # virsh migrate centos --live qemu+ssh://192.168.30.132/system
     error: internal error hostname on destination resolved to localhost, but migration requires an FQDN
    ��������锛�涓ゅ�板�夸富�烘��娉�瑙f��涓绘�哄��
    ����瑙e�虫�规�锛����伴��缃�涓绘�哄����ip��瑙f��
    4��杩�绉绘�惰��娉���璇�锛�
    # virsh migrate centos --live qemu+ssh://192.168.30.132:/system
    error: internal error Unable to parse URI qemu+ssh://192.168.30.132:/system
    ��������锛�qemu+ssh璇�娉�����河渠�
    ����瑙e�虫�规�锛�姝g‘��搴�璇ユ��锛�virsh migrate centos --live qemu+ssh://192.168.30.132/system