True Zero Downtime HAProxy Reloads--转载

原文地址:http://engineeringblog.yelp.com/2015/04/true-zero-downtime-haproxy-reloads.html

HAProxy: Cornerstone of Reliable Websites

One primary goal of the infrastructure teams here at Yelp is to get as close to zero downtime as possible. This means that when users make requests for www.yelp.com we want to ensure that they get a response, and that they get a response as fast as possible. One way we do that at Yelp is by using the excellent HAProxy load balancer. We use it everywhere: for our external load balancing, internal load balancing, and with our move to a Service Oriented Architecture, we find ourselves running HAProxy on every machine at Yelp as part of SmartStack.

We love the flexibility that SmartStack gives us in developing our SOA, but that flexibility comes at a cost. When services or service backends are added or permanently removed, HAProxy has to reload across our entire infrastructure. These reloads can cause reliability problems because while HAProxy is top notch at not dropping traffic while it is running, it can (and does) drop traffic during reloads.

HAProxy Reloads Drop Traffic

As of version 1.5.11, HAProxy does not support zero downtime restarts or reloads of configuration. Instead, it supports fast reloads where a new HAProxy instance starts up, attempts to useSO_REUSEPORT to bind to the same ports that the old HAProxy is listening to and sends a signal to the old HAProxy instance to shut down. This technique is very close to zero downtime on modern Linux kernels, but there is a brief period of time during which both processes are bound to the port. During this critical time, it is possible for traffic to get dropped due to the way that the Linux kernel(mis)handles multiple accepting processes. In particular, the issue lies with the potential for new connections to result in a RST from HAProxy. The issue is that SYN packets can get put into the old HAProxy’s socket queue right before it calls close, which results in a RST of those connections.

There are various workarounds to this issue. For example, Willy Tarreau, the primary maintainer of HAProxy, has suggested that users can drop SYN packets for the duration of the HAProxy restart so that TCP automatically recovers. Unfortunately, RFC 6298 dictates that the initial SYN timeout should be 1s, and the Linux kernel faithfully hardcodes this. As such, dropping SYNs mean that any connections that attempt to establish during the 20-50ms of an HAProxy reload will encounter an extra second of latency or more. The exact latency depends on the TCP implementation of the client, and while some mobile devices retry as fast as 200ms, many devices only retry after 3s. Given the number of HAProxy reloads and the level of traffic Yelp has, this becomes a barrier to the reliability of our services.

Making HAProxy Reloads Not Drop Traffic

To avoid this latency, we built on the solution proposed by Willy. His solution actually works very well at not dropping traffic, but the extra second of latency is a problem. A better solution for us would be to delay the SYN packets until the reload was done, as that would only impose the latency of the HAProxy reload on new connections. To do this, we turned to Linux queueing disciplines (qdiscs). Queueing disciplines manipulate how network packets are handled within the Linux kernel. Specifically you can control how packets are enqueued and dequeued, which provides the ability to rate limit, prioritize, or otherwise order outgoing network traffic. For more information on qdiscs, I highly recommend reading the lartc howto as well as the relevant man pages.

After some light bedtime reading of the Linux kernel source code, one of our SREs, Josh Snyder, discovered a relatively undocumented qdisc that has been available since Linux 3.4: the plugqueueing discipline. Using the plug qdisc, we were able to implement zero downtime HAProxy reloads with the following standard Linux technologies:

  • tc: Linux traffic control. This allows us to set up queueing disciplines that route traffic based on filters. On newer Linux distributions there is also libnl-utils which provide interfaces to some of the newer qdiscs (such as the plug qdisc).
  • iptables: Linux tool for packet filtering and NAT. This allows us to mark incoming SYN packets.

Smartstack clients connect to the loopback interface to make a request to HAProxy, which fortunately turns incoming traffic into outgoing traffic. This means that we can set up a queuing discipline on the loopback interface that looks something like Figure 1.

Figure 1: Queueing Discipline

This sets up a classful implementation of the standard pfifo_fast queueing discipline using theprio qdisc, but with a fourth “plug” lane . A plug qdisc has the capability to queue packets without dequeuing them, and then on command flush those packets. This capability in combination with an iptables rule allows us to redirect SYN packets to the plug during a reload of HAProxy and then unplug after the reload. The handles (e.g ‘1:1’, ‘30:’) are labels that allow us to connect qdiscs together and send packets to particular qdiscs using filters; for more information consult the lartc howto referenced above.

