lvs(DR)+keepalived+mysqld主从

三台机器:
director(eth0192.168.0.8, vip eth0:0: 192.168.0.101)
real server1(eth0 rip: 192.168.0.140 vip lo:0:192.168.0.101)
real server2(eth0 rip: 192.168.0.141, vip lo:0:192.168.0.101)

1、自己编写的一键源码安装的lnmp脚本

2、安装LVS(DR)

yum install ipvsadm

Director 上 vim /usr/local/sbin/lvs_dr.sh //增加

vi /usr/local/sbin/lvs_DR.sh  #编辑一个脚本

#!/bin/bash

# directory 服务器开启路由转发功能:

echo 1 > /proc/sys/net/ipv4/ip_forward

ipv=‘/sbin/ipvsadm‘

vip=192.168.0.100

rs1=192.168.0.7
rs2=192.168.0.5
ifconfig eth0:0 $vip broadcast $vip netmask255.255.255.255 up
route add -host $vip dev eth0:0
$ipv -C
$ipv -A -t $vip:80 -s rr 
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1       #-g代表是DR模式
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1

两台rs上:vim/usr/local/sbin/lvs_dr_rs.sh
#! /bin/bash
vip=192.168.0.100
ifconfig lo:0 $vip broadcast $vip netmask255.255.255.255 up 
route add -host $vip lo:0
echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce

3、 分别配置三台机器的keepalived的配置:(配置完毕启动是先从后主)

yum安装keepalived,

Keeplived(负载均衡+HA合成的二者为一体)

vi /etc/keepalived/keepalived.conf

##########MASTER###########

global_defs {

#   notification_email {

#          [email protected]

#}

#notification_email_from [email protected]

#smtp_server mail.qq.com

#smtp_connect_timeout30

router_id LVS1

}

#vrrp_script chk_http_port {

#       script "/etc/keepalived/nginx_check.sh"

#       interval 2

#       weight 2

vrrp_sync_group VS_group {

group {

VS_1

}

}

vrrp_instance VS_1 {

state MASTER   #备用服务器上为 BACKUP,主是MASTER

interface eth0

lvs_sync_daemon_interface eth0

virtual_router_id  60

priority 150     #备用服务器上为100,主是150

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.101

}

}

