Installazione di un servizio DHCP ridondante

From RVM Wiki
Jump to navigation Jump to search

Configurazione Master

  • Occorre che la configurazione di partenza conetnga un pool, e che la configurazione base sia contenuta in un file a parte. Eventuali dichiarazioni di host statici vanno incluse in un file a parte.
  • Configurazione base:
sudoedit /etc/dhcp/dhcpd.conf
# /etc/dhcp/dhcp.conf MASTER
# vim: set syntax=dhcpd.conf :
ddns-update-style none;
deny unknown-clients;
authoritative;

# FAILOVER SETTINGS
failover peer "dhcp-failover" {
    primary; # declare this to be the primary server
    address 192.168.6.100;
    port 647;
    peer address 192.168.6.101;
    peer port 647;
    max-response-delay 30;
    max-unacked-updates 10;
    load balance max seconds 3;
    mclt 1800;
    split 128;
}

subnet 192.168.6.0 netmask 255.255.255.0 {
    option domain-name "example.com";
    option domain-name-servers 192.168.6.100, 192.168.6.101; 
    option routers 192.168.6.254;
    pool {
        range 192.168.6.51 192.168.6.61;
        next-server 192.168.6.100;
        default-lease-time 600;
        max-lease-time 7200;
        log-facility local7;
        server-name pubserver;
        filename = "pxelinux.0";
		# FAILOVER SETTINGS
        failover peer "dhcp-failover";
    }
}

include "/etc/dhcp/dhcpd.conf.hosts";

  • Questo è il file con gli ip assegnati ai mac:
sudoedit /etc/dhcp/dhcpd.conf.hosts
# /etc/dhcp/dhcpd.conf.hosts
# vim: set syntax=dhcpd.conf :

host client011 {
    # Client011
    hardware ethernet 00:0F:FE:20:59:4F;
    fixed-address 192.168.6.11;
}

host client012 {
    # Client012
    hardware ethernet 00:21:5a:20:81:76;
    fixed-address 192.168.6.12;
}
#.....

Configurazione server Slave

  • Installare
sudo apt install isc-dhcp-server


  • Copiare la stessa configurazione del master, ma cambiare:
sudoedit /etc/dhcp/dhcpd.conf
# /etc/dhcp/dhcp.conf SLAVE
#...
    secondary; # declare this to be the secondary server
    address 192.168.6.101;
	#...
    peer address 192.168.6.100;
	#...
    #mclt 1800;
    #split 128;
#...
    # invertire i DNS
    option domain-name-servers 192.168.6.101, 192.168.6.100; 
#...
  • notare, che se si hanno i server dns master e slave, si possono invertire sul dhcp server secondario.
  • Copiare anche il file con gli ip assegnati ai mac
/etc/dhcp/dhcpd.conf.hosts
  • Avviare e verificare che non ci siano errori
sudo systemctl restart isc-dhcp-server
  • Riavviare anche sul primary:
sudo systemctl restart isc-dhcp-server
  • Provare ora a stoppare il master ed a rinnovare un ip.

Sincronizzazione modifiche assegnazioni IP

  • Se si effettuano modifiche sui mac, è possibile aggiornare il file anche sullo slave. Allo scopo si usa lsyncd
  • Installarlo
sudo apt-get install lsyncd

Debian Squeeze

  • Creare il file di init.d
sudo wget http://lsyncd.googlecode.com/svn-history/r73/package/debian/init.d -O /etc/init.d/lsyncd
sudo chmod +x /etc/init.d/lsyncd
sudo update-rc.d lsyncd defaults
  • Modificare la riga 59:
log_failure_msg "lsyncd daemon failed to start"
  • Creare il file di configurazione:
sudo cp /usr/share/doc/lsyncd/examples/lsyncd.conf.xml /etc/lsyncd.conf.xml
  • abilitare debug
  <debug/>
  • Disabilitare la cancellazione dei files se non serve, eliminado la riga:
<option text="--delete"/>
  • Dichiarare la diretcory da monitorare e da mirrorare:
<directory>
    <source path="/etc/dhcp"/>
    <target path="root@pubstor01.pubblistil.priv:/etc/dhcp"/>
    <exclude-from filename="/etc/lsyncd.exclude" /
