Nginx + Keepalived 如何更好的把2台服务器利用起来,可以借助Nginx + Keepalived 双主架构来实现,同时两台对外提供服务,拥有2个VIP地址,同时接收用户的请求。
安装Nginx 与 Keepalived 安装方法不再赘述
(1)master1 上的keepalived.conf 配置文件内容如下:
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/data/sh/check_nginx.sh" interval 2 weight 2 } # VIP1 vrrp_instance VI_1 { state MASTER interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 151 priority 100 adver_int 5 nopreempt authentication { auth_type PASS auth_type 1111 } virtual_ipaddress{ 192.168.33.188 } track_script { chk_nginx } } # VIP1 vrrp_instance VI_2 { state BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 152 priority 90 adver_int 5 nopreempt authentication { auth_type PASS auth_type 2222 } virtual_ipaddress{ 192.168.33.199 } track_script { chk_nginx } }
(2)master2上的keepalived.conf配置文件内容如下:
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/data/sh/check_nginx.sh" interval 2 weight 2 } # VIP1 vrrp_instance VI_1 { state MASTER interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 151 priority 90 adver_int 5 nopreempt authentication { auth_type PASS auth_type 1111 } virtual_ipaddress{ 192.168.33.188 } track_script { chk_nginx } } # VIP1 vrrp_instance VI_2 { state BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 152 priority 100 adver_int 5 nopreempt authentication { auth_type PASS auth_type 2222 } virtual_ipaddress{ 192.168.33.199 } track_script { chk_nginx } }
(3)两台Nginx服务器上配置 /data/sh/chechk_nginx.sh脚本内容如下:
#!/bin/bash # auto check nginx process killall -0 nginx if [[ $? -ne 0 ]]; then /etc/init.d/keepalived stop fi