java技术研发

Keepalived

2020-10-22  本文已影响0人  麦芽maiya

收藏了

高可用:两台业务系统启动着相同的服务,如果有一台故障,另一台自动接管,我们将将这个称之为高可用;

Keekpalived工作原理:通过vrrp协议实现

vrrp原理

vrrp抓包

Keepalived工作方式:抢占式、非抢占式

安装:yum install keepalived  -y

日志存放位置:/var/log/messages

1、keepaliaved 抢占式配置

master配置

[root@lb01 ~]# rpm-qc keepalived/etc/keepalived/keepalived.conf/etc/sysconfig/keepalived[root@lb01 ~]# cat/etc/keepalived/keepalived.conf!Configuration Fileforkeepalivedglobal_defs{router_id lb02 #标识信息,一个名字而已;}vrrp_instance VI_1{state MASTER    #角色是masterinterfaceeth0  #vip 绑定端口    virtual_router_id50#让master 和backup在同一个虚拟路由里,id 号必须相同;    priority150#优先级,谁的优先级高谁就是master;advert_int1#心跳间隔时间    authentication{auth_type PASS      #认证        auth_pass1111#密码}virtual_ipaddress{10.0.0.3#虚拟ip}}

backup配置

[root@lb02 ~]# cat/etc/keepalived/keepalived.conf!Configuration Fileforkeepalivedglobal_defs{router_id lb02}vrrp_instance VI_1{state BACKUPinterfaceeth0    virtual_router_id50priority100advert_int1authentication{auth_type PASS        auth_pass1111}virtual_ipaddress{10.0.0.3}}[root@lb02 ~]#

2、Keepalived非抢占式配置:

非抢占式不再有主从之分,全部都为BACKUP,并且配置文件中添加nopreempt,用来标识为非抢占式;

[root@lb01/etc/nginx/upstream]# cat/etc/keepalived/keepalived.conf!Configuration Fileforkeepalivedglobal_defs{router_id lb01}vrrp_instance VI_1{state MASTERinterfaceeth0    virtual_router_id50priority150advert_int1authentication{auth_type PASS        auth_pass1111}virtual_ipaddress{10.0.0.3}}

[root@lb02/etc/nginx/upstream]# cat/etc/keepalived/keepalived.conf!Configuration Fileforkeepalivedglobal_defs{router_id lb02}vrrp_instance VI_1{state BACKUPinterfaceeth0    virtual_router_id50priority100nopreempt    advert_int1authentication{auth_type PASS        auth_pass1111}virtual_ipaddress{10.0.0.3}}You havenewmail in/var/spool/mail/root[root@lb02/etc/nginx/upstream]#

3、nginx+keepalived

实现思路:将keepalived 中的vip作为nginx负载均衡的监听地址,并且域名绑定的也是vip的地址。

说明:Nginx 负载均衡实现高可用,需要借助Keepalived地址漂移功能。

在不考虑后端数据库和存储的时候如下架构

keepalived+Nginx

两台负载均衡配置:

[root@lb01/etc/nginx/upstream]# ip add show eth02:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdisc pfifo_fast state UP groupdefaultqlen1000link/ether00:0c:29:97:e1:ff brd ff:ff:ff:ff:ff:ff    inet10.0.0.5/24brd10.0.0.255scope global eth0      valid_lft forever preferred_lft forever    inet10.0.0.3/32scope global eth0      valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe97:e1ff/64scope link        valid_lft forever preferred_lft forever[root@lb01/etc/nginx/upstream]# cat/etc/keepalived/keepalived.conf!Configuration Fileforkeepalivedglobal_defs{router_id lb01}vrrp_instance VI_1{state MASTERinterfaceeth0    virtual_router_id50priority150advert_int1authentication{auth_type PASS        auth_pass1111}virtual_ipaddress{10.0.0.3}}[root@lb01/etc/nginx/upstream]#

[root@lb02/etc/nginx/upstream]# ip add show eth02:eth0:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdisc pfifo_fast state UP groupdefaultqlen1000link/ether00:0c:29:6f:18:48brd ff:ff:ff:ff:ff:ff    inet10.0.0.6/24brd10.0.0.255scope global eth0      valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe6f:1848/64scope link        valid_lft forever preferred_lft forever[root@lb02/etc/nginx/upstream]# cat/etc/keepalived/keepalived.conf!Configuration Fileforkeepalivedglobal_defs{router_id lb02}vrrp_instance VI_1{state BACKUPinterfaceeth0    virtual_router_id50priority100nopreempt    advert_int1authentication{auth_type PASS        auth_pass1111}virtual_ipaddress{10.0.0.3}}[root@lb02/etc/nginx/upstream]#

4、keepalived脑裂现象

由于某些原因,导致两台keepalived高可用服务器在指定时间内,无法检测到对方存活心跳信息,从而导致互相抢占对方的资源和服务所有权,然而此时两台高可用服务器有都还存活。

可能出现的原因:

1、服务器网线松动等网络故障;

2、服务器硬件故障发生损坏现象而崩溃;

3、主备都开启了firewalld 防火墙。

4、在Keepalived+nginx 架构中,当Nginx宕机,会导致用户请求失败,但是keepalived不会进行切换,

所以需要编写一个检测nginx的存活状态的脚本,如果nginx不存活,则kill掉宕掉的nginx主机上面的keepalived。(所有的keepalived都要配置)

架构如下:

image.png

脚本如下:

[root@lb01/server/scripts]# cat/server/scripts/check_list #!/bin/shnginxpid=$(ps-C nginx--no-header|wc-l)#1.判断Nginx是否存活,如果不存活则尝试启动Nginxif[$nginxpid-eq0];then    systemctl start nginx    sleep3#2.等待3秒后再次获取一次Nginx状态    nginxpid=$(ps-C nginx--no-header|wc-l)#3.再次进行判断,如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本if[$nginxpid-eq0];then        systemctl stop keepalived  fifi[root@lb01/server/scripts]#

配置文件如下:

[root@lb01/server/scripts]# cat/etc/keepalived/keepalived.conf!Configuration Fileforkeepalivedglobal_defs{router_id lb01}vrrp_script check{script"/server/scripts/check_list"interval10}vrrp_instance VI_1{state MASTERinterfaceeth0    virtual_router_id50priority150advert_int1authentication{auth_type PASS        auth_pass1111}virtual_ipaddress{10.0.0.3}track_script{check}}[root@lb01/server/scripts]#

30人点赞

Linux

作者:你好_请关照

链接:https://www.jianshu.com/p/a6b5ab36292a

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

上一篇下一篇

猜你喜欢

热点阅读