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.www58058.com/2453

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

相關推薦

  • 初涉Linux基本要點概括(1)

    計算機,這里通常指可以儲存程序的計算機,根據馮諾依曼體系,計算機組成部件包括控制單元(Control Unit)、算術邏輯單元(Arithmetic Logic Unit)、內存(Memory)、輸入設備(Input)、輸出設備(Output)。計算機通過輸入的數據和指令,可以完成各種復雜的運算任務,小到文字處理、游戲、影音、大到衛星定位,火箭發射,人工智能…

    Linux干貨 2016-09-26
  • linux防火墻介紹

    一、前言firewall(防火墻):工作在網絡進入或者流包,進出的網絡數據包進行一定的規則進行檢查過濾系統。包括iptables和netfilter組件。iptables 是與 Linux 內核集成的 IP 信息包過濾系統。如果 Linux 系統連接到因特網或 LAN、服務器或連接 LAN 和因特網的代理服務器, 則該系統系統中更好地控制 IP 信息包過濾和…

    2017-04-30
  • lvs

    Lvs 一、lvs集群的類型:4類工作模式 1.1、lvs-nat 特點:通過將請求報文中的目標地址和目標端口修改為挑選出的某RS的RIP和PORT實現轉發。 客戶端訪問lvs集群服務,此時報文的源地址為cip,目標地址為vip,通過lvs進行dnat轉發后端服務器主機,此時,報文的源地址為cip,目標地址為rip;后端主機響應時,報文源地址為rip,目標地…

    Linux干貨 2016-10-30
  • 權限管理

    權限管理包括普通權限、特殊權限、默認權限、ACL權限。 這些權限都是針對u(owner)、g(group)、o(other)三類定義的。 設置權限時文件和目錄不同(此處把文件和目錄分開,一般說文件就包含目錄(linux思想:一切皆文件))如下圖所示, 分別用兩個用戶分別創建了一個目錄一個文件,可以看到同是目錄的liutest和test的權限不同,它倆同為目錄…

    Linux干貨 2017-07-30
  • keepalived實現高可用LVS

    前言 vrrp介紹:  vrrp(Virtual Router Redundancy Protocol)虛擬路由冗余協議;就是把多個路由器或多個網關設備加入到組中,然后在這一組中虛擬出一個路由器,此虛擬路由器有真是的IP和MAC地址,客戶端將網關指向此虛擬路由器的IP;客戶端向此虛擬路由器的IP和MAC地址發起請求時,由活動路由器負責響應;當活動路…

    Linux干貨 2015-07-23
  • 計算機基礎與Linux初識

    一、 計算機的組成及其功能。      計算機五大組成部件:運算器、控制器、存儲器、輸入設備、輸出設備。其中CPU主要由運算器及控制器組成。      1、運算器:進行算數運算、邏輯運算,對數據進行加工處理。      2、控制器…

    2017-07-02
欧美性久久久久