HAproxy reload config file with uninterrupt session

HAProxy is a high performance load balancer. It is very light-weight, and free, making it a great option if you are in the market for a load balancer and need to keep your costs down.

Lately we’ve been making a lot of load balancer changes at work to accommodate new systems and services. Even though we have two load balancers running with keepalived taking care of any failover situations, I was thinking about how we go about reloading our configuration files. In the event of a change, the “common” way to get the changes to take effect is to run /etc/init.d/haproxy restart. This is bad for a couple major reasons:

You are temporarily shutting your load balancer down
You are severing any current connections going through the load balancer
You might say, “if you have two load balancers with keepalived, restarting the service should be fine since keepalived will handle the failover.” This, however, isn’t always true. Keepalived uses advertisements to determine when to fail over. The default advertisement interval is 1 second (configurable in keepalived.conf). The skew time helps to keep everyone from trying to transition at once. It is a number between 0 and 1, based on the formula (256 – priority) / 256. As defined in the RFC, the backup must receive an advertisement from the master every (3 * advert_int) + skew_time seconds. If it doesn’t hear anything from the master, it takes over.

Let’s assume you are using the default interval of 1 second. On my test machine, this is the duration of time it takes to restart haproxy:

time /etc/init.d/haproxy restart
Restarting haproxy haproxy
   ...done.real    0m0.022s
user    0m0.000s
sys     0m0.016s

In this situation, haproxy would restart much faster than your 1 second interval. You could get lucky and happen to restart it just before the check, but luck is not consistent enough to be useful. Also, in very high-traffic situations, you’ll be causing a lot of connection issues. So we cannot rely on keepalived to solve the first problem, and it definitely doesn’t solve the second problem.

After sifting through haproxy documentation (the text-based documentation, not the man page) (/usr/share/doc/haproxy/haproxy-en.txt.gz on Ubuntu), I came across this:

    313
    314     global    315         daemon    316         quiet    317         nbproc  2
    318         pidfile /var/run/haproxy-private.pid    319
    320     # to stop only those processes among others :    321     # kill $(</var/run/haproxy-private.pid)    
    322
    323     # to reload a new configuration with minimal service impact and without    
    324     # breaking existing sessions :    
    325     # haproxy -f haproxy.cfg -p $(</var/run/haproxy-private.pid) -st $(</var/run/haproxy-private.pid)

That last command is the one of interest. The -p asks the process to write down each of its children’s pids to the specified pid file, and the -st specifies a list of pids to send a SIGTERM to after startup. But it does this in an interesting way:

    609 The '-st' and '-sf' command line options are used to inform previously running
    610 processes that a configuration is being reloaded. They will receive the SIGTTOU    
    611 signal to ask them to temporarily stop listening to the ports so that the new
    612 process can grab them. If anything wrong happens, the new process will send
    613 them a SIGTTIN to tell them to re-listen to the ports and continue their normal
    614 work. Otherwise, it will either ask them to finish (-sf) their work then softly    
    615 exit, or immediately terminate (-st), breaking existing sessions. A typical use    
    616 of this allows a configuration reload without service interruption :    
    617
    618  # haproxy -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)

The end-result is a reload of the configuration file which is not visible by the customer. It also solves the second problem! Let’s look at an example of the command and look at the time compared to our above example:

# time haproxy -f /etc/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid)

real    0m0.018s
user    0m0.000s
sys     0m0.004s

I’ve specified the config file I want to use and the pid file haproxy is currently using. The $(cat /var/run/haproxy.pid) takes the output of cat /var/run/haproxy.pid and passes it in to the -sf parameter as a list, which is what it is expecting. You will notice that the time is actually faster too (.012s sys, and .004s real). It may not seem like much, but if you are dealing with very high volumes of traffic, this can be pretty important. Luckily for us it doesn’t matter because we’ve been able to reload the haproxy configuration without dropping any connections and without causing any customer-facing issues.

UPDATE: There is a reload in some of the init.d scripts (I haven’t checked every OS, so this can vary), but it uses the -st option which will break existing sessions, as opposed to using -sf to do a graceful hand-off. You can modify the haproxy_reload() function to use the -sf if you want. I also find it a bit confusing that the documentation uses $(cat /path/to/pidfile) whereas this haproxy_reload() function uses $(<$PIDFILE). Either should work, but really, way to lead by example…

转自:http://www.cnblogs.com/Bozh/p/4169969.html

原创文章,作者:s19930811,如若转载,请注明出处:http://www.178linux.com/2453

(0)
s19930811s19930811
上一篇 2015-04-03 22:07
下一篇 2015-04-03 22:10

相关推荐

  • httpd功能配置之CGI程序

        httpd服务中有一个cgi-bin目录,此目录专门用于存放cgi脚本。CGI即网关通用接口,用于实现动态网页。下面简单编写一个CGI脚本来进行测试此功能:     1、在/var/www/cgi-bin/目录下创建一个脚本     2、重启服务     3、验证 &nb…

    Linux干货 2016-03-11
  • 推荐-LVM

    LVM LVM Linux应用 1.LVM简介 LVM:Logical Volume Manager,逻辑卷管理器。LVM利用Linux内核的device-mapper模块来实现存储系统的虚拟化(系统分区独立于底层硬件)。通过LVM,你可以实现存储空间的抽象化并在上面建立虚拟分区(virtual partitions),可以更简便地扩大和缩小分区,可以增删分…

    2016-04-11
  • 第十九周作业

    1、描述Tomcat的架构; 核心架构图: 架构简介: Server是管理Service接口的,是Tomcat的一个顶级容器。管理着多个Service; Service 是服务,管理着一个Container和多个Connector,Service的存在依赖于Server; Container : 一个或者多个Container 可以对应一个Connector…

    2017-06-23
  • 马哥教育网络21期+第十周练习博客

    马哥教育网络21期+第十周练习博客 1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) CentOS启动流程:1,加电自检->Boot Sequence–>加载内核文件 BOOT Sequence中包含了MBR和GRUB     MBR:记录磁盘扇区,共512字…

    Linux干货 2016-09-19
  • shell之变量总结

    1 什么是shell变量 按照定义来说,变量是存储设备中的一些可读写的存储单元.变量可以定制用户本身的工作环境。使用变量可以保存有用信息,使系统获知用户相关设置。变量也用 于保存暂时信息。例如:一变量为EDITOR,系统中有许多编辑工具,但哪一个适用于系统呢?将此编辑器名称赋给EDITOR,这样,在使用cron或其 他需要编辑器的应用时,这就是你将一直使用的…

    Linux干货 2016-08-15
  • Linux中的文本处理工具

    本章内容:   一、各种文本工具来查看、分析,统计文本文件   二、grep   三、正则表达式   四、扩展正则表达式   五、Sed 具体内容如下: 一、抽取文本的工具:      文件内容:less和cat      文件截取:head和tai…

    Linux干货 2016-08-08