1、使用的镜像
sudo docker pull networkboot/dhcpd
2、测试的局域网拓扑结构如下:h为 DHCP服务器,ovs交换机s1、s2、s3各连着一台等待分配ip地址的主机。
3、h DHCP服务器通过一对暴露于root namespace 的veth pair相连,具体操作如下
3.1、生成交换机s1、s2、s3
sudo ovs-vsctl add-br s1
sudo ovs-vsctl add-br s2
sudo ovs-vsctl add-br s3
3.2、创建veth pair 即 v1-v2
sudo ip link add v1 type veth peer name v2
3.3、激活up
sudo ip link set dev v1 up
sudo ip link set dev v2 up
3.4、给v1配置ip及子网掩码
sudo ifconfig v1 192.168.1.254 netmask 255.255.255.0
3.5、生成DHCP服务器,监听v1 ,挂载本地data文件夹,data里面中只有dhcpd.conf文件
sudo docker run -it \
--rm --net host \
--init \
--name=dhcpServer1 \
-v ~/emxbook/docker-dhcpd/data:/data \
networkboot/dhcpd v1
dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 {
option routers 192.168.1.1;
option subnet-mask 255.255.255.0;
option domain-name "emx.local";
option domain-name-servers 8.8.8.8;
range 192.168.1.2 192.168.1.100;
}
3.5、将v2连接到交换机上
sudo ovs-vsctl add-port s1 v2
3.6、交换机s1、s2、s3彼此相连
sudo ovs-vsctl add-port s1 s1p1
sudo ovs-vsctl set Interface s1p1 type=patch
sudo ovs-vsctl set Interface s1p1 options:peer=s2p1
sudo ovs-vsctl add-port s2 s2p1
sudo ovs-vsctl set Interface s2p1 type=patch
sudo ovs-vsctl set Interface s2p1 options:peer=s1p1
sudo ovs-vsctl add-port s1 s1p2
sudo ovs-vsctl set Interface s1p2 type=patch
sudo ovs-vsctl set Interface s1p2 options:peer=s3p1
sudo ovs-vsctl add-port s3 s3p1
sudo ovs-vsctl set Interface s3p1 type=patch
sudo ovs-vsctl set Interface s3p1 options:peer=s1p2
3.7、创建主机h1、h2、h3,并将其连接到交换机上,其中ubunut:5是添加了基本net-tools、isc-dhcp-client,创建的时候记得使用 --cap-add=NET_ADMIN
sudo docker run -itd --network=none --name=h1 --cap-add=NET_ADMIN ubuntu:5
sudo docker run -itd --network=none --name=h2 --cap-add=NET_ADMIN ubuntu:5
sudo docker run -itd --network=none --name=h3 --cap-add=NET_ADMIN ubuntu:5
sudo ovs-docker add-port s1 eth1 h1
sudo ovs-docker add-port s2 eth1 h2
sudo ovs-docker add-port s3 eth1 h3
3.8、可以使用floot-light控制交换机s1、s2、s3交换机(选择)
sudo ovs-vsctl set-controller s1 tcp:127.0.0.1:6653
sudo ovs-vsctl set-controller s2 tcp:127.0.0.1:6653
sudo ovs-vsctl set-controller s3 tcp:127.0.0.1:6653
4、使用dhclient eht1 向DHCP服务器申请
4.1、申请ip之前
4.2、申请ip(服务器端界面)
4.3申请ip之后,h1(192.168.1.38):
同理,h2(192.168.1.39):
h3(192.168.1.40):
5、测试
5.1、h1 ping h2(192.168.1.39):
5.2、h2 ping h3(192.168.1.40):
6、最后拓扑结构图,ip分配之后的拓扑结构:DHCP服务器(192.168.1.6)、h1(192.168.1.38)、h2(192.168.1.39)、h3(192.168.1.40)
7、代码汇总参考
sudo ovs-vsctl add-br s1
sudo ovs-vsctl add-br s2
sudo ovs-vsctl add-br s3
sudo ip link add v1 type veth peer name v2
sudo ip link set dev v1 up
sudo ip link set dev v2 up
sudo ifconfig v1 192.168.1.6 netmask 255.255.255.0
sudo docker run -it \
--rm --net host \
--init \
--name=dhcpServer \
-v ~/emxbook/docker-dhcpd/data:/data \
networkboot/dhcpd v1
sudo ovs-vsctl add-port s1 v2
sudo ovs-vsctl add-port s1 s1p1
sudo ovs-vsctl set Interface s1p1 type=patch
sudo ovs-vsctl set Interface s1p1 options:peer=s2p1
sudo ovs-vsctl add-port s2 s2p1
sudo ovs-vsctl set Interface s2p1 type=patch
sudo ovs-vsctl set Interface s2p1 options:peer=s1p1
sudo ovs-vsctl add-port s1 s1p2
sudo ovs-vsctl set Interface s1p2 type=patch
sudo ovs-vsctl set Interface s1p2 options:peer=s3p1
sudo ovs-vsctl add-port s3 s3p1
sudo ovs-vsctl set Interface s3p1 type=patch
sudo ovs-vsctl set Interface s3p1 options:peer=s1p2
sudo docker run -itd --network=none --name=h1 --cap-add=NET_ADMIN ubuntu:5
sudo docker run -itd --network=none --name=h2 --cap-add=NET_ADMIN ubuntu:5
sudo docker run -itd --network=none --name=h3 --cap-add=NET_ADMIN ubuntu:5
sudo ovs-docker add-port s1 eth1 h1
sudo ovs-docker add-port s2 eth1 h2
sudo ovs-docker add-port s3 eth1 h3
sudo ovs-vsctl set-controller s1 tcp:127.0.0.1:6653
sudo ovs-vsctl set-controller s2 tcp:127.0.0.1:6653
sudo ovs-vsctl set-controller s3 tcp:127.0.0.1:6653
作者:Light_Travelling