阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

KVM安装及配置详解

273次阅读
没有评论

共计 11401 个字符,预计需要花费 29 分钟才能阅读完成。

KVM 安装

yum install kvm libvirt Python-virtinst qemu-kvm virt-viewer bridge-utils virt-install

修改网卡信息

/etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eno16777736
DEVICE=eno16777736
ONBOOT=on
#IPADDR=192.168.209.20
#NETMAKE=255.255.255.0
#GATEWAY=192.168.209.2
#DNS1=202.106.0.20
BRIDGE=br0
Vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
BROADCAST=192.168.0.255
IPADDR=192.168.1.101
NETMASK=255.255.255.0
NETWORK=192.168.1.0
ONBOOT=yes

查看网卡接口

brctl show

添加接口

brctl addbr br2

映射接口

brctl addif br2 eno16777736

/etc/init.d/libvirtd start

RedHat/CentOS6.2 x86 系统 KVM 虚拟机网络配置  http://www.linuxidc.com/Linux/2012-05/61445.htm

1.nat模式配置

KVM默认采用 nat 模式,用户网络(User Networking):让虚拟机访问主机、互联网或本地网络上的资源的简单方法,但是不能从网络或其他的客户机访问客户机。在公网 IP 不够使用 KVM 还需要上网的时候可以使用,大大节省了公网 IP!同时这种模式也使得KVM 不用暴露在公网之中,也增加了安全性。

下图是虚拟机管理模块产生的接口关系:

其中 virbr0 是由宿主机虚拟机支持模块安装时产生的虚拟网络接口,也是一个 switchbridge,负责把内容分发到各虚拟机。

从图上可以看出,虚拟接口和物理接口之间没有连接关系,所以虚拟机只能在通过虚拟的网络访问外部世界,无法从网络上定位和访问虚拟主机。

KVM 安装及配置详解

2.Bridge方式

虚拟网桥(Virtual Bridge):设置好后客户机与互联网,客户机与主机之间都可以通信,试用于需要多个公网 IP 的环境。

Bridge方式即虚拟网桥的网络连接方式,是客户机和子网里面的机器能够互相通信。可以使虚拟机成为网络中具有独立 IP 的主机。

桥接网络(也叫物理设备共享)被用作把一个物理设备复制到一台虚拟机。网桥多用作高级设置,特别是主机多个网络接口的情况。

KVM 安装及配置详解

如上图,网桥的基本原理就是创建一个桥接接口br0,在物理网卡和虚拟网络接口之间传递数据。

创建方法:

不用过多命令,只需要修改好网卡的桥接方式,配好 IP 即可,下面是范例,不用注释仔细读一遍就能看懂了!

=======kvm_server==========

[root@localhost network-scripts]# cat ifcfg-br0 
DEVICE=br0
BOOTPROTO=none
TYPE=Bridge
IPADDR=114.137.122.233
NETMASK=255.255.255.128
GATEWAY=114.137.102.139
DNS1=202.96.122.83
IPV6INIT=no
NM_CONTROLLED=yes
ONBOOT=yeSUSERCTL=no

[root@localhost network-scripts]# cat ifcfg-br1
DEVICE=br1
BOOTPROTO=none
TYPE=Bridge
PADDR=192.168.1.12
NETMASK=255.255.255.0
IPV6INIT=no
ONBOOT=yes
USERCTL=no

把虚拟机多添加一块网卡

NAT模式的网络

<interface type='network'>
      <mac address='52:54:00:31:60:b3'/>
      <source network='default'/>
      <model type='rtl8139'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>

BRIDGE模式的网络

<interface type='bridge'>
      <mac address='52:54:00:6c:be:3d'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
 </interface>

创建磁盘

qemu-img create -f qcow2 -o preallocation=metadata /home/kvm/centos65-x64-mysql.qcow2 200G

Brdge网络模式

virt-install --name=centos --ram 512 --vcpus=1 -f /home/kvm/centos65-x64-mysql.qcow2 --cdrom /ios/CentOS-6.5-x86_64-bin-DVD1.iso --graphics vnc,listen=0.0.0.0,port=5920, --network bridge=virbr0 --force --autostart

NAT网络模式

virt-install --name= openfielr--ram 512 --vcpus=1 -f /home/kvm/test.qcow2 --cdrom /opt/CentOS-6.5-x86_64-bin-DVD1.iso --graphics vnc,listen=0.0.0.0,port=5988, --network network=default, --force --autostart

查看虚拟机状态

virsh list –all

停止虚拟机

virsh destroy  openfile

启动虚拟机

virsh  start  openfielr

删除虚拟机实例

virsh  undefine  openfielr

Kvm版本

virt-install –version

Disk详细信息

qemu-img info /ios/kvm/windows7.img

克隆虚拟机

virt-clone  -o win2003 -n xp1  -f /home/data/xp1.img

