背景:
最近为一传统行业客户部署阿里中间件的过程中,需要使用负载均衡技术,最初计划使用LVS的DR模式,但考虑到使用LVS DR+keepalived模式 配置较为复杂,服务器数量较多,需要在后端服务器上做VIP绑定和ARP抑制操作,后期维护困难,还有windows系统,而且用户对Linux系统不熟悉。后来准备使用LVS的NAT模式,因为NAT模式不需要对后端服务器做修改,可是NAT模式对网络结构有要求,我们不能改变用户的网络结构。于是使用了HAproxy的反向代理功能。与keepalived做高可用使用了负载的功能。
1 安装HAproxy
yum install haproxy –y
rpm -ql haproxy
/etc/haproxy
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/rc.d/init.d/haproxy
/usr/bin/halog
/usr/sbin/haproxy
2 HAproxy主要配置
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend address
bind 10.6.68.200:8080
mode tcp
default_backend address-rs
frontend diamond
bind 10.6.68.201:8080
mode tcp
default_backend diamond-rs
frontend dauth-80
bind 10.6.68.202:80
mode tcp
default_backend dauth-80-rs
frontend dauth-443
bind 10.6.68.202:443
mode tcp
default_backend dauth-443-rs
………………
………………
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend address-rs
balance roundrobin
server server 10.6.68.101:8080 check
server server 10.6.68.102:8080 check
backend diamond-rs
balance roundrobin
server server 10.6.68.126:8080 check
server server 10.6.68.127:8080 check
server server 10.6.68.128:8080 check
backend dauth-80-rs
balance roundrobin
server server 10.6.68.124:80 check
server server 10.6.68.125:80 check
backend dauth-443-rs
balance roundrobin
server server 10.6.68.124:443 check
server server 10.6.68.125:443 check
……………………
……………………
3 keepalived 配置
[root@haproxy01 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
router_id HAproxy01
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass ……
}
track_script {
chk_http_port
}
virtual_ipaddress {
10.6.68.200
10.6.68.201
10.6.68.202
…………
…………
}
}
[root@haproxy 02 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
}
router_id HAproxy02
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass ……
}
track_script {
chk_http_port
}
virtual_ipaddress {
10.6.68.200/24
10.6.68.201/24
10.6.68.202/24
………
………
}
}
4 haproxy的检测脚本--判断haproxy是否是存活的。
vim check_haproxy.sh
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
/etc/init.d/haproxy restart &>/dev/null
fi
sleep 2