mysql复制+keepalived+haproxy配置(负载均衡)

双主+keepalived+haproxy配置(负载均衡)

实验系统:CentOS 6.5_x86_64
实验前提:防火墙和selinux都关闭
实验软件:keepalived-1.2.13  haproxy-1.8.13  mysql—5.7.21
主1 ip:192.168.226.134
主2 ip:192.168.226.135
vip 192.168.226.150

一、安装mysql

获取mysql安装包:wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
安装mysql过程省略

二、配置主主复制

命令 service iptables stop
检查:service iptables status
关闭vi /etc/selinux/config
SELINUX=disabled

两台服务器上都要执行:
创建复制用户
grant replication slave on *.* to ‘repl‘@‘%‘ identified by ‘123‘;

进入从服务器mysql
命令: # mysql -uroot -p
关闭slave
命令:stop slave;
开始配置:
输入下面代码即可:
CHANGE MASTER TO MASTER_HOST=‘192.168.226.135‘, MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123‘, MASTER_LOG_FILE=‘mysql-bin.000002‘, MASTER_LOG_POS=681;
先在从服务器配置完成,启动从服务器:
命令: start slave;
反向再配置一次

三、安装haproxy(两台节点都要安装)

tar -zxvxf haproxy-1.8.13.tar.gz
cd haproxy-1.8.13
make TARGET=linux2628 //根据自己主机设定
make install

提供启动脚本

vi /etc/init.d/haproxy

#!/bin/sh
#
# haproxy
#
# chkconfig: - 85 15
# description: HAProxy is a free, very fast and reliable solution \
# offering high availability, load balancing, and \
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

exec="/usr/local/sbin/haproxy"
prog=$(basename $exec)

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy

check() {
$exec -c -V -f $cfgfile $OPTIONS
}

start() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi

echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}

reload() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
retval=$?
echo
return $retval
}

force_reload() {
restart
}

fdr_status() {
status $prog
}

case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
check)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
esac

chkconfig --add haproxy
chkconfig haproxy on
chmod +x /etc/init.d/haproxy

提供配置文件

mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy

vi /etc/haproxy/haproxy.cfg

global

log 127.0.0.1 local2

chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon

stats socket /var/lib/haproxy/stats

defaults
mode tcp //haproxy运行模式
log global
option dontlognull
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 600 //最大连接数

listen stats //配置haproxy状态页
mode http
bind :6677 //找一个比较特殊的端口
stats enable
stats hide-version //隐藏haproxy版本号
stats uri /haproxyadmin?stats //一会用于打开状态页的uri
stats realm Haproxy\ Statistics //输入账户密码时的提示文字
stats auth admin:admin //用户名:密码
stats admin if TRUE //开启状态页的管理功能

frontend main
bind *:80
default_backend mysql //后端服务器组名

backend mysql
balance leastconn //使用最少连接方式调度
server m1 192.168.226.134:80 check port 80 maxconn 300
server m2 192.168.226.135:80 check port 80 maxconn 300

启动日志:
vi /etc/rsyslog.conf

# Provides UDP syslog reception //去掉下面两行注释,开启UDP监听
$ModLoad imudp
$UDPServerRun 514

local2.* /var/log/haproxy.log //添加此行

service rsyslog restart

启动测试haproxy:

service haproxy start
netstat -tnlp

四、安装keepalived (两台服务器都要执行)
yum install -y keepalived

chkconfig --add keepalived
chkconfig keepalived on

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vi /etc/keepalived/keepalived.conf

粘贴如下内容
! Configuration File for keepalived
global_defs {
router_id Mysql_ha
}

vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight 5
}

vrrp_script chk_haproxy {
script "/etc/keepalived/chk.sh"
interval 2
weight 5
}

vrrp_instance mysql-instance {
state MASTER #另一台为BACKUP
interface enp4s0 #与网卡名称对应
virtual_router_id 11 #每一个IP唯一,另一台绑定相同IP要与整个ID一致
priority 10 #另一台为9
advert_int 1
authentication {
auth_type PASS
auth_pass password321
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.226.150/24
}
}

vrrp_instance mysql-ha {
state MASTER
interface enp4s0
virtual_router_id 13
priority 10
advert_int 1
# nopreempt
authentication {
auth_type PASS
auth_pass password321
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.226.14/24
}
notify_backup "/etc/init.d/haproxy restart"
notify_fault "/etc/init.d/haproxy stop"
}

在两台机器上创建chk.sh文件:
vi /etc/keepalived/chk.sh

#!/bin/bash
#
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
/etc/init.d/keepalived stop
fi

chmod +x /etc/keepalived/chk.sh

vi /etc/keepalived/check_mysql.sh
#!/bin/bash
MYSQL_PORT=:3306
alive=$(netstat -apn |grep $MYSQL_PORT|grep LISTEN)
if [ -z "$alive" ]
then
exit 1
else
exit 0
fi

五、在两台机器上测试
.在浏览器打开http://192.168.226.150:6677/haproxyadmin?stats,打开haproxy状态页:

原文地址:https://www.cnblogs.com/sunkang-dba/p/11778431.html

