基于lxd容器搭建路由器

网络拓扑结构如下:
基于lxd容器搭建路由器
自己画的,请勿盗用!

先连接主机c1与路由器R1
R1前面已经启动,现在启动c1
sudo lxc start c1
创建网络并绑定端口
sudo lxc network create C1R1 ipv6.address=none ipv4.address=12.12.12.1/24
sudo lxc network attach C1R1 c1 eth0
sudo lxc network attach C1R1 R1 eth0
进入R1
sudo lxc exec R1 bash
分配地址
[email protected]:~# ip addr add 12.12.12.2/24 dev eth0
启动网卡
[email protected]:~# ip link set eth0 up
查看网卡配置
[email protected]:~# ip addr
退出容器
[email protected]:~# exit
进入c1
sudo lxc exec c1 bash
分配地址
[email protected]:~# ip addr add 12.12.12.1/24 dev eth0
启动网卡
[email protected]:~# ip link set eth0 up
替换默认网关
[email protected]:~# route delete default
[email protected]:~# route add default gw 12.12.12.2
测试能否ping通
ping 12.12.12.2
退出容器
[email protected]:~# exit

接下来按照上面方法连接主机c2 eth0与路由器R2 eth0并测试
地址分别为12.12.17.1/24与12.12.17.2/24

再连接路由器R1与路由器R2
创建网络并绑定端口
sudo lxc network create R1R2 ipv6.address=none ipv4.address=12.12.14.1/24
sudo lxc network attach R1R2 R1 eth1
sudo lxc network attach R1R2 R2 eth1
进入R1
sudo lxc exec R1 bash
分配地址
[email protected]:~# ip addr add 12.12.14.1/24 dev eth1
启动网卡
[email protected]:~# ip link set eth1 up
查看网卡配置
[email protected]:~# ip addr
退出容器
[email protected]:~# exit
进入R2
sudo lxc exec R2 bash
分配地址
[email protected]:~# ip addr add 12.12.14.2/24 dev eth1
启动网卡
[email protected]:~# ip link set eth1 up
查看网卡配置
[email protected]:~# ip addr
测试
[email protected]:~#ping 12.12.14.1
退出容器
[email protected]:~# exit
9、开启RIP路由协议
进入R1
sudo lxc exec R1 bash
复制RIP协议的配置文件,以便自动配置
[email protected]:~#cp /usr/local/etc/ripd.conf.sample /usr/local/etc/ripd.conf
[email protected]:~#vi /usr/local/etc/ripd.conf
加入如下内容
router rip
version 2
network eth0
network eth1
:wq保存退出,rip配置完成,执行
[email protected]:~#zebra –d
[email protected]:~#ripd –drip
正常启动后,可以用ip route show查看路由是否已经建立
退出[email protected]:~#exit
再进入R2,执行同样操作。用ip route show查看路由会发现多出一条R1分享过来的路由。
基于lxd容器搭建路由器
此时进入c2访问c1
[email protected]:~#ping 12.12.12.1会发现能够ping通
基于lxd容器搭建路由器
至此完成了c1、c2、R1、R2之间的全部配置,根据拓扑图将c3、R3按照上述步骤,进行主机c3与路由器R3间的配置和路由器R1R2R3间的两两配置,将其添加到网络中。
配置完成六个容器的地址信息分别如下图所示:
基于lxd容器搭建路由器
我个人中心有篇资源是整实验的全部过程,从创建容器安装quagga开始,最后附有常见问题解决方案,如有需要,可前往下载。