MySQL 5.5 keepalived + haproxy + MHA 自动容灾

标签: mysql keepalived haproxy | 发表时间:2013-02-19 21:36 | 作者:ylqmf
出处:http://blog.csdn.net

vip 192.168.161.80

mysql master 192.168.161.81

mysql backup 192.168.161.82(haproxy master)

mysql slave     192.168.161.83 (haproxy backup)

keepalived安装:

yum -y install ipvsadm openssl-devel libnl* popt-static
uname -r
ln -s /usr/src/kernels/2.6.32-279.el6.x86_64 /usr/src/linux
wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz
tar zxf keepalived-1.2.6.tar.gz 
cd keepalived-1.2.6
./configure
make 
make install 
#在82上配置
vim /usr/local/etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id  NodeA
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 82
    nopreempt
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.161.80/24
    }
}

在83上配置
  
vim /usr/local/etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id  NodeA
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 83
    nopreempt
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.161.80/24
    }
}


/usr/local/sbin/keepalived -f /usr/local/etc/keepalived/keepalived.conf

haproxy安装:

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
tar zxvf haproxy-1.4.22.tar.gz
cd haproxy-1.4.22
uname -a //查看linux内核版本
make TARGET=linux26 PREFIX=/usr/local/haproxy-1.4.22
make install PREFIX=/usr/local/haproxy-1.4.22
#在82和83上创建conf文件
vim /usr/local/haproxy-1.4.22/conf/haproxy.conf
global
        maxconn 10240
        chroot /usr/local/haproxy-1.4.22
        uid 99
        gid 99
        daemon
        quiet
        nbproc 3
        pidfile /tmp/haproxy.pid

defaults
        mode http
        retries 2
        option redispatch
        option abortonclose
        maxconn 4096
        timeout check 2000
        log 127.0.0.1 local0 err #[err warning info debug]

listen test1
        bind 0.0.0.0:5306
        mode tcp
        maxconn 4086
        #log 127.0.0.1 local0 debug
        server s1 192.168.161.81:3306

#创建替换脚本 这里没有使用平滑重启
vim /opt/shell/haproxy_reload.sh
#!/bin/sh
sed 's/'$1'/'$2'/g' -i /usr/local/haproxy-1.4.22/conf/haproxy.conf
ps aux | grep 'haproxy -f /usr/local/haproxy-1.4.22/conf/haproxy.conf' | grep -v grep | awk '{print $2}' | xargs kill
/usr/local/haproxy-1.4.22/sbin/haproxy -f /usr/local/haproxy-1.4.22/conf/haproxy.conf

#启动haproxy
/usr/local/haproxy-1.4.22/sbin/haproxy  -f /usr/local/haproxy-1.4.22/conf/haproxy.conf 



在81和83上安装mha node,在82上安装mha manager

