keepalived高可用

1. keepalived简介

  keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived是自动完成,不需人工干涉。keepalived 软件有两种功能,分别是监控检查、VRRP冗余协议。

  Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。

  layer3, 4 & 5工作在 IP/TCP 协议栈的 IP层、传输层和应用层,实现原理分别如下:

  Layer3::Keepalived使用Layer3的方式工作式时,Keepalived会定期向服务器群中的服务器发送一个ICMP的数据包(既我们平时用的Ping程序),如果发现某台服务的IP地址没有激活,Keepalived便报告这台服务器失效,并将它从服务器群中剔除,这种情况的典型例子是某台服务器被非法关机。Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准。

  Layer4:Layer4主要以TCP端口的状态来决定服务器工作正常与否。如web server 的服务端口一般是80,如果keepalived 检测到80 端口没有启动,测 keepalived 将把这套服务器从服务器群中剔除。

  Layer5:可以运行FTP,TELNET,SMTP,DNS等各种不同类型的高层协议,Keepalived的运行方式也更加全面化和复杂化,用户可以通过自定义Keepalived工作方式,例如:可以通过编写程序或者脚本来运行Keepalived,而Keepalived将根据用户的设定参数检测各种程序或者服务是否允许正常,如果Keepalived的检测结果和用户设定的不一致时,Keepalived将把对应的服务器从服务器集群中剔除。

2. keepalived VRRP 原理剖析

  VRRP 的目的是为了解决静态路由单点问题。VRRP 通过竞选(election)协议动态的将路由认为交给LAN 中虚拟路由器中的某台VRRP路由器。

  在VRRP虚拟路由集群中,。。

3. nginx+keepalived

3.1 环境准备

  nginx-1:192.168.80.133(master)

  nginx-2:192.168.80.134(backup)

3.2 nginx 安装

https://www.cnblogs.com/sswind/p/11996629.html

3.3 keepalived安装配置

下载地址:https://www.keepalived.org/download.html

源码安装

# tar -zxvf keepalived-2.0.20.tar.gz
# cd keepalived-2.0.20
# ./configure --prefix=/usr/local/keepalived
# make && make install
# cp keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir -p /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# *** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

# yum -y install libnl libnl-devel  
# yum -y install keepalived
3.3.1 配置文件

# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lvs1
}
vrrp_script check {
   script "/data/sh/check_nginx.sh"
   interval 2
   weight 2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.80.250
    }
    track_script {
    check
    }
}

# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lvs2
}
vrrp_script check {
   script "/data/sh/check_nginx.sh"
   interval 2
   weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.80.250
    }
    track_script {
    check
    }
}

3.4 检测脚本

需创建脚本用以检查本地nginx是否存活,如果不存活,则停止 keepalived 服务。

# vim check_nginx.sh 

#!/bin/bash
#auto check nginx process
killall -0 nginx
if [[ $? -ne 0 ]];then
    killall keepalived
fi

在两台nginx服务器上新建测试页面,然后启动nginx服务,访问VIP地址。即http://192.168.80.250/

 4. nginx+keepalived 双主架构

  nginx+keepalived 主备模式,始终存在一台服务器处于空闲状态,如何更好地把两台服务器利用起来?可以借助 nginx+keepalived 双主架构来实现。

4.1 master1 上keepalived.conf 配置文件内容

# cat keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lvs1
}
vrrp_script check {
   script "/data/sh/check_nginx.sh"
   interval 2
   weight 2
}
# VIP1 vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id
51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.80.250 } track_script { check } } #VIP2 vrrp_instance VI_2 { state BACKUP interface ens33 virtual_router_id 52 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.80.251 } track_script { check } }

4.2 master2 上keepalived.conf 配置文件内容

# cat keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lvs2
}
vrrp_script check {
   script "/data/sh/check_nginx.sh"
   interval 2
   weight 2
}
#VIP1
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.80.250
    }
    track_script {
    check
    }
}
#VIP2
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }
    virtual_ipaddress {
        192.168.80.251
    }
    track_script {
    check
    }
}

4.3 nginx+keepalived 双主企业架构,在日常维护及管理过程中需注意以下几个方面

  keepalived 主配置文件必须设置不同的 VRRP 名称,同时优先级和VIP设置也各不相同

  nginx 网站总访问量为两台nginx服务器访问之和,可以写脚本自动统计访问量

  两台nginx 为master,存在两个 VIP 地址,用户从外网访问VIP,需配置域名映射到两个VIP上即可

  通过外网DNS 映射不同 VIP 的方法也称为DNS 负载模式

  可以通过zabbix 实时监控 VIP访问状态是否正常

 

posted @ 2020-03-14 17:20  一窗明月半帘风  阅读(218)  评论(0编辑  收藏  举报