安裝依賴套件

yum -y install wget openssl-devel
yum -y install gcc

下載haproxy安裝包
各版本載點

wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.9.tar.gz

解壓縮編譯haproxy

tar zxvf haproxy-1.7.9.tar.gz
make TARGET=linux26 ARCH=X86_64 PREFIX=/usr/local/haproxy USE_OPENSSL=1 ADDLIB=-lz
make install PREFIX=/usr/local/haproxy

建立設定檔路徑

mkdir /etc/haproxy/

vim /etc/haproxy/haproxy.cfg

global
        log 127.0.0.1 local5
        chroot /usr/local/haproxy
        pidfile /var/run/haproxy.pid
        maxconn 5000
        user haproxy
        group haproxy
        daemon
        stats socket /etc/haproxy/haproxy.sock mode 600 level admin
        stats timeout 2m
defaults
        mode tcp
        log global
        option httplog
        option dontlognull
        option log-health-checks
        retries 3
        timeout http-request 30s
        timeout queue 300s
        timeout connect 600s
        timeout client 10m
        timeout server 10m
        timeout http-keep-alive 30s
        timeout check 600s
        maxconn 4000
listen stats
        bind  192.168.1.123:9090
        mode    http
        log     127.0.0.1 local0 err
        stats   uri     /hastats
        stats auth    admin:admin
        stats refresh 10s
        stats hide-version
        stats admin if TRUE

frontend mysql
        bind *:3306
        mode tcp
        option tcplog
        default_backend mysqlservers

backend mysqlservers
        balance leastconn
        server DB01 192.168.1.124:3306 check port 3306 rise 1 fall 2 maxconn 2000 weight 100
        server DB02 192.168.1.125:3306 check port 3306 rise 1 fall 2 maxconn 2000 weight 100

建立啟動檔
vim /etc/init.d/haproxy

#!/bin/sh
#
# chkconfig: - 85 15
# description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \
#              for high availability environments.
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid

# Script Author: Simon Matter <[email protected]>
# Version: 2004060600

# 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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# This is our service name
BASENAME=`basename $0`
if [ -L $0 ]; then
  BASENAME=`find $0 -name $BASENAME -printf %l`
  BASENAME=`basename $BASENAME`
fi

BIN=/usr/sbin/$BASENAME

CFG=/etc/$BASENAME/$BASENAME.cfg
[ -f $CFG ] || exit 1

PIDFILE=/var/run/$BASENAME.pid
LOCKFILE=/var/lock/subsys/$BASENAME

RETVAL=0

start() {
  quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi

  echo -n "Starting $BASENAME: "
  daemon $BIN -D -f $CFG -p $PIDFILE
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch $LOCKFILE
  return $RETVAL
}

stop() {
  echo -n "Shutting down $BASENAME: "
  killproc $BASENAME -USR1
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
  [ $RETVAL -eq 0 ] && rm -f $PIDFILE
  return $RETVAL
}

restart() {
  quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi
  stop
  start
}

reload() {
  if ! [ -s $PIDFILE ]; then
    return 0
  fi

  quiet_check
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi
  $BIN -D -f $CFG -p $PIDFILE -sf $(cat $PIDFILE)
}

check() {
  $BIN -c -q -V -f $CFG
}

quiet_check() {
  $BIN -c -q -f $CFG
}

rhstatus() {
  status $BASENAME
}

condrestart() {
  [ -e $LOCKFILE ] && restart || :
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  reload)
    reload
    ;;
  condrestart)
    condrestart
    ;;
  status)
    rhstatus
    ;;
  check)
    check
    ;;
  *)
    echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status|check}"
    exit 1
esac

exit $?

給予啟動權限

chmod 755 /etc/init.d/haproxy

建立啟動檔連結

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy

版本檢查

haproxy -v

啟動服務

servcie haproxy start