系统 | IP | 服务 |
---|---|---|
Rocky-9.3 | 192.168.117.10 | haproxy |
Rocky-9.3 | 192.168.117.11 | nginx |
Rocky-9.3 | 192.168.117.12 | nginx |
软件下载地址:haproxy.org
192.168.117.10端
安装依赖包
[root@localhost ~]# yum -y install make gcc pcre2-devel bzip2-devel openssl-devel systemd-devel gcc-c++
创建系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin haproxy
[root@localhost ~]# id haproxy
uid=978(haproxy) gid=977(haproxy) groups=977(haproxy)
解压软件包
[root@localhost ~]# ls
anaconda-ks.cfg Documents haproxy-2.9.6.tar.gz Pictures Templates
Desktop Downloads Music Public Videos
[root@localhost ~]# tar xf haproxy-2.9.6.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg Documents haproxy-2.9.6 Music Public Videos
Desktop Downloads haproxy-2.9.6.tar.gz Pictures Templates
编译安装
[root@localhost ~]# cd haproxy-2.9.6/
[root@localhost haproxy-2.9.6]# ls
addons BSDmakefile dev include MAINTAINERS reg-tests SUBVERS VERSION
admin CHANGELOG doc INSTALL Makefile scripts tests
BRANCHES CONTRIBUTING examples LICENSE README src VERDATE
[root@localhost haproxy-2.9.6]# make clean
[root@localhost haproxy-2.9.6]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE2=1 USE_SYSTEMD=1
[root@localhost haproxy-2.9.6]# make install PREFIX=/usr/local/haproxy
[root@localhost haproxy-2.9.6]# ls /usr/local/haproxy/
doc sbin share
配置负载的核心参数
[root@localhost ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@localhost ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
创建配置文件
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
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
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web1 192.168.117.11:80 check inter 2000 fall 5
server web2 192.168.117.12:80 check inter 2000 fall 5
启用日志文件
[root@localhost ~]# vim /etc/rsyslog.conf
# Save boot messages also to boot.log
local0.* /var/log/haproxy.log
local7.* /var/log/boot.log
[root@localhost ~]# systemctl restart rsyslog
配置haproxy服务文件
[root@localhost ~]# vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now haproxy
[root@localhost haproxy-2.9.6]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 4096 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 3000 0.0.0.0:80 0.0.0.0:*
LISTEN 0 4096 [::1]:631 [::]:*
LISTEN 0 128 [::]:22 [::]:*
192.168.117.11端
配置httpd网页服务
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo 'rs1' > /var/www/html/index.html
[root@localhost ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
192.168.117.12端
配置httpd网页服务
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo 'rs2' > /var/www/html/index.html
[root@localhost ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
测试:192.168.117.10端
[root@localhost haproxy-2.9.6]# curl 192.168.117.10
rs1
[root@localhost haproxy-2.9.6]# curl 192.168.117.10
rs2