Configuração de Alta Disponibilidade com Keepalived e Nginx

. Preparação do Ambiente

1. VMware;
2. 4 máquinas virtuais com CentOS7: 192.168.122.217, 192.168.122.165
3. Serviços do sistema: LVS, Keepalived
4. Servidor Web: nginx<br></br><br></br>

. Instalação de Software

Nas duas máquinas virtuais, configuraremos o cluster da seguinte forma:

192.168.122.217 servidor-nginx-1+keepalived-1 192.168.122.165 servidor-nginx-2+keepalived-2

Instale o keepalived e nginx em ambas as máquinas<br></br>

yum  install -y keepalived nginx

Agora, vamos configurar o serviço keepalived

Configuração para 192.168.122.217

cat > /etc/keepalived/keepalived.conf <<EOF
! Arquivo de Configuração do keepalived
global_defs {
   router_id servidor-principal
}
vrrp_script verificar_nginx {
    script "/etc/keepalived/verificar_nginx.sh"
    interval 2
    weight -20
}
vrrp_instance INSTANCIA_1 {
    state MASTER
    interface eth0
    virtual_router_id 251
    priority 100
    advert_int 1
    mcast_src_ip 192.168.122.217
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 88888888
    }
    track_script {
         verificar_nginx
    }
    virtual_ipaddress {
        192.168.122.50
    }
}
EOF

## 192.168.122.217 é o IP do nó primário, 192.168.122.50 é o VIP

Configuração para 192.168.122.165

cat > /etc/keepalived/keepalived.conf <<EOF
! Arquivo de Configuração do keepalived
global_defs {
   router_id servidor-reserva
}
vrrp_script verificar_nginx {
    script "/etc/keepalived/verificar_nginx.sh"
    interval 2
    weight -20
}
vrrp_instance INSTANCIA_1 {
    state BACKUP
    interface eth0
    virtual_router_id 251
    priority 90
    advert_int 1
    mcast_src_ip 192.168.122.165
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 88888888
    }
    track_script {
         verificar_nginx
    }
    virtual_ipaddress {
        192.168.122.50
    }
}
EOF

## 192.168.122.165 é o IP do nó secundário, 192.168.122.50 é o VIP

Crie o script de verificação de saúde, salve-o em /etc/keepalived/verificar_nginx.sh em ambas as máquinas

[root@servidor1 ~]# cat verificar_nginx.sh 
        PROCESSOS_NGINX=`ps aux|grep -v grep|grep nginx|wc -l`
        if [ $PROCESSOS_NGINX -eq 0 ];then
                echo "Nginx não está em execução, finalizando."
                exit 1
        fi
        echo "Verificação do Nginx concluída com sucesso!"

Inicie o serviço keepalived em ambas as máquinas

[root@servidor1 ~]# systemctl enable --now keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
<br></br>

Após a inicialização, faça ping no VIP 192.168.122.50

 

[root@cliente ~]# ping 192.168.122.50
PING 192.168.122.50 (192.168.122.50) 56(84) bytes of data.
64 bytes from 192.168.122.50: icmp_seq=1 ttl=64 time=1.37 ms
64 bytes from 192.168.122.50: icmp_seq=2 ttl=64 time=0.515 ms
64 bytes from 192.168.122.50: icmp_seq=3 ttl=64 time=0.594 ms
64 bytes from 192.168.122.50: icmp_seq=4 ttl=64 time=0.831 ms
^C
--- 192.168.122.50 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.515/0.829/1.376/0.336 ms

#Se o serviço não iniciar, verifique as causas. ps -ef|grep keep para verificar se o serviço está em execução
#Se não iniciar, execute o seguinte comando para reiniciar. Se o serviço iniciar com sucesso, o VIP estará acessível
systemctl start keepalived

Tags: Keepalived nginx alta-disponibilidade balanceamento-de-carga

Publicado em 6-18 05:36