
In questo articolo, avremo spiegato i passaggi necessari per installare e configurare HAProxy Load Balancer su Ubuntu 18.04 LTS. Prima di continuare con questo tutorial, assicurati di aver effettuato l'accesso come utente con privilegi sudo. Tutti i comandi in questo tutorial devono essere eseguiti come utente non root.
HAProxy è uno strumento Linux open source che fornisce servizi proxy e bilanciamento del carico ad alta disponibilità per applicazioni di rete basate su TCP e HTTP. Grazie alla sua facile integrazione nelle architetture esistenti, all'idoneità per siti Web ad alto traffico, all'estrema affidabilità e all'attenzione alla compatibilità verso l'alto, viene fornito per impostazione predefinita dalla maggior parte delle distribuzioni Linux tradizionali.
Installa HAProxy su Ubuntu
Passaggio 1. Dettagli di rete
Per semplicità, assumeremo i seguenti indirizzi IP e nomi host per le istanze:
- haproxy-server :indirizzo IP pubblico 198.18.0.1
- server-backend1 :indirizzo IP privato 172.16.0.1 , indirizzo IP pubblico 198.18.0.1
- server-backend2 :indirizzo IP privato 172.16.0.2 , indirizzo IP pubblico 198.18.0.2
Passaggio 2. Innanzitutto, prima di iniziare a installare qualsiasi pacchetto sul tuo server Ubuntu, ti consigliamo sempre di assicurarti che tutti i pacchetti di sistema siano aggiornati.
sudo apt update sudo apt upgrade
Passaggio 3. Installa HaProxy su Ubuntu 18.04 LTS.
HaProxy è disponibile nel repository del software Ubuntu, quindi possiamo installarlo utilizzando il gestore di pacchetti eseguendo il comando seguente:
sudo add-apt-repository ppa:vbernat/haproxy-1.8 sudo apt-get update sudo apt-get install haproxy
Passaggio 4. Configurazione del bilanciamento del carico con HAProxy.
Ora modifica il file di configurazione predefinito di haproxy /etc/haproxy/haproxy.cfg e avvia la configurazione:
sudo nano /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256::RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http Quindi verso la fine del file, aggiungi il contenuto di seguito:
frontend ourwebsitefrontend
bind *:80
mode http
default_backend ourwebsiteendpoin Il parametro bind dice ad HaProxy di ascoltare la porta 80 per le connessioni. Alla fine del testo, abbiamo specificato ourwebsiteendpoint come la direttiva in cui si trovano i nostri endpoint. Ora possiamo andare avanti e aggiungere i dettagli di configurazione del back-end come segue:
backend ourwebsiteendpoint
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server backend-server1 172.16.0.1:8080 check
server backend-server2 172.16.0.2:8080 check Ora, se vuoi, puoi abilitare le statistiche Haproxy aggiungendo la seguente configurazione nel file di configurazione HAProxy:
listen stats
bind :32600
stats enable
stats uri /
stats hide-version
stats auth username:password Passaggio 5. File di configurazione HAProxy finale.
Il file di configurazione finale potrebbe essere simile al seguente:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM$
ssl-default-bind-options no-sslv3
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend ourwebsitefrontend
bind *:80
mode http
default_backend ourwebsiteendpoint
backend ourwebsiteendpoint
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server backend-server1 172.16.0.1:8080 check
server backend-server2 172.16.0.2:8080 check
listen stats
bind :32600
stats enable
stats uri /
stats hide-version
stats auth username:password Quindi, riavvia il server HaProxy per ricaricare le modifiche:
sudo service haproxy restart
Passaggio 6. Verifica della configurazione.
In questa fase, abbiamo una configurazione HAProxy completamente funzionale. In ogni nodo del server web ho una pagina demo index.html che mostra il nome host dei server, così possiamo facilmente distinguere tra le pagine web dei server.
Ora accedi alla porta 80 su IP 198.18.0.1 (come configurato sopra) nel browser web e premi Aggiorna. Vedrai che HAProxy sta inviando le richieste al server back-end una per una (come per l'algoritmo round robin).
Questo è tutto ciò che devi fare per installare HAProxy Load su Ubuntu 18.04. Spero che tu possa trovare utile questo suggerimento rapido. Se hai domande o suggerimenti, sentiti libero di lasciare un commento qui sotto.