High Availability Configuration

High Availability Configuration

Implement high availability reverse proxy setup:

#!/bin/bash
# /usr/local/bin/setup-ha-proxy.sh

# Install keepalived for VRRP
apt-get install keepalived

# Configure keepalived for primary node
cat > /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
    notification_email {
        [email protected]
    }
    notification_email_from [email protected]
    smtp_server localhost
    smtp_connect_timeout 30
    router_id NGINX_MASTER
}

vrrp_script check_nginx {
    script "/usr/local/bin/check_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass SecurePass123
    }
    virtual_ipaddress {
        192.168.1.100/24 dev eth0 label eth0:1
    }
    track_script {
        check_nginx
    }
    notify_master "/usr/local/bin/notify_master.sh"
    notify_backup "/usr/local/bin/notify_backup.sh"
    notify_fault "/usr/local/bin/notify_fault.sh"
}
EOF

# Create nginx check script
cat > /usr/local/bin/check_nginx.sh << 'EOF'
#!/bin/bash
if ! pidof nginx > /dev/null; then
    exit 1
fi

# Check if nginx is responding
if ! curl -f http://localhost/health > /dev/null 2>&1; then
    exit 1
fi

exit 0
EOF

chmod +x /usr/local/bin/check_nginx.sh

# Notification scripts
cat > /usr/local/bin/notify_master.sh << 'EOF'
#!/bin/bash
echo "$(date): Became MASTER" >> /var/log/keepalived-state.log
# Update DNS or other services
EOF

chmod +x /usr/local/bin/notify_master.sh