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.1

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
#       redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000


listen  webcluster 0.0.0.0:80
        option  httpchk GET /index.html
        balance roundrobin
        server  inst1 192.168.10.101:80   check inter 2000 fall 3
        server  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 群集

 

 

 

  • 20
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值