<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[运维进行时]]></title> 
<link>https://www.liuts.com/index.php</link> 
<description><![CDATA[互联网运维与架构]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[运维进行时]]></copyright>
<item>
<link>https://www.liuts.com/post/223/</link>
<title><![CDATA[基于Keepalived+Haproxy搭建四层负载均衡器[原创]]]></title> 
<author>刘天斯 &lt;liutiansi@gmail.com&gt;</author>
<category><![CDATA[Haproxy]]></category>
<pubDate>Thu, 10 Mar 2011 10:17:57 +0000</pubDate> 
<guid>https://www.liuts.com/post/223/</guid> 
<description>
<![CDATA[ 
	<strong>一、前言</strong><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Haproxy是稳定、高性能、高可用性的负载均衡解决方案，支持HTTP及TCP代理后端服务器池，因支持强大灵活的7层acl规则，广泛作为HTTP反向代理。本文则详细介绍如何利用它的四层交换与Keepalived实现一个负载均衡器，适用于Socket、ICE、Mail、Mysql、私有通讯等任意TCP服务。系统架构图如下：<br/><br/><a href="https://www.liuts.com/attachment.php?fid=288" target="_blank"><img src="https://www.liuts.com/attachment.php?fid=288" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/><strong>二、平台环境</strong><br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>OS:Centos5.4(64X)<br/>MASTER:192.168.0.20<br/>BACKUP:192.168.0.21<br/>VIP:192.168.0.100<br/>Serivce Port:11231<br/></div></div><br/><strong>三、平台安装配置</strong><br/>1、添加非本机IP邦定支持<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>#vi /etc/sysctl.conf<br/>net.ipv4.ip_nonlocal_bind=1<br/>#sysctl –p<br/></div></div><br/>2、配置平台日志支持<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>#vi /etc/syslog.conf<br/>添加：<br/>local3.*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/var/log/haproxy.log<br/>local0.*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/var/log/haproxy.log<br/><br/>#vi /etc/sysconfig/syslog<br/>修改：<br/>SYSLOGD_OPTIONS="-r -m 0"<br/>#/etc/init.d/syslog restart<br/></div></div><br/>3、关闭SELINUX<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>vi /etc/sysconfig/selinux<br/>修改：<br/>SELINUX=disabled<br/>#setenforce 0<br/></div></div><br/>4、配置iptables，添加VRRP通讯支持<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>iptables -A INPUT -d 224.0.0.18 -j ACCEPT<br/></div></div><br/>5、Keepalived的安装、配置<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>#mkdir -p /home/install/keepalivedha<br/>#cd /home/install/keepalivedha<br/>#wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz<br/>#tar zxvf keepalived-1.2.2.tar.gz<br/>#cd keepalived-1.2.2<br/>#./configure<br/>#make && make install<br/></div></div><br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/<br/>#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/<br/>#mkdir /etc/keepalived<br/>#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/<br/>#cp /usr/local/sbin/keepalived /usr/sbin/<br/></div></div><br/>#vi /etc/keepalived/keepalived.conf<br/><textarea name="code" class="c" rows="15" cols="100">
! Configuration File for keepalived

global_defs &#123;
&nbsp;&nbsp; notification_email &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; liutiansi@gmail.com
&nbsp;&nbsp; &#125;
&nbsp;&nbsp; notification_email_from liutiansi@gmail.com
&nbsp;&nbsp; smtp_connect_timeout 3
&nbsp;&nbsp; smtp_server 127.0.0.1
&nbsp;&nbsp; router_id LVS_DEVEL
&#125;
vrrp_script chk_haproxy &#123;
&nbsp;&nbsp;&nbsp;&nbsp;script "killall -0 haproxy"
&nbsp;&nbsp;&nbsp;&nbsp;interval 2
&nbsp;&nbsp;&nbsp;&nbsp;weight 2
&#125;
vrrp_instance VI_1 &#123;
&nbsp;&nbsp;&nbsp;&nbsp;interface eth1
&nbsp;&nbsp;&nbsp;&nbsp;state MASTER # 从为BACKUP
&nbsp;&nbsp;&nbsp;&nbsp;priority 101 # 从为100
&nbsp;&nbsp;&nbsp;&nbsp;virtual_router_id 50 #路由ID，可通过#tcpdump vrrp查看。
&nbsp;&nbsp;&nbsp;&nbsp;garp_master_delay 1 #主从切换时间，单位为秒。

&nbsp;&nbsp;&nbsp;&nbsp;authentication &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auth_type PASS
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auth_pass KJj23576hYgu23IP
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;track_interface &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eth0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eth1
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;virtual_ipaddress &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;192.168.0.100
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;track_script &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chk_haproxy
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;#状态通知
&nbsp;&nbsp;&nbsp;&nbsp;notify_master "/etc/keepalived/Mailnotify.py master"
&nbsp;&nbsp;&nbsp;&nbsp;notify_backup "/etc/keepalived/Mailnotify.py backup"
&nbsp;&nbsp;&nbsp;&nbsp;notify_fault "/etc/keepalived/Mailnotify.py fault"
&#125;
</textarea><br/>6、Haproxy的安装与配置<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>#cd /home/install/keepalivedha<br/>#wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.11.tar.gz<br/>#tar -zxvf haproxy-1.4.11.tar.gz<br/>#cd haproxy-1.4.11<br/>#make install<br/>#mkdir -p /usr/local/haproxy/etc<br/>#mkdir -p /usr/local/haproxy/sbin<br/>#cp examples/haproxy.cfg /usr/local/haproxy/etc<br/>#ln -s /usr/local/sbin/haproxy /usr/local/haproxy/sbin/haproxy <br/></div></div><br/>#vi /usr/local/haproxy/etc/haproxy.cfg<br/><textarea name="code" class="c" rows="15" cols="100">
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log 127.0.0.1&nbsp;&nbsp; local0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log 127.0.0.1&nbsp;&nbsp; local1 notice
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxconn 5000
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uid 99
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gid 99
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;daemon
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pidfile /usr/local/haproxy/haproxy.pid


defaults
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log&nbsp;&nbsp;&nbsp;&nbsp; global
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode&nbsp;&nbsp;&nbsp;&nbsp;http
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#option httplog
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;option&nbsp;&nbsp;dontlognull
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retries 3
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;option redispatch
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxconn 2000
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contimeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5000
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clitimeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50000
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srvtimeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50000

listen&nbsp;&nbsp;ICE01&nbsp;&nbsp; 192.168.0.100:11231
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode tcp #配置TCP模式
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxconn 2000
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;balance roundrobin
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.128 192.168.0.128:11231 check inter 5000 fall 1 rise 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.129 192.168.0.129:11231 check inter 5000 fall 1 rise 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.130 192.168.0.130:11231 check inter 5000 fall 1 rise 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.131 192.168.0.131:11231 check inter 5000 fall 1 rise 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.132 192.168.0.132:11231 check inter 5000 fall 1 rise 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.34 192.168.0.34:11231 check inter 5000 fall 1 rise 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srvtimeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20000

listen stats_auth 192.168.0.20:80
# listen stats_auth 192.168.0.21:80 # backup config
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats enable
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats uri&nbsp;&nbsp;/admin-status #管理地址
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats auth&nbsp;&nbsp;admin:123456 #管理帐号:管理密码
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats admin if TRUE
</textarea><br/>7、邮件通知程序(python实现)<br/>#vi /etc/keepalived/Mailnotify.py<br/><textarea name="code" class="python" rows="15" cols="100">
#!/usr/local/bin/python
#coding: utf-8
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.header import Header
import sys
import smtplib

#---------------------------------------------------------------
# Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mailnotify.py
# Purpose:&nbsp;&nbsp;&nbsp;&nbsp; Mail notify to SA
# Author:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Liutiansi
# Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; liutiansi@gamil.com
# Created:&nbsp;&nbsp;&nbsp;&nbsp; 2011/03/09
# Copyright:&nbsp;&nbsp; (c) 2011
#--------------------------------------------------------------
strFrom = 'admin@domain.com'
strTo = 'liutiansi@gmail.com'
smtp_server='smtp.domain.com'
smtp_pass='123456'

if sys.argv[1]!="master" and sys.argv[1]!="backup"&nbsp;&nbsp;and sys.argv[1]!="fault":
&nbsp;&nbsp;&nbsp;&nbsp;sys.exit()
else:
&nbsp;&nbsp;&nbsp;&nbsp;notify_type=sys.argv[1]


mail_title='[紧急]负载均衡器邮件通知'
mail_body_plain=notify_type+'被激活，请做好应急处理。'
mail_body_html='<b><font color=red>'+notify_type+'被激活，请做好应急处理。</font></b>'

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] =Header(mail_title,'utf-8')
msgRoot['From'] = strFrom
msgRoot['To'] = strTo

msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText(mail_body_plain, 'plain', 'utf-8')
msgAlternative.attach(msgText)


msgText = MIMEText(mail_body_html, 'html','utf-8')
msgAlternative.attach(msgText)


smtp = smtplib.SMTP()
smtp.connect(smtp_server)
smtp.login(smtp_user,smtp_pass)
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
</textarea><br/><span style="color: #FF0000;">注：修改成系统python实际路径“#!/usr/local/bin/python”(第一行)</span><br/>#chmod +x /etc/keepalived/Mailnotify.py<br/>#/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg<br/>#service keepalived start<br/><br/>8、查看VRRP通讯记录<br/>#tcpdump vrrp<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><br/>tcpdump: verbose output suppressed, use -v or -vv for full protocol decode<br/>listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes<br/>15:49:05.270017 IP 192.168.0.20 > VRRP.MCAST.NET: VRRPv2, Advertisement, vrid 50, prio 100, authtype simple, intvl 1s, length 20<br/></div></div><br/><strong>四、Haproxy界面</strong><br/>访问http://192.168.0.20/admin-status，输入帐号admin密码123456进入管理监控平台。<br/><a href="https://www.liuts.com/attachment.php?fid=290" target="_blank"><img src="https://www.liuts.com/attachment.php?fid=290" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>haproxy-1.4.9以后版本最大的亮点是添加了手工启用/禁用功能，对升级变更应用时非常有用。<br/><br/><strong>五、邮件通知</strong><br/><a href="https://www.liuts.com/attachment.php?fid=291" target="_blank"><img src="https://www.liuts.com/attachment.php?fid=291" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>如大家有什么疑问或感兴趣的话题可以通过weibo与我交流：<a href="http://t.qq.com/yorkoliu" target="_blank">http://t.qq.com/yorkoliu</a><br/>Tags - <a href="https://www.liuts.com/tags/keepalived/" rel="tag">keepalived</a> , <a href="https://www.liuts.com/tags/haproxy/" rel="tag">haproxy</a> , <a href="https://www.liuts.com/tags/%25E5%259B%259B%25E5%25B1%2582/" rel="tag">四层</a> , <a href="https://www.liuts.com/tags/4layer/" rel="tag">4layer</a> , <a href="https://www.liuts.com/tags/%25E8%25B4%259F%25E8%25BD%25BD%25E5%259D%2587%25E8%25A1%25A1/" rel="tag">负载均衡</a>
]]>
</description>
</item><item>
<link>https://www.liuts.com/post/174/</link>
<title><![CDATA[Load Balancing  QoS with HAProxy]]></title> 
<author>root &lt;admin@yourname.com&gt;</author>
<category><![CDATA[Haproxy]]></category>
<pubDate>Mon, 23 Nov 2009 02:40:44 +0000</pubDate> 
<guid>https://www.liuts.com/post/174/</guid> 
<description>
<![CDATA[ 
	<a href="http://www.igvita.com/posts/05-08/haproxy.png" target="_blank"><img src="http://www.igvita.com/posts/05-08/haproxy.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" align="left"/></a><br/>A brand new Rails/Merb app you put together over a weekend, a pack of Mongrels, a reverse proxy (like Nginx), and you're up and running. Well, almost, what about that one request that tends to run forever, often forcing the user to double check their internet connection? Response time is king, and you always want to make sure that your site feel snappy to the user. Did you know that Flickr optimizes all of their pages to render in sub 250ms?<br/>When you're fighting with response times, the worst thing you can possibly do is queue up another request behind an already long running process. Not only does the first request take forever, but everyone else must wait in line for it to finish as well! To mitigate the problem HAProxy goes beyond a simple round-robin scheduler, and implements a very handy feature: intelligent request queuing!<br/><a href="http://www.igvita.com/posts/05-08/haproxy-diagram.png" target="_blank"><img src="http://www.igvita.com/posts/05-08/haproxy-diagram.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" align="left"/></a><br/>.................................<br/>更多<a href="http://www.igvita.com/2008/05/13/load-balancing-qos-with-haproxy/" target="_blank">http://www.igvita.com/2008/05/13/load-balancing-qos-with-haproxy/</a><br/>Tags - <a href="https://www.liuts.com/tags/haproxy/" rel="tag">haproxy</a>
]]>
</description>
</item>
</channel>
</rss>