keepalive+haproxy +mysql 实现读负载均衡

Keepalive 安装

[root@plvskygis01 tmp]# tar zxf keepalived-2.0.6.tar.gz

[root@plvskygis01 tmp]# cd keepalived-2.0.6

[root@plvskygis01 keepalived-2.0.8]# pwd

/tmp/keepalived-2.0.6

[root@plvskygis01 keepalived-2.0.6]#  yum install openssl openssl-devel

[root@plvskygis01 keepalived-2.0.6]# yum install -y libnfnetlink-devel

[root@plvskygis01 keepalived-2.0.6]# yum install gcc libnl3-devel net-snmp-devel

[root@plvskygis01]#./configure --with-init=SYSV --prefix=/usr/local/keepalived

[root@plvskygis01]#make &make install

整理管理文件:

 [root@plvskygis01]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[root@plvskygis01]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

[root@plvskygis01]#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

[root@plvskygis01]#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d

[root@phapxykdgis01 haproxy]# chmod +x /etc/init.d/keepalived

[root@plvskygis01 ]# mkdir /etc/keepalived

[root@plvskygis01]#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

[root@plvskygis01 keepalived]# chkconfig  keepalived on

[root@plvskygis01 keepalived]# chmod +x /etc/init.d/keepalived

[root@plvskygis01 keepalived]# service keepalived start       

Starting keepalived:                                       [  OK  ]

 

keepalive配置文件

keepalived 配置

keepalive 主节点

[root@plvskygis01 keepalived]# vi  /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 172.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

   vrrp_skip_check_adv_addr

!   vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

vrrp_script check_haproxy {

  script "/etc/keepalived/check_haproxy.sh"

  interval 5

  weight 2

  fall   3

  rise   1

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 220

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

#为避免同一个局域网中有多个keepalived组中的多播相互响应,采用单播通信

      unicast_peer {

    172.17.103.218

    172.17.103.219

  }

    virtual_ipaddress {

        172.17.103.220

    }

track_script {

     check_haproxy

    }

  notify "/etc/keepalived/notify.sh"

}

 

备keepalive

[root@phapxykygis02 keepalived]# cat keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 172.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
!   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}


vrrp_script check_haproxy {
  script "/etc/keepalived/check_haproxy.sh"
  interval 5
  weight 2

}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 220
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
        unicast_peer {
    172.17.103.218
    172.17.103.219
  }
    virtual_ipaddress {
        172.17.103.220
    }


track_script {
     check_haproxy
    }
    notify_master "/etc/keepalived/notify.sh"  
}

keepalive 调用的配置文件

[root@plvskygis01 keepalived]# cat /etc/keepalived/check_haproxy.sh

#!/bin/bash

A=`ps -C haproxy --no-header | wc -l`

if [ $A -eq 0 ]

then service haproxy start

sleep 3

if [ `ps -C haproxy --no-header | wc -l ` -eq 0 ]

then service keepalived stop

fi

fi

 

[root@phapxykygis02 keepalived]# cat notify.sh

#!/bin/bash

# for ANY state transition.

# "notify" script is called AFTER the

# notify_* script(s) and is executed

# with 3 arguments provided by keepalived

# (ie don't include parameters in the notify line).

# arguments

# $1 = "GROUP"|"INSTANCE"

# $2 = name of group or instance

# $3 = target state of transition

#     ("MASTER"|"BACKUP"|"FAULT")

TYPE=$1

NAME=$2

STATE=$3

case $STATE in

        "MASTER") echo "I'm the MASTER! Whup whup." > /usr/local/keepalived/log/keepalive.log

                  exit 0

                  ;;

        "BACKUP") echo "Ok, i'm just a backup, great." > /usr/local/keepalived/log/keepalive.log

                  exit 0

                  ;;

        "FAULT")  echo "Fault, what ?" > /usr/local/keepalived/log/keepalive.log

                  exit 0

                  ;;

        *)        echo "Unknown state" > /usr/local/keepalived/log/keepalive.log

                  exit 1

                  ;;

Esac

 

Hapoxy 安装

 

[root@plvskygis01 haproxy-1.8.14]# uname -a

Linux plvskygis01 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:51:26 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux

 

安装

make TARGET=linux26 ARCH=x86_64 PREFIX=/usr/local/haproxy

其中TARGET表示OS的内核版本,ARCH表示OS的位数,PREFIX表示haprxoy的安装路径。

 

make install PREFIX=/usr/local/haproxy

 

[root@plvskygis01 haproxy]# ll

total 12

drwxr-xr-x 3 root root 4096 Oct 26 15:08 doc

drwxr-xr-x 2 root root 4096 Oct 26 15:08 sbin

drwxr-xr-x 3 root root 4096 Oct 26 15:08 share

 

创建用户

[root@plvskygis01 haproxy]# useradd haproxy

 

haproxy 参数文件配置

[root@phapxykygis01 ~]# cat /etc/haproxy/haproxy.cfg

global

    log         127.0.0.1 local2

    chroot        /usr/local/haproxy

    pidfile         /var/run/haproxy.pid

    maxconn      4000

    user           haproxy

    group         haproxy

    daemon

    stats socket /usr/local/haproxy/stats

defaults

    mode                    tcp

    log                     global

    log                    127.0.0.1 local2

   option                  dontlognull

    option                  redispatch

    option                  abortonclose

    retries                 3

    timeout http-request    10s

    timeout queue           1m

    timeout connect         10s

    timeout client          20m

    timeout server          20m

    timeout http-keep-alive 10s

    timeout check           10s

    maxconn                 600

 

listen stats

    mode http

    bind  *:8080

    stats enable

    stats hide-version

    stats uri /haproxy-stats

    stats realm Haproxy\ Statistics

    stats auth admin:admin

    stats admin if TRUE

    stats refresh 30s

    option forwardfor

 

#    default_backend             mysql  

listen mysql

    bind  172.17.103.220:11856

    mode tcp

    balance roundrobin

    #balance     leastconn             

     server ss2 172.17.103.217:3306 check port 3306 maxconn 500 rise 3 fall 3

     server ss3 172.17.103.218:3306 check port 3306 maxconn 500  rise 3 fall 3

     server ss4 172.17.103.219:3306 check port 3306 maxconn 500  rise 3 fall 3

 

option forwardfor              
#如果后端服务器需要获得客户端真实ip需要配置的参数,必须要放在listen模块下 mode必须是mode http

检测文件内容是否正常

[root@plvskygis01 haproxy]# /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg  -c     

Configuration file is valid

 

启动服务

/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg

 

设置启动服务

[root@plvskygis01 examples]# cp haproxy.init  /etc/rc.d/init.d/

[root@plvskygis01 examples]#mv /etc/init.d/haproxy.init /etc/init.d/haproxy

[root@plvskygis02 examples]# chmod +x /etc/rc.d/init.d/haproxy

[root@plvskygis01 ]#vi  /etc/init.d/haproxy

改成

BIN=/usr/local/haproxy/sbin/$BASENAME

或者cp /usr/local/haproxy/sbin/haproxy  /usr/sbin/haproxy

 

[root@phapxykygis01 ~]# cat /etc/sysctl.conf

net.ipv4.ip_forward = 1
开启IP转发功能

net.ipv4.ip_nonlocal_bind = 1
开启允许绑定非本机的IP

 

你可能感兴趣的:(mysql)