Keepalived+nginx造成流量异常

简介:

1、使用虚拟机搭建环境

也是A、B 2台机器,使用一样的配置和软件。

环境:CentOS6.5 2台虚拟机

keepalived版本1.2.19

tengine版本2.1.2

节点A :

[root@A keepalived]# cat keepalived.conf

! Configuration File for keepalived

global_defs { 
   notification_email { 
     admin@localhost.com             #设置报警邮件地址,可以设置多个,每行一个。 需开启本机的sendmail服务 
   } 
   notification_email_from  admin@lvtao.net        #设置邮件的发送地址 
   smtp_server 127.0.0.1                                #设置smtp server地址 
   smtp_connect_timeout 30                              #设置连接smtp server的超时时间 
   router_id LVS_DEVEL                                  #表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题的信息 
}

vrrp_script check_nginx { 
    script "/etc/keepalived/check_http_port" 
    interval 2                  #检查间隔 
    weight 5                    #权重 
}

vrrp_instance VI_1 { 
    state BACKUP              #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器 
    interface eth0            #指定HA监测网络的接口 
    virtual_router_id 51      #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的 
    priority 100               #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级 
    advert_int 1              #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒 
    nopreempt                 #设置 nopreempt 防止抢占资源,只生效BACKUP节点 
    authentication {          #设置验证类型和密码 
        auth_type PASS        #设置验证类型,主要有PASS和AH两种 
        auth_pass 1111        #设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信 
    } 
    virtual_ipaddress {       #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个 
        10.0.0.15 
    } 
    track_script { 
        check_nginx 
    } 
}

virtual_server 10.0.0.15 80 { 
    delay_loop 6 
    lb_algo wrr 
    lb_kind DR 
    persistence_timeout 50 
    protocol TCP

    real_server 10.0.0.13 80 { 
        weight 1 
        TCP_CHECK { 
            connect_port 80 
            connect_timeout 3 
            delay_before_retry 3 
        } 
    }

    real_server 10.0.0.14 80 { 
        weight 1 
        TCP_CHECK { 
            connect_port 80 
            connect_timeout 3 
            delay_before_retry 3 
        } 
    } 
}

 

节点B:

[root@B keepalived]# cat keepalived.conf

! Configuration File for keepalived

global_defs { 
   notification_email { 
     admin@localhost.com             #设置报警邮件地址,可以设置多个,每行一个。 需开启本机的sendmail服务 
   } 
   notification_email_from  admin@lvtao.net        #设置邮件的发送地址 
   smtp_server 127.0.0.1                                #设置smtp server地址 
   smtp_connect_timeout 30                              #设置连接smtp server的超时时间 
   router_id LVS_DEVEL                                  #表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题的信息 
}

vrrp_script check_nginx { 
    script "/etc/keepalived/check_http_port" 
    interval 2                  #检查间隔 
    weight 5                    #权重 
}

vrrp_instance VI_1 { 
    state BACKUP              #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器 
    interface eth0            #指定HA监测网络的接口 
    virtual_router_id 51      #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的 
    priority 99               #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级 
    advert_int 1              #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒 
    nopreempt                 #设置 nopreempt 防止抢占资源,只生效BACKUP节点 
    authentication {          #设置验证类型和密码 
        auth_type PASS        #设置验证类型,主要有PASS和AH两种 
        auth_pass 1111        #设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信 
    } 
    virtual_ipaddress {       #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个 
        10.0.0.15 
    } 
    track_script { 
        check_nginx 
    } 
}

virtual_server 10.0.0.15 80 { 
    delay_loop 6 
    lb_algo wrr 
    lb_kind DR 
    persistence_timeout 50 
    protocol TCP

    real_server 10.0.0.13 80 { 
        weight 1 
        TCP_CHECK { 
            connect_port 80 
            connect_timeout 3 
            delay_before_retry 3 
        } 
    }

    real_server 10.0.0.14 80 { 
        weight 1 
        TCP_CHECK { 
            connect_port 80 
            connect_timeout 3 
            delay_before_retry 3 
        } 
    } 
}

 

[root@A keepalived]# cat relserver.sh 
#!/bin/bash 
#description: Config realserver

VIP=10.0.0.15

. /etc/rc.d/init.d/functions

case "$1" in

start)

       /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP

       /sbin/route add -host $VIP dev lo:0

       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

       sysctl -p >/dev/null 2>&1

       echo "RealServer Start OK"

       ;;

stop)

       /sbin/ifconfig lo:0 down

       /sbin/route del $VIP >/dev/null 2>&1

       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

       echo "RealServer Stoped"

       ;;

*)

       echo "Usage: $0 {start|stop}"

       exit 1

esac

exit 0

[root@A keepalived]#

 

[root@A keepalived]# cat check_http_port 
#!/bin/bash 
#思路:1、使用curl检查本地nginx可用性 
#      2、检查失败尝试启动nginx 
#      3、仍失败,则关闭本地keepalived

