HAProxy 是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件(负载均衡器),支持虚拟主机,它是免费、快速并且可靠的一种解决方案。 HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在时下的硬件上,完全可以支持数以万计的 并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy的作用实现负载均衡,将请求分发到多个服务器上,从而提高系统的可用性和性能。它可以根据不同的负载均衡算法(如轮询、加权轮询、IP哈希等)来分配请求,并支持会话保持、健康检查、SSL终止等功能。此外,HAProxy还可以实现反向代理、SSL加速、HTTP压缩等功能,使其成为一款功能强大的代理软件。
环境说明:
IP | 版本 | |
---|---|---|
haproxy | 192.168.200.42 | redhat8 |
rs1 | 192.168.200.43 | redhat8 |
rs2 | 192.168.200.44 | redhat8 |
#关闭防火墙和selinux
[root@haproxy ~]# systemctl disable --now firewalld
[root@haproxy ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@haproxy ~]# setenforce 0
[root@rs1 ~]# systemctl disable --now firewalld
[root@rs1 ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@rs1 ~]# setenforce 0
[root@rs2 ~]# systemctl disable --now firewalld
[root@rs2 ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
[root@rs2 ~]# setenforce 0
#rs上配置http并启动
[root@rs1 ~]# yum -y install httpd
[root@rs1 ~]# echo 'RS1' > /var/www/html/index.html
[root@rs1 ~]# systemctl enable --now httpd
[root@rs1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@rs2 ~]# yum -y install httpd
[root@rs2 ~]# echo 'RS2' > /var/www/html/index.html
[root@rs2 ~]# systemctl enable --now httpd
[root@rs2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
#将rs2的httpd服务端口80更改为8080,测试实验效果
[root@rs2 ~]# vi /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 8080
[root@rs2 ~]# systemctl restart httpd
[root@rs2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
#先装依赖包
[root@haproxy ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel vim wget
#安装haproxy并创建用户
[root@haproxy ~]# useradd -r -M -s /sbin/nologin haproxy
#在haproxy官网下载软件包
[root@haproxy ~]# wget https://www.haproxy.org/download/2.7/src/haproxy-2.7.10.tar.gz
[root@haproxy ~]# ls
anaconda-ks.cfg haproxy-2.7.10.tar.gz
#解压
[root@haproxy ~]# tar xf haproxy-2.7.10.tar.gz
[root@haproxy ~]# ls
anaconda-ks.cfg haproxy-2.7.10 haproxy-2.7.10.tar.gz
[root@haproxy ~]# cd haproxy-2.7.10
[root@haproxy haproxy-2.7.10]# make clean //该命令用于清理之前编译的参数
[root@haproxy ~]# cd haproxy-2.7.10
[root@haproxy haproxy-2.7.10]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
#指定路径进行安装
[root@haproxy haproxy-2.7.10]# make install PREFIX=/usr/local/haproxy
[root@haproxy haproxy-2.7.10]# ls /usr/local/
bin etc games haproxy include lib lib64 libexec sbin share src
[root@haproxy haproxy-2.7.10]# ls /usr/local/haproxy/
doc sbin share
#通过软链接的方式设置环境变量
[root@haproxy haproxy-2.7.10]# cd /usr/local/haproxy/
[root@haproxy haproxy]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/
[root@haproxy haproxy]# which haproxy
/usr/sbin/haproxy
[root@haproxy haproxy]# haproxy -v
HAProxy version 2.7.10-d796057 2023/08/09 - https://haproxy.org/
Status: stable branch - will stop receiving fixes around Q1 2024.
Known bugs: http://www.haproxy.org/bugs/bugs-2.7.10.html
Running on: Linux 4.18.0-193.el8.x86_64 #1 SMP Fri Mar 27 14:35:58 UTC 2020 x86_64
#配置各个负载的内核参数
[root@haproxy haproxy]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@haproxy haproxy]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@haproxy haproxy]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
#编写haproxys.service文件
[root@haproxy ~]# vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
[root@haproxy ~]# systemctl daemon-reload
#启动日志
配置日志记录功能
[root@haproxy ~]# vim /etc/rsyslog.conf
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local0.* /var/log/haproxy.log //添加此行
#重启日志服务
[root@haproxy ~]# systemctl restart rsyslog.service
#提供配置文件
[root@haproxy ~]# mkdir /etc/haproxy
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# cat /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 //访问状态页面的URI
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 rs1 192.168.200.43:80 check inter 2000 fall 5 //加入做负载均衡的主机
server rs2 192.168.200.44:8080 check inter 2000 fall 5 //加入做负载均衡的主机
//重启haproxy服务,并将haproxy服务设置开机自启
[root@haproxy ~]# systemctl restart haproxy
[root@haproxy ~]# systemctl enable --now haproxy.service
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@haproxy ~]# systemctl status haproxy.service
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabl>
Active: active (running) since Tue 2023-10-10 21:41:07 CST; 21s ago
Process: 16679 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=ex>
Main PID: 16683 (haproxy)
#查看端口
[root@haproxy ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
访问负载均衡器的http页面测试