Configurar LVS con Keepalived. En un anterior articulo ya vimos como instalar un LVS (Server Load Balancer), hoy le sumamos Keepalived, que es un excelente software de enrutamiento escrito en C.
El objetivo principal de este proyecto es ofrecer instalaciones simples y robustas, que den un equilibrio de carga y alta disponibilidad en infraestructuras Linux.
El equilibrio de carga se basa en un servidor Linux Virtual Server (IPVS) que nos brinda un balanceo de carga Layer4.
Keepalived implementa un conjunto de comprobadores para mantener y administrar de forma dinámica y adaptativa el grupo de servidores con equilibrio de carga.
Por otro lado con VRRP logramos una alta disponibilidad en el protocolo.
| + ---------------- + ----------------- + | | 192.168.0.30 | eth0 --- VIP: 192.168.0.29 --- eth0 | 192.168.0.31 + ------- + -------- + + -------- + ------- + | LVS + Keepalived | | LVS + Keepalived | + ------- + -------- + + -------- + ------- + 10.0.0.30 | eth1 ----- VIP: 10.0.0.29 ---- eth1 | 10.0.0.31 | | + ---------------- + ----------------- + | + ------------ + | + ------------ + | Backend01 | 10.0.0.51 | 10.0.0.52 | Backend02 | | Servidor web + ------------ + ------------- + Servidor web | | | eth0 eth0 | | + ------------ + + ------------ +
En el ejemplo enviamos los paquetes HTTP a eth0 de nuestro servidor LVS, a la vez esos paquetes se envían a los servidores Backend01 y Backend02 con NAT.
Antes de continuar con este articulo debes leer el anterior.
Instalar ipvsadm con keepalived
yum -y install ipvsadm # enable IP forward echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf sysctl -p touch /etc/sysconfig/ipvsadm systemctl start ipvsadm systemctl enable ipvsadm
Configurar LVS con Keepalived
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.org nano /etc/keepalived/keepalived.conf
Crea la configuración en cada servidor.
# create new global_defs { notification_email { root@miservidor.es } notification_email_from root@miservidor.es smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_Server } vrrp_instance VI_1 { state BACKUP # monitored interface interface eth0 # virtual router's ID virtual_router_id 51 # set priority (change this value on each server) # (large number means priority is high) priority 100 nopreempt # VRRP sending interval advert_int 1 # authentication info between Keepalived servers authentication { auth_type PASS auth_pass password } virtual_ipaddress { # virtual IP address 192.168.0.29 dev eth0 10.0.0.29/24 dev eth1 } } virtual_server 192.168.0.29 80 { # monitored interval delay_loop 3 # distribution method lvs_sched rr # routing method lvs_method NAT protocol TCP # backend server#1 real_server 10.0.0.51 80 { weight 1 HTTP_GET { url { # monitored path path / # status code for normally state status_code 200 } # timeout(sec) connect_timeout 3 } } # backend server#2 real_server 10.0.0.52 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 } } }
systemctl start keepalived
systemctl enable keepalived
Ahora puedes acceder a la dirección IP del servicio, asegúrate de que funciona correctamente.