本文所使用的环境:
10.6.2.128 centos6.5
10.6.2.129 centos6.5
VIP 为10.6.2.150
要实现的目标:
实现10.6.2.128和10.6.2.129的9998端口的服务通过haproxy负载,并通过keepalived实现高可用。
1、安装haproxy
yum install -y haproxy
2、配置haproxy
vi /etc/haproxy/haproxy.cfg
修改代码如下:
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#--------------------------------------------------------------------- #---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# ) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# ) 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 100000 #最大连接数
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
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/
option redispatch
retries 3 #定义连接后端服务器的失败重连次数,连接失败次数超过次值后就会将对应后端服务器标记为不可用
timeout http-request 10s #http请求超时时间
timeout queue 1m #一个请求在队列里的超时时间
timeout connect 10s #连接超时时间
timeout client 1m #客户端超时时间
timeout server 1m #服务器端超时时间
timeout http-keep-alive 10s #设置http-keep-alive的超时时间
timeout check 10s #检查超时的间隔
maxconn #每个进程可用的最大连接数 #---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend pay_test *:9999
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js # use_backend static if url_static
default_backend pay_test #---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1: check #---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend pay_test
balance roundrobin #负载均衡的算法 roundrobin:轮询 source:根据请求源ip
fullconn 10000 #定义后端组的最大连接数
server pay_test_1 10.6.2.128:9998 inter 2000 rise 2 fall 3 check maxconn 5000 #inter 2000代表执行健康检查的间隔(ms),rise代表离线server转换到上线需要检查的次数,fall代表server从正常转到离线的检查次数
server pay_test_2 10.6.2.129:9998 inter 2000 rise 2 fall 3 check maxconn 5000 #check代表启动对此server执行健康检查,maxconn代表此服务器接受的最大并发连接数
listen stats
mode http
bind 0.0.0.0:9997
stats enable #开启监控页面
stats refresh 3s #页面刷新频率
stats hide-version #隐藏版本信息(为安全考虑)
stats uri /monitor #后台监控页面得uri
stats realm Haproxy\ monitor #提示信息
stats auth admin:admin #后台监控页面的用户名密码
stats admin if TRUE
3、服务启动
service haproxy start
4、将1-3步骤在10.6.2.129机器上也执行一次。
安装keepalived
1、下载安装keepalived
yum install -y keepalived
2、配置keepalived
vi /etc/keepalived/keepalived.conf
配置文件如下:
! Configuration File for keepalived global_defs {
notification_email {
bs_wjg@163.com #keepalived发生错误时候发送报警的邮箱 }
notification_email_from notify@163.com #发件人邮箱
smtp_server mail.163.com #发送email所使用的smtp服务器地址
smtp_connect_timeout 30 #连接stmp的超时时间
router_id LVS_DEVEL
}
#检查haproxy的进程状态,每1s执行一次
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval
weight
} vrrp_instance VI_1 {
state MASTER #主为MASTER 从为BACKUP
interface eth0 #实例绑定的网卡,视实际情况而定
virtual_router_id #这里设置vrid,如果两台机器属于同一组,设置为一样
priority #设置本节点的优先级,高的为master,不能超过255 一般master设置101 backup设置100
advert_int #组波信息发送间隔,默认为1s,同一本分组的两机器必须一样
authentication {
auth_type PASS
auth_pass 123456 } #验证密码,统一备份组的机器必须一致。
virtual_ipaddress {
10.6.2.150/ #虚拟IP的地址
}
track_interface {
eth0
}
track_script {
chk_haproxy
}
#状态通知
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault" }
notify.sh 脚本如下:
vip=10.6.2.150
contact='bs_wjg@163.com'
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
} checkHA(){
counter=$(ps -C haproxy --no-heading|wc -l)
if [ "${counter}" = "" ]; then
/etc/rc.d/init.d/haproxy start
sleep
counter=$(ps -C haproxy --no-heading|wc -l)
if [ "${counter}" = "" ]; then
/etc/init.d/keepalived stop
fi
fi
} checkNG(){
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "" ]; then
/usr/local/bin/nginx
sleep
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "" ]; then
/etc/init.d/keepalived stop
fi
fi
} case "$1" in
master)
notify master
exit
;;
backup)
notify backup
exit
;;
fault)
notify fault
exit
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit
;;
esac
3、服务启动
service keepalived start
4、10.6.2.129机器执行1-3步骤,根据从节点的配置进行配置。
到此配置结束,自行测试吧。