We then programmed this functionality into a script we call qdisc_tool. This tool allows our infrastructure to “protect” a HAProxy reload where we plug traffic, restart haproxy, and then release the plug, delivering all the delayed SYN packets. This invocation looks something like:

1
qdisc_tool protect <normal HAProxy reload command>

view rawqdisc_commands hosted with ? by GitHub

We can easily reproduce this technique with standard userspace utilities on modern Linux distributions such as Ubuntu Trusty. If your setup does not have nl-qdisc-add but does have a 3.4+ Linux kernel, you can manipulate the plug via netlink manually.

Set up the Queuing Disciplines

Before we can do graceful HAProxy reloads, we must first set up the queueing discipline described above using tc and nl-qdisc-add. Note that every command must be run as root.

1234567891011121314
# Set up the queuing discipline

tc qdisc add dev lo root handle 1: prio bands 4

tc qdisc add dev lo parent 1:1 handle 10: pfifo limit 1000

tc qdisc add dev lo parent 1:2 handle 20: pfifo limit 1000

tc qdisc add dev lo parent 1:3 handle 30: pfifo limit 1000

# Create a plug qdisc with 1 meg of buffer

nl-qdisc-add --dev=lo --parent=1:4 --id=40: plug --limit 1048576

# Release the plug

nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug --release-indefinite

# Set up the filter, any packet marked with “1” will be

# directed to the plug

tc filter add dev lo protocol ip parent 1:0 prio 1 handle 1 fw classid 1:4

view rawsetup_qdisc.sh hosted with ? by GitHub

Mark SYN Packets

We want all SYN packets to be routed to the plug lane, which we can accomplish with iptables. We use a link local address so that we redirect only the traffic we want to during the reload, and clients always have the option of making a request to 127.0.0.1 if they wish to avoid the plug. Note that this assumes you have set up a link local connection at 169.254.255.254.

1
iptables -t mangle -I OUTPUT -p tcp -s 169.254.255.254 --syn -j MARK --set-mark 1

view rawsetup_iptables.sh hosted with ? by GitHub

Toggle the Plug While Reloading

Once everything is set up, all we need to do to gracefully reload HAProxy is to buffer SYNs before the reload, do the reload, and then release all SYNs after the reload. This will cause any connections that attempt to establish during the restart to observe latency equal to the amount of time it takes HAProxy to restart.

123
nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug --buffer

service haproxy reload

nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug --release-indefinite

view rawplug_manipulation.sh hosted with ? by GitHub

In production we observe that this technique adds about 20ms of latency to incoming connections during the restart, but drops no requests.

Design Tradeoffs

This design has some benefits and some drawbacks. The largest drawback is that this works only for outgoing links and not for incoming traffic. This is because of the way that queueing disciplines work in Linux, namely that you can only shape outgoing traffic. For incoming traffic, one must redirect to an intermediary interface and then shape the outgoing traffic from that intermediary. We are working on integrating a solution similar to this for our external load balancers, but it is not yet in production.

Furthermore, the qdiscs could also probably be tuned more efficiently. For example, we could insert the plug qdisc at the first prio lane and adjust the priomap accordingly to ensure that SYNs always get processed before other packets or we could tune buffer sizes on the pfifo/plug qdiscs. I believe that for this to work with an interface that is not loopback, the plug lane would have to be moved to the first lane to ensure SYN deliverability.

The reason that we decided to go with this solution over something like huptime, hacking file descriptor passing into HAProxy, or dancing between multiple local instances of HAProxy is because we deemed our qdisc solution the lowest risk. Huptime was ruled out quickly as we were unable to get it to function on our machines due to an old libc version, and we were uncertain if the LD_PRELOAD mechanism would even work for something as complicated as HAProxy. One engineer did implement a proof of concept file descriptor patch during a hackathon but the complexity of the patch and the potential for a large fork caused us to abandon that approach; it turns out that doing file descriptor passing properly is really hard. Of the three options, we most seriously considered running multiple HAProxy instances on the same machine and using either NAT, nginx, or another HAProxy instance to switch traffic between them. Ultimately we decided against it because of the number of unknowns in implementation, and the level of maintenance that would be required for the infrastructure.

