Nginx主从配置、Keepalived、VRRP协议
实现方法
1、部署两台nginx服务器
2、在上面两台服务器中下载安装keepalived,yum -y install keepalived, 生产上最好使用源码安装方式,因为它为外部调用
3、配置keepalived.conf,vim /etc/keepalived/keepalived.conf
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 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL # 默认是LVS_DEVEL,/etc/hosts里需要配置127.0.0.1为LVS_DEVEL vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } # 检测脚本设置,这个原文件没有 vrrp_script chk_http_port { script "/usr/local/src/nginx_check.sh" interval 2 # 检测脚本执行的间隔,秒 weight 2 } # 虚拟ip配置 vrrp_instance VI_1 { state MASTER # 主 MASTER 从 BACKUP interface ens33 # 网卡 virtual_router_id 51 # 主从必须相同,标识主从服务器为一组,一方出错另一方替代 priority 100 # 主备优先级,主100,从90 advert_int 1 # 发送心跳间隔时间 authentication {# 权限 auth_type PASS # 使用密码方式校验 auth_pass 1111 # 密码是1111 } virtual_ipaddress { # 虚拟地址,可以绑定多个 192.168.200.16 192.168.200.17 192.168.200.18 } } virtual_server 192.168.200.100 443 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 192.168.201.100 443 { weight 1 SSL_GET { url { path / digest ff20ad2481f97b1754ef3e12ecd3a9cc } url { path /mrtg/ digest 9b3a0c85a887a256d6939da88aabd8cd } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } virtual_server 10.10.10.2 1358 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP sorry_server 192.168.200.200 1358 real_server 192.168.200.2 1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl2/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl3/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.200.3 1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334c } url { path /testurl2/test.jsp digest 640205b7b0fc66c1ea91c463fac6334c } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } virtual_server 10.10.10.3 1358 { delay_loop 3 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP real_server 192.168.200.4 1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl2/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl3/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.200.5 1358 { weight 1 HTTP_GET { url { path /testurl/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl2/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } url { path /testurl3/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
脚本文件,由keepalived.conf文件指定了路径
#!/bin/bash A=`ps -C nginx -no-header | wc -1` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header | wc -1` -eq 0 ];then killall keepalived fi fi