Tuesday, July 23, 2019

DHCP 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.
  1. create macvlan network:
    1. docker network create -d macvlan -o parent=enp3s0 --subnet 172.16.3.0/24 --gateway 172.16.3.4  --aux-address 'host=172.16.3.250' mynet
  2. add macvlan-aux to the docker-host (to ping directly from docker-host) - including ip link, route etc:
    1. ip link add mynet-aux link enp3s0 type macvlan mode bridge
    2. ip addr add 172.16.3.250/32 dev mynet-aux 
  3. run container with macvlan driver (assign static IP) and run /bin/bash:
    1. docker run --name='ctr0' --hostname='ctr0' --net=mynet --ip=172.16.3.249 -it centos /bin/bash
  4. Ping container IP from the Docker-host:
    1. ping 172.16.3.249
  5. on container (all of these can be done with docker-file):
    1. yum install net-tools -y
    2. yum install dhcp -y
    3. 1.1.6.1 is DHCP relay IP address
    4. dhcpd listens *only* on interfaces for which it finds subnet declaration in dhcpd.conf
vi dhcp.conf:
# this server is primary and thus - authorative server on that network
authoritative;
subnet 172.16.3.0 netmask 255.255.255.0 {
           range 172.16.3.1 172.16.3.3;
           option routers 172.16.3.4;
           option domain-name-servers 172.16.3.6;
}     

    Run dhcp service with specified file: dhcpd -cf dhcp.conf

    to kill process on container:
    top > k > PID > Enter

    No comments:

    Post a Comment