With our solution, we maintain basically zero infrastructure and trust the Linux kernel and HAProxy to handle the heavy lifting. This trust appears to be well placed as in the months this has been running in production we have observed no issues.

Experimental Setup

To demonstrate that this solution really works, we can fire up an nginx HTTP backend with HAProxy sitting in front, generate some traffic with Apache Benchmark, and see what happens when we restart HAProxy. We can then evaluate a few different solutions this way.

All tests were carried out on a freshly provisioned c3.large AWS machine running Ubuntu Trusty and a 3.13 Linux kernel. HAProxy 1.5.11 was compiled locally with TARGET=linux2628. Nginx was started locally with the default configuration except that it listens on port 8001 and serves a simple “pong” reply instead of the default html. Our compiled HAProxy was started locally with a basic configuration that had a single backend at port 8001 and a corresponding frontend at port 16000.

Just Reload HAProxy

In this experiment, we only restart HAProxy with the ‘-sf’ option, which initiates the fast reload process. This is a pretty unrealistic test because we are restarting HAProxy every 100ms, but it illustrates the point.

Experiment

12345
# Restart haproxy every 100ms

while [ 1 ]; do

./haproxy -f /tmp/haproxy.cfg -p /tmp/haproxy.pid -sf $(cat /tmp/haproxy.pid)

sleep 0.1

done

view rawreload_experiment.sh hosted with ? by GitHub

Results

12345
$ ab -c 10 -n 10000 169.254.255.254:16000/

Benchmarking 169.254.255.254 (be patient)

...

apr_socket_recv: Connection reset by peer (104)

Total of 3652 requests completed

view rawreload_result hosted with ? by GitHub

Socket reset! Restarting HAProxy has caused us to fail a request even though our backend was healthy. If we tell apache benchmark to continue on receive errors and do more requests:

12345678910
$ ab -r -c 10 -n 200000 169.254.255.254:16000/

Benchmarking 169.254.255.254 (be patient)

...

Complete requests: 200000

Failed requests: 504

...

50% 2

95% 2

99% 3

100% 15 (longest request)

view rawreload_longer_result hosted with ? by GitHub

Only 0.25% of requests failed. This is not too bad, but well above our goal of zero.

Drop SYNs and Let TCP Do the Rest

Now we try the method where we drop SYNs. This method seems to completely break with high restart rate as you end up with exponentially backing off connections, so to get reliable results I could only restart HAProxy every second.

Experiment

12345678
# Restart haproxy every second

while [ 1 ]; do

sudo iptables -I INPUT -p tcp --dport 16000 --syn -j DROP

sleep 0.2

./haproxy -f /tmp/haproxy.cfg -p /tmp/haproxy.pid -sf $(cat /tmp/haproxy.pid)

sudo iptables -D INPUT -p tcp --dport 16000 --syn -j DROP

sleep 1

done

view rawiptables_experiment.sh hosted with ? by GitHub

Results

12345678910
$ ab -c 10 -n 200000 169.254.255.254:16000/

Benchmarking 169.254.255.254 (be patient)

...

Complete requests: 200000

Failed requests: 0

...

50% 2

95% 2

99% 6

100% 1002 (longest request)

view rawiptables_result hosted with ? by GitHub

Figure 2: Iptables Experiment Results

As expected, we drop no requests but incur an additional one second of latency. When request timings are plotted in Figure 2 we see a clear bimodal distribution where any requests that hit the restart take a full second to complete. Less than one percent of the test requests observe the high latency, but that is still enough to be a problem.

Use Our Graceful Restart Method

In this experiment, we restart HAProxy with the ‘-sf’ option and use our queueing strategy to delay incoming SYNs. Just to be sure we are not getting lucky, we do one million requests. In the process of this test we restarted HAProxy over 1500 times.

Experiment

1234567
# Restart haproxy every 100ms

while [ 1 ]; do

sudo nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug --buffer &> /dev/null

./haproxy -f /tmp/haproxy.cfg -p /tmp/haproxy.pid -sf $(cat /tmp/haproxy.pid)

sudo nl-qdisc-add --dev=lo --parent=1:4 --id=40: --update plug--release-indefinite &> /dev/null

sleep 0.100

done

view rawtc_experiment.sh hosted with ? by GitHub

Results

12345678910
$ ab -c 10 -n 1000000 169.254.255.254:16000/

