转载:http://www.cnblogs.com/chenmh/p/5194636.html
本章节主要介绍配置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 allmycat_service 192.168.57.180:8098 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
配置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地址,这个ip必须与我们在lvs客户端设定的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地址,这个ip必须与我们在lvs客户端设定的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
权威指南上面代码部分好多处都存在问题,几乎每一块代码都存在问题,有时候往往一个空格需要花很长的时间去找这个问题,所以在代码方面要细心;特别是复制别人的代码不要原本照抄最好是检查一下,除了代码本身的问题权限有时候也是容易忽略的错误,特别是可执行文件要注意是否有可执行权限。
相关推荐
在HAProxy+Keepalived的组合中,Keepalived用于监控HAProxy服务的状态,并在HAProxy节点发生故障时快速切换VIP到正常运行的节点,保证服务的连续性。 在实现过程中,需要对操作系统、JDK、Keepalived、HAProxy、...
HAProxy+Keepalived+LVS实现负载平衡高可用集群最佳实践
总之,`keepalived+haproxy+mycat` 的组合为 MySQL 数据库提供了高可用性和负载均衡的解决方案,能够在保障服务稳定的同时,提升系统的处理能力和容错性。通过合理配置和维护,可以有效地应对大规模业务场景下的...
【标题】:“第53节--MyCat高可用负载均衡集群实现(HAProxy+Keepalived+MyCat” 【描述】:本节课程主要讲解如何通过HAProxy、Keepalived和MyCat搭建一个高可用、负载均衡的数据库集群。这个集群能够确保数据服务...
### MyCat高可用负载均衡集群实现(HAProxy+Keepalived+MyCat) #### 一、概述 在当今高度依赖数据库应用的企业环境中,确保数据库系统的稳定性和高效性至关重要。为了达到这一目标,采用高可用(High ...
总结来说,这个集群环境通过MySQL提供数据库服务,Mycat进行数据分片和负载均衡,Keepalived确保主从切换的高可用性,而HAProxy则进一步增强了对外服务的负载均衡能力。这样的组合可以有效提升系统的稳定性和处理...
主机名IP地址操作系统组件备注环境说明架构图目录结构配置文件docker-compose配置文件keepalived配置文件keepalived检测脚本hapr
"Role部署LNMP+HAproxy+keepalived高可用" 本文将详细介绍如何使用Ansible搭建高可用的LNMP(Linux、Nginx、Mysql、PHP)环境,并使用HAproxy和keepalived实现负载均衡和高可用性。 一、Ansible简介 Ansible是一种...
HaProxy + Keepalived实现高可用负载均衡
本篇文章十分详细地记录了Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)的部署全过程,可作为线上实操手册。特在此分享,希望能帮助到有用到的朋友。
kubeadm部署高可用k8s集群,haproxy+keepalived-版本k8s1.20.4,详细笔记总结文档
【标题】:“Haproxy+Keepalived+Nginx+Lamp+Nfs实现高可用集群1” 【描述】:“在互联网服务中,高可用性是关键。本案例将通过Haproxy、Keepalived、Nginx、LAMP(Linux、Apache、MySQL、PHP)和NFS(网络文件系统...
Mysql+haproxy+mycat+pxc+zookeeper实现高可用集群 本文将详细介绍如何使用Mysql、Haproxy、Mycat、PXC、Zookeeper实现高可用集群,涵盖了整个架构图、应用程序的访问流程、数据的读写分离、负载均衡、数据库节点的...
本文将详细介绍如何在Linux环境中搭建HAProxy+Keepalived+MySQL的高可用架构,以实现MySQL数据库服务的负载均衡和故障切换。 首先,HAProxy是一个高性能的负载均衡器,用于分发网络流量到多个后端服务器,以提高...
通过配置 Haproxy 和 Keepalived 实现高可用性,确保集群的可用性和负载均衡。 知识点: 1. 高可用集群的定义和重要性 高可用集群是指一个系统或应用程序在出现故障或宕机时仍能继续提供服务的能力。高可用集群...
Rabbitmq+HAproxy+Keepalived实现高可用加负载均衡-附件资源
在构建高可用性服务时,`Haproxy` 和 `...总之,`Haproxy+Keepalived` 配合使用能够构建出高可用的负载均衡解决方案,确保服务的稳定性和可靠性。正确配置这两个工具,可以显著提升系统性能并降低单点故障的风险。