欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 04 Haproxy搭建Web集群

04 Haproxy搭建Web集群

2024/10/25 14:27:15 来源:https://blog.csdn.net/qq_51678989/article/details/141025754  浏览:    关键词:04 Haproxy搭建Web集群

4.1 案例分析

4.1.1 案例概述

Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如LVS和Nginx。相比较而言,LVS 性能最好,但是搭建相对复杂;Nginx 的upstream模块支持群集功能,但是对群集节点健康检查功能不强,高并发性能没有Haproxy好。Haproxy 官方网站是http://www.haproxy.org/。

4.1.2 案例前置知识点

1.http请求

通过URL访问网站使用的协议是HTTP协议,此类请求一般为HTTP请求。HTTP请求的方式分为GET方式和POST方式。当使用浏览器访问某一个URL,会根据请求URL放回状态码,通常正常的状态码为2XX,3XX(如 200,301),如果出现异常会返回4XX,5XX(如400,500)

例如,访问 http://www,test.com/a.php?Id=123,就是一个GET请求,如果访问正常,会从服务器的日志中获取 200状态码。假如此请求使用 POST 方式,那么传递给 a.php 的 Id参数依旧是 123,但是浏览器的 URL,将不会显示后面的 Id=123 字样,因此表单类或者有用户名、密码等内容提交时建议使用 POST方式。不管使用哪种方式,最终a.php 获取的值是一样的。

2.负载均衡常用调度算法LS、Haproxy、Nginx 最常用的调度算法有三种,如下所述,

(1)RR(Round Robin)。RR算法是最简单最常用的一种算法,即轮询调度

(2)LC(Least Connections)。LC算法即最小连接数算法,根据后端的节点连接数大小动态分配前端请求。

(3)SH(Source Hashing)。SH 即基于来源访问调度算法,此算法用于一些有 Session会话记录在服务器端的场景,可以基于来源的 IP、Cookie 等做群集调度。

3.常见的 Web 群集调度器

目前,常见的Web 群集调度器分为软件和硬件。软件通常使用开源的LVS、Haproxy、Nginx,硬件一般使用比较多的是F5。也有很多人使用国内的一些产品,如梭子鱼、绿盟等。

4.1.3

1.本案例环境

本案例使用三台服务器模拟搭建一套 Web 群集,具体的拓扑如图 4.1所示。案例环境如表 4-1 所示。

2.案例需求

测试安装nginx、haproxy;Haproxy、nginx 配置。

4.2 案例实施

1.编译安装Nginx服务器

(1)搭建 Nginx-node-1,2,使用 nginx-1.12.0.tar.gz 安装包进行编译安装
[root@bogon nginx-1.12.0]#  yum -y install pcre-devel zlib-devel gcc*
[root@bogon ~]# tar zxvf nginx-1.12.0.tar.gz 
[root@bogon nginx-1.12.0]# useradd -M -s /sbin/nologin nginx
[root@bogon nginx-1.12.0]# ./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx  --with-http_stub_status_module
[root@bogon nginx-1.12.0]#  make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@bogon nginx-1.12.0]# cd /usr/local/nginx/html/[root@bogon html]# vim test.html
test01
[root@bogon html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@bogon html]# nginx 
[root@bogon html]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4027/nginx: master  
[root@bogon html]# systemctl stop firewalld
[root@bogon html]# setenforce 0
[root@bogon html]# 
(2)在客户端测试看看是否能够访问 
[root@bogon ~]# curl 192.168.10.101/test.html
test01[root@bogon ~]# tar zxvf haproxy-1.5.19.tar.gz [root@bogon ~]# curl 192.168.10.102/test.html
test02
[root@bogon ~]# 

2.编译安装 Haproxy

在 Haproxy 服务器使用 haproxy-1.5.19.tar.gz安装包进行编译安装

[root@bogon ~]# yum -y install prce-devel bzip2-devel gcc*
[root@bogon ~]# tar zxvf haproxy-1.5.19.tar.gz 
[root@bogon haproxy-1.5.19]# make TARGET=linux26
[root@bogon haproxy-1.5.19]# make install

3.Haproxy 服务器配置

下面是 Haproxy 服务器的配置步骤。

(1)建立 Haproxy 的配置文件。
[root@bogon haproxy-1.5.19]# mkdir /etc/haproxy
[root@bogon haproxy-1.5.19]# cd examples/
[root@bogon examples]# cp haproxy.cfg  /etc/haproxy/
[root@bogon examples]# cp haproxy.init  /etc/init.d/haproxy
[root@bogon examples]# vim /etc/haproxy/haproxy.cfg # this config needs haproxy-1.1.28 or haproxy-1.2.1globallog 127.0.0.1   local0log 127.0.0.1   local1 notice#log loghost    local0 infomaxconn 4096#chroot /usr/share/haproxyuid 99gid 99daemon#debug#quietdefaultslog     globalmode    httpoption  httplogoption  dontlognullretries 3
#       redispatchmaxconn 2000contimeout      5000clitimeout      50000srvtimeout      50000listen  webcluster 0.0.0.0:80option  httpchk GET /index.htmlbalance roundrobinserver  inst1 192.168.10.101:80   check inter 2000 fall 3server  inst2 192.168.10.102:81  check inter 2000 fall 3                           

4.创建自启动脚本 

[root@bogon examples]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@bogon examples]# chmod +x /etc/init.d/haproxy [root@bogon examples]# chkconfig --add /etc/init.d/haproxy 
[root@bogon examples]# /etc/init.d/haproxy start 
Starting haproxy (via systemctl):                          [  确定  ]
[root@bogon examples]# 
[root@bogon examples]# systemctl stop firewalld

 

自启动脚本命令如下:

5.测试 Web 群集

 

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com