搭建复杂网络
搭建如下图所示网络(VAP仅作为二层交换机使用):
基于lxd容器搭建多主机网络
- 在记录一中已经创建了VAP、host1、host2节点以及相应流表,能互相ping通。现在基于记录一,再创建一个主机节点。添加host3节点,
lxc launch <template> host3
, - 创建网桥连接host3的eth0接口以及VAP的eth2接口,并进入host3节点,配置其ip地址。
lxc network create n3
lxc network attach n3 host3 eth0
lxc network attach n3 VAP eth2
#登进host3
lxc exec host3 bash
#修改配置文件
vi /etc/network/interfaces
#添加如下内容
auto eth0
iface eth0 inet static
address 192.168.10.13
netmask 255.255.255.0
#保存,重启服务
service networking restart
- 进入VAP交换机,挂载eth2端口,启动端口,配置流表。
lxc exec VAP bash
#挂载端口
ovs-vsctl add-port br0 eth3
#启动端口
ip link set eth3 up
#添加流表
ovs-ofctl add-flow br0 in_port=4,actions=all
之前只有两个主机,设置的流表为从某端口进的从某端口出,现在可以直接改为广播
例如将1端口的改为广播模式:ovs-ofctl mod-flows br0 in_port=1,actions=all
- 此时host3便可以与其它主机通信。
搭建路由网络
- 创建R1节点以及host4节点,创建过程之前讲过,这里再次写出
lxc launch <template> host4
#其中<template>为lxc中的一种镜像文件lxc image list可查看全部,若没有,需要先下载
lxc launch <template> R1
#创建网桥连接host4的eth0端口与R1的eth0端口,与上面host3步骤相同
lxc network create n4
lxc network attach n4 host4 eth0
lxc network attach n4 R1 eth0
lxc exec host4 bash
vi /etc/network/interfaces
#编辑内容,需要添加网关,即R1的eth0端口ip
auto eth0
iface eth0 inet static
address 192.168.11.11
netmask 255.255.255.0
gateway 192.168.11.253
#重启
service networking restart
#进入路由器R1,先配置端口ip,再配置rip协议
lxc exec R1 bash
vi /etc/network/interfaces
#编辑内容
auto eth0
iface eth0 inet static
address 192.168.11.253
netmask 255.255.255.0
auto eth1
iface eth1 inet static
address 192.168.10.14
netmask 255.255.255.0
service networking restart
vi /usr/local/etc/ripd.conf
#添加如下内容,表示eth0、eth1端口都参与对外通信
router rip
version 2
network eth0
network eth1
#保存后启动路由器以及rip
zebra -d
ripd -d
- 创建网桥,连接VAP的eth3端口与R1的eth1端口,过程同上
- 进入VAP,挂载eth3端口,并启动端口,添加流表,过程同上
- 此时host1、2、3能ping通路由器的eth1接口,但找不到host4,因为它们没有配置网关。
分别进入host1、2、3的配置文件vi /etc/network/interfaces
,在netmask后面一行添加网关地址即路由器eth1的ip地址:gateway 192.168.10.14。 - 此时,各主机之间都能ping通了。