haproxy2.6.2编译安装
操作系统: redhat7.9
安装依赖包
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
配置内核转发
[root@haproxy ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@haproxy ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@haproxy ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
lua环境安装
下载->解压->编译三步曲:
wget http://www.lua.org/ftp/lua-5.3.4.tar.gz
tar -xzf lua-5.3.4.tar.gz
cd lua-5.3.4
make linux test
替换原来的lua
|
|
更改主服务器名称(按需)
hostnamectl set-hostname ma
useradd -r -M -s /sbin/nologin haproxy
进入到源码目录
make clean
make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/lua-5.4.4/src/ LUA_LIB=/usr/local/lua-5.4.4/src/
make install PREFIX=/usr/local/haproxy
创建软链接
ln -sv /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
创建启动脚本
vi /usr/lib/systemd/system/haproxy.service
[Unit]Description=HAProxy Load BalancerAfter=syslog.target network.target
[Service]ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -qExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pidExecReload=/bin/kill -USR2
[Install]WantedBy=multi-user.target
重载服务
systemctl daemon-reload
新建配置文件目录
mkdir -p /etc/haproxy
创建配置文件
vim /etc/haproxy/haproxy.cfg
global maxconn 8192 log 127.0.0.1 local0 info #log 127.0.0.1 #log loghost local0 info uid 988 gid 982 daemon nbthread 8 #debug #quiet ulimit-n 65535 chroot /usr/local/haproxy stats socket /usr/local/haproxy/haproxy.sock
defaults log global mode http #mode tcp option httplog option httpclose option dontlognull option forwardfor option redispatch option forwardfor option redispatch option abortonclose retries 3 maxconn 20480 balance roundrobin #balance source #balance leastconn timeout connect 30s timeout client 30s timeout server 30s timeout check 30s
#--------------web配置----------------- listen admin_stats bind 0.0.0.0:8000 stats enable mode http log global stats uri /haproxy_stats stats realm Haproxy\ Statistics stats auth admin:admin #stats hide-version stats admin if TRUE stats refresh 30s
frontend web1 bind :8080 log global mode http use_backend webserver1
backend webserver1 mode http #option httpchk GET /gioi cookie SERVERID insert indirect nocacheserver 141-7007 172.18.189.16:7007 cookie s1 check inter 5000 #后端的主机 IP &权衡server 141-7009 172.18.189.16:7009 cookie s2 check inter 5000 #后端的主机 IP &权衡
日志文件配置
vim /etc/rsyslog.conf
#去掉以下两列注释:
$ModLoad imudp
UDPServerRun 514
在添加如下内容:
#Save haproxy messages also to haproxy.log
local0.* /var/log/haproxy.log
重起日志服务
systemctl restart rsyslog.service
启动haproxy服务
systemctl start haproxy.service

浙公网安备 33010602011771号