安装haproxy
yum install haproxy -y
编辑配置文件:vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://cbonte.github.io/haproxy-dconv
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http //设定默认运行模式
log global
option httplog //启用http请求日志。
option dontlognull //不记录空信息
option http-server-close //开启服务器端关闭,支持客户端一侧的长连接。
option forwardfor except 127.0.0.0/8 //在转发请求的时候,给请求报文添加一个首部信息,忽略本机。
option redispatch //请求重新分发
retries 3 //向后端服务器发出请求失败后,重试的次数。
timeout http-request 10s //请求的超时时间
timeout queue 1m //在请求等待队列中的超时时间
timeout connect 10s //paproxy连接后端服务器的超时时间
timeout client 1m //客户端非活动长连接的超时时间
timeout server 1m //服务器端非活动长连接的超时时间
timeout http-keep-alive 10s
timeout check 10s //健康状态检测超时时间
maxconn 5000 //设定每个进程所响应的最大连接数
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen stats
mode http
bind *:9999 //设置侦听的端口
stats enable //启用stats
stats hide-version //隐藏程序的版本号
stats uri /administrator/login // 指定url
stats realm HAProxy\ stats messages //认证的提示标题信息
stats auth admin:admin //定义登录stats帐号和密码。
stats admin if TRUE //启用管理功能,必须通过认证才允许登录管理
frontend main
bind *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js .bmp .ico .txt .html
use_backend static if url_static
default_backend dynamic //定义默认代理的后端服务器群
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static 定义静态请求响应服务器群
balance roundrobin //定义负载均衡算法
server static1 172.16.1.110:80 check maxconn 6000
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend dynamic 定义动态请求响应服务器群
balance roundrobin
server sport1 172.16.1.100:80 check inter 2 rise 1 fall 3 maxconn 2000
server sport2 172.16.1.101:80 check inter 2 rise 1 fall 3 maxconn 2000
notify.sh,这个脚本功能是在主备状态发生转换时发送邮件通知管理员,并且启动haproxy服务。
#!/bin/bash
#description: An example of notify script
#
vip=192.168.18.254
contact='root@localhost'
notify() {
mailsubject="`hostname` to be $1: $vip floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}