(原创)Haproxy+keepalived+mycat安装与配置

栏目: 集群运维

一.mycat机器中安装安装xinetd插件来监听Mycat服务器状态

  • 安装

      yum install xinetd 

       cd /etc/xinetd.d

       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

  添加执行权限          

          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






二.安装keepalived

下载地址:http://www.keepalived.org/software/keepalived-1.2.20.tar.gz

yum install openssl-devel

解压 keepaliived-1.2.20.tar.gz

./configure --prefix=/usr/local/keepalived

make && make install

设置keepalived开机启动脚本

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

chkconfig keepalived on

mkdir /etc/keepalived

cd /etc/keepalived/

cp /usr/local/keepalived/etc/keepalived/keepalived.conf  /etc/keepalived

vim /etc/keepalived/keepalived.conf




global_defs { notification_email { kezs@fsmeeting.com } notification_email_from root@localhostsmtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script"/etc/keepalived/scripts/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER #另一台备机上改为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.5.222 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 keepalivedchkconfig --level 2345 keepalived on--启动服务service keepalived start

参考:http://www.cnblogs.com/chenmh/p/5194636.html

     

三. Haproxy 安装

参考: http://valleylord.github.io/post/201603-mycat-haproxy/

1.下载地址 : https://github.com/haproxy/haproxy.git

2. 安装

--创建haproxy用户

useradd haproxy

--解压完后进入haproxy目录

cd haproxy

--编译安装

make install PREFIX=/usr/local/haproxy

3.编辑配置文件

   

cd /usr/local/haproxy touch haproxy.cfg vim haproxy.cfg

 

   注意centos7 的网卡名称不一定交做eth0




192.168.5.74 是当前机器用来安装Haprox

   192.168.6.110  mycat安装机器   mycat 连接端口 8066 管理端口9066

   192.168.6.111  mycat安装机器 mycat 连接端口 8066 管理端口9066

   

global maxconn 512 daemon pidfile /usr/local/haproxy/haproxy.pid defaults mode tcp option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接 timeout connect 5000ms #连接超时 timeout client 30000ms #客户端超时 timeout server 30000ms #服务器超时 #timeout check 2000 #=心跳检测超时 balance roundrobin #负载均衡算法 log 127.0.0.1 local0 info #[err warning info debug] listen admin_status bind 192.168.5.222:48800 ##VIP stats uri /admin-status ##统计页面 stats auth admin:admin mode http option httplog listen allmycat_service bind 192.168.5.222:8096 ##转发到mycat的8066端口,即mycat的服务端口 mode tcp option tcplog option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www balance roundrobin server mycat_110 192.168.6.110:8066 check port 48700 inter 5s rise 2 fall 3 server mycat_111 192.168.6.111:8066 check port 48700 inter 5s rise 2 fall 3 listen allmycat_admin bind 192.168.5.222:8097 ##转发到mycat的9066端口,及mycat的管理控制台端口 mode tcp option tcplog option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www balance roundrobin server mycat_110 192.168.6.110:9066 check port 48700 inter 5s rise 2 fall 3 server mycat_111 192.168.6.111:9066 check port 48700 inter 5s rise 2 fall 3 ~

安装rsyslog用来记录haproxy日志 yum 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

  配置Haxproxy 启动脚本

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 &

配置Haproxy 关闭脚本

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/*

启动 haproxy

/usr/local/haproxy/sbin/start


测试连接Haproxy:mysql -h 192.168.5.222 -umycat -pmycat -P8096



遇到坑:

 不能绑定到虚拟IP,因为我用的是虚拟机 eth0没有启用,启用的eth2 ,解决办法,修改/etc/keepalived/keepalived.conf



如果主机启动后,再启动备机出现以下错误是正常的,因为备机没有抢到VIP


当主机挂掉之后自动会切换到备机 (手动停止主机service keepalived stop)

使用ip a就可以查看到备机已经抢到vip了(192.168.5.222)






曾梦想仗剑走天涯 看一看世界的繁华 年少的心总有些轻狂 如今你四海为家 曾让你心疼的姑娘 如今已悄然无踪影 爱情总让你渴望又感到烦恼 曾让你遍体鳞伤 Dilililidilililidenda Dilililidilililidada Dilililidilililidada 走在勇往直前的路上 Dilililidilililidenda Dilililidilililidada Dilililidilililidada 有难过也有精彩 每一次难过的时候 就独自看一看大海 总想起身边走在路上的朋友 有多少正在疗伤 Dilililidilililidenda Dilililidilililidada Dilililidilililidada 不知多少孤独的夜晚 Dilililidilililidenda Dilililidilililidada Dilililidilililidada 从昨夜酒醉醒来 每一次难过的时候 就独自看一看大海 总想起身边走在路上的朋友 有多少正在醒来 让我们干了这杯酒 好男儿胸怀像大海 经历了人生百态世间的冷暖 这笑容温暖纯真 每一次难过的时候 就独自看一看大海 总想起身边走在路上的朋友 有多少正在醒来 让我们干了这杯酒 好男儿胸怀像大海 经历了人生百态世间的冷暖 这笑容温暖纯真

关键字

最近访客