Cluster Web · 6 min read · Oct 08, 2025
Il Cluster Web Perfetto Bilanciato e ad Alta Disponibilità con 2 Server che Eseguono Xen su Ubuntu 8.04 Hardy Heron - Pagina 8
14. Mirroring dei file web e mail con rsync (web1, web2)
Questa è la parte complicata. A seconda del tipo di sito web che metterai sul cluster, la tecnica utilizzata per mirroring dei file web potrebbe essere diversa.
Considereremo web1.example.com come il master, web2.example.com si sincronizzerà con web1.example.com.
Questo può creare problemi in alcuni casi. Se hai un sito web dove gli utenti possono caricare file, non vuoi che vengano caricati su web2.example.com perché i due server non saranno sincronizzati.
Nel mio caso, reindirizzo tutte le pagine dove gli utenti possono caricare dati alla porta HTTP 81, che è solo inoltrata a un server, web1.example.com. Se il server master fallisce, tutto il traffico sulla porta 81 viene inoltrato al server di fallback, web2.example.com e il mirroring viene temporaneamente interrotto.
Il reindirizzamento può essere fatto in apache vhost.conf :
[...]
Redirect /admin http://www.example.com:81/webmail
[...]Se hai molti caricamenti di file da parte degli utenti, potresti considerare di utilizzare un NFS bilanciato:
https://www.howtoforge.com/high_availability_nfs_drbd_heartbeat
14.1 Installare rsync
apt-get install rsync
14.2 Creare le Chiavi su web2.example.com
Ora creiamo la coppia di chiavi privata/pubblica su web2.example.com:
mkdir /root/rsync
ssh-keygen -t dsa -b 1024 -f /root/rsync/mirror-rsync-key
Vedrai qualcosa del genere:
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase): [premi invio qui]
Enter same passphrase again: [premi invio qui]
Your identification has been saved in /root/rsync/mirror-rsync-key.
Your public key has been saved in /root/rsync/mirror-rsync-key.pub.
The key fingerprint is:
68:95:35:44:91:f1:45:a4:af:3f:69:2a:ea:c5:4e:d7 root@web2È importante che tu non inserisca una passphrase altrimenti il mirroring non funzionerà senza interazione umana, quindi premi semplicemente invio!
Successivamente, copiamo la nostra chiave pubblica su web1.example.com:
scp /root/rsync/mirror-rsync-key.pub [email protected]:/home/vmail/
La chiave pubblica mirror-rsync-key.pub dovrebbe ora essere disponibile in /home/vmail su web1.example.com.
14.3 Configurare web1.example.com
web1.example.com
Ora accedi tramite SSH come vmail (non come root!) e fai questo:
login vmail
mkdir ~/.ssh
chmod 700 ~/.ssh
mv ~/mirror-rsync-key.pub ~/.ssh/
cd ~/.ssh
touch authorized_keys
chmod 600 authorized_keys
cat mirror-rsync-key.pub >> authorized_keys
Facendo questo, abbiamo aggiunto il contenuto di mirror-rsync-key.pub al file /home/vmail/.ssh/authorized_keys. /home/vmail/.ssh/authorized_keys dovrebbe apparire simile a questo:
(Ancora come webmaster! su web1.example.com)
vi ~/.ssh/authorized_keys
ssh-dss AAAAB3NzaC1kc3MAAA[...]lSUom root@web2Ora vogliamo consentire connessioni solo da web2.example.com, e l’utente connesso dovrebbe essere autorizzato a utilizzare solo rsync, quindi aggiungiamo
command="/home/vmail/rsync/checkrsync",from="192.168.0.105",no-port-forwarding,no-X11-forwarding,no-ptyproprio all’inizio di /home/vmail/.ssh/authorized_keys:
command="/home/vmail/rsync/checkrsync",from="192.168.0.105",no-port-forwarding,no-X11-forwarding,no-pty ssh-dss AAAAB3NzaC1kc3MAAA[...]lSUom root@
web2Ora creiamo lo script /home/vmail/rsync/checkrsync che rifiuta tutti i comandi tranne rsync su web1.example.com.
(Lo facciamo ancora come vmail!)
mkdir ~/rsync
vi ~/rsync/checkrsync
#!/bin/sh
case "$SSH_ORIGINAL_COMMAND" in
*\&*)
echo "Rejected"
;;
*\(*)
echo "Rejected"
;;
*\{*)
echo "Rejected"
;;
*\;*)
echo "Rejected"
;;
*\<*)
echo "Rejected"
;;
*\`*)
echo "Rejected"
;;
rsync\ --server*)
$SSH_ORIGINAL_COMMAND
;;
*)
echo "Rejected"
;;
esacchmod 700 ~/rsync/checkrsync
14.4 Testare rsync automatizzato
web2.example.com
Prima dobbiamo dire a rsync quali file non vogliamo sincronizzare (come munit graph!)
vi /root/exclude_www.txt
e farlo apparire così :
/example/web/monit/
/example/web/monitoring/
/example/ssl/
/yoursite/ssl/ e ora per la mail :
vi /root/exclude_mail.txt
/rsync/
/.ssh/
#if you use my monitoring script, see next page
server1_was_down/
.bash_history/
.bash_logout/
.bashrc/
.mysql_history/
.profile/
.viminfo/Ora dobbiamo testare su web2.example.com se possiamo mirroring web1.example.com senza essere invitati a inserire la password di vmail. Lo facciamo:
(Lo facciamo come root!)
web2.example.com
rsync -avz –delete –ignore-errors –exclude-from ‘/root/exclude_www.txt’ -e “ssh -i /root/rsync/mirror-rsync-key” [email protected]:/var/www/ /var/www/
seguito da :
rsync -avz –delete –ignore-errors –exclude-from ‘/root/exclude_mail.txt’ -e “ssh -i /root/rsync/mirror-rsync-key” [email protected]:/home/vmail/ /home/vmail/
Dovresti ora vedere che il mirroring avviene senza essere invitato a inserire una password :
receiving file list ... done
sent 71 bytes received 643 bytes 476.00 bytes/sec
total size is 64657 speedup is 90.5614.5 Creare un cron job
Prima creeremo uno script che garantirà che se per qualche motivo web1.example.com è andato giù, rsync verrà interrotto e che root deve riavviare manualmente il processo.
Il motivo per cui dobbiamo fare questo è che web2.example.com prenderà il controllo di web1.example.com per la mail se è andato giù. Nuova mail verrà quindi ricevuta su web2.example.com durante quel periodo, quindi diciamo che web1.example.com torna online un’ora dopo, tutta la nuova mail durante quel periodo di tempo verrebbe eliminata su web2.example.com a causa del comando rsync –delete. Vedremo di seguito come sincronizzare di nuovo entrambi i server se ciò accade.
Per ora scriviamo quello script.
Su web2.example.com
vi /root/rsync_web1
#!/bin/bash
# Script per rsyncare i dati web e mail tra 2 server bilanciati
# Copyright (c) 2008 blogama.org
# Questo script è concesso in licenza sotto GNU GPL versione 2.0 o superiore
# ---------------------------------------------------------------------
### Questo script ha 2 scopi ###
### 1) Controllare la connessione sulla porta 25 al server master e poi rsyncare la mail ###
### 2) Controllare la connessione sulla porta 80 al server master e poi rsyncare i dati www ###
### Da modificare ###
MASTERSERVERIP="192.168.0.104"
WEBPORT="80"
SMTPPORT="25"
SSHPORT="22"
EMAIL="[email protected]"
WWWRSYNCUSER="vmail"
WWWDIR="/var/www/"
MAILRSYNCUSER="vmail"
MAILDIR="/home/vmail/"
###### Non apportare modifiche sotto ######
### Binaries ###
MAIL=$(which mail)
TELNET=$(which telnet)
RSYNC=$(which rsync)
SSH=$(which ssh)
### Per ripristinare all'originale quando il problema è risolto ###
if [ $1 ]; then
if [ $1=="fix" ]; then
if [ -f smtp_down.txt ]; then
rm /root/smtp_down.txt
fi
if [ -f www_down.txt ]; then
rm /root/www_down.txt
fi
fi
fi
####################SMTP####################
### Se già notificato per problema SMTP esci ###
cd /root
if [ -f smtp_down.txt ]; then
exit 1;
fi
### Controlla se il server 1 sta rispondendo su SMTP. Se sì rsynca la mail ###
### Se il server 1 era giù, non può rsyncare --delete server 2 con 1 ###
### Deve risincronizzare server1 con 2 (senza eliminare) e eseguire /root/sync fix ###
(
echo "quit"
) | $TELNET $MASTERSERVERIP $SMTPPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then
$RSYNC -avz --delete --ignore-errors --exclude-from '/root/exclude_mail.txt' -e "ssh -p $SSHPORT -i /root/rsync/mirror-rsync-key" $MAILRSYNCUSER@$MASTERSERVERIP:$MAILDIR $MAILDIR
else
echo "Server 1 giù. La sincronizzazione della mail non funziona più" > /root/smtp_down.txt
$MAIL -s "Server 1 giù porta 25" $EMAIL < /root/smtp_down.txt
fi
####################HTTP####################
### Se già notificato per problema HTTP esci ###
cd /root
if [ -f http_down.txt ]; then
exit 1;
fi
### Controlla se il server 1 sta rispondendo su HTTP. Se sì rsynca i dati www ###
(
echo "quit"
) | $TELNET $MASTERSERVERIP $WEBPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then
$RSYNC -avz --delete --ignore-errors --exclude-from '/root/exclude_www.txt' -e "ssh -p $SSHPORT -i /root/rsync/mirror-rsync-key" $WWWRSYNCUSER@$MASTERSERVERIP:$WWWDIR $WWWDIR
else
echo "Server 1 giù. La sincronizzazione WWW non funziona più" > /root/http_down.txt
$MAIL -s "Server 1 giù porta 80" $EMAIL < /root/http_down.txt
fiE rendi questo file eseguibile :
chmod +x /root/rsync_web1
Certo puoi mettere quello che vuoi in quei file ;) Se non metti la barra ignorerà la parola chiave ricorsivamente (es : *.gz), se metti la barra è per una posizione precisa (es : /example/ssl/).
Ora aggiungiamo lo script al nostro crontab facendo quanto segue :
crontab -e
e aggiungi questa riga :
[...]
*/5 * * * * /root/rsync_web1 >/dev/null 2>&1
[...]Questo verrà eseguito ogni 5 minuti, ovviamente puoi cambiarlo in base alle tue esigenze.
14.6 Cosa fare se web1.example.com era giù
In generale, se web1.example.com è giù, devi quindi :
- rsyncare WEB1 A WEB2 senza il comando –delete :
web1.example.com
rsync -avz [email protected]:/var/www/ /var/www/
rsync -avz [email protected]:/home/vmail/ /home/vmail/
Ricevi i nuovi post nella tua casella di posta.
Nessuno spam. Disiscriviti in qualsiasi momento.