1、下载源码地址:https://github.com/haproxy/haproxy:
wget https://github.com/haproxy/haproxy/archive/v2.0.0.zip
2、解压安装:
unzip v2.0.0.zip
cd haproxy-2.0.0
make clean
make -j $(nproc) TARGET=linux-glibc PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
#验证安装成功
haproxy -v
3、设置开机启动
cp examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
4、创建haproxy系统账号账号
useradd -r haproxy
5、创建配置文件:
#1、创建配置文件夹和添加配置文件
mkdir /usr/local/haproxy/etc
vim /usr/local/haproxy/etc/haproxy.cfg
#2、配置文件内容如下:
global
log 127.0.0.1 local3 info
chroot /usr/local/haproxy
user haproxy
group haproxy
daemon
maxconn 4000
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http_front
bind *:8088
stats uri /haproxy?stats
default_backend http_back
backend http_back
balance roundrobin
option httpchk GET /index.html
option forwardfor header X-Forwarded-For
server node1 192.168.124.122:8082 check inter 2000 rise 3 fall 3 weight 30
server node2 192.168.124.122:8083 check inter 2000 rise 3 fall 3 weight 30
#3、编辑系统日志配置
vim /etc/rsyslog.conf
#4、以下两行去掉注释
$ModLoad imudp
$UDPServerRun 514
#5、添加local3
local3.* /var/log/haproxy.log
#6、重启rsyslog
systemctl restart rsyslog
#7、编辑服务脚本 修改三行 以下是行号和对应改后的代码:
vim /etc/init.d/haproxy
26 [ "${NETWORKING}" = "no" ] && exit 0
35 BIN=/usr/local/sbin/$BASENAME
37 CFG=/usr/local/$BASENAME/etc/$BASENAME.cfg
#8、重新加载脚本:
systemctl daemon-reload
6、启动Haproxy
[root@bogon haproxy-2.0.0]# service haproxy start
Starting haproxy (via systemctl): [ 确定 ]
ps -ef|grep haproxy
haproxy 20517 1 0 06:57 ? 00:00:00 /usr/local/sbin/haproxy -D -f /usr/local/haproxy/etc/haproxy.cfg -p /var/run/haproxy.pid
启动成功 虽然提示后台服务不可用