集群部署图的理解:
1、 keepalive和haproxy必须装在同一台机器上,keepalived负责为该服务器抢占VIP。
2、 192.168.1.107上的keepalived会抢占VIP,抢占VIP有优先级,配置keepalived.conf中参数priority值越大优先越高,在同等情况下,keepalived服务器启动先得会先抢到VIP。
3、 Harpoxy负责将对VIP的请求分发到mycat上,起到负载均衡的作用,同时haproxy也能检测到mycat是否存活,haproxy只会将请求转发到存活的mycat上。
4、 如果一台服务器(keepalive+mycat)宕机,另外一台上的keepalived会立刻抢占vip并接管服务,如果一台mycat服务器宕机,haproxy转发时候不会转发到宕机的mycat上.
keepalived的安装配置:
两台机器进行keepalived安装,个别参数区别其他一致。
1、 安装:
#tar -zxvf keepalived-1.2.19.tar.gz
# cd keepalived-1.2.19 &&./configure --prefix=/usr/local/keepalived &&make&& make install
不报错,表示ok,有些系统可能需要安装gcc这个依赖包
Yum –y install gcc
2、
配置
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
vi/etc/keepalived/keepalived.conf ####Master
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server192.168.200.1
smtp_connect_timeout 30
router_idLVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER ##把MASTER改成BACKUP
interface eth1 #和本机网卡接口名一致
virtual_router_id 51
priority 100 ###权重 ,SLAVE点改小于100就好
advert_int 1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress {
192.168.1.219 dev eth1 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
}
###到这里可以测试下VIP的漂移功能。
二、安装HAPROXY
1、安装HAPROXY:
#useradd haproxy
tar -zxvfhaproxy-1.4.20.tar.gz
cdhaproxy-1.4.20 && make TARGET=linux26 PREFIX=/usr/local/haproxyARCH=X86_64 && make install PREFIX=/usr/local/haproxy
chown –R haproxy.haproxy /usr/local/haprox
cd /usr/local/haproxy
编辑配置文件haproxy.cfg,以提供,修改内容即可
2、haproxy记录日志,默认haproxy是不记录日志的,为了记录日志还需要配置syslog模块,在linux下是rsyslogd服务,
#yum –y installrsyslog先安装rsyslog
若yum安装rsyslog 有这样问题:
一般安装好rsyslog会自动生成rsyslog.d这个目录,若无自己创建
#cd /etc/rsyslog.d/ && touch haproxy.conf
#vim /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local0.* /var/log/haproxy.log ###这个必须和haproxy.cfg的配置文件一致。
#vim /etc/rsyslog.cnf
3、重启服务
#service rsyslogrestart
现在你就可以看到日志(/var/log/haproxy.log)了
到此haproxy+keepalived 的结合完毕,只需要在haproxy.cfg添加服务即可。
配置监听 mycat是否存活
在192.168.1.112 和192.168.1.113 上添加检测端口48700,为此需要用到xinetd,xinetd为LINUX系统基础服务
1、若无xinetd服务,则安装
#yum –y installxinetd
2、检查/etc/xined.conf是否有:
3、添加/etc/xinetd.d/mycat_status,此目录安装后默认存在。
#vimmycat_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
}
4、把mycat_status添加到/etc/services服务中去
5、/usr/local/bin/mycat_status的脚本,并设置权限777
6、重启服务、验证mycat_status服务是否启动成功
#service xinetd restart
#netstat –antup |grep 48700
配置haproxy的脚本脚本,在/etc/keepalived目录添加目录sripts,存放haproxy的脚本脚本。
1、check_haproxy.sh
vi/etc/keepalived/scripts/check_haproxy.sh
##脚本含义:如果没有haproxy进程存在,就启动haproxy,停止keepalived
#!/bin/bash
STARTHAPROXY="/usr/local/haproxy/sbin/haproxy -f/usr/local/haproxy/haproxy.cfg"
STOPKEEPALIVED="/etc/init.d/keepalived stop"
LOGFILE="/usr/local/keepalived/var/log/keepalived-haproxy-state.log"
echo "[check_haproxystatus]" >>$LOGFILE
A=`ps-C haproxy --no-header |wc-l`
echo "[check_haproxystatus]" >>$LOGFILE
date >>$LOGFILE
if [ $A-eq 0 ];then
echo $STARTHAPROXY>> $LOGFILE
$STARTHAPROXY >> $LOGFILE 2>&1
sleep5
fi
if [`ps -C haproxy --no-header |wc-l` -eq 0 ];then
exit 0
else
exit 1
fi
2、haproxy_master.sh(master和slave一样):
3、haproxy_backup.sh(master和slave一样)
4、haproxy_fault.sh(master和slave一样)
5、haproxy_stop.sh
在/etc/keepalived.conf配置文件去掉#好,启动这些脚本
要给脚本的权限:777
5、重启keepalived 服务
#Service keepalived restart
测试~~~Ing~~~~
本文转自 DBAspace 51CTO博客,原文链接:http://blog.51cto.com/dbaspace/1867872