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

[经验分享] kvm安装及配置

[复制链接]

尚未签到

发表于 2017-6-24 06:14:05 | 显示全部楼层 |阅读模式
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
1.nat模式配置
KVM默认采用nat模式,用户网络(User Networking):让虚拟机访问主机、互联网或本地网络上的资源的简单方法,但是不能从网络或其他的客户机访问客户机。在公网IP不够使用KVM还需要上网的时候可以使用,大大节省了公网IP!同时这种模式也使得KVM不用暴露在公网之中,也增加了安全性。
下图是虚拟机管理模块产生的接口关系:

其中virbr0是由宿主机虚拟机支持模块安装时产生的虚拟网络接口,也是一个switch和bridge,负责把内容分发到各虚拟机。
从图上可以看出,虚拟接口和物理接口之间没有连接关系,所以虚拟机只能在通过虚拟的网络访问外部世界,无法从网络上定位和访问虚拟主机。
DSC0000.png





2.Bridge方式
虚拟网桥(Virtual Bridge):设置好后客户机与互联网,客户机与主机之间都可以通信,试用于需要多个公网IP的环境。
Bridge方式即虚拟网桥的网络连接方式,是客户机和子网里面的机器能够互相通信。可以使虚拟机成为网络中具有独立IP的主机。
桥接网络(也叫物理设备共享)被用作把一个物理设备复制到一台虚拟机。网桥多用作高级设置,特别是主机多个网络接口的情况。
DSC0001.png

如上图,网桥的基本原理就是创建一个桥接接口br0,在物理网卡和虚拟网络接口之间传递数据。
创建方法:
不用过多命令,只需要修改好网卡的桥接方式,配好IP即可,下面是范例,不用注释仔细读一遍就能看懂了!



=======kvm_server==========
[iyunv@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
[iyunv@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




#virsh nodeinfo
CPU 型号:        x86_64
CPU:               2
CPU 频率:        3292 MHz
CPU socket:        1
每个 socket 的内核数: 2
每个内核的线程数: 1
NUMA 单元:       1
内存大小:      1870516 KiB




查看虚拟机配置



[iyunv@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





查看存储设备



virsh domblklist 60.28.198.39



Windows启动实例




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

  冷迁移
  导出虚拟机的配置文件



[iyunv@mysql-master ~]# virsh dumpxml centos > /ios/kvm/centos.xml


scp /home/kvm/centos65-x64-mysql.qcow2 root@192.168.209.22:/home/kvm/
[iyunv@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
[iyunv@localhost ~]# systemctl restart libvirtd
[iyunv@localhost ~]# virsh start centos
  windows系统虚拟机的文件


DSC0002.gif DSC0003.gif


[iyunv@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>
[iyunv@mysql-master ~]#
View Code  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>
View Code  为虚拟机添加磁盘



[iyunv@cobbler kickstarts]#virsh edit centos6.5
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/var/lib/libvirt/images/test_add.img'/>
<target dev='vdb' bus='virtio'/>
</disk>
  创建网卡



virsh asttach-interface centos --ytpe network --source default --config
virsh asttach-interface centos --ytpe  bridge   --source br0 --config
  查看网卡信息



virsh domiflist centos

运维网声明 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-387422-1-1.html 上篇帖子: kvm虚拟化关闭虚拟网卡virbr0的方法 下篇帖子: KVM的ept机制
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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