https://github.com/haproxy/haproxy


已成功安装HAproxy 1.7.9


yum -y install openssl-devel zlib-devel

tar -zxvfhaproxy-1.7.9.tar.gz

cdhaproxy-1.7.9

make TARGET=linux2628 PREFIX=/usr/local/haprpxy


Linux >= 2.6.28 with SSL and ZLIB support

make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1PREFIX=/usr/local/haprpxy;

make install PREFIX=/usr/local/haproxy


TARGET则根据当前操作系统内核版本指定

- linux22 for Linux 2.2

- linux24 for Linux 2.4 and above (default)

- linux24e for Linux 2.4 with support for a working epoll (> 0.21)

- linux26 for Linux 2.6 and above

- linux2628 for Linux 2.6.28, 3.x, and above (enables splice and tproxy)



可以把/usr/local/haproxy/sbin 添加到系统环境变量 /etc/profile



启动脚本:

cp path/examples/haproxy.init /etc/init.d/haproxy

#######

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

#######


chmod +x /etc/init.d/haproxy


mkdir -p/var/lib/haproxy; mkdir /usr/local/haproxy/etc

ln -s /usr/local/haproxy/etc /etc/haproxy


useradd haproxy -M -s /sbin/nologin


cp /usr/local/src/haproxy-1.7.9/haproxy-systemd-wrapper /usr/local/haproxy/sbin


Centos 7 haproxy启动脚本

/usr/lib/systemd/system/haproxy.service

########

[Unit]

Description=HAProxy Load Balancer

After=network.target


[Service]

ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q

ExecStart=/usr/local/haproxy/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid

ExecReload=/bin/kill -USR2 $MAINPID

KillMode=mixed

Restart=always


[Install]

WantedBy=multi-user.target

########

systemctl enablehaproxy

systemctl starthaproxy


配置文件 /etc/haproxy/haproxy.cfg:

mkdir -p/var/lib/haproxy

#######

global

# to have these messages end up in /var/log/haproxy.log you will

# need to:

#

# 1) configure syslog to accept network log events. This is done

# by adding the '-r' option to the SYSLOGD_OPTIONS in

# /etc/sysconfig/rsyslog

#

# 2) configure local2 events to go to the /var/log/haproxy.log

# file. A line like the following can be added to

# /etc/sysconfig/rsyslog

#

# local2.* /var/log/haproxy.log

#

log 127.0.0.1 local2


chroot /var/lib/haproxy

pidfile /var/run/haproxy.pid

maxconn 50000

user haproxy

group haproxy

daemon


# turn on stats unix socket

stats socket /var/lib/haproxy/stats


defaults

mode http

log global

option httplog

option tcplog

option dontlognull

option http-server-close

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 50000


listen UserAPI

bind 0.0.0.0:6004

mode http

balance roundrobin

option forwardfor except 127.0.0.0/8

#balance source

server serv1.184 192.168.1.184:6004 check inter 2000 rise 2 fall 3maxconn 20480

server serv1.185 192.168.1.185:6004 check inter 2000 rise 2 fall 3maxconn 20480



listen admin_status #Frontend和Backend的组合体,监控组的名称,按需自定义名称

bind 0.0.0.0:17818 #监听端口

mode http #http的7层模式

log 127.0.0.1 local2 err #错误日志记录

stats refresh 5s #每隔5秒自动刷新监控页面

stats uri /admin?admin #监控页面的url

stats realm HAProxy #监控页面的提示信息

stats auth tdman:adminpwd #监控页面的用户和密码admin,可以设置多个用户名

# stats hide-version #隐藏统计页面上的HAproxy版本信息

stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本)


#######


开启haproxy日志

touch/var/log/haproxy.log; chown haproxy:haproxy/var/log/haproxy.log

修改文件 /etc/sysconfig/rsyslog

SYSLOGD_OPTIONS="-r -m 0 -c 2"



编辑 /etc/rsyslog.conf 开启下面两行

$ModLoadimudp

$UDPServerRun514

在下面内容

#Savebootmessagesalsotoboot.log

local7.*/var/log/boot.log

之后添加:

# Save haproxy log

local2.* /var/log/haproxy.log


改完重启 rsyslog服务

如果开了防火墙,请开启相应的服务端口