haproxy主从集群搭建

2018/07/08

参考文档:

http://www.cnblogs.com/chenmh/p/5194636.html

http://www.cnblogs.com/dkblog/archive/2011/07/06/2098949.html

https://blog.liuts.com/page/1/1/

http://blog.csdn.net/lmocm/article/details/53175578

带视频教程:
http://www.jianshu.com/p/f4b02609a500

必要组件下载:

下载 nginx:
wget http://nginx.org/download/nginx-1.13.6.tar.gz

下载 mycat:
wget http://dl.mycat.io/1.6-RELEASE/Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz

下载 zookeeper:
wget http://dl.mycat.io/zookeeper-3.4.6.tar.gz

下载 mycat-eye:
wget http://dl.mycat.io/mycat-web-1.0/Mycat-web-1.0-SNAPSHOT-20170102153329-linux.tar.gz

下载 haproxy:
wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.9.tar.gz

下载:keepalived:
wget http://www.keepalived.org/software/keepalived-1.3.8.tar.gz

下载:mysql
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

CentOS7 添加网络配置信息:

ip addr

查看当前活动网卡
ifconfig

http://weizhifeng.net/HA-with-HAProxy-and-KeepAlived.html haproxy与keepalived安装与配置

官网:www.haproxy.org/

下载 haproxy:
wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.9.tar.gz

yum install -y gcc

useradd haproxy
tar -zxvf haproxy-1.7.9.tar.gz
cd haproxy-1.7.9/
make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=x86_64
make install PREFIX=/usr/local/haproxy
cd /usr/local/haproxy
chown -R haproxy.haproxy *

mkdir /usr/local/haproxy/conf -p and mkdir /usr/local/haproxy/var/run -p and mkdir /usr/local/haproxy/var/chroot -p and mkdir /usr/local/haproxy/logs -p

vi /usr/local/haproxy/conf/haproxy.cfg
-----------------------------------------------------------------------------
global
    log 127.0.0.1   local0 info ##记日志的功能
    log 127.0.0.1   local1 notice
    maxconn 94096
    user  haproxy
    group haproxy
    daemon #后台运行
    nbproc  1
    pidfile   /usr/local/haproxy/var/run/haproxy1.pid
defaults
    log    global
    option    tcplog
    retries    3
    option redispatch
    maxconn    2000
    timeout   connect  50000ms
    timeout   client   50000ms
    timeout   server   50000ms

#加入haproxy的监控页面
listen  admin_status
    bind 0.0.0.0:1080
    mode http
    log 127.0.0.1 local0 err
    stats refresh 3s
    stats uri /admin?stats
    stats auth admin:abcd-1234
    stats auth haproxy:abcd-1234

#数据库集群节点
listen  db-cluster
    bind 10.120.121.45:3399  #客户端监听端口
    mode tcp
    balance leastconn
    server sandbox-retail 10.120.121.41:3306 check
    server sandbox-huishang 10.120.121.35:3306 check
-----------------------------------------------------------------------------

测试配置文件的有效性:
./sbin/haproxy -f conf/haproxy.cfg -c
启动haproxy:
./sbin/haproxy -f conf/haproxy.cfg
查看运行状态:
ps aux|grep haproxy

访问监控页面:http://10.120.121.45:1080/admin?stats

生产环境 http://10.96.83.5:1080/admin?stats

安装与配置keepalived

参考:
http://blog.csdn.net/huoyuanshen/article/details/68483188
http://www.linuxidc.com/Linux/2017-03/141593p2.htm
http://www.linuxidc.com/Linux/2016-12/138917.htm

- keepalived和haproxy必须装在同一台机器上, keepalived负责为该服务器抢占vip(虚拟ip)
下载:keepalived:
wget http://www.keepalived.org/software/keepalived-1.3.8.tar.gz


查看keepalived是否已经安装:
whereis keepalived

在线安装:
yum -y install keepalived ipvsadm

源码安装:
tar -zxvf keepalived-1.3.8.tar.gz
cd keepalived-1.3.8/

安装依赖的组件:
yum install  -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel ncurses-devel bison cmake libnl* libpopt* popt-static libnfnet* libnfnetlink-devel  psmisc  ipvsadm

./configure --prefix=/usr/local/keepalived
make && make install

keepalived配置:
参考:http://cpper.info/2016/09/15/keepalived-for-master-backup.html
1)注册为系统服务
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/


touch /etc/init.d/keepalived
chmod +x /etc/init.d/keepalived
vi /etc/init.d/keepalived
添加以下内容:
-----------------------------------------------------------------------
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived

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

# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived

RETVAL=0

prog="keepalived"

start() {
    echo -n $"Starting $prog: "
    daemon keepalived ${KEEPALIVED_OPTIONS}
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

stop() {
    echo -n $"Stopping $prog: "
    killproc keepalived
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}

reload() {
    echo -n $"Reloading $prog: "
    killproc keepalived -1
    RETVAL=$?
    echo
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$prog ]; then
            stop
            start
        fi
        ;;
    status)
        status keepalived
        RETVAL=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
        RETVAL=1
esac

exit $RETVAL
----------------------------------------------------------------------

启动:
service keepalived start/restart/stop/reload

查看keepalived进程:
ps -ef|grep keepalived |grep -v grep

查看须有IP持有者
ip addr sh eth0

查看日志:
tail -f ./etc/keepalived/keepalived.conf


MASTER节点配置
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

BACKUP 节点配置
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------


扫描关注:Andy风雨无阻

(转载本站文章请注明作者和出处 风雨无阻

Show Disqus Comments

Post Directory