Docs/应用相关/基于Keepalived-Haproxy搭建四层负载均衡器.html
2022-10-18 16:59:37 +08:00

349 lines
No EOL
18 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>基于Keepalived+Haproxy搭建四层负载均衡器</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">基于Keepalived+Haproxy搭建四层负载均衡器</h1>
<div class="content-html" id="leanote-content"><h2>一、前言</h2><p>Haproxy是稳定、高性能、高可用性的负载均衡解决方案支持HTTP及TCP代理后端服务器池因支持强大灵活的7层acl规则广泛作为HTTP反向代理。本文则详细介绍如何利用它的四层交换与Keepalived实现一个负载均衡器适用于Socket、ICE、<a title="mail命令" href="http://man.linuxde.net/mail" target="_blank" data-mce-href="http://man.linuxde.net/mail">mail</a><a title="mysql命令" href="http://man.linuxde.net/mysql" target="_blank" data-mce-href="http://man.linuxde.net/mysql">mysql</a>、私有通讯等任意TCP服务。系统架构图如下</p><p><img src="基于Keepalived-Haproxy搭建四层负载均衡器_files/599b462cd01cce1c4d000016.png" alt="" data-mce-src="/api/file/getImage?fileId=599b462cd01cce1c4d000016"></p><h2>二、平台环境</h2><pre id="leanote_ace_1479781780621_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 105px;">OS:Centos5.4(64X)
MASTER:192.168.0.20
BACKUP:192.168.0.21
VIP:192.168.0.100
Serivce&nbsp;Port:11231</pre><h2>三、平台安装配置</h2><p><strong>1、添加非本机<a title="ip命令" href="http://man.linuxde.net/ip" target="_blank" data-mce-href="http://man.linuxde.net/ip">ip</a>邦定支持</strong></p><pre id="leanote_ace_1479781780643_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 63px;">#vi&nbsp;/etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1
#sysctl&nbsp;p</pre><p><strong>2、配置平台日志支持</strong></p><pre id="leanote_ace_1479781780653_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 189px;">#vi&nbsp;/etc/syslog.conf
添加:
local3.*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/var/log/haproxy.log
local0.*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/var/log/haproxy.log
#vi&nbsp;/etc/sysconfig/syslog
修改:
SYSLOGD_OPTIONS="-r&nbsp;-m&nbsp;0"
#/etc/init.d/syslog&nbsp;restart</pre><p><strong>3、关闭SELINUX</strong></p><pre id="leanote_ace_1479781780663_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 84px;">vi&nbsp;/etc/sysconfig/selinux
修改:
SELINUX=disabled
#setenforce&nbsp;0</pre><p><strong>4、配置<a title="iptables命令" href="http://man.linuxde.net/iptables" target="_blank" data-mce-href="http://man.linuxde.net/iptables">iptables</a>添加VRRP通讯支持</strong></p><pre id="leanote_ace_1479781780673_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 21px;">iptables&nbsp;-A&nbsp;INPUT&nbsp;-d&nbsp;224.0.0.18&nbsp;-j&nbsp;accept</pre><p><strong>5、Keepalived的安装、配置</strong></p><pre id="leanote_ace_1479781780682_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 1239px;">#mkdir&nbsp;-p&nbsp;/home/install/keepalivedha
#cd&nbsp;/home/install/keepalivedha
#wget&nbsp;http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
#tar&nbsp;zxvf&nbsp;keepalived-1.2.2.tar.gz
#cd&nbsp;keepalived-1.2.2
#./configure
#make&nbsp;&amp;&amp;&nbsp;make&nbsp;install
#cp&nbsp;/usr/local/etc/rc.d/init.d/keepalived&nbsp;/etc/rc.d/init.d/
#cp&nbsp;/usr/local/etc/sysconfig/keepalived&nbsp;/etc/sysconfig/
#mkdir&nbsp;/etc/keepalived
#cp&nbsp;/usr/local/etc/keepalived/keepalived.conf&nbsp;/etc/keepalived/
#cp&nbsp;/usr/local/sbin/keepalived&nbsp;/usr/sbin/
#vi&nbsp;/etc/keepalived/keepalived.conf
!&nbsp;Configuration&nbsp;file&nbsp;for&nbsp;keepalived&nbsp;&nbsp;
&nbsp;&nbsp;
global_defs&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;notification_email&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liutiansi@gmail.com&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;notification_email_from&nbsp;liutiansi@gmail.com&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;smtp_connect_timeout&nbsp;3&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;smtp_server&nbsp;127.0.0.1&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;router_id&nbsp;LVS_DEVEL&nbsp;&nbsp;
}&nbsp;&nbsp;
vrrp_script&nbsp;chk_haproxy&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;script&nbsp;"killall&nbsp;-0&nbsp;haproxy"&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;interval&nbsp;2&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;weight&nbsp;2&nbsp;&nbsp;
}&nbsp;&nbsp;
vrrp_instance&nbsp;VI_1&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;interface&nbsp;eth1&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;state&nbsp;MASTER&nbsp;#&nbsp;从为BACKUP&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;priority&nbsp;101&nbsp;#&nbsp;从为100&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;virtual_router_id&nbsp;50&nbsp;#路由ID可通过#tcpdump&nbsp;vrrp查看。&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;garp_master_delay&nbsp;1&nbsp;#主从切换时间,单位为秒。&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;authentication&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auth_type&nbsp;PASS&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auth_pass&nbsp;KJj23576hYgu23IP&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;track_interface&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eth0&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eth1&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;virtual_ipaddress&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;192.168.0.100&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;track_script&nbsp;{&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chk_haproxy&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;#状态通知&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;notify_master&nbsp;"/etc/keepalived/Mailnotify.py&nbsp;master"&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;notify_backup&nbsp;"/etc/keepalived/Mailnotify.py&nbsp;backup"&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;notify_fault&nbsp;"/etc/keepalived/Mailnotify.py&nbsp;fault"&nbsp;&nbsp;
}</pre><p><strong>6、Haproxy的安装与配置</strong></p><pre id="leanote_ace_1479781780693_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 231px;">#cd&nbsp;/home/install/keepalivedha
#wget&nbsp;http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.11.tar.gz
#tar&nbsp;-zxvf&nbsp;haproxy-1.4.11.tar.gz
#cd&nbsp;haproxy-1.4.11
#make&nbsp;install
#mkdir&nbsp;-p&nbsp;/usr/local/haproxy/etc
#mkdir&nbsp;-p&nbsp;/usr/local/haproxy/sbin
#cp&nbsp;examples/haproxy.cfg&nbsp;/usr/local/haproxy/etc
#ln&nbsp;-s&nbsp;/usr/local/sbin/haproxy&nbsp;/usr/local/haproxy/sbin/haproxy&nbsp;
#vi&nbsp;/usr/local/haproxy/etc/haproxy.cfg</pre><pre id="leanote_ace_1479781780704_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 924px;">#&nbsp;this&nbsp;config&nbsp;needs&nbsp;haproxy-1.1.28&nbsp;or&nbsp;haproxy-1.2.1&nbsp;&nbsp;
&nbsp;&nbsp;
global&nbsp;&nbsp;
#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log&nbsp;127.0.0.1&nbsp;&nbsp;&nbsp;local0&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log&nbsp;127.0.0.1&nbsp;&nbsp;&nbsp;local1&nbsp;notice&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxconn&nbsp;5000&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uid&nbsp;99&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gid&nbsp;99&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;daemon&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pidfile&nbsp;/usr/local/haproxy/haproxy.pid&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;
defaults&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode&nbsp;&nbsp;&nbsp;&nbsp;http&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#option&nbsp;httplog&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;option&nbsp;&nbsp;dontlognull&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retries&nbsp;3&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;option&nbsp;redispatch&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxconn&nbsp;2000&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contimeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5000&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clitimeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50000&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srvtimeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50000&nbsp;&nbsp;
&nbsp;&nbsp;
listen&nbsp;&nbsp;ICE01&nbsp;&nbsp;&nbsp;192.168.0.100:11231&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode&nbsp;tcp&nbsp;#配置TCP模式&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxconn&nbsp;2000&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;balance&nbsp;roundrobin&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.128&nbsp;192.168.0.128:11231&nbsp;check&nbsp;inter&nbsp;5000&nbsp;fall&nbsp;1&nbsp;rise&nbsp;2&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.129&nbsp;192.168.0.129:11231&nbsp;check&nbsp;inter&nbsp;5000&nbsp;fall&nbsp;1&nbsp;rise&nbsp;2&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.130&nbsp;192.168.0.130:11231&nbsp;check&nbsp;inter&nbsp;5000&nbsp;fall&nbsp;1&nbsp;rise&nbsp;2&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.131&nbsp;192.168.0.131:11231&nbsp;check&nbsp;inter&nbsp;5000&nbsp;fall&nbsp;1&nbsp;rise&nbsp;2&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.132&nbsp;192.168.0.132:11231&nbsp;check&nbsp;inter&nbsp;5000&nbsp;fall&nbsp;1&nbsp;rise&nbsp;2&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;&nbsp;ice-192.168.0.34&nbsp;192.168.0.34:11231&nbsp;check&nbsp;inter&nbsp;5000&nbsp;fall&nbsp;1&nbsp;rise&nbsp;2&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srvtimeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20000&nbsp;&nbsp;
&nbsp;&nbsp;
listen&nbsp;stats_auth&nbsp;192.168.0.20:80&nbsp;&nbsp;
#&nbsp;listen&nbsp;stats_auth&nbsp;192.168.0.21:80&nbsp;#&nbsp;backup&nbsp;config&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats&nbsp;enable&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats&nbsp;uri&nbsp;&nbsp;/admin-status&nbsp;#管理地址&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats&nbsp;auth&nbsp;&nbsp;admin:123456&nbsp;#管理帐号:管理密码&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats&nbsp;admin&nbsp;if&nbsp;TRUE</pre><p><strong>7、邮件通知程序(python实现)<br></strong></p><pre id="leanote_ace_1479781780716_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 21px;">#vi&nbsp;/etc/keepalived/Mailnotify.py</pre><pre id="leanote_ace_1479781780725_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 1113px;">#!/usr/local/bin/python&nbsp;&nbsp;
#coding:&nbsp;utf-8&nbsp;&nbsp;
from&nbsp;email.MIMEMultipart&nbsp;import&nbsp;MIMEMultipart&nbsp;&nbsp;
from&nbsp;email.MIMEText&nbsp;import&nbsp;MIMEText&nbsp;&nbsp;
from&nbsp;email.MIMEImage&nbsp;import&nbsp;MIMEImage&nbsp;&nbsp;
from&nbsp;email.header&nbsp;import&nbsp;Header&nbsp;&nbsp;
import&nbsp;sys&nbsp;&nbsp;
import&nbsp;smtplib&nbsp;&nbsp;
&nbsp;
#---------------------------------------------------------------&nbsp;&nbsp;
#&nbsp;Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mailnotify.py&nbsp;&nbsp;
#&nbsp;Purpose:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mail&nbsp;notify&nbsp;to&nbsp;SA&nbsp;&nbsp;
#&nbsp;Author:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Liutiansi&nbsp;&nbsp;
#&nbsp;Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;liutiansi@gamil.com&nbsp;&nbsp;
#&nbsp;Created:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2011/03/09&nbsp;&nbsp;
#&nbsp;Copyright:&nbsp;&nbsp;&nbsp;(c)&nbsp;2011&nbsp;&nbsp;
#--------------------------------------------------------------&nbsp;&nbsp;
strFrom&nbsp;=&nbsp;'admin@domain.com'&nbsp;&nbsp;
strTo&nbsp;=&nbsp;'liutiansi@gmail.com'&nbsp;&nbsp;
smtp_server='smtp.domain.com'&nbsp;&nbsp;
smtp_pass='123456'&nbsp;&nbsp;
&nbsp;&nbsp;
if&nbsp;sys.argv[1]!="master"&nbsp;and&nbsp;sys.argv[1]!="backup"&nbsp;&nbsp;and&nbsp;sys.argv[1]!="fault":&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;sys.exit()&nbsp;&nbsp;
else:&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;notify_type=sys.argv[1]&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;
mail_title='[紧急]负载均衡器邮件通知'&nbsp;&nbsp;
mail_body_plain=notify_type+'被激活,请做好应急处理。'&nbsp;&nbsp;
mail_body_html='&lt;b&gt;&lt;font&nbsp;color=red&gt;'+notify_type+'被激活,请做好应急处理。&lt;/font&gt;&lt;/b&gt;'&nbsp;&nbsp;
&nbsp;&nbsp;
msgRoot&nbsp;=&nbsp;MIMEMultipart('related')&nbsp;&nbsp;
msgRoot['Subject']&nbsp;=Header(mail_title,'utf-8')&nbsp;&nbsp;
msgRoot['From']&nbsp;=&nbsp;strFrom&nbsp;&nbsp;
msgRoot['To']&nbsp;=&nbsp;strTo&nbsp;&nbsp;
&nbsp;&nbsp;
msgAlternative&nbsp;=&nbsp;MIMEMultipart('alternative')&nbsp;&nbsp;
msgRoot.attach(msgAlternative)&nbsp;&nbsp;
&nbsp;&nbsp;
msgText&nbsp;=&nbsp;MIMEText(mail_body_plain,&nbsp;'plain',&nbsp;'utf-8')&nbsp;&nbsp;
msgAlternative.attach(msgText)&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;
msgText&nbsp;=&nbsp;MIMEText(mail_body_html,&nbsp;'html','utf-8')&nbsp;&nbsp;
msgAlternative.attach(msgText)&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;
smtp&nbsp;=&nbsp;smtplib.SMTP()&nbsp;&nbsp;
smtp.connect(smtp_server)&nbsp;&nbsp;
smtp.login(smtp_user,smtp_pass)&nbsp;&nbsp;
smtp.sendmail(strFrom,&nbsp;strTo,&nbsp;msgRoot.as_string())&nbsp;&nbsp;
smtp.quit()</pre><p>修改成系统python实际路径“#!/usr/local/bin/python”(第一行)</p><pre id="leanote_ace_1479781780738_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 63px;">#chmod&nbsp;+x&nbsp;/etc/keepalived/Mailnotify.py
#/usr/local/haproxy/sbin/haproxy&nbsp;-f&nbsp;/usr/local/haproxy/etc/haproxy.cfg
#service&nbsp;keepalived&nbsp;start</pre><p><strong>8、查看VRRP通讯记录<br></strong></p><pre id="leanote_ace_1479781780748_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 21px;">#tcpdump&nbsp;vrrp</pre><pre id="leanote_ace_1479781780758_0" class="ace-tomorrow" data-mce-style="line-height: 1.5; font-size: 14px; height: 63px;">tcpdump:&nbsp;verbose&nbsp;output&nbsp;suppressed,&nbsp;use&nbsp;-v&nbsp;or&nbsp;-vv&nbsp;for&nbsp;full&nbsp;protocol&nbsp;decode
listening&nbsp;on&nbsp;eth0,&nbsp;link-type&nbsp;EN10MB&nbsp;(Ethernet),&nbsp;capture&nbsp;size&nbsp;96&nbsp;bytes
15:49:05.270017&nbsp;IP&nbsp;192.168.0.20&nbsp;&gt;&nbsp;VRRP.MCAST.NET:&nbsp;VRRPv2,&nbsp;Advertisement,&nbsp;vrid&nbsp;50,&nbsp;prio&nbsp;100,&nbsp;authtype&nbsp;simple,&nbsp;intvl&nbsp;1s,&nbsp;length&nbsp;20</pre><h2>四、Haproxy界面</h2><p>访问http://192.168.0.20/admin-status输入帐号admin密码123456进入管理监控平台。</p><p><img src="基于Keepalived-Haproxy搭建四层负载均衡器_files/599b462cd01cce1c4d000015.png" alt="" data-mce-src="/api/file/getImage?fileId=599b462cd01cce1c4d000015"></p><p>haproxy-1.4.9以后版本最大的亮点是添加了手工启用/禁用功能,对升级变更应用时非常有用。</p><h2>五、邮件通知</h2><p><img src="基于Keepalived-Haproxy搭建四层负载均衡器_files/599b462cd01cce1c4d000014.png" alt="" data-mce-src="/api/file/getImage?fileId=599b462cd01cce1c4d000014"></p></div>
</div>
<!-- 该js供其它处理 -->
<script src="../leanote-html.js"></script>
</body>
</html>