时间: 2024-10-08 11:09:53

mysql复制+keepalived+haproxy配置(负载均衡)的相关文章

Keepalived + Haproxy实现负载均衡高可用

基于LVS实现Keepalived的高可用见:http://amelie.blog.51cto.com/12850951/1979636 负载均衡为将用户并发请求转发到后台若干服务器,以分流方式均衡负载.均衡负载借用的软件,我们这里是Haproxy.而负责转发的服务器,我们称之为负载均衡服务器,也称反向代理服务器.这种情况下,如果负载均衡服务器挂掉,后台服务器再多也会导致服务无法访问.为提高负载均衡服务器的可用性,我们可以架设从服务器随时接管主负载均衡服务器的工作,而这种方式的实现,就要借助于相

案例一(haproxy+keepalived高可用负载均衡系统)【转】

1.搭建环境描述: 操作系统: [[email protected] ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 地址规划: 主机名 IP地址 集群角色 虚拟IP haproxy-server 10.0.0.35 主HAProxyServer 10.0.0.40 haproxy-backup 10.0.0.36 备用HAProxyServer webapp1 10.0.0.150 Backend Server 无 webapp2

Haproxy+多台MySQL从服务器(Slave) 实现负载均衡

本系统采用MySQL一主多从模式设计,即1台 MySQL"主"服务器(Master)+多台"从"服务器(Slave),"从"服务器之间通过Haproxy进行负载均衡,对外只提供一个访问IP,当程序需要访问多台"从"服务器时,只需要访问Haproxy,再由Haproxy将请求分发到各个数据库节点. 我们的程序可以有俩个数据源(DataSourceA,DataSourceB),一个(DataSourceA)直接连接主库,另外一个(

Ubuntu下配置 keepalived+nginx+tomcat 负载均衡

本文力图阐述在 Ubuntu Server 环境下使用 Keepalived + Nginx + Tomcat 搭建高可用负载均衡环境的操作步骤和简约配置,这里不涉及性能调优.先说一下他们各自扮演的角色: Tomcat – 应用服务器 Nginx – 反向代理服务器,作为负载均衡器 Keepalived – 用以检测 Nginx 状态,保证高可用 整个环境都在Virtual Box中的虚拟机上完成,以下是要完成此环境需要的软件: Oracle VM Virtual Box 4.3 Ubuntu

LVS+Keepalived实现MySQL从库读操作负载均衡

http://www.osyunwei.com/archives/7464.html (学习运维知识好站) 说明: 操作系统:CentOS 5.X 64位 MySQL主服务器:192.168.21.126 MySQL从服务器:192.168.21.127,192.168.21.128 MySQL主从同步的数据库为:osyunweidb 实现目的: 增加两台服务器(主备),通过LVS+Keepalived实现MySQL从库读操作负载均衡 架构规划: 操作系统:CentOS 5.X 64位 LVS主

(2)LVS+Keepalived高可用负载均衡架构原理及配置

1.keepalived 介绍2.keepalived 优缺点3.keepalived 应用场景4.keepalived 安装配置5.keepalived+lvs 高可用6.keepalived+nginx 高可用7.keepalived 切换原理8.性能优化9.常见故障 一.keepalived 介绍 1.keepalived 定义keepalived是一个基于VRRP(virtual route redundent protocol)协议来实现的LVS服务高可用方案,可以利用其来避免单点故障

Keepalived高可用服务器以及haproxy的负载均衡

Keepalived高可用服务器以及haproxy的负载均衡 Keepalive运行原理 Keepalived检测每个服务节点状态 当服务器节点异常或工作出现故障,keepalived将故障节点从集群从集群系统中踢除 故障节点恢复后,自动将其加入 使用keepalived软件配置HA集群 使用keepalived软件配置HA集群 1.提供服务的主机需要装keepalived和web服务 yum -y install httpd keepalived 2.修改提供服务的主机的keepalived的

HAproxy的负载均衡+日志独立+动静分离+读写分离的配置

主机环境   redhat6.5 64位 实验环境   服务端1 ip 172.25.29.2            服务端2 ip 172.25.29.3             管理端1 ip 172.25.29.1  防火墙状态:关闭 1.Haproxy的负载均衡及测试 A)负载均衡的配置 [[email protected] ~]# yum install haproxy -y            #安装haproxy [[email protected] ~]# vim /etc/h

配置nginx+keepalived高可用负载均衡的时候。主从服务器都出现了VIP 阿里云问题

配置nginx+keepalived高可用负载均衡的时候.主从服务器都出现了VIP 出现这问题的场景是在阿里VPS云服务器网络环境中,因为路由交换层禁用了ARP的广播限制,造成KEEPALIVE主备协议无法通过广播的方式进行通信,造成主备两台服务器都强占HAVIP地址,出现同时两台服务器都有VIP地址的情况出现,必须通过配置来指定IP的两台服务器间进行通讯(阿里说明文档中解释只能支持两台使用同一个HAVIP地址),基于以下方法可以的情况下,多备方式用同样的方式也应该可行 ,有需要的兄弟可以测试下