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
下一篇 2015-04-03

相关推荐

  • Linux路由实验

    一    实验背景:路由器的功能是将不同网段的主机建立通信,本次试验是在使用linux主机当作路由器,完成主机间通信。如下图所示 二、实验环境:主机A和B为CentOS7.2,主机C和D为CentOS6.8,且C和D上分别都有两块网卡 三、实验步骤     注意:为避免干扰项,需将每台主机…

    Linux干货 2016-09-07
  • redis-cli的一些有趣也很有用的功能

    redis-cli我们最常用的三个参数就是-h、-p、-a选项,分配用来指定连接的redis-server的host、port和登录密码。通过redis-cli –help发现,redis-cli还提供了其他很多的参数和功能。 1)-x-x选项从标准输入(stdin)读取最后一个参数。 比如从管道中读取输入: echo -en &quot…

    Linux干货 2016-04-13
  • 用户创建过程&grep练习

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。     ~]# cp -r /etc/skel /home/tuser1     ~]# ls -la /home/tuser1  &…

    Linux干货 2016-10-17
  • 用户,组和权限管理 基础命令整理

    安全3A任何一个安全策略都应该有Authentication(认证)、Authentication(授权)、Accouting|Audition(审计)这三个措施。 用户(User)Linux用户是安全3A中的Authentication(认证)措施。用户在登录时,系统会检查用户输入的用户名和密码。如果用户输入的用户名和密码匹配,会根据用户信息给用户分配一个…

    Linux干货 2017-03-16
  • 权限管理

           学习文件和目录的权限管理,不仅在学习上对我们的帮助很大,而且在工作上帮助也很大,因为你对文件或目录设置了权限,其他用户只能根据你设置的权限对文件或目录进行浏览,修改,删除或执行,也正是如此,权限的管理很重要。下面我就对Linux的权限管理进行简单的介绍。 Linux下的文件类型 -:普…

    Linux干货 2016-08-08
  • 萝卜运维记–第一周

    part1— ①描述计算机的组成及功能 ▲计算机是什么? 计算机(ENIAC):简述—-接收用户的指令,经过中央处理器处理过后,产生对使用者有用的信息。具有存储记忆,处理复杂数据运算的计算机器 Ⅰ 计算机组成及功能 ☆硬件部分: 1.中央处理器CPU(Central Processing Unit)↓↓↓↓:  &n…

    Linux干货 2016-12-06