mycat+haproxy服务与高可用(
1.服务器
192.168.9.34 haproxy --->haproxy
192.168.9.37 haproxy-keeplived --->haproxy
192.168.9.35 service1 --->nginx
192.168.9.36 service2 --->nginx+tomcat
2.进行web进行haproxy负载均衡
#---------------------------------------------------------------------
# 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:
#
# 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
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
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# listen page
#---------------------------------------------------------------------
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /listen-page
stats realm welcome login\ listen-page
stats auth admin:000000
stats admin if TRUE
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:80 #haproxy的监听端口
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 app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
option httpchk GET /index.html
server static1 192.168.9.35:80 check port 80 inter 5s rise 2 fall 3
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 192.168.9.35 check port 80 inter 5s rise 2 fall 3
server app2 192.168.9.36 check port 80 inter 5s rise 2 fall 3
3.其他应用的后端健康检查与负载均衡
3.1摘录
3.2摘录
3.3摘录
4.haproxy的keepalived高可用
4.1安装keeplived
[root@haproxy ~]# yum -y install keepalived
4.2修改keeplived 的配置文件,配置自己需要的配置
4.2.1修改主从的配置文件
[root@haproxy ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id master #此处唯一
}
vrrp_script checkhaproxy {
script "/usr/local/bin/chk_haproxy.bash"
weight -20
interval 3
}
vrrp_instance VI_1 {
state MASTER #此处为MASTER或BACKUP
interface ens33
virtual_router_id 51 #此处为虚拟组,要相同表示一个组
priority 100 #此处为优先级,MASTR要大
advert_int 1 #广播时间间隔
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
checkhaproxy
}
virtual_ipaddress {
192.168.9.202/24
}
}
[root@haproxy-keeplived ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id slave
}
vrrp_script checkhaproxy {
script "/usr/local/bin/chk_haproxy.bash"
weight -20
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
checkhaproxy
}
virtual_ipaddress {
192.168.9.202/24
}
}
track_script 中调用 checkhaproxy
checkhaproxy 的意思是:
每3秒种执行一次 /usr/local/keepalived/script/chk_haproxy.bash 脚本 如果反馈结果是 0,就保持原优先级 priority ,如果是 1 ,就将优先级降低 20,也就是检查到 haproxy 状态异常后,就降级,以便让另一台服务器的keepalived进程可以抢到IP
为了避免网络的不稳定还可以加入 fall N (代表连续N次检查失败才算异常) 和 rise N (代表连续N次检查成功就算正常)
Tip: keepalived 相互之间的通讯要使用到组播,如果没打开,会出现几个实例同时抢占着IP的情况,打开方式是在iptables中加入 -A INPUT -d 224.0.0.18 -j ACCEPT,然后重载iptables配置(这个ip可以更改)
如果同一个局域网内存在多组Keepalived服务器对,就会造成IP多播地址冲突问题,导致接管错乱,不同组的Keepalived都会使用默认的224.0.0.18作为多播地址。此时的解决办法是,在同组的Keepalived服务器所有的配置文件里指定独一无二的多播地址,配置如下:
global_defs {
router_id LVS_19
vrrp_mcast_group4 224.0.0.19 #这个就是指定多播地址的配置
}
#组播组用于IPv4 VRRP通告
#(默认值:224.0.0.18)
vrrp_mcast_group4 224.0.0.18
#Multicast Group用于IPv6 VRRP通告
#(默认值:ff02 :: 12)
vrrp_mcast_group6 ff02 :: 12
4.2.2检查状态脚本
[root@haproxy ~]# vim /usr/local/bin/chk_haproxy.bash
#!/bin/bash
count=`ps aux | grep -v grep | grep haproxy.cfg | wc -l`
if [ $count -gt 0 ]; then
exit 0
else
pkill keepalived
exit 1
fi
增加执行权限
[root@haproxy ~]# chmod +x /usr/local/bin/chk_haproxy.bash
5.裂脑检查脚本
[root@lb02 scripts]# cat check_split_brain.sh
#!/bin/bash
lb01_vip=192.168.0.240
lb01_ip=192.168.0.221
while true
do
ping -c 2 -W 3 $lb01_ip &>/dev/null
if [ $? -eq 0 -a `ip a | grep "$lb01_vip" | wc -l` -eq 1 ];then
echo "ha is split brain.warning."
else
echo "ha is OK"
fi
sleep 5
done
可以将此脚本整合到Nagios或Zabbix监控服务里,进行监控报警。
6: 配置日志
keepalived 默认将日志输出到系统日志/var/log/messages中,因为系统日志很多,查询问题时相对麻烦。
我们可以将 keepalived 的日志单独拿出来,这需要修改日志输出路径。
修改 Keepalived 配置
[root@haproxy ~]# vim /etc/sysconfig/keepalived
更改如下:
即把 KEEPALIVED_OPTIONS=”-D” 修改为KEEPALIVED_OPTIONS=”-D -d -S 0”,其中 -S 指定 syslog 的 facility
修改日志rsyslog.con配置
[root@haproxy ~]# vim /etc/rsyslog.conf
mofa模仿上面添加
local0.* /var/log/keepalived.log
重启日志记录服务
[root@haproxy ~]# systemctl restart rsyslog
重启 keepalived
[root@haproxy ~]# systemctl restart keepalived
此时,可以从 /var/log/keepalived.log 查看日志了。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341