-o win2003 指定哪台虚拟机背克隆

-n xp1 克隆的虚拟机的名称

-f /home/data/xp1.img 

配置文件存放的位置

创建快照

virsh snapshot-create-as centos centos.snap4

查询快照

virsh snapshot-list centos

删除快照

virsh snapshot-delete centos centos.snap4

查看物理 cpu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#virsh nodeinfo
 
CPU 型号:x86_64
 
CPU:2
 
CPU 频率:3292 MHz
 
CPU socket:1
 
每个 socket 的内核数:2
 
每个内核的线程数:1
 
NUMA 单元:1
 
内存大小:1870516 KiB

查看虚拟机配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@mysql-master kvm]# virsh dominfo centos
 
Id:            -
 
名称:centos
 
UUID:          e2c53bb2-69e1-46be-9ddd-25b3f56e3b3f
 
OS 类型:hvm
 
状态:关闭
 
CPU:1
 
最大内存:524288 KiB
 
使用的内存:0 KiB
 
持久:是
 
自动启动:启用
 
管理的保存:否
 
安全性模式:none
 
安全性 DOI:0

查看存储设备

1
virsh domblklist 60.28.198.39

Windows启动实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
virt-install --name windows --ram 512 \
 
--vcpus=1 --disk path=/data/KVM/139.220.241.53.img,cache=writeback,size=2000\
 
--network  bridge:br0 \
 
--cdrom /data/en_win_srv_2003_r2_datacenter_x64_with_sp2_vl_cd1_X13-47474.iso \
 
--accelerate  --vnc  --vncport=5921 --vnclisten=0.0.0.0 \
 
--noautoconsole --os-type  windows --hvm  --os-variant=win2k3
   
 
 
 
测试 windows7 可以安装
qemu-img create -f raw -o size=10G /ios/kvm/winds.img
virt-install --name=win2003 --ram=512 --vcpus=1 --boot=cdrom --os-type windows \
--os-variant win2k3 \
--disk path=/ios/kvm/winds.img,device=disk,cache=writeback,bus=ide  \
--cdrom=/ios/kvm/cn_windows_7_ultimate_x64_dvd_x15-66043.iso \
--network network:default,model=virtio --soundhw=ac97 --video=cirrus \
--vnc --vncport=59000 --vnclisten=0.0.0.0 --accelerate --hvm --noautoconsole

冷迁移

导出虚拟机的配置文件

[root@mysql-master ~]# virsh dumpxml centos > /ios/kvm/centos.xml
 scp /home/kvm/centos65-x64-mysql.qcow2 root@192.168.209.22:/home/kvm/

[root@mysql-master kvm]# scp centos.xml root@192.168.209.22:/etc/libvirt/qemu/

把虚拟机的配置文件和磁盘,scp 到第二台 kvm 虚拟机中

第二台机器

查看虚拟机的注册

virsh list –all

把虚拟机注册,注意(这里是我把第一台的虚拟机 scp 到第二台的虚拟机中,没有修改配置文件,正常情况下是要修改配置文件的 虚拟机的名称,id 网卡的 mac 磁盘文件存放的位置)

root@localhost ~]#virsh define /etc/libvirt/qemu/centos.xml 

root@localhost ~]#virsh list --all
[root@localhost ~]# systemctl restart libvirtd
[root@localhost ~]# virsh start centos

Windows 系统虚拟机的文件

[root@mysql-master ~]# cat /etc/libvirt/qemu/win2003.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit win2003
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>win2003</name>
  <uuid>adf84c63-60d0-43d5-878c-20f1e17ae11f</uuid>
  <memory unit='KiB'>524288</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <hyperv>
      <relaxed state='on'/>
      <vapic state='on'/>
      <spinlocks state='on' retries='8191'/>
    </hyperv>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Haswell-noTSX</model>
  </cpu>
  <clock offset='localtime'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
    <timer name='hypervclock' present='yes'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw' cache='writeback'/>
      <source file='/ios/kvm/winds.img'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/ios/kvm/cn_windows_7_ultimate_x64_dvd_x15-66043.iso'/>
      <target dev='hdb' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='1'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:72:6e:24'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='59000' autoport='no' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <sound model='ac97'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='cirrus' vram='16384' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
</domain>
[root@mysql-master ~]#

 Linux 系统虚拟机的配置文件

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit centos
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>centos</name>
  <uuid>e2c53bb2-69e1-46be-9ddd-25b3f56e3b3f</uuid>
  <memory unit='KiB'>524288</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Haswell-noTSX</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/kvm/centos65-x64-mysql.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:6c:be:3d'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <interface type='network'>
      <mac address='52:54:00:31:60:b3'/>
      <source network='net'/>
      <model type='rtl8139'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-centos/org.qemu.guest_agent.0'/>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='tablet' bus='usb'/>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='5920' autoport='no' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
  </devices>
</domain>

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133732.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计11401字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中