nginx-keepalive-高可用

 

keepalive的HA分为抢占模式和非抢占模式

 

抢占模式指:在master挂机恢复后会把VIP从backup节点中抢占过来,再次升级为master。

非抢占模式指:在master挂机恢复后不会抢占backup再次升级master。 

配置

基本环境:nginx01Master:192.168.80.137
          nginx02Backup:192.168.80.132
          VIP    :192.168.80.100
         关闭iptables,firewall,selinux
基本操作:
        时间校准:update ntp.api.bz

     

  抢占模式配置

 

	#####master(192.168.80.137)keepalived.conf配置如下>#######
[root@liusq-2 keepalived]# cat 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
}
vrrp_script chk_nginx {
	script "/etc/keepalived/nginx_check.sh"
	interval 4
	weight -20
	}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.80.100
    }
	track_script {
	chk_nginx
	}
}
	#####master(192.168.80.132)keepalived.conf配置如下>#######
[root@liusq-1 keepalived]# cat 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
}
vrrp_script chk_nginx {
	script "/etc/keepalived/nginx_check.sh"
	interval 4
	weight -20
	}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.80.100
    }
	track_script {
	chk_nginx
	}
}



##################nginx_check.sh#############
[root@liusq-2 keepalived]# cat nginx_check.sh
#!/bin/sh
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT=80
netstat -antp|grep nginx 

if [ $? -ne 0 ];then
    $NGINX -s stop
    $NGINX
    sleep 3

netstat -antp|grep nginx 
    [ $? -ne 0 ] && service keepalived stop
fi

 


非抢占模式
    
    #####master(192.168.80.10)配置如下>#######
    global_defs {
   router_id nginx_01  #标识本节点的名称,通常为hostname
}

vrrp_script chk_nginx {
       script "/etc/keepalived/nginx_check.sh"
       interval 2
       weight -20
}

vrrp_instance VI_1 {
    state BACKUP                  
    interface enp0s3              
    virtual_router_id 51          
    mcast_src_ip 192.168.1.201    
    priority 100                  
    advert_int 1                  
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.210
    }

    track_script {
       chk_nginx  # nginx存活状态检测脚本
    }
}

    #####backup(192.168.80.20)配置如下>#######

global_defs {
   router_id nginx_02
}

vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface enp0s3
    virtual_router_id 51
    mcast_src_ip 192.168.1.202
    priority 90
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.210
    }
    track_script {
       chk_nginx
    }
}

和非抢占模式的配置相比,只改了两个地方:
1> 在vrrp_instance块下两个节点各增加了nopreempt指令,表示不争抢vip
2> 节点的state都为BACKUP
两个keepalived节点都启动后,默认都是BACKUP状态,双方在发送组播信息后,会根据优先级来选举一个MASTER出来。
由于两者都配置了nopreempt,所以MASTER从故障中恢复后,不会抢占vip。这样会避免VIP切换可能造成的服务延迟。
 

有兴趣学习交流IT行业技术的兄弟们,请加群89331935,加的时候请备注:CSDN博客看到的

我微信号:Agoni399     加的时候请备注:CSDN博客看到的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值