第 2 步 - 安装和配置 HAProxy

[jack@onitroad ~]# yum install haproxy -y
[jack@onitroad ~]# systemctl enable haproxy && systemctl start haproxy
  • 现在让我们在 haproxy.cfg 文件下添加我们的后端和前端配置
[jack@onitroad ~]# vi /etc/haproxy/haproxy.cfg

首先想到要做的是添加Frontend设置:

#--------------------------------------------------------------------
# Frontend
#--------------------------------------------------------------------
frontend web_cluster
bind *:80
#stats uri /haproxy?stats
default_backend web_cluster_http

第二件事是添加后端设置:

#--------------------------------------------------------------------
# round robin balancing backend HTTP
#--------------------------------------------------------------------
backend web_cluster_http
balance roundrobin
# balance leastconn
mode http
server webserver01 192.168.1.71:80 check
server webserver02 192.168.1.72:80 check
#

最后,我们还可以启用“统计页面”部分,让我们监控代理的状态,不要忘记在防火墙中打开该端口,这里我们选择端口 9000,我们设置用户和密码来访问“统计页面”:

#--------------------------------------------------------------------
# Configuration for the Statistics Report
#--------------------------------------------------------------------
listen stats
bind :9000
stats enable
stats hide-version
stats refresh 20s
stats show-node
stats uri /stats
stats auth username:password

完成后,退出并保存文件,最后不要忘记重新加载haproxy服务。

最后,我们可以通过将此链接放入浏览器 http://load_balancer_ip:9000/stats 来访问统计页面。

HAProxy:

HAProxy 是一种免费、非常快速且可靠的解决方案,可为基于 TCP 和 HTTP 的应用程序提供高可用性、负载平衡和代理。
它特别适用于流量非常高的网站,并为许多世界上访问量最大的网站提供支持。

如何在 CentOS 7/RHEL 7 上为 Nginx 设置 HAProxy 作为第 7 层负载均衡器

本文介绍如何在centos 7/RHEL 7上为nginx安装HAProxy作为第7层负载均衡器。

环境:

在本教程中,我将使用 3 个 Centos 7 服务器:
HAProxy 负载均衡器。
2 个 nginx 网络服务器节点。

HAPproxy 算法:

  • Round-Robin (roundrobin)
  • 最少连接(leastconn)
  • 来源(source)
  • URI (uri)

第 1 步 - 安装和配置 Nginx

这一步我们需要在两个服务器节点安装nginx

[jack@onitroad ~]# yum update
[jack@onitroad ~]# yum install nginx -y
[jack@onitroad ~]# systemctl enable && systemctl start nginx
[jack@onitroad ~]# systemctl status nginx
[jack@onitroad ~]# firewall-cmd --permanent --add-service=http
[jack@onitroad ~]# firewall-cmd --reload

Nginx 现在已安装。
进入 Nginx 的 web 目录并更改索引文件,以便我们可以看到两个节点中的哪个正在工作:

[jack@onitroad ~]# echo " Welcome to my first web server" > /src/share/nginx/html/index.html
[jack@onitroad ~]# echo " Welcome to my Second web server" > /src/share/nginx/html/index.html

完成配置后,我们现在需要在两个节点上重新加载 nginx。

[jack@onitroad ~]# systemctl reload nginx
日期:2020-06-02 22:18:53 来源:oir作者:oir