#mysql bin
cp /usr/local/mysql/bin/* /usr/local/bin/

#mysql node
yum install perl-CPAN -y
tar zxf mha4mysql-node-0.53.tar.gz
cd mha4mysql-node-0.53
perl Makefile.PL 
make && make install

#mha manager
yum install perl-Config-Tiny perl-Params-Validate perl-Log-Dispatch perl-Parallel-ForkManager -y
tar zxf mha4mysql-manager-0.53.tar.gz
cd mha4mysql-manager-0.53
perl Makefile.PL 
make && make install

#在三台服务器上分别执行public ssh
ssh-keygen -t rsa
   
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
 ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

#replication user
GRANT ALL PRIVILEGES ON *.* TO 'rep_user'@'192.168.%' IDENTIFIED BY '123456';

#slave set
set global read_only=1;
set global relay_log_purge=0;
01 00 * * * /usr/local/bin/purge_relay_logs -user=rep_user -password=123456 -disable_relay_log_purge >> /tmp/purge_relay_logs.log 2>&1

#check
masterha_check_ssh --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf 
masterha_check_repl --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf

#start
nohup masterha_manager --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /tmp/mha_manager.log 2>&1 &

#status
masterha_check_status --conf=/usr/local/mha4mysql-manager-0.53/conf/app1.cnf

#log
tail -1000 /var/log/masterha/app1/manager.log

#error
#[error][/usr/lib/perl5/site_perl/5.8.8/MHA/ManagerUtil.pm, ln178] Got ERROR: Use of uninitialized value in scalar chomp at /usr/lib/perl5/site_perl/5.8.8/MHA/ManagerConst.pm line 90.
vi /usr/local/share/perl5/MHA/ManagerConst.pm
89   my $msg  = $args{message};
90   $msg = "" unless($msg);  <=插入一行
91   chomp $msg;

   

mha 配置文件

    
vim/usr/local/mha4mysql-manager-0.53/conf/app1.cnf

[server default]
manager_workdir=/usr/local/mha4mysql-manager-0.53
manager_log=/usr/local/mha4mysql-manager-0.53/manager.log
master_ip_failover_script="/usr/local/mha4mysql-manager-0.53/scripts/master_ip_failover"
master_ip_online_change_script="/usr/local/mha4mysql-manager-0.53/scripts/master_ip_online_change"
ping_interval=1
user=xm_dba
password=123456
repl_user=rep_user
repl_password=123456
ssh_user=root

[server1]
hostname=192.168.161.81
port=3306
candidate_master=1
master_binlog_dir="/home/mysql/data3306"

[server2]
hostname=192.168.161.82
port=3306
candidate_master=1
master_binlog_dir="/home/mysql/data3306"

[server3]
hostname=192.168.161.83
port=3306
no_master=1
master_binlog_dir="/home/mysql/data3306"


    

     
vim /usr/local/mha4mysql-manager-0.53/scripts/master_ip_failover

#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';

use Getopt::Long;

my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port
);

my $vip = '192.168.161.80';  # Virtual IP 
my $key = "80"; 
my $ssh_start_vip = "ifup eth0:$key ";
my $ssh_stop_vip = "ifdown eth0:$key ";
my $check_vip = "ip add";
my $m_haproxy_ip = "192.168.161.82";
my $s_haproxy_ip = "192.168.161.83";
my $haproxy_change_master = "";
GetOptions(
    'command=s'          => \$command,
    'ssh_user=s'         => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'orig_master_ip=s'   => \$orig_master_ip,
    'orig_master_port=i' => \$orig_master_port,
    'new_master_host=s'  => \$new_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
);
#my $haproxy_change_master = "/opt/shell/haproxy_reload.sh $orig_master_ip $new_master_ip ";

exit &main();

sub main {
    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; 

    if ( $command eq "stop" || $command eq "stopssh" ) {

        # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
        # If you manage master ip address at global catalog database,
        # invalidate orig_master_ip here.
        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {

        # all arguments are passed.
        # If you manage master ip address at global catalog database,
        # activate new_master_ip here.
        # You can also grant write access (create user, set read_only=0, etc) here.
        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n"; 
        `ssh $ssh_user\@$vip \" $check_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}

# A simple system call that enable the VIP on the new master 
sub start_vip() {
    $haproxy_change_master = "/opt/shell/haproxy_reload.sh $orig_master_ip $new_master_ip ";
    `ssh $ssh_user\@$m_haproxy_ip \" $haproxy_change_master \"`;
    `ssh $ssh_user\@$s_haproxy_ip \" $haproxy_change_master \"`;
    #`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
    #`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}

sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}


这个脚本是从切换vip的脚本改过来的,以后在进一步优化吧。


     

     

     


             
作者:ylqmf 发表于2013-2-19 21:36:50 原文链接
阅读:156 评论:1 查看评论

相关 [mysql keepalived haproxy] 推荐:

MySQL 5.5 keepalived + haproxy + MHA 自动容灾

- - CSDN博客数据库推荐文章
keepalived安装:. } } 在83上配置 vim /usr/local/etc/keepalived/keepalived.conf global_defs {. 在81和83上安装mha node,在82上安装mha manager. vi /usr/local/share/perl5/MHA/ManagerConst.pm 89 my $msg = $args{message}; 90 $msg = "" unless($msg); <=插入一行 91 chomp $msg;.

Haproxy+KeepAlived 负载均衡

- - CSDN博客系统运维推荐文章
软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现. LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. 还可以使用nginx来实现,不过nginx只工作在7层网络之上. 详细请参考 抚琴煮酒写的“ 软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比”这篇文章,简单很详细,很好.

Keepalived+Mysql 高可用方案

- - 数据库 - ITeye博客
Keepalived+Mysql 高可用方案. 安装: 略  Keepalived可从CentOS系统安装包中获得. 建立一个脚本用于在keepalived检测到mysql挂了的时候调用该脚本把keepalived杀死. 当然,在这个文件你也可以加入邮件通知等功能,这里就不列举了.. 配置keepalived.conf.

[转][转]MySQL高可用性之Keepalived+Mysql(双主热备)

- - heiyeluren的blog(黑夜路人的开源世界)
1、配置两台Mysql主主同步. #backup这台设置2 log-bin = mysql-bin binlog-ignore-db = mysql,information_schema. #忽略写入binlog日志的库 auto-increment-increment = 2. #字段变化增量值 auto-increment-offset = 1.

使用HAProxy对MySQL进行负载均衡和状态监控

- Kevin - Michael`s blog
转载请保留原文内容,并声明转载地址:http://www.toplee.com/blog/1284.html. 乐搜(lesoo.com)使用HAProxy已经很久了,但主要用在前端web请求的负载均衡和状态监控上,对于后端的Memcached以及MySQL却一直没有应用,最近对系统架构进行了小规模的整理,把MySQL也收编到了HAProxy下,经过一段时间的使用,体验还是不错的.

atlas+lvs+keepalived mysql多主复制负载均衡搭建

- - x-marker的博客
1.1 mysql数据节点:. 三个节点为xtradb cluster节点. 1.2 keepalived节点:. 虚拟ip为192.168.1.201 . haproxy节点(仅为了对比lvs的性能才安装的):db169(部署在xtradb cluster的一个节点上). 1.3 atlas节点:和xtradb cluster节点部署在一起,也为三个节点.

利用keepalived构建高可用MySQL-HA(转)

- - 数据库 - ITeye博客
关于MySQL-HA,目前有多种解决方案,比如heartbeat、drbd、mmm、共享存储,但是它们各有优缺点. heartbeat、drbd配置较为复杂,需要自己写脚本才能实现MySQL自动切换,对于不会脚本语言的人来说,这无疑是一种脑裂问题;对于mmm,生产环境中很少有人用,且mmm 管理端需要单独运行一台服务器上,要是想实现高可用,就得对mmm管理端做HA,这样无疑又增加了硬件开支;对于共享存储,个人觉得MySQL数据还是放在本地较为安全,存储设备毕竟存在单点隐患.

Keepalived安装

- - CSDN博客架构设计推荐文章
Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived可以用来防止服务器单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生,通过配合Nginx可以实现web前端服务的高可用. 安装Keeplived依赖. 安装 openssl. #通知email,根据实际情况配置 { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id node1.

配置LVS+Keepalived

- -
lvs说明:目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);. 八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh). 在调度器的实现技术中,IP负载均衡技术是效率最高的. 在分析VS/NAT的缺点和网络服务的非对称性的基础上,我们提出通过IP隧道实现虚拟服务器的方法VS/TUN (Virtual Server via IP Tunneling),和通过直接路由实现虚拟服务器的方法VS/DR(Virtual Server via Direct Routing),它们可以极大地提高系统的伸缩性.

haproxy负载均衡 xtracluster

- - x-marker的博客
上一篇为xtracluster的安装(http://xmarker.blog.163.com/blog/static/226484057201472610520306/),本篇将记录下使用haproxy在三个数据节点做负载均衡,haproxy可以安装在数据节点,也可以安装在别的节点,如果安装在数据节点,需要注意端口号不能和mysql的3306冲突.