DHCP over Relay on Docker
DHCP (Dynamic Host Configuration Protocol) helps us to address dynamically our hosts on the network. In fact, when a Host is configured to get its IP address dynamically, it will broadcast a DHCP Request on the network searching for a DHCP server. DHCP server has to be on the same broadcast domain as the CLIENTS since routers do not forward broadcast packets.
For Docker container it means that we must connect our container to each subnet in the network of our company. But we want to use just one interface (in that post I'll use macvlan) on our container. But problem is:
As our DHCP Client wants to get an IP address, it will send a DHCP Discover message which is a broadcast message. As the Router/Gateway/Firewall do not forward broadcast packets, this message will never reach the DHCP Server (our Docker Container).
To solve this issue we'll use DHCP Relay Agent. This feature is activated on a network device having interfaces in all subnets of the network of the company:
- this device (router/gateway/firewall) forwards DHCP messages to the DHCP Server, and when the DHCP Server responds, this device forwards the replies to the Client.
- DHCP Realy Agent adds giaddr (gateway interface address) field to the DHCP Packet. This field contains DHCP Relay Agent interface IP address which received DHCP Request and also this field helps to identify pool from which DHCP Server has to select IP addresses.
- After identifying pool DHCP Server replies with DHCP Offer broadcast message and this message forwarded by DHCP Relay Agent to the DHCP Client.
- DHCP Client replies with DHCP Request message
- this message also forwarded to the DHCP Server by DHCP Relay Agent
- DHCP Server replies with DHCP Ack
- this message forwarded to the DHCP Client by DHCP Relay Agent
- finally DHCP Clietn is assigned an IP address
If you want to use Cisco ISR as Relay Agent:
- Setup interface which will be used to interconnect DHCP Relay Agent and DHCP Server:
- conf term
- int fa0/1 # DHCP Server facing interface
- ip address 172.16.3.4 255.255.255.0
- Setup interface which will use DHCP Relay Agent and enable IP-helper (DHCP Server IP address) on that interface - all DHCP messages will be forwarded to that IP address:
- int fa 0/0
- ip address 10.10.6.1 255.255.225.0
- ip helper-address 172.16.3.249
- do wr
- Check configuration:
- show ip int fa0/0
- Also we need to configure static route on the DHCP Server if DHCP Relay Agent is not default gateway for the DHCP Server:
- ip route add 10.10.6.0/24 via 172.16.3.4 # this is not persistent setup to make it persistent create route file for needed interface
Because of using macvlan for Docker Container, you need to enable IP forwarding on Docker Host:
echo 1 /proc/sys/net/ipv4/ip_forward . Previous is not persistent setup, to make it persistent:
- sudo vi /etc/sysctl.conf and add net.ipv4.ip_forward = 1
- sudo sysctl -p
If you want to use CentOS 7 as Relay Agent:
- Setup interface which needs to use DHCP Relay Agent:
- vi ifcfg-eth0
- IPADDR=10.10.6.1
- PREFIX=24
- vi ifcfg-eth1 # DHCP Server facing interface
- IPADDR=172.16.3.4
- PREFIX=24
- yum install dhcp # dhcp-relay is part of dhcp package
- cp /usr/lib/systemd/system/dhcrelay.service /etc/systemd/system
- vi /etc/systemd/system
- under [Service]
- append IP address of the DHCP server to the ExecStart after --no-pid:
- ExecStart=/usr/sbin/dhcrelay -d --no-pid 172.16.3.249
- Also you can choose interfaces to activate DHCP Relay on them (by default all interfaces are used). You must use separate "-i" option for each additional interface:
- ExecStart=/usr/sbin/dhcrelay -d --no-pid 172.16.3.249 -i eth1 -i eth2.20
- systemctl --system daemon-reload
- systemctl start dhcrelay
- systemctl enable dhcrelay
- systemctl status dhcrelay
- Also we need to configure static route on the DHCP Server if DHCP Relay Agent is not default gateway for the DHCP Server:
- ip route add 10.10.6.0/24 via 172.16.3.4 # this is not persistent setup to make it persistent create route file for needed interface
If you want to use CentOS 6 as Relay Agent:
- Setup interface which needs to use DHCP Relay Agent:
- vi ifcfg-eth0
- IPADDR=10.10.6.1
- NETMASK=24
- vi ifcfg-eth1 # DHCP Server facing interface
- IPADDR=172.16.3.4
- NETMASK=24
- yum install dhcp # dhcp-relay is part of dhcp package
- vi /etc/sysconfig/dhcrelay
- INTERFACES= "eth1 eth2.20" #which interfaces must use DHCP Relay Agent
- DHCPSERVERS="172.16.3.249" # DHCP server IP address
- service dhcrelay start
- chkconfig dhcrelay on
- service dhcrelay status
- Also we need to configure static route on the DHCP Server if DHCP Relay Agent is not default gateway for the DHCP Server:
- ip route add 10.10.6.0/24 via 172.16.3.4 # this is not persistent setup to make it persistent create route file for needed interface
Interface with DHCP relay must use static IP address (no DHCP is allowed).
dhcp.conf
# this server is primary and authorative server on that network
authoritative;
# dhcpd listens *only* on interfaces for which it finds subnet declaration in dhcpd.conf
# empty declaration for local IP subnet to start listening on eth0 interface
subnet 172.16.3.0 netmask 255.255.255.0 { }
subnet 10.10.6.0 netmask 255.255.255.0 {
range 10.10.6.2 10.10.6.3;
option routers 10.10.6.1;
#option domain-name-servers 8.8.8.8, 8.8.4.4;
}
to kill process on container:
top > k > PID > Enter
dhcp -cf dhcp.conf
No comments:
Post a Comment