数据库运维,无忧IT学习

数据库运维经验分享-IT学习

  • 博客访问: 43611
  • 博文数量: 13
  • 用 户 组: 普通用户
  • 注册时间: 1970-01-01 08:00
个人简介

分享系统运维相关

ITPUB论坛APP

ITPUB论坛APP



APP发帖 享双倍积分

文章分类

全部博文(13)

文章存档

2014年(13)

我的朋友
微信关注

IT168企业级官微



微信号:IT168qiye



系统架构师大会



微信号:SACC2013

订阅
热词专题

分类: MySQL

HAProxy + Pacemaker实现MySQL的高可用负载均衡

  • 当前环境说明

    A: 192.168.1.191 (MySQL Slave)
    B: 192.168.1.192 (MySQL Slave)
    C: 192.168.1.216 (Pacemaker + CoroSync + HAProxy)
    D: 192.168.1.217 (Pacemaker + CoroSync + HAProxy)
    虚拟VIP: 192.168.1.210.
    这里配置的HAProxy负载均衡器为双机Active/Passive模式.

    haproxy_pacemaker
  • 安装与配置HAProxy

    调整内核参数,允许绑定VIP:

    # vi /etc/sysctl.conf
    net.ipv4.ip_nonlocal_bind=1
    
    # sysctl -p
    

    添加用户haproxy:

    # groupadd haproxy
    # useradd haproxy -g haproxy
    

    在负载均衡器后端的MySQL上分别添加健康检测MySQL用户:

    mysql> create user 'haproxy'@'192.168.1.%';
    mysql> flush privileges;
    

    安装HAProxy:

    # wget -c http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.23.tar.gz
    # tar zxvf haproxy-1.4.23.tar.gz
    # cd haproxy-1.4.23
    # make TARGET=linux26 #如果是32位机器,则make TARGET=linux26 ARCH=i386
    # make install
    # mkdir -p /etc/haproxy
    # cp examples/haproxy.cfg /etc/haproxy/haproxy.cfg
    

    配置HAProxy:

    # cat /etc/haproxy/haproxy.cfg
    global
        maxconn 40000
        user haproxy
        group haproxy
        stats socket /tmp/haproxy  user haproxy group haproxy mode 600 level admin
        node lb1
        nbproc 1
        daemon
        spread-checks 3
        quiet
    
    defaults
        log global
        mode tcp
        option tcplog
        option dontlognull
        option tcp-smart-accept
        option tcp-smart-connect
        #option dontlog-normal
        retries 3
        option redispatch
        timeout connect 500
        timeout client  2000    
        timeout server  2000  
        maxconn 40000
        option redispatch
    
    listen slave_clusters *:3306
        mode tcp
        balance roundrobin
        #balance leastconn
        option tcpka       # enable the keepalive
        option mysql-check user haproxy
        
        server MySQL191 192.168.1.191:3306 check weight 1 maxconn 2000
        server MySQL192 192.168.1.192:3306 check weight 1 maxconn 2000
    
    
    # HAProxy管理用户(admin/stats)
    userlist STATSUSERS
        group admin users admin
        user admin insecure-password 1234567
        user stats insecure-password 123456
    
    # HAProxy的管理界面
    listen stats *:8081
        mode http
        stats enable
        stats refresh 60s
        stats uri /
        stats hide-version
        acl AuthOkay_ReadOnly http_auth(STATSUSERS)
        acl AuthOkay_Admin http_auth_group(STATSUSERS) admin
        stats http-request auth realm Haproxy_Admin_Page unless AuthOkay_ReadOnly
        stats admin if AuthOkay_Admin
    

    HAProxy的启动管理脚本:

    # cat /etc/init.d/haproxy 
    #!/bin/bash
    #
    # chkconfig: 2345 85 15
    # description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \
    #              for high availability environments.
    # processname: haproxy
    # config: /etc/haproxy.cfg
    # pidfile: /var/run/haproxy.pid
    
    # Source function library.
    if [ -f /etc/init.d/functions ]; then
      . /etc/init.d/functions
    elif [ -f /etc/rc.d/init.d/functions ] ; then
      . /etc/rc.d/init.d/functions
    else
      exit 0
    fi
    
    CONF_FILE="/etc/haproxy/haproxy.cfg"
    HAPROXY_BINARY="/usr/local/sbin/haproxy"
    PID_FILE="/var/run/haproxy.pid"
    
    # Source networking configuration.
    . /etc/sysconfig/network
    
    # Check that networking is up.
    [ ${NETWORKING} = "no" ] && exit 0
    
    [ -f ${CONF_FILE} ] || exit 1
    
    RETVAL=0
    
    start() {
      $HAPROXY_BINARY -c -q -f $CONF_FILE
      if [ $? -ne 0 ]; then
        echo "Errors found in configuration file."
        return 1
      fi
    
      echo -n "Starting HAproxy: "
      daemon $HAPROXY_BINARY -D -f $CONF_FILE -p $PID_FILE
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
      return $RETVAL
    }
    
    stop() {
      echo -n "Shutting down HAproxy: "
      killproc haproxy -USR1
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy
      [ $RETVAL -eq 0 ] && rm -f $PID_FILE
      return $RETVAL
    }
    
    restart() {
      $HAPROXY_BINARY -c -q -f $CONF_FILE
      if [ $? -ne 0 ]; then
        echo "Errors found in configuration file, check it with 'haproxy check'."
        return 1
      fi
      stop
      start
    }
    
    check() {
      $HAPROXY_BINARY -c -q -V -f $CONF_FILE
    }
    
    rhstatus() {
      pid=$(pidof haproxy)
      if [ -z "$pid" ]; then
        echo "HAProxy is stopped." 
        exit 3
      fi
      status haproxy
    }
    
    condrestart() {
      [ -e /var/lock/subsys/haproxy ] && restart || :
    }
    
    # See how we were called.
    case "$1" in
      start)
        start
        ;;
      stop)
        stop
        ;;
      restart)
        restart
        ;;
      reload)
        restart
        ;;
      condrestart)
        condrestart
        ;;
      status)
        rhstatus
        ;;
      check)
        check
        ;;
      *)
        echo $"Usage: haproxy {start|stop|restart|reload|condrestart|status|check}"
        RETVAL=1
    esac
    
    exit $RETVAL
    

    检查HAProxy的配置是否正确:

    # /etc/init.d/haproxy check
    Configuration file is valid
    

    注:在这里HAProxy不需要单独启动.我们在后面让Pacemaker来管理HAProxy的启动.

  • Pacemaker + CoroSync安装和配置

    Pacemaker + CoroSync安装和配置过程参考《CentOS 6 安装和配置Pacemaker》
    安装配置好之后:
    两台均衡机器分别启动CoroSync:

    # /etc/init.d/corosync start
    Starting Corosync Cluster Engine (corosync):               [  OK  ]
    

    查看两台机的CoroSync之间是否成功通信:

    [root@centos216 ~]# corosync-objctl | grep members | grep ip
    runtime.totem.pg.mrp.srp.members.-670979904.ip=r(0) ip(192.168.1.216) 
    runtime.totem.pg.mrp.srp.members.-654202688.ip=r(0) ip(192.168.1.217) 
    

    然后再分别启动Pacemaker:

    # /etc/init.d/pacemaker start
    Starting Pacemaker Cluster Manager:                        [  OK  ]
    
  • 资源约束配置

    先定义一些资源属性约束(包括禁止STONITH错误,忽略Quorum,防止资源在恢复之后移动等):

    # crm configure
    crm(live)configure# property stonith-enabled=false
    crm(live)configure# property no-quorum-policy=ignore
    crm(live)configure# rsc_defaults resource-stickiness=100
    crm(live)configure# rsc_defaults failure-timeout=0
    crm(live)configure# rsc_defaults migration-threshold=10
    

    配置VIP资源:

    crm(live)configure# primitive failover-ip ocf:heartbeat:IPaddr2 params ip=192.168.1.210 cidr_netmask=24 op monitor interval=5s
    

    配置HAProxy资源:

    crm(live)configure# primitive haproxy lsb:haproxy op monitor interval="5s"
    

    定义运行的HAProxy和VIP必须在同一节点上:

    crm(live)configure# colocation haproxy-with-public-IPs INFINITY: haproxy failover-ip
    

    定义先接管VIP之后才启动HAProxy:

    crm(live)configure# order haproxy-after-IP mandatory: failover-ip haproxy
    

    验证并提交配置:

    crm(live)configure# verify
    crm(live)configure# commit
    crm(live)configure# quit
    
  • 资源状态查看

    查看资源状态:

    [root@centos216 ~]# crm_mon -1
    Last updated: Mon May 20 01:55:31 2013
    Last change: Mon May 20 01:27:39 2013 via cibadmin on centos216
    Stack: classic openais (with plugin)
    Current DC: centos216 - partition with quorum
    Version: 1.1.8-7.el6-394e906
    2 Nodes configured, 2 expected votes
    2 Resources configured.
    
    
    Online: [ centos216 centos217 ]
    
     failover-ip	(ocf::heartbeat:IPaddr2):	Started centos216
     haproxy	(lsb:haproxy):	Started centos216
    

    216机器上查看:

    [root@centos216 ~]# /etc/init.d/haproxy status
    haproxy (pid  1629) is running...
    
    [root@centos216 ~]# ip addr show eth0
    2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 08:00:27:67:ab:7e brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.216/24 brd 192.168.1.255 scope global eth0
        inet 192.168.1.210/24 brd 192.168.1.255 scope global secondary eth0
        inet6 fe80::a00:27ff:fe67:ab7e/64 scope link 
           valid_lft forever preferred_lft forever
    

    217机器上查看:

    [root@centos217 ~]# /etc/init.d/haproxy status
    HAProxy is stopped.
    [root@centos217 ~]# ip addr show eth0
    2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 08:00:27:67:ab:7e brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.217/24 brd 192.168.1.255 scope global eth0
        inet6 fe80::a00:27ff:fe67:ab7e/64 scope link tentative dadfailed 
           valid_lft forever preferred_lft forever
    

    从上面的这些信息,可以知道VIP绑定在216机器,同时只有216机器上的HAProxy已经启动.

  • Failover测试
    [root@centos216 ~]# crm node standby
    [root@centos216 ~]# crm_mon -1
    Last updated: Mon May 20 02:13:30 2013
    Last change: Mon May 20 02:13:17 2013 via crm_attribute on centos216
    Stack: classic openais (with plugin)
    Current DC: centos216 - partition with quorum
    Version: 1.1.8-7.el6-394e906
    2 Nodes configured, 2 expected votes
    2 Resources configured.
    
    
    Node centos216: standby
    Online: [ centos217 ]
    
     failover-ip	(ocf::heartbeat:IPaddr2):	Started centos217
     haproxy	(lsb:haproxy):	Started centos217
    [root@centos216 ~]# crm node online
    

    可以看到216机器standby之后,217机器成功接管VIP和启动HAProxy.

  • MySQL连接测试

    PHP脚本通过VIP连接负载均衡器后端的MySQL:

    [root@centos192 test]# cat test.php 
    connect_errno) {
        die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
    }
    
    echo 'Success... ' . $mysqli->host_info . "\n";
    
    $query = $mysqli->query("select * from mtest");
    
    if ($mysqli->errno) {
       echo "Query Error:" . $mysqli->error, PHP_EOL;
       exit;
    }
    
    $res = $query->fetch_assoc();
    
    var_dump($res);
    
    $mysqli->close();
    
    [root@centos192 test]# php test.php 
    Success... 192.168.1.210 via TCP/IP
    array(1) {
      ["a"]=>
      string(1) "0"
    }
  • 下一篇:基于DRBD+Pacemaker+Corosync实现高可用的MySQL
阅读(5027) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册