</directory>
  • Come tutti i programmi basati su inotify, lsyncd monitora le directory, non i singoli file, quindi occorre dire a rsync cosa escludere dalla copia. Creare il file con le esclusioni che deve avere il formato dell'opzione --exclude-from di rsync:
sudoedit /etc/lsyncd.exclude 
 
dhcpd.conf
dhclient.conf
/etc/dhcp/dhclient-exit-hooks.d/
/etc/dhcp/dhclient-enter-hooks.d/
  • Collegarsi come via ssh root la prima volta dal server master al server slave, per verificare il funzionamento del collegamento rsync.
  • Avviare lsyncd e verificare i log:
sudo invoke-rc.d lsyncd start
sudo tail -f /var/log/lsyncd 

  • Verificare che le modifiche si propaghino, e che i files esclusi nons iano propagati
  • ATTENZIONE DEBIAN SQUEEZE UTILIZZA LA VERSIONE 1.X DI LSYNCD, CHE HA LA CONFIGURAZIOEN TOTALMENTE DIVERSA DAL 2.X

Debian Wheezy

  • Utilizza la versione 2.x, che ha formati e file di configurazione diversi. Se si è installato lo script init.d precedente, occorre eliminarlo ed estrarlo a mano dall'archivio .deb
sudo apt-get install lsyncd 


  • Il file di configurazione è:
sudo mkdir /etc/lsyncd/
sudoedit /etc/lsyncd/lsyncd.conf.lua

settings = {
	logfile = "/var/log/lsyncd.log",
	statusFile = "/var/run/lsyncd.status"
}

sync {
	default.rsyncssh,
	source		= "/etc/dhcp",
	host		= "pubstor01.pubblistil.priv",
	targetdir	= "/etc/dhcp",
	rsyncOpts		= { "-ltsd", "--verbose"} ,
	excludeFrom = "/etc/lsyncd/lsyncd.exclude"
}
  • Il file exclude è
sudoedit /etc/lsyncd/lsyncd.exclude
dhcpd.conf
dhclient.conf
dhclient-exit-hooks.d/
dhclient-enter-hooks.d/
  • Il file di log è
sudo tail -f /var/log/lsyncd.log

Debian Jessie

  • Utilizza la versione 2.1
sudo apt-get install lsyncd 


  • Il file di configurazione è:
sudo mkdir /etc/lsyncd/
sudoedit /etc/lsyncd/lsyncd.conf.lua

settings {
	logfile = "/var/log/lsyncd.log",
	statusFile = "/var/run/lsyncd.status"
}

sync {
	default.rsyncssh,
	source		= "/etc/dhcp",
	host		= "pubstor01.pubblistil.priv",
	targetdir	= "/etc/dhcp",
	rsyncOpts		= { "-ltsd", "--verbose"} ,
	excludeFrom = "/etc/lsyncd/lsyncd.exclude"
}
  • Il file exclude è
sudoedit /etc/lsyncd/lsyncd.exclude
dhcpd.conf
dhclient.conf
dhclient-exit-hooks.d/
dhclient-enter-hooks.d/
  • Il file di log è
sudo tail -f /var/log/lsyncd.log

Debian Bullseye

  • Utilizza la versione 2.1
sudo apt-get install lsyncd 


  • Il file di configurazione è:
sudo mkdir /etc/lsyncd/
sudoedit /etc/lsyncd/lsyncd.conf.lua

settings {
	logfile = "/var/log/lsyncd.log",
	statusFile = "/var/run/lsyncd.status"
}

sync {
	default.rsyncssh,
	source		= "/etc/dhcp",
	host		= "gapsrv03.ad.gapartners.eu",
	targetdir	= "/etc/dhcp",
	rsync		= {
		 _extra= { "-ltsd", "--verbose"},
	} ,
	excludeFrom = "/etc/lsyncd/lsyncd.exclude"
}

  • Il file exclude è
sudoedit /etc/lsyncd/lsyncd.exclude
dhcpd.conf
dhclient.conf
dhclient-exit-hooks.d/
dhclient-enter-hooks.d/
  • Il file di log è
