如何在Debian 10上安装和配置HAProxy(Buster)
时间:2020-02-23 14:32:41 来源:igfitidea点击:
本教程将讨论如何在Debian 10 Buster上安装和配置Haproxy Load Balancer。
HAProxy是一个开源,可靠,高性能的TCP/HTTP负载均衡器和代理服务器,在Linux,FreeBSD和Solaris上运行。
HAProxy是用C编写的,它为在多个服务器上运行的TCP和基于HTTP的应用程序提供了高可用性负载平衡器。
在我的设置中,我将有两个后端的Web应用程序,并配置HAProxy以将余额流量加载到这些HTTP服务器。
使用的服务器如下图所示。
|||
| --- - | --- |
|服务器|服务|
| 192.168.10.10 | haproxy |
| 192.168.10.11 | Apache Web服务器1 |
| 192.168.10.12 | Apache Web服务器2 |
第1步:更新Debian系统
更新将用于最新可用软件包的所有服务器。
sudo apt update && sudo apt -y upgrade sudo reboot
第2步:配置Apache后端服务器
对于此演示,我们将使用Apache Web服务器作为Haproxy Frontend代理的后端。
我将使用以下命令在我的两个Debian 10服务器上安装Apache。
在Server1上
sudo apt update sudo apt -y install apache2 echo "<H1>Hello from Server1</H1>" | sudo tee /var/www/html/index.html
在Server2上
sudo apt update sudo apt -y install apache2 echo "<H1>Hello from Server2</H1>" | sudo tee /var/www/html/index.html
我们可以尝试访问Server1和Server2 Web URL并查看响应。
第3步:在Debian 10上安装Haproxy(Buster)
要在Debian 10上安装HAProxy(Buster),请在终端上运行以下命令。
sudo apt -y install haproxy
配置haproxy。
$sudo nano /etc/haproxy/haproxy.cfg
# Add to the end
# Define frontend
frontend apache_front
# Frontend listen port - 80
bind *:80
# Set the default backend
default_backend apache_backend_servers
# Enable send X-Forwarded-For header
option forwardfor
# Define backend
backend apache_backend_servers
# Use roundrobin to balance traffic
balance roundrobin
# Define the backend servers
server backend01 192.168.10.11:80 check
server backend02 192.168.10.12:80 check
重新启动Haproxy服务
sudo systemctl restart haproxy
验证配置是否正常工作以访问前端HAProxy服务器。
再次重新加载页面,我们应该从Server2获取响应。
对于TCP后端,配置将如下所示。
frontend rserve_frontend
bind *:80
mode tcp
option tcplog
timeout client 1m
default_backend rserve_backend
backend rserve_backend
mode tcp
option tcplog
option log-health-checks
option redispatch
log global
balance roundrobin
timeout connect 10s
timeout server 1m
server rserve1 <rserve hostname1>:6311 check
server rserve2 <rserve hostname2>:6311 check
server rserve2 <rserve hostname3>:6311 check
配置SSL.
结合证书和私钥。
cat fullchain.pem privkey.pem > haproxy.pem
然后配置HAProxy以在前端使用SSL证书。
frontend apache-frontend
bind *:80
bind *:443 ssl crt /etc/letsencrypt/live/webapp.theitroad.com/haproxy.pem

