部署图:
集群部署的理解:
、keepalived呾haproxy必项装在同一台机器上(如172.17.210.210.83机器上,keepalived呾haproxy都要安装),keepalived负责为该服务器抢占vip(虚拟ip),抢占vip后,对主机的访问可以通过原来的ip(172.17.210.210.83)访问,也可以通过vip(172.17.210.210.103)访问。
172.17.210.64上癿keepalived也会去抢占vip,抢占vip有优先级,配置keepalived.conf中的(priority 150 #数值愈多,优先级越高,172.17.210.64上改为120,master和slave上该值配置不同)决定。但是一般哪台主机上的keepalived服务先启动就会抢占到vip,即使是slave,只要先启动也能抢到
haproxy负责将对vip癿请求分发到mycat上。起到负载均衡的作用,同时haproxy也能检测到mycat是否存活,haproxy只会将请求转发到存货的mycat上。
如果一台朋务器(keepalived+haproxy朋务器)宕机,另外一台上癿keepalived会立刻抢占vip并接管服务。
haproxy的安装和部署:
下载haproxy linux 安装包:haproxy-1.5.8.tar.gz
tar zxvf haproxy-1.5.8.tar.gz
cd haproxy-1.4.25
make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=x86_64
make install PREFIX=/usr/local/haproxy
cd /usr/local/haproxy #chown -R haproxy.haproxy *
haproxy.cfg
global log 127.0.0.1 local0 debug ##记日志的功能 maxconn 4096 user sa group sa pidfile /opt/app/tomcat/haproxy-1.5.8/haproxy.pid daemon defaults log global option dontlognull retries 3 option redispatch maxconn 2000 timeout connect 5000 timeout client 50000 timeout server 50000 listen admin_status ip:48800 ##VIP stats uri /admin-status ##统计页面 stats auth admin:admin mode http option httplog listen allmycat_service ip1:8096 ##转发到mycat的8066端口,即mycat的服务端口 mode tcp option tcplog option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www balance roundrobin #负载均衡算法 server mycat_31 ip1:8066 check port 48700 inter 5s rise 2 fall 3 server mycat_67 ip2:8066 check port 48700 inter 5s rise 2 fall 3 timeout server 20000 mode http option httplog listen allmycat_admin ip:8097 ##转发到mycat的9066端口,及mycat的管理控制台端口 mode tcp option tcplog option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www balance roundrobin server mycat_31_9066 ip1:9066 check port 48700 inter 5s rise 2 fall 3 server mycat_67_9066 ip2:9066 check port 48700 inter 5s rise 2 fall 3 timeout server 20000
haproxy日志记录
默认haproxy是不记录日志的 ,为了记录日志需要配置syslog,在linux下是rsyslogd服务, 先安装
rsyslog yum –y install rsyslog
配置监听mycat是否存活
在Mycat server1 Mycat server2上都需要添加检测端口48700的脚本,为此需要用到xinetd,xinetd为linux系统的基础服务。
首先在xinetd目录下面增加脚本与端口映射配置文件
如果xinetd没有安装,使用如下命令安装:
yum install xinetd -y
检查 /etc/xinetd.d文件夹是否存在,不存在加上
cd /etc
mkdir xinetd.d
增加 /etc/xinetd.d/mycat_status
vim /etc/xinetd.d/mycat_status
service mycat_status
{
flags = REUSE
socket_type = stream
port = 48700
wait = no
user = root
server = /usr/local/bin/mycat_status
log_on_failure += USERID
disable = no
}
/usr/local/bin/mycat_status脚本
#!/bin/bash
#/usr/local/bin/mycat_status.sh
# This script checks if a mycat server is healthy running on localhost. It will
# return:
#
# "HTTP/1.x 200 OK\r" (if mycat is running smoothly)
#
# "HTTP/1.x 503 Internal Server Error\r" (else)
mycat=`ps -ef|grep 'usr/local/bin/mycat_status' | grep 'not running'| wc -l`
if [ "$mycat" = "0" ];
then
/bin/echo -e "HTTP/1.1 200 OK\r\n"
else
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
fi
/etc/services中加入mycat_status服务
加入mycat_status服务,
cd /etc vi services
在末尾加入以下内容: mycat_status 48700/tcp # mycat_status
重启xinetd朋务 service xinetd restart
验证mycat_status服务是否启动成功
netstat -antup|grep 48700
keepalived安装
tar zxvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13 ./configure--prefix=/usr/local/keepalived
make make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
mkdir /etc/keepalived cd /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived
mkdir-p/usr/local/keepalived/var/log
keepalived配置
keepalived.conf:
vi /etc/keepalived/keepalived.conf
! Configuration Fileforkeepalived
vrrp_script chk_http_port
{
script "/etc/keepalived/scripts/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0 #对外提供朋务癿网络接口:ipconfig 查看网卡
virtual_router_id 60 #VRRP组名,两个节点设置必项一样,指明各个节点属二同一VRRP组
priority 150 #数值愈多,优先级越高,上改为120
advert_int 1 #同步通知间隔
authentication { #包含验证类型和验证密码。类型主要有PASS、AH丟种,通常使用类型为PASS,据说AH使用有时问题
auth_type PASS
auth_pass 1111
}
track_script
{
chk_http_port #调用脚本check_haproxy.sh检查haproxy是否存活
}
virtual_ipaddress { #vip地址,返个ip必项不我们在lvs宠户竢讴定癿vip相一致
192.168.10.10 dev eth0 scope global
}
notify_master /etc/keepalived/scripts/haproxy_master.sh
notify_backup /etc/keepalived/scripts/haproxy_backup.sh
notify_fault /etc/keepalived/scripts/haproxy_fault.sh
notify_stop /etc/keepalived/scripts/haproxy_stop.sh
}
注意: 默认为51,需要修改一下,要不在keepallived会报错。
interface eth1 和172.17.210.103 dev eth1 scope global中癿eth1指的是网卡,如果是多网卡,可能会有eth0,eth1,eth2…,可以使用ifconfig命令查看,确保eth0是本机存在的网卡地址。有的服务器如果只有一个网卡,但被人为把eth0改成eth1了,你在写eth0就找不到了。
virtual_router_id 60
/etc/keepalived/scripts 检查脚本:
-rwxr-xr-x 1 root root 528 Apr 14 22:08 check_haproxy.sh
-rwxr-xr-x 1 root root 531 Apr 14 22:14 haproxy_backup.sh
-rwxr-xr-x 1 root root 104 Apr 14 22:11 haproxy_fault.sh
-rwxr-xr-x 1 root root 531 Apr 14 22:10 haproxy_master.sh
-rwxr-xr-x 1 root root 103 Apr 14 22:11 haproxy_stop.sh
check_haproxy.sh:
#!/bin/bash
STARTHAPROXY="/opt/app/tomcat/haproxy-1.5.8/haproxy -f /opt/app/tomcat/haproxy-1.5.8/haproxy.cfg"
STOPKEEPALIVED="/etc/init.d/keepalived stop"
LOGFILE="/var/log/keepalived-haproxy-state.log"
echo "[check_haproxy status]" >> $LOGFILE
A=`ps -C haproxy --no-header | wc -l`
echo "[check_haproxy status]" >> $LOGFILE
date >> $LOGFILE
if [ $A -eq 0 ];then
echo $STARTHAPROXY >> $LOGFILE
$STARTHAPROXY >> $LOGFILE 2>&1
sleep 5
fi
if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then
exit 0
else exit 1
fi
haproxy_backup.sh
[root@kaifa scripts]# cat haproxy_backup.sh
#!/bin/bash
STARTHAPROXY=`/opt/app/tomcat/haproxy-1.5.8/haproxy -f /opt/app/tomcat/haproxy-1.5.8/haproxy.cfg`
STOPHAPROXY=`ps -ef |grep haproxy-1.5.8/haproxy|grep -v grep|awk '{print $2}'|xargs kill -s 9`
LOGFILE="/var/log/keepalived-haproxy-state.log"
echo "[backup]" >> $LOGFILE date >> $LOGFILE
echo "Being backup...." >> $LOGFILE 2>&1
echo "stop haproxy...." >> $LOGFILE 2>&1
$STOPHAPROXY >> $LOGFILE 2>&1
echo "start haproxy...." >> $LOGFILE 2>&1
$STARTHAPROXY >> $LOGFILE 2>&1
echo "haproxy stared ..." >> $LOGFILE
[root@kaifa scripts]# cat haproxy_fault.sh
#!/bin/bash
LOGFILE=var/log/keepalived-haproxy-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
[root@kaifa scripts]# cat haproxy_master.sh
#!/bin/bash
STARTHAPROXY=`/opt/app/tomcat/haproxy-1.5.8/haproxy -f /opt/app/tomcat/haproxy-1.5.8/haproxy.cfg`
STOPHAPROXY=`ps -ef |grep haproxy-1.5.8/haproxy|grep -v grep|awk '{print $2}'|xargs kill -s 9`
LOGFILE="/var/log/keepalived-haproxy-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "stop haproxy...." >> $LOGFILE 2>&1
$STOPHAPROXY >> $LOGFILE 2>&1
echo "start haproxy...." >> $LOGFILE 2>&1
$STARTHAPROXY >> $LOGFILE 2>&1
echo "haproxy stared ..." >> $LOGFILE
[root@kaifa scripts]# cat haproxy_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-haproxy-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
启用keepalived服务
启用服务 service keepalived start
[root@kaifa scripts]# cat /etc/sysconfig/keepalived
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp -P Only run with VRRP subsystem.
# --check -C Only run with Health-checker subsystem.
# --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs -I Dont remove IPVS topology on daemon stop.
# --dump-conf -d Dump the configuration data.
# --log-detail -D Detailed log messages.
# --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON)
#
KEEPALIVED_OPTIONS="-D -d -S 0"