Benchmarking 169.254.255.254 (be patient)

...

Complete requests: 1000000

Failed requests: 0

...

50% 2

95% 2

99% 8

100% 29 (longest request)

view rawtc_result hosted with ? by GitHub

Figure 3: TC Experiment Results

Success! Restarting HAProxy has basically no effect on our traffic, causing only minor delays as can be seen in Figure 3. Note that this method is heavily dependent on how long HAProxy takes to load its configuration, and because we are running such a reduced configuration, these results are deceivingly fast. In our production environment we do observe about a 20ms penalty during HAProxy restarts.

Conclusion

This technique appears to work quite well to achieve our goal of providing a rock-solid service infrastructure for our developers to build on. By delaying SYN packets coming into our HAProxy load balancers that run on each machine, we are able to minimally impact traffic during HAProxy reloads, which allows us to add, remove, and change service backends within our SOA without fear of significantly impacting user traffic.

Acknowledgements

Thanks to Josh Snyder, John Billings and Evan Krall for excellent design and implementation discussions.

时间: 2024-11-10 06:10:10

True Zero Downtime HAProxy Reloads--转载的相关文章

Using HAProxy as an API Gateway, Part 3 [Health Checks]

转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-3-health-checks/ Achieving high availability rests on having good health checks. HAProxy as an API gateway gives you several ways to do this. Run your service on multiple servers.Pl

Saltstack安装nginx+haproxy

@font-face {  font-family: "Times New Roman";}@font-face {  font-family: "宋体";}@font-face {  font-family: "Calibri";}p.MsoNormal { margin: 0 0 0; text-align: justify; font-family: Calibri; font-size: 14px; }span.msoIns { text

HAproxy动静分离

一:HAproxy       HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.      HAProxy特别 适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接.并且它的 运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上.   HAProxy实现了一种事件驱动,单一进程模型,此模型支持非常

Part1 - 修改haproxy配置文件

README: 查看.添加.修改.删除分别是用不同函数实现, 运行程序时,有选择列表,并且每项功能都有example提示 流程图: 代码: #!/usr/bin/env python # coding:utf8 import sys import os import shutil import time import re import json if os.path.exists('tab.py'): import tab class handleHaproxyConf(object): "s

Haproxy介绍与基本应用初探

HAProxy是什么 TCP代理软件:L4(伪四层)http反向代理软件:七层应用代理支持SSL连接:支持客户端到到Haproxy,Haproxy到后面服务器,以及全程SSL的支持负载均衡器,支持会话粘性;HTTP协议的修正与保护,以及内容压缩总体来说:HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力(反向代理).采用单线程事件驱动型非阻塞引擎;媲美商用负载均衡器的性能和稳定性.haproxy常用架构: HAProxy的核心功能 负载均衡:L4(伪四层)和L7两种模式,支持R

Android之判断某个服务是否正在运行的方法

/** * 判断某个服务是否正在运行的方法 * * @param mContext * @param serviceName * 是包名+服务的类名(例如:net.loonggg.testbackstage.TestService) * @return true代表正在运行,false代表服务没有正在运行 */ public boolean isServiceWork(Context mContext, String serviceName) { boolean isWork = false;

QML 语言基础

在<Qt Quick 简单介绍>中我们提到 QML 语法和 Json 相似,请參考<Qt on Android: http下载与Json解析>查看 Json 语法.当然这里我们是期望从零開始也能学会 QML ,所以呢,你也能够直接往下看. 版权全部 foruok,转载请注明出处:http://blog.csdn.net/foruok 对象 QML 文件的后缀是 qml ,事实上就是个文本文件.以下是 一个简单的 QML 文件: import QtQuick 2.0 import Q

webview loadUrl() 弹出系统浏览器解决办法

有很多时候,我们请求的网站会直接跳转到一个位置,这样会直接全屏浏览器加载被跳转的网页,或者弹出浏览器选择(除了系统的,你还自己安装了其他浏览器). 于是解决办法的原理就是,在webview中跳转. 办法一: mywebView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { //重写此方法表明点击网页里面的链接 //还是在当前的we

Base64加解密

Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式.此时,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到.金融数据也常以base64编码格式提供. Base64是一种基于64个可打印字符来表示二进制数据的表示方法.由于2的6次方等于64,所以每6个比特为一个单元,对应某个可打印字符.三个字节有24个比特,对应于