Docs/linux基础/优化Haproxy/1.html
2022-10-18 16:59:37 +08:00

165 lines
No EOL
8.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="tool" content="leanote-desktop-app">
<title>1</title>
<style>
*{font-family:"lucida grande","lucida sans unicode",lucida,helvetica,"Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
body {
margin: 0;
}
/*公用文字样式*/
h1{font-size:30px}h2{font-size:24px}h3{font-size:18px}h4{font-size:14px}
.note-container{
width:850px;
margin:auto;
padding: 10px 20px;
box-shadow: 1px 1px 10px #eee;
}
#title {
margin: 0;
}
table {
margin-bottom: 16px;
border-collapse: collapse;
}
table th, table td {
padding: 6px 13px;
border: 1px solid #ddd;
}
table th {
font-weight: bold;
}
table tr {
background-color: none;
border-top: 1px solid #ccc;
}
table tr:nth-child(2n) {
background-color: rgb(247, 247, 249);
}
.mce-item-table, .mce-item-table td, .mce-item-table th, .mce-item-table caption {
border: 1px solid #ddd;
border-collapse: collapse;
padding: 6px 13px;
}
blockquote {
border-left-width:10px;
background-color:rgba(128,128,128,0.05);
border-top-right-radius:5px;
border-bottom-right-radius:5px;
padding:15px 20px;
border-left:5px solid rgba(128,128,128,0.075);
}
blockquote p {
margin-bottom:1.1em;
font-size:1em;
line-height:1.45
}
blockquote ul:last-child,blockquote ol:last-child {
margin-bottom:0
}
pre {
padding: 18px;
background-color: #f7f7f9;
border: 1px solid #e1e1e8;
border-radius: 3px;
display: block;
}
code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
white-space: nowrap;
background-color: #f9f2f4;
border-radius: 4px;
}
.footnote {
vertical-align: top;
position: relative;
top: -0.5em;
font-size: .8em;
}
hr {
margin:2em 0
}
img {
max-width:100%
}
pre {
word-break:break-word
}
p,pre,pre.prettyprint,blockquote {
margin:0 0 1.1em
}
hr {
margin:2em 0
}
img {
max-width:100%
}
.sequence-diagram,.flow-chart {
text-align:center;
margin-bottom:1.1em
}
.sequence-diagram text,.flow-chart text {
font-size:15px !important;
font-family:"Source Sans Pro",sans-serif !important
}
.sequence-diagram [fill="#ffffff"],.flow-chart [fill="#ffffff"] {
fill:#f6f6f6
}
.sequence-diagram [stroke="#000000"],.flow-chart [stroke="#000000"] {
stroke:#3f3f3f
}
.sequence-diagram text[stroke="#000000"],.flow-chart text[stroke="#000000"] {
stroke:none
}
.sequence-diagram [fill="#000"],.flow-chart [fill="#000"],.sequence-diagram [fill="#000000"],.flow-chart [fill="#000000"],.sequence-diagram [fill="black"],.flow-chart [fill="black"] {
fill:#3f3f3f
}
ul,ol {
margin-bottom:1.1em
}
ul ul,ol ul,ul ol,ol ol {
margin-bottom:1.1em
}
kbd {
padding:.1em .6em;
border:1px solid rgba(63,63,63,0.25);
-webkit-box-shadow:0 1px 0 rgba(63,63,63,0.25);
box-shadow:0 1px 0 rgba(63,63,63,0.25);
font-size:.7em;
font-family:sans-serif;
background-color:#fff;
color:#333;
border-radius:3px;
display:inline-block;
margin:0 .1em;
white-space:nowrap
}
.toc ul {
list-style-type:none;
margin-bottom:15px
}
</style>
<!-- 该css供自定义样式 -->
<link href="../leanote-html.css" rel="stylesheet">
</head>
<body>
<div class="note-container">
<h1 class="title" id="leanote-title">1</h1>
<div class="content-html" id="leanote-content"><p><img id="__LEANOTE_D_IMG_1505974980394" src="1_files/59c3cc8cd01cce1c4d000028.png" alt="" width="550" data-mce-src="/api/file/getImage?fileId=59c3cc8cd01cce1c4d000028" data-mce-style="margin: 0px 10px 10px 0px; padding: 0px;"></p><p>仔细上上图,我们可以发现两个信息:</p><ol><li>该机器建立了238万个TCP连接</li><li>此时内存使用量大约在48G左右。</li></ol><p>看上去很赞吧如果有人能够提供配置并且在单台部署HAProxy的服务器上完成这样规模的调优是不是更赞本文将详细描述这个过程;)</p><p>本文是一系列关于HAProxy压力测试文章的最后一篇。如果有时间建议读者能够先阅读本系列的其余两部分。这样能够更好的帮助我们了解本文所提及的内核级别调优需要的一些背景知识</p><p>为了达到前面提到的效果整个设置过程依赖许多小型组件。在贴出HAProxy的最终配置之前如果读者实在没有耐心可以滚动到底部直接查看本文会按照笔者思路一步步实现最终目标</p><h2>我们要测试哪些内容</h2><p>本文测试使用的组件软件是Haproxy 1.6版本。我们一直在生产环境的4核30GB内存服务器上使用该版本不过目前所有的连接都不是基于SSL的。</p><div id="lowerFullwidthVCR">&nbsp;</div><p>本次实验我们将测试2个指标</p><ul><li>首先是CPU使用率既我们将所有负载从非SSL改成SSL连接时的CPU使用率上升情况。在这种情况下由于建立SSL连接需要的5次握手和数据包加密CPU使用率肯定会上升。</li><li>其次,我们希望能够测试出当前生产环境配置下,请求数和最大并发连接数的拐点。</li></ul><p>我们需要获取第一个指标是因为我们即将全面铺开的特性依赖基于SSL的通信。而第二个指标的目的是能够合理安排生产环境中的HAProxy专用机器的数量。</p><h2>测试使用的组件</h2><ul><li>多个施压机</li><li>几个不同配置的HAProxy 1.6机器:<ul><li>4核30GB内存</li><li>16核30GB内存</li><li>16核64GB内存</li></ul></li><li>用于支撑这些并发连接的后端机器</li></ul><h2>HTTP和MQTT</h2><p>如果读者阅读过本系列的<a href="https://medium.com/@sachinmalhotra/load-testing-haproxy-part-1-f7d64500b75d" data-mce-href="https://medium.com/@sachinmalhotra/load-testing-haproxy-part-1-f7d64500b75d">第一篇文章</a>,应该知道我们整个基础设施支持两种协议:</p><ul><li>HTTP</li><li>MQTT</li></ul><p>在我们的技术栈中没有使用HTTP 2.0因此没有持久化HTTP连接的功能性需求。因此在生产环境中单台HAProxy机器的连接数输入+输出大约在2*150k。虽然并发连接数不高但是每秒请求数还是相当高的。</p><p>和HTTP协议不同MQTT协议是另一种通信协议。它提供了服务质量QoS相关参数同时也支持连接持久化。因此在MQTT信道上可以进行双向的持续通信。由于HAProxy可以支持MQTT基于TCP连接我们统计到单台服务器高峰期有大约600-700k个TCP连接。</p><p>我们希望压力测试能够给出HTTP和MQTT两种协议连接数的具体数据。</p><p>针对HTTP应用服务器有很多现成工具可以帮助我们完成压力测试并且能够提供该诸如结果汇总、图表绘制等但是对于MQTT协议却鲜有类似的工具。我们自己开发了一个工具但是在这种高压力场景下不够稳定。</p><p>因此我们决定使用HTTP压力测试客户端来模拟MQTT协议配置。很有意思吧让我们继续。</p><h2>初始设置</h2><p>后面将会详细描述整个调优过程,希望能够对类似场景压力测试和性能测试有所帮助。</p><ul><li>我们首先使用了一台16核30GB的机器来安装HAProxy。我们没有直接使用当前生产环境服务器的配置因为预期HAProxy终端的SSL连接会消耗大量CPU。</li><li>在服务端我们使用Node.js服务针对<code>ping</code>请求响应<code>pong</code></li><li>对于客户端,我们刚开始使用了<a href="https://httpd.apache.org/docs/2.4/programs/ab.html" data-mce-href="https://httpd.apache.org/docs/2.4/programs/ab.html">Apache Bench</a>。究其原因,主要是因为<code>ab</code>是HTTP协议压力测试工具中较为有名且比较稳定的工具同时它提供了不错的汇总报告能够帮助分析结果。</li></ul><p><code>ab</code>&nbsp;提供了许多有用的参数,在后面的压力测试中也使用到了,例如:</p><ul><li><code>- c并发量</code>定义并发请求服务的数量;</li><li><code>-n请求数</code>定义当前压力测试的总请求数;</li><li><code>-p 发送文件</code>&nbsp;通过POST请求发送文件</li></ul><p>如果仔细观察这些参数,我们可以发现通过这三个参数可以组合出很多不同的用例。一个简单的<code>ab</code>命令例如:</p><pre id="leanote_ace_1505974835046_0" class="brush:sh ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 21px;">ab&nbsp;-S&nbsp;-p&nbsp;post_smaller.txt&nbsp;-T&nbsp;application/json&nbsp;-q&nbsp;-n&nbsp;100000&nbsp;-c&nbsp;3000&nbsp;http://test.haproxy.in:80/ping</pre><p>该命令的输出结果类似:</p><p><img id="__LEANOTE_D_IMG_1505974964836" src="1_files/59c3cc8cd01cce1c4d000027.png" alt="" width="550" data-mce-src="/api/file/getImage?fileId=59c3cc8cd01cce1c4d000027" data-mce-style="margin: 0px 10px 10px 0px; padding: 0px;"></p></div>
</div>
<!-- 该js供其它处理 -->
<script src="../leanote-html.js"></script>
</body>
</html>