sudo tail -f /var/log/lsyncd.log

Test

  • Testare con
sudo lsyncd -nodaemon /etc/lsyncd/lsyncd.conf.lua
  • Avviare con
sudo systemctl restart lsyncd

Riavvio del server SLAVE a seguito di modifiche

Debian < Bullseye

  • Si utilizza incron, che a seguito della sincronizzazione del file dhcpd.conf.hosts provvede al riavvio di isc-dhcpd-server
  • Installare incron
sudo apt-get install incron
  • Creare al regola di monitoring:
sudoedit /etc/incron.d/dhcpd.conf.hosts
/etc/dhcp IN_CLOSE_WRITE /usr/local/sbin/incron-dhcpd.conf.hosts $#
  • Creare lo scipt che provvede al riavvio:
sudoedit /usr/local/sbin/incron-dhcpd.conf.hosts
#!/bin/bash
FILENAME=$@
logger -t "incron-dhcpd.conf.hosts" "Monitoring script started."
logger -t "incron-dhcpd.conf.hosts" "File modified is $FILENAME."

if ( echo "$FILENAME" | grep dhcpd.conf.hosts )
then
        logger -t "incron-dhcpd.conf.hosts" "dhcpd.conf.hosts modified. Restarting..."
        /usr/sbin/dhcpd -t && \
                /usr/sbin/invoke-rc.d isc-dhcp-server restart && \
                logger -t "incron-dhcpd.conf.hosts" "isc-dhcp-server restarted"
else
        logger -t "incron-dhcpd.conf.hosts" "File modified was $FILENAME. Not restarting."
fi
  • Renderlo eseguibile
sudo chmod +x /usr/local/sbin/incron-dhcpd.conf.hosts

Debian >= Bullseye

  • Non esiste più incrond, si usa systemd
  • Creare la unit che monitora il file che si desidera:
sudoedit /etc/systemd/system/dhcpd_conf-monitor.path
[Unit]
Description="Monitor the /etc/dhcp/dhcpd.conf.hosts file for changes"

[Path]
PathChanged=/etc/dhcp/dhcpd.conf.hosts
Unit=dhcpd_conf-restart.service

[Install]
WantedBy=multi-user.target
  • Verificare
sudo systemd-analyze verify /etc/systemd/system/dhcpd_conf-monitor.path 
  • Creare la unite che esegue il comando quando il file viene cambiato e chiuso:
sudoedit /etc/systemd/system/dhcpd_conf-restart.service
[Unit]
Description=Restart isc-dhcpd-server upon /etc/dhcp/dhcpd.conf change

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart isc-dhcp-server.service 
  • Verificare:
sudo systemd-analyze verify /etc/systemd/system/dhcpd_conf-restart.service
  • Questo service non va abilitato o startato.
  • Abilitare e avviare il monitor del file:
sudo systemctl enable dhcpd_conf-monitor.path
sudo systemctl restart dhcpd_conf-monitor.path


  • Testare modificando dhcpd.conf.hosts, verificando in syslog:
sudo tail -f /var/log/syslog | grep systemd
Dec  1 15:37:19 gapsrv03 systemd[1]: Starting Restart isc-dhcpd-server upon /etc/dhcp/dhcpd.conf change...
Dec  1 15:37:19 gapsrv03 systemd[1]: Stopping LSB: DHCP server...
Dec  1 15:37:19 gapsrv03 systemd[1]: isc-dhcp-server.service: Succeeded.
Dec  1 15:37:19 gapsrv03 systemd[1]: Stopped LSB: DHCP server.
Dec  1 15:37:19 gapsrv03 systemd[1]: Starting LSB: DHCP server...
Dec  1 15:37:21 gapsrv03 systemd[1]: Started LSB: DHCP server.
Dec  1 15:37:21 gapsrv03 systemd[1]: dhcpd_conf-restart.service: Succeeded.
Dec  1 15:37:21 gapsrv03 systemd[1]: Finished Restart isc-dhcpd-server upon /etc/dhcp/dhcpd.conf change.

Riferimenti

Riferimenti