kvm初体验之五:vm连接网络的两种方式:bridge和nat

1. 在安装vm时指定网络连接方式

1)bridge

virt-install --name vm1 --ram=1024 --vcpus=1 --disk path=/vm-images/vm1,size=2 --cdrom /root/iso/CentOS-6.6-x86_64-minimal.iso --network bridge:br0

2)nat

virt-install --name vm1 --ram=1024 --vcpus=1 --disk path=/vm-images/vm1,size=2 --cdrom /root/iso/CentOS-6.6-x86_64-minimal.iso --network network:default

若没有指定—network参数,默认为bridge方式

2. 在安装vm之后修改网络连接方式

修改vm网络连接方式之前先将vm关机,修改完成之后启动vm即可生效。

1)通过图形界面修改

用virt-manager打开vm的管理页面,如下图所示

nat:选择”Source
device”为Virtual
network 'default': NAT

bridge: 选择”Source
device”为Host
device eth0 (Bridge 'br0')

kvm初体验之五:vm连接网络的两种方式:bridge和nat

2)通过命令行修改

virsh edit vm1修改vm1的配置文件(vm的配置文件以xml形式存在,如vm1.xml)

<interface>选项用来指定vm的网络连接方式

nat:

<interface type='network'>

<mac address='52:54:00:eb:d7:7d'/>

<source network='default'/>

<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

</interface>

bridge:

<interface type='bridge'>

<mac address='52:54:00:eb:d7:7d'/>

<source bridge='br0'/>

<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>

</interface>

3. 新增virtual network

1)通过图形界面添加

virt-manager打开vm管理界面,选择Edit->Connection Details,在弹出的localhost Connection Details窗口的Virtual Networks选项卡中显示了当前已经存在virtual network。在host中安装了libvirt之后,会默认创建虚拟网络接口virbr0,它的forwarding方式为nat。

kvm初体验之五:vm连接网络的两种方式:bridge和nat

现在我们创建一个虚拟网络nat1,forwarding方式为nat

选择上图左框中的“+”

kvm初体验之五:vm连接网络的两种方式:bridge和nat

Forward

kvm初体验之五:vm连接网络的两种方式:bridge和nat

Forwad

kvm初体验之五:vm连接网络的两种方式:bridge和nat

Forwad

kvm初体验之五:vm连接网络的两种方式:bridge和nat

Forwad

kvm初体验之五:vm连接网络的两种方式:bridge和nat

Forwad

kvm初体验之五:vm连接网络的两种方式:bridge和nat

Finish.

Localhost Connection Details窗口中显示出了新增的nat1网络的信息。

kvm初体验之五:vm连接网络的两种方式:bridge和nat

将vm1的虚拟网络接口选为刚刚创建的nat1

kvm初体验之五:vm连接网络的两种方式:bridge和nat

将vm1先关机后启动

由下图可以看到,vm1获取到了nat1网络所配地址段中的地址。

kvm初体验之五:vm连接网络的两种方式:bridge和nat

2)通过命令行添加

参考:http://wiki.libvirt.org/page/Networking

virsh下的net-xxx命令用来管理虚拟网络

[root@tang* thm]# virsh

Welcome to virsh, the virtualization interactive terminal.

Type: 'help' for help with commands

'quit' to quit

virsh # net-

net-autostart net-define net-dumpxml net-info net-name net-undefine net-uuid

net-create net-destroy net-edit net-list net-start net-update

net-list –all列出当前已经存在的virtual
network

virsh # net-list --all

Name State Autostart Persistent

--------------------------------------------------

default active yes yes

nat1 active yes yes

virtual network的配置文件同样是以xml的形式存在,如nat1.xml

[root@tang* thm]# find / -name "nat1.xml"

/etc/libvirt/qemu/networks/nat1.xml

/etc/libvirt/qemu/networks/autostart/nat1.xml

/var/lib/libvirt/network/nat1.xml

以default网络的配置为模板创建虚拟网络nat2的配置文件

[root@tang* thm]# virsh net-dumpxml default >
/etc/libvirt/qemu/networks/nat2.xml

修改nat2.xml中参数使与已存在的虚拟网络不重合。

<network>

<name>nat2</name>

<uuid>d8d274ff-fe1f-4e88-94f9-222c1ae11c69</uuid>

<forward mode='nat'/>

<bridge name='virbr2' stp='on' delay='0' />

<mac address='52:54:00:25:90:C2'/>

<ip address='192.168.101.1' netmask='255.255.255.0'>

<dhcp>

<range start='192.168.101.2' end='192.168.101.254' />

</dhcp>

</ip>

</network>

net-define从xml文件定义虚拟网络

virsh # net-list --all

Name State Autostart Persistent

--------------------------------------------------

default active yes yes

nat1 active yes yes

virsh # net-define /etc/libvirt/qemu/networks/nat2.xml

Network nat2 defined from /etc/libvirt/qemu/networks/nat2.xml

virsh # net-list --all

Name State Autostart Persistent

--------------------------------------------------

default active yes yes

nat1 active yes yes

nat2 inactive no yes

将nat2设为自启动,并启动nat2

virsh # net-autostart nat2

Network nat2 marked as autostarted

virsh # net-start nat2

Network nat2 started

virsh # net-list --all

Name State Autostart Persistent

--------------------------------------------------

default active yes yes

nat1 active yes yes

nat2 active yes yes

将vm1的虚拟网络接口选为刚刚创建的nat2

virsh edit vm1

......

......

<interface type='network'>

<mac address='52:54:00:eb:d7:7d'/>

<source network='nat2'/>

<address type='pci' domain='0x0000' bus='0x00'
slot='0x03' function='0x0'/>

</interface>

......

......

将vm1先关机后启动

由下图可以看到,vm1获取到了nat2网络所配地址段中的地址。

[root@localhost ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 52:54:00:EB:D7:7D

inet addr:192.168.101.21 Bcast:192.168.101.255
Mask:255.255.255.0

inet6 addr: fe80::5054:ff:feeb:d77d/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:12 errors:0 dropped:0 overruns:0 frame:0

TX packets:16 errors:0 dropped:0 overruns:0
carrier:0

collisions:0 txqueuelen:1000

RX bytes:1198 (1.1 KiB) TX bytes:2178 (2.1 KiB)

Interrupt:11 Base address:0x8000

lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:65536 Metric:1

RX packets:0 errors:0 dropped:0 overruns:0 frame:0

TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

[root@localhost ~]# ping 192.168.1.1

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

64 bytes from 192.168.1.1: icmp_seq=1 ttl=63 time=5.53 ms

#

--- 192.168.1.1 ping statistics ---

1 packets transmitted, 1 received, 0% packet loss, time 926ms

rtt min/avg/max/mdev = 5.531/5.531/5.531/0.000 ms

上一篇:ylb: 触发器(Trigger)之Instead Of触发器 [注:没内容]


下一篇:kvm初体验之三:vm的安装及管理