haproxy配置安装
haproxy是一种高性能、开源的负载均衡器和代理服务器软件。它可以在应用层(HTTP、HTTPS、SMTP等)和传输层(TCP、UDP)上进行负载均衡,将客户端请求分发到多个后端服务器上,以实现高可用性和性能扩展。
HAProxy的特点包括:
- 高性能:haproxy使用事件驱动的架构和多线程模型,可以处理高并发的请求,具有较低的延迟和高吞吐量。
- 可靠性:haproxy支持多种负载均衡算法,如轮询、加权轮询、最少连接数等,可以根据实际需求选择合适的算法。同时,haproxy还提供了健康检查机制,可以检测后端服务器的可用性,自动剔除不可用的服务器。
- 灵活性:haproxy支持动态配置和热重新加载,可以在运行时修改配置文件,无需重启服务。此外,haproxy还支持SSL终止、HTTP请求重定向、请求过滤等功能,可以根据需要进行定制。
- 可扩展性:haproxy可以通过添加多个实例和使用负载均衡器集群来实现高可用性和性能扩展。它还支持会话保持和粘性会话,确保客户端的请求始终发送到同一台后端服务器上。
安装流程
//安装之前现在两台电脑上把网站服务配置好,并配置证书
//安装依赖
[root@localhost ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//创建haproxy用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin haproxy
//下载haproxy二进制包
[root@localhost ~]# wget https://www.haproxy.org/download/2.7/src/haproxy-2.7.10.tar.gz
[root@localhost ~]# tar xf haproxy-2.7.10.tar.gz
[root@localhost ~]# cd haproxy-2.7.10
[root@localhost haproxy-2.7.10]# make clean
[root@localhost haproxy-2.7.10]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_SYSTEMD=1
[root@localhost haproxy-2.7.10]# make install PREFIX=/usr/local/haproxy
[root@localhost haproxy-2.7.10]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/
[root@localhost haproxy-2.7.10]# haproxy -V
HAProxy version 2.7.10-d796057 2023/08/09 - https://haproxy.org/
...
[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 ~]# mkdir /etc/haproxy
[root@localhost ~]# cat > /etc/haproxy/haproxy.cfg <<EOF
> #--------------全局配置----------------
> 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 web01 172.16.103.130:80 check inter 2000 fall 5
> #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
> EOF
//编写service文件
[root@localhost ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
> [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
> EOF
[root@localhost ~]# systemctl daemon-reload
修改配置文件
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
#---------------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 web01 192.168.37.130:80 check inter 2000 fall 5
server web02 192.168.37.140:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
#---------------web,https设置-----------------------
listen webcluster-https
bind 0.0.0.0:443
mode tcp
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web03 192.168.37.130:443 check inter 2000 fall 5
server web04 192.168.37.140:443 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
这样负载均衡就配置好了
//启动日志
[root@localhost ~]# vim /etc/rsyslog.conf.
local0.* /var/log/haproxy.log
[root@localhost ~]# systemctl restart rsyslog
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 2048 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 2048 0.0.0.0:443 0.0.0.0:*
LISTEN 0 2048 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
然后访问http://192.168.37.120:8189/haproxy_stats