virtual_server 192.168.0.101 80 {

delay_loop 6                  #(每隔10秒查询realserver状态)

lb_algo rr                  #(lvs 算法)

lb_kind DR                  #(Direct Route)

persistence_timeout 20    #(同一IP的连接60秒内被分配到同一台realserver)

protocol TCP                #(用TCP协议检查realserver状态)

real_server 192.168.0.140 80 {

weight 100               #(权重)

TCP_CHECK {

connect_timeout 10       #(10秒无响应超时)

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

real_server192.168.0.141 80 {

weight 100

TCP_CHECK {

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

}

至此,lvs(DR)+keepalived已完成

4、添加防火墙规则后并保存:

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 80 -jACCEPT #允许80端口对外提供服务

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 10050 -jACCEPT

iptables -A INPUT -d 192.168.2.0/24 -j ACCEPT

iptables -A INPUT -p vrrp -j ACCEPT    #LVS DR模式。当用户请求LVS-DR的VIP时,只有DR响应客户端的ARP广播包,允许vrrp虚拟路由器冗余协议

iptables -A INPUT -p icmp -j ACCEPT

iptables -A INPUT -jREJECT --reject-with icmp-host-prohibited

iptables -A FORWARD -j REJECT --reject-withicmp-host-prohibited

service iptables save

5、mysql主从配置

理论:主(A)的数据库数据有变化记录一个bin_log,再推给从(B),B数据库的bin_log会记录去更改自己数据库(数据同步)

(1)打开主的mysql配置(my.cnf)添加或打开注释如下配置:

[nysqld]

log-bin=mysql-bin            #打开这两个注释,log-bin的值可以自定义

server  id      = 1

binlog-do-db=db1     #只针对指定数据库做主从

#binlog-ignore-db=mysql      #打开注释是不去同步指定的库(黑名单)

#log-slave-updates = true        #是否继续传递下去为“是”,这个是用于主主,若是多个主的server id

(2)从的my.cnf将原来的配置改为以下:   #(用于多实例)

[mysqld]

port            = 3307

socket          =/tmp/mysql_slave.sock

datadir         =/data/mysql_slave

server id  =1 改成server id   =2

replicate-do-db=db1     #只针对指定数据库做主从

#replicate-ignore-db=mysql      #不去同步指定的库(黑名单)

(2)<1>cp /etc/init.d/mysqld  /etc/init.d/mysqldslave

<2>vim /etc/init.d/mysqldslave将原来的配置改为以下:

找到第一个basedir:

basedir=/usr/local/mysql_slave

datadir=/data/mysql_slave

conf=$basedir/my.cnf

PS:上面mysql的从是用于多实例

(2)从的mysql配置:

[mysqld]

server id =2     #server id的变更

replicate-do-db=db1     #只针对指定数据库做主从

#replicate-ignore-db=mysql      #不去同步指定的库(黑名单)

6、测试能否进入数据库(前提在/etc/profile.d/path.sh里加入PATH=$PATH:/usr/local/mysql/bin后source /etc/profile.d/path.sh; 通过sock登录:mysql -S sock所在位置

(想添加多一个mysql,按照slave的做法即可)

7、做主从前的最后一个主要配置(主从数据库库信息一致):

mysqldump  -S  /tmp/mysql.sock  mysql > 123.sql   #备份mysql数据库

mysql -S /tmp/myslq.sock db1 < 123.sql        #将mysql数据库里的东西导入db1数据库

8、进入主的mysql:

mysql>grant replication slave on *.* to ‘repl’@’127.0.0.1’ identifiedby ‘123456’;       #针对本地做主从,只赋予replication权限

mysql>grant replication slave on *.* to ‘repl‘@‘从ip’ identifiedby ‘密码’;

mysql> flush privileges;

mysql> flush tables with read lock;       #对表的读锁死(read lock)的作用是让show master status的file和position不变

mysql> show master status;           #查看master数据信息

+------------------+----------+--------------+------------------+

| File                            | Position| Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000009  |         106 |                db1      |                                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

9、在从的将主传过来的123.sql导入本地数据库

主的操作:scp 123.sql  从ip:123.sql

[[email protected] ~]# mysql -S /tmp/mysql_slave.sock -e "createdatabase db1"    #在从上新建db1数据库

[[email protected] ~]# mysql -S /tmp/mysql_slave.sock db1 < 123.sql     #导入备份的sql去从的db1,实现主从相同

mysql> slave stop;                    #先停掉从的

Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql>change master tomaster_host=‘主ip‘,master_port=3306,master_user=‘repl‘,master_password=‘‘,master_log_file=‘mysql-bin.000009‘,master_log_pos=106;          #这一步很重要,核心命令

mysql>flush privileges;

mysql>slave start;

mysql> show slave status\G;      #查看从库的配置,是否成功看Slave_IO_Running和Slave_SQL_Running是否为 Yes

至此,没有出现任何错误,mysql主从已基本完成

以下是mysql主从过程中出现的一些错误或解决方法:

mysql> show slave status\G;

*************************** 1. row***************************

Slave_IO_State: Connecting tomaster

Master_Host: 192.168.0.140

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000048

Read_Master_Log_Pos: 106

Relay_Log_File:user14-relay-bin.000003

Relay_Log_Pos: 4

Relay_Master_Log_File: mysql-bin.000048

Slave_IO_Running: No

Slave_SQL_Running: Yes

Replicate_Do_DB: test

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 106

Relay_Log_Space: 106

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: NULL

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 2013

Last_IO_Error: error connectingto master ‘[email protected]:3306‘ - retry-time: 60  retries: 86400

Last_SQL_Errno: 0

Last_SQL_Error:

1 row in set (0.00 sec)

ERROR:

No query specified

查看mysql的slave错误日志

[[email protected] ~]# tail -f/data/mysql/user14.err

160227 21:58:18 [Note] Slave I/O threadexiting, read up to log ‘mysql-bin.000048‘, position 106

160227 21:58:18 [Note] Error reading relaylog event: slave SQL thread was killed

160227 22:01:11 [Note] ‘CHANGE MASTER TOexecuted‘. Previous state master_host=‘192.168.0.140‘, master_port=‘3306‘,master_log_file=‘mysql-bin.000048‘, master_log_pos=‘106‘. New statemaster_host=‘192.168.0.140‘, master_port=‘3306‘,master_log_file=‘mysql-bin.000048‘, master_log_pos=‘106‘.

160227 22:01:33 [Note] Slave SQL threadinitialized, starting replication in log ‘mysql-bin.000048‘ at position 106,relay log ‘./user14-relay-bin.000001‘ position: 4

160227 22:01:33 [ERROR] Slave I/O: errorconnecting to master ‘[email protected]:3306‘ - retry-time: 60  retries: 86400, Error_code: 2013

160227 22:01:41 [Note] Slave I/O threadkilled while connecting to master

160227 22:01:41 [Note] Slave I/O threadexiting, read up to log ‘mysql-bin.000048‘, position 106

160227 22:01:41 [Note] Error reading relaylog event: slave SQL thread was killed

160227 22:01:47 [Note] Slave SQL threadinitialized, starting replication in log ‘mysql-bin.000048‘ at position 106,relay log ‘./user14-relay-bin.000001‘ position: 4

160227 22:01:47 [ERROR] Slave I/O: errorconnecting to master ‘[email protected]:3306‘ - retry-time: 60  retries: 86400, Error_code: 2013

Last_IO_Errno: 1593

Last_IO_Error: Fatal error: Theslave I/O thread stops because master and slave have equal MySQL server ids;these ids must be different for replication to work (or the--replicate-same-server-id option must be used on slave but this does notalways make sense; please check the manual before using it).

配置完Error_code: 2013从没重启mysql服务,Last_IO_Errno: 1593是与主的id号冲突

[[email protected] ~]# mysqldump -S/tmp/mysql.sock -p123456  mysql >aaa.sql

-- Warning: Skipping the data of tablemysql.event. Specify the --events option explicitly.

mysqldump-uroot -pxxxxx --events --ignore-table=mysql.events  需要备份库 > 自定义备份库名字

ERROR 1201 (HY000): Could not initializemaster info structure; more error messages can be found in the MySQL error log

reset slave后再重新授权

时间: 2024-11-08 22:04:50

lvs(DR)+keepalived+mysqld主从的相关文章

虚拟机 搭建LVS + DR + keepalived 高可用负载均衡

一:环境说明:   LVS-DR-Master:    10.3.0.82   LVS-DR-Backup:    10.3.0.70   VIP:                10.3.0.60   RealServer1:    10.3.0.69   RealServer1:    10.3.0.83   Centos:5.7    二:安装LVS和Keepalvied软件包   1. 下载相关软件包    mkdir /usr/local/src/lvs    cd /usr/loca

lvs/dr+keepalived搭建成功后,开启防火墙,虚拟IP不能访问,但是真实IP却可以访问

lvs/dr+keepalived搭建成功后,开启防火墙,虚拟IP不能访问,但是真实IP却可以访问,如果关闭防火墙虚拟IP就可以访问网站了,这个问题肯定是防火墙在作怪. 经过这两的不懈奋斗和大家的帮助,我的lvs在开启了iptables后,终于实现了负载均衡了,添加的几条命令分别是1.在lvs上执行iptables -t nat -A PREROUTING -p tcp -d 192.168.1.200 --dport 80 -j REDIRECT iptables -I INPUT -i et

LVS(DR)+Keepalived 服务器集群

LVS(DR)+Keepalived 服务器集群 LVS轮询服务器 可以保障当一个网站服务器宕掉后,仍能正常访问 Keepalived高可用服务器 可以保障当LVS宕掉后,仍能正常工作 搭建服务器集群 192.168.4.50 LVS+Keepalived VIP 192.168.4.253 192.168.4.53 192.168.4.51 网站服务器 192.168.4.52 [50:53] #yum -y install keepalived ipvsadm vim /etc/keepal

LVS/DR + keepalived配置

1.LVS/DR 搭建的不足及解决方法 前面的lvs虽然已经配置成功也实现了负载均衡,但是我们测试的时候发现,当某台real server把httpd进程停掉,那么director照样会把请求转发过去,这样就造成了某些请求不正常.所以需要有一种机制用来检测real server的状态,这就是keepalived.它的作用除了可以检测rs状态外,还可以检测备用director的状态,也就是说keepalived可以实现ha集群的功能,当然了也需要一台备用director. 2.搭建环境前的工作 h

RHEL 5.4下部署LVS(DR)+keepalived实现高性能高可用负载均衡

原文地址:http://www.cnblogs.com/mchina/archive/2012/05/23/2514728.html 一.简介 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. 目前有三种IP负载均衡技术(VS/NAT.VS/TUN和VS/DR):十种调度算法(rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq). K

LVS(DR)+keepalived+nfs+raid+LVM

LVS理论篇 1.Client 向目标VIP 发出请求,Director(负载均衡器)接收.此时IP 包头及数据帧信息为:       2.Director 根据负载均衡算法选择RealServer_1,不修改也不封装IP 报文,而是将数据帧的MAC 地址改为RealServer_1 的MAC 地址,然后在局域网上发送.IP 包头及数据帧头信息如下:   3.RealServer_1 收到这个帧,解封装后发现目标IP 与本机匹配(RealServer 事先绑定了VIP,必须的!)于是处理这个报文

基于云端虚拟机的LVS/DR+Keepalived+nginx的高可用集群架构配置

最近,公司要我部署一个集群架构,选来选取还是选择了大家都很熟悉的基于DR的LVS+Keepalived做负载分发,然后使用轻量级的nginx做中间代理层,这里呢先暂时实现一个简单web应用功能,对于代理功能和后续的web层部署.数据层安排将择机更新! 首先看一下集群框架:   下面开始我们的集群搭建行程: 一.IP规划: 做一件事情需要想想怎么去做既简单又有条理,我们做项目也应该如此.所以这里一定先做一个简单的规划,对于你后续测试的时候出现不能分发的情况有所解决. 负载均衡层          

Linux下部署LVS(DR)+keepalived+Nginx负载均衡

架构部署 LVS/keepalived(master):192.168.21.3 LVS/keepalived(Slave):192.168.21.6 Nginx1:192.168.21.4 Nginx2:192.168.21.5 VIP:192.168.21.10 1.安装ipvsadm.keepalived(Master/Slave) yum -y install keepalived ipvsadm 2.修改keepalived.conf文件 LVS_master cd /etc/keep

LVS DR+keepalived实现HA+LB

首先需要4台服务器 2台dir分别是  dirmaster  dirslave 2台RS 分别运行nginx测试服务 dir 192.168.186.129 192.168.186.135 RS 192.168.186.130 192.168.186.132 首先搭建LVS-DR服务 a.dir上面安装ipvsadm yum install ipvsadm b.更改ipvsadm配置文件 vim /etc/usr/local/sbin/lvs_dr.sh #! /bin/bashecho 1 >