keepalived + haproxy + mysql 构建高可用数据库

keepalived + haproxy + mysql 构建高可用

  • keepalived 的高可用是主备,有一台作为备用
  • keepalived + haproxy 搭建的高可用是可以两台都会调度的高可用

拓扑图:

keepalived + haproxy + mysql 构建高可用数据库

keepalived:负责抢占虚拟ip,使用vrrp协议
haproxy:负责做访问调度,减轻单点压力,单独监听一个端口,这里用23306

1、安装mysql

分别在两台机器上面搭建mysql,并做主从配置,这里不做介绍

2、搭建haproxy

  1. download 源码包,下载地址:http://www.haproxy.org/#down
  2. 在81.128和81.129解压缩安装
tar xf haproxy-1.8.4.tar.gz
cd haproxy-1.8.4
yum install -y gcc
make TARGET=linux310 ARCH=x86_64 # uname -a查看主机信息填写
make install SBINDIR=/usr/sbin/ MANDIR=/usr/share/man/ DOCDIR=/usr/share/doc/

3、提供启动脚本

#!/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/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

4、提供配置文件

mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy
vim /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
    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
    mode http
    bind :6677 
    stats enable
    stats hide-version              
    stats uri     /haproxyadmin?stats
    stats realm   Haproxy Statistics
    stats auth    admin:admin
    stats admin if TRUE 

frontend  main 
    bind  *:23306
    default_backend             mysql

backend mysql
    balance     leastconn
    server m1 192.168.81.128:3306 check port 3306 maxconn 300
    server m2 192.168.81.129:3306 check port 3306 maxconn 300

5、修改日志系统

###Provides UDP syslog reception //去掉下面两行注释,开启UDP监听

$ModLoad imudp
$UDPServerRun 514

local2.* /var/log/haproxy.log //添加此行
service rsyslog restart

6、启动测试haproxy

service haproxy start 
chkconfig --add haproxy
chkconfig haproxy on
netstat -tnlp
mysql -P23306 -uroot -p123456 -h192.168.81.129 # 查看

server_id,判断是否成功

7、搭建keepalived

1、download 源码包,下载地址:http://www.keepalived.org/download.html

2、在81.128和81.129解压缩安装

tar xf keepalived-1.2.7.tar.gz 
cd keepalived-1.2.7
./configure --prefix=/usr/local/keepalived --sbindir=/usr/sbin/ --sysconfdir=/etc/ --mandir=/usr/local/share/man/
make && make install
chkconfig --add keepalived
chkconfig keepalived on

3、提供配置文件

vim /etc/keepalived/keepalived.conf # 两个机器配置文件不同

! Configuration File for keepalived

global_defs {           
notification_email {          # 忽略
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_haproxy {
    script "/etc/keepalived/chk.sh"     # 检查haproxy的脚本
    interval 2                          # 每两秒检查一次
}

vrrp_instance VI_1 {
    state BACKUP                        # 定义为BACKUP节点
    nopreempt                           # 开启不抢占,另一个不写
    interface ens33
    virtual_router_id 51
    priority 100                        # 开启了不抢占,所以此处优先级必须高于另一台,另一个写99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcd
    }
    virtual_ipaddress {
        192.168.81.150                  # 配置VIP
    }
    track_script {
        chk_haproxy                     # 调用检查脚本
    }

    notify_backup "/etc/init.d/haproxy restart"
    notify_fault "/etc/init.d/haproxy stop"
} 

4、创建check文件

vim /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 
service keepalived start

5、测试

ip addr # 查看是否绑定了虚ip
tcpdump -nn -i ens33 vrrp # 抓包查看
http://192.168.81.128:6677/haproxyadmin?stats # 通过haproxy查看状态

原创文章,作者:kinpo, k,如若转载,请注明出处:https://www.yidc.net/archives/5222