NGINX=/usr/local/nginx/sbin/nginx 
PORT="80"

#curl -v -I -m 10 -o /dev/null -s -w %{http_code}"\n" http://127.0.0.1/ 
curl http://127.0.0.1:$PORT 
if [ $? -ne 0 ]; then 
        #重启nginx 
        /etc/init.d/nginx restart 
#       $NGINX -s stop 
#       $NGINX 
        sleep 3 
        curl http://127.0.0.1/ 
        [ $? -ne 0 ] && /etc/init.d/keepalived stop 
fi

exit 0 
[root@A keepalived]#

 

2、3个客户端,依次访问。就能重现流量异常。

[root@tsm-test-centos6 ~]# curl 10.0.0.15 
<!DOCTYPE html> 
<html> 
<head> 
<title>Welcome to tengine!</title> 
<style> 
    body { 
        35em; 
        margin: 0 auto; 
        font-family: Tahoma, Verdana, Arial, sans-serif; 
    } 
</style> 
</head> 
<body> 
Welcome to tengine! B 

If you see this page, the tengine web server is successfully installed and 
working. Further configuration is required.

For online documentation and support please refer to 
<a href="http://tengine.taobao.org/">tengine.taobao.org.

<em>Thank you for using tengine.</em>


</body> 
</html> 
[root@tsm-test-centos6 ~]#

 

 

[root@tsm-app-dev1 ~]# curl 10.0.0.15 
<!DOCTYPE html> 
<html> 
<head> 
<title>Welcome to tengine!</title> 
<style> 
    body { 
        35em; 
        margin: 0 auto; 
        font-family: Tahoma, Verdana, Arial, sans-serif; 
    } 
</style> 
</head> 
<body> 
Welcome to tengine! A 

If you see this page, the tengine web server is successfully installed and 
working. Further configuration is required.

For online documentation and support please refer to 
<a href="http://tengine.taobao.org/">tengine.taobao.org.

<em>Thank you for using tengine.</em>


</body> 
</html> 
[root@tsm-app-dev1 ~]#

 

10.0.0.8(我的笔记本)

image

 

image

 

原因不得而知,在此记录。便日后寻得原因。



本文转自 ygqygq2 51CTO博客,原文链接:http://blog.51cto.com/ygqygq2/1785087,如需转载请自行联系原作者

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
2月前
|
前端开发 Java 应用服务中间件
Nginx访问异常的解决方法
Nginx访问异常的解决方法
|
4月前
|
应用服务中间件 nginx
百度搜索:蓝易云【利用nginx内置ngx_http_mirror_module模块实现流量复制及流量放大】
以上就是使用Nginx内置 `ngx_http_mirror_module`模块实现流量复制和流量放大的简要示例。通过合理配置和利用该模块,可以实现更复杂的流量控制和调试需求。
60 1
|
25天前
|
运维 应用服务中间件 Linux
keepalived详解(三)——keepalived与Nginx配合实战
keepalived详解(三)——keepalived与Nginx配合实战
30 1
|
5天前
|
负载均衡 网络协议 应用服务中间件
【亮剑】在 Linux 中如何使用 HAProxy、Nginx 和 Keepalived 进行负载均衡?
【4月更文挑战第30天】本文介绍了在Linux中构建高可用性和高性能网络服务的负载均衡工具HAProxy、Nginx和Keepalived。HAProxy是一个高性能的开源TCP和HTTP负载均衡器,适合处理大量并发连接;Nginx是一个多功能Web服务器和反向代理,支持HTTP、HTTPS和TCP负载均衡,同时提供缓存和SSL功能;Keepalived用于监控和故障切换,通过VRRP实现IP热备份,保证服务连续性。文中详细阐述了如何配置这三个工具实现负载均衡,包括安装、配置文件修改和启动服务,为构建可靠的负载均衡系统提供了指导。
|
24天前
|
存储 安全 应用服务中间件
解密Nginx限流机制:有效应对DDoS攻击与高并发流量
解密Nginx限流机制:有效应对DDoS攻击与高并发流量
38 0
|
20天前
|
Kubernetes 搜索推荐 应用服务中间件
通过keepalived+nginx实现 k8s apiserver节点高可用
通过keepalived+nginx实现 k8s apiserver节点高可用
36 16
|
5月前
|
应用服务中间件 nginx
nginx405异常原因与解决方法,最详细解释
nginx405异常原因与解决方法,最详细解释
134 1
|
3月前
|
负载均衡 监控 应用服务中间件
Nginx负载均衡:你的网站流量翻倍利器
Nginx负载均衡:你的网站流量翻倍利器
46 0
|
5月前
|
负载均衡 前端开发 应用服务中间件
NGINX高可用之keepalived+nginx主从模式+主主模式配置实践
NGINX高可用之keepalived+nginx主从模式+主主模式配置实践
148 1
|
6月前
|
运维 监控 应用服务中间件
用 Golang 采集 Nginx 接口流量大小
用 Golang 采集 Nginx 接口流量大小