概述
本章节主要介绍配置HaProxy+Keepalived高可用群集,Mycat的配置就不在这里做介绍,可以参考我前面写的几篇关于Mycat的文章。
部署图:
配置
HaProxy安装
181和179两台服务器安装haproxy的步骤一致
--创建haproxy用户 useradd haproxy --解压完后进入haproxy目录 cd haproxy-1.4.25/ --编译安装 make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=x86_64 make install PREFIX=/usr/local/haproxy
HaProxy配置
cd /usr/local/haproxy touch haproxy.cfg vim haproxy.cfg
global
log 127.0.0.1 local0 ##记日志的功能
maxconn 4096
chroot /usr/local/haproxy
user haproxy
group haproxy
daemon
defaults
log global
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen admin_stats 192.168.57.180:48800 ##统计页面
stats uri /admin-status
stats auth admin:admin
mode http
option httplog
listen mycat_service 192.168.57.180:18066 ##客户端就是通过这个ip和端口进行连接,这个vip和端口绑定的是mycat8066端口
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
balance roundrobin
server mycat_181 192.168.57.181:8066 check port 48700 inter 5s rise 2 fall 3
server mycat_179 192.168.57.179:8066 check port 48700 inter 5s rise 2 fall 3
srvtimeout 20000
listen mycat_admin 192.168.57.180:19066 ##客户端就是通过这个ip和端口进行连接,这个vip和端口绑定的是mycat9066端口
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
balance roundrobin
server mycat_181 192.168.57.181:9066 check port 48700 inter 5s rise 2 fall 3
server mycat_179 192.168.57.179:9066 check port 48700 inter 5s rise 2 fall 3
srvtimeout 20000
配置haproxy记录日志功能
yum –y install rsyslog
mkdir /etc/rsyslog.d
cd /etc/rsyslog.d/
touch haproxy.conf
vim haproxy.conf
$ModLoad imudp $UDPServerRun 514 local0.* /var/log/haproxy.log
vim /etc/rsyslog.conf
在#### RULES ####上面一行加入以下内容
# Include all config files in /etc/rsyslog.d/ $IncludeConfig /etc/rsyslog.d/*.conf
在local7.* /var/log/boot.log下面加入以下内容
local0.* /var/log/haproxy.log
重启rsyslog服务
service rsyslog restart
将rsyslog加入自动启动服务
chkconfig --add rsyslog chkconfig --level 2345 rsyslog on
配置监听mycat是否存活
安装xinetd插件
yum install xinetd -y
cd /etc/xinetd.d touch mycat_status
vim mycat_status
service mycat_status { flags = REUSE socket_type = stream port = 48700 wait = no user = nobody server =/usr/local/bin/mycat_status log_on_failure += USERID disable = no }
创建xinetd启动服务脚本
vim /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=`/usr/local/mycat/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
修改脚本文件权限
我就是在这里被坑了很久,根据权威指南上面mycat_status这个脚本里面的内容也有很多问题,好几个地方没有空格.
chmod 777 /usr/local/bin/mycat_status chmod 777 /etc/xinetd.d/mycat_status
将启动脚本加入服务
vim /etc/services
在末尾加入
mycat_status 48700/tcp # mycat_status
重启xinetd服务
service xinetd restart
将xinetd加入自启动服务
chkconfig --add xinetd chkconfig --level 2345 xinetd on
页面测试
http://192.168.57.180:48800/admin-status
由于179还没有安装好,所以这里179显示连接失败
创建haproxy启停脚本
启动脚本
touch /usr/local/haproxy/sbin/start chmod +x /usr/local/haproxy/sbin/start vim /usr/local/haproxy/sbin/start
#!/bin/sh /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg &
关闭脚本
touch /usr/local/haproxy/sbin/stop chmod +x /usr/local/haproxy/sbin/stop vim /usr/local/haproxy/sbin/stop
#!/bin/sh ps -ef | grep sbin/haproxy | grep -v grep |awk '{print $2}'|xargs kill -s 9
授权
chown -R haproxy.haproxy /usr/local/haproxy/*
Keepalived安装步骤
keepalived的安装步骤过程在两台服务器上除了keepalived.conf配置文件稍微有点区别外其他的地方都一致。
openssl安装
./config --prefix=/usr/local/openssl ./config -t make depend make make test make install ln -s /usr/local/openssl /usr/local/ssl vim /etc/ld.so.conf
在文件末尾加入以下内容
/usr/local/openssl/lib
修改环境变量
vim /etc/profile
在文件末尾加入以下内容
export OPENSSL=/usr/local/openssl/bin export PATH=$PATH:$OPENSSL
使环境变量立刻生效
source /etc/profile
安装openssl-devel
yum install openssl-devel -y
测试
ldd /usr/local/openssl/bin/openssl
linux-vdso.so.1 => (0x00007fff996b9000) libdl.so.2 =>/lib64/libdl.so.2 (0x00000030efc00000) libc.so.6 =>/lib64/libc.so.6 (0x00000030f0000000) /lib64/ld-linux-x86-64.so.2 (0x00000030ef800000)
which openssl
/usr/bin/openssl
openssl version
OpenSSL 1.0.0-fips 29 Mar 2010
keepalived安装
./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
创建配置文件和脚本
mkdir /etc/keepalived/scripts
cd /etc/keepalived/scripts
vim /etc/keepalived/keepalived.conf
master
! Configuration Fileforkeepalived vrrp_script chk_http_port { script"/etc/keepalived/scripts/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER #192.168.57.179上改为BACKUP interface eth0 #对外提供服务的网络接口 virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组 priority 150 #数值愈大,优先级越高,backup上改为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地址,haproxy配置的使用的就是这里配置的VIP 192.168.57.180 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 }
backup
! Configuration Fileforkeepalived vrrp_script chk_http_port { script"/etc/keepalived/scripts/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP #192.168.57.179上改为BACKUP interface eth0 #对外提供服务的网络接口 virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组 priority 120 #数值愈大,优先级越高,backup上改为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地址 192.168.57.180 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 }
vim /etc/keepalived/scripts/check_haproxy.sh
#!/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_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
vim /etc/keepalived/scripts/haproxy_master.sh
#!/bin/bash STARTHAPROXY=`/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg`
STOPHAPROXY=`ps -ef | grep sbin/haproxy | grep -v grep | awk '{print $2}'| xargs kill -s 9` LOGFILE="/usr/local/keepalived/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
vim /etc/keepalived/scripts/haproxy_backup.sh
#!/bin/bash STARTHAPROXY=`/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg` STOPHAPROXY=`ps -ef | grep sbin/haproxy | grep -v grep | awk '{print $2}'| xargs kill -s 9` LOGFILE="/usr/local/keepalived/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
vim /etc/keepalived/scripts/haproxy_fault.sh
#!/bin/bash LOGFILE=/usr/local/keepalived/var/log/keepalived-haproxy-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE
vim /etc/keepalived/scripts/haproxy_stop.sh
#!/bin/bash LOGFILE=/usr/local/keepalived/var/log/keepalived-haproxy-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
赋予脚本可执行权限
chmod 777 /etc/keepalived/scripts/*
将keepalived加入自启动服务
chkconfig --add keepalived chkconfig --level 2345 keepalived on --启动服务 service keepalived start
总结
权威指南上面代码部分好多处都存在问题,几乎每一块代码都存在问题,有时候往往一个空格需要花很长的时间去找这个问题,所以在代码方面要细心;特别是复制别人的代码不要原本照抄最好是检查一下,除了代码本身的问题权限有时候也是容易忽略的错误,特别是可执行文件要注意是否有可执行权限。
本文转自pursuer.chen(陈敏华)博客园博客,原文链接:http://www.cnblogs.com/chenmh/p/5194636.html,如需转载请自行联系原作者