使用Haproxy搭建部署Web群集

  • HAProxy是一个使用C语言编写的自由及开放源代码软件[1],其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,
    同时可以保护你的web服务器不被暴露到网络上。HAProxy实现了一种事件驱动,
    单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制
    、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space)
    实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以
    使每个CPU时间片(Cycle)做更多的工作。包括 GitHub、Bitbucket[3]、Stack
    Overflow[4]、Reddit、Tumblr、Twitter[5][6]和
    Tuenti[7]在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。

  • 配置HAProxy Session亲缘性的三种方式

    haproxy负载均衡保持客户端和服务器Session亲缘性的三种方式:

  • 1 用户IP 识别
    haproxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类似于nginx 的IP hash 指令)
    配置指令 balance source

  • 2 cookie 识别
    haproxy 将WEB服务端发送给客户端的cookie中插入(或添加前缀)haproxy定义的后端的服务器COOKIE ID。
    配置指令例举 cookie SESSION_COOKIE insert indirect nocache
    用firebug可以观察到用户的请求头的cookie里 有类似" Cookie jsessionid=0bc588656ca05ecf7588c65f9be214f5; SESSION_COOKIE=app1" SESSION_COOKIE=app1就是haproxy添加的内容

  • 3 session 识别
    配置举例:
    #vi /usr/local/haproxy/haproxy.cfg
    backend COOKIE_srv
    mode http
    cookie SESSION_COOKIE insert indirect nocache
    server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
    server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1
    backend SOURCE_srv
    mode http
    balance source
    server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
    server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1
    backend APPSESSION_srv
    mode http
    appsession JSESSIONID len 64 timeout 5h request-learn
    server REALsrv_70 184.82.239.70:80 cookie 11 check inter 1500 rise 3 fall 3 weight 1
    server REALsrv_120 220.162.237.120:80 cookie 12 check inter 1500 rise 3 fall 3 weight 1

    一、Haproxy
    1、Haproxy的作用和特点
    1)Haproxy的作用

    是一个故障转移群集调度器
    支持静态网站配置故障转移群集
    2)Haproxy的特点
    支持动态分离
    处理数据速度快
    支持高并发
    一般静态网站使用
    2、常见的负载均衡调度器类型
    1)LVS
    一般动态网站使用
    支持网络负载均衡
    故障转移群集需要和keepalived结合使用
    2)nginx
    nginx可以是一个代理工具或者负载均衡调度器
    支持动静分离
    需要使用upstream实现故障转移
    3)keepalived
    是一个故障转移工具
    依赖VRRP协议
    可以和apache、ngin、tomcat结合工作
    keepalived使用比较广泛
    3、负载均衡调度器的类型
    1)软件

    处理速度慢稳定性差
    LVS、nginx、haproxy
    2)硬件调度器
    处理数据速度快稳定性强
    F5、梭子鱼、绿盟
    4、常见的负载均衡调度器算法
    1)RR

    轮询调度算法
    参与负载均衡的服务器各占50%负载
    2)最小连接数
    调度器选择负债量最少的服务器相应客户端请求
    3)基于来源访问调度
    将不同地理位置的客户端请求转发到不同的服务器相应客户端请求
    需要基于源IP地址和cookie实现
    5、http请求的方式和http代码
    1)http请求的方式

    GET:安全性差速度快
    post:安全性强速度慢
    2)http的状态代码
    200、301都是正常访问
    400、500都属于异常访问

二.两台服务器安装nginx,配置命令一样
(1)
安装nginx的依赖程序

[root@centos01 ~]# yum -y install
pcre-devel zlib-devel

(2) 创建管理nginx的用户

[root@centos01 ~]# useradd -M -s
/sbin/nologin nginx

切换linux光盘解压配置nginx,

[root@centos01 ~]# umount /mnt/

[root@centos01 ~]# tar zxf /mnt/nginx-1.6.0.tar.gz
-C /usr/src/

[root@centos01 ~]# cd /usr/src/nginx-1.6.0/

[root@centos01 nginx-1.6.0]# ./configure
–prefix=/usr/local/nginx --user=nginx --group=nginx
–with-http_stub_status_module

(3)编译安装nginx

[root@centos01 nginx-1.6.0]# make && make install

3 优化nginx执行命令,创建测试文件

[root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

[root@centos01 ~]# echo
“www.benet.com” > /usr/local/nginx/html/index.html

客户端访问测试,网站服务器
在这里插入图片描述
在这里插入图片描述

三.安装配置haproxy

1.安装haproxy

(1)安装依赖程序

[root@centos03 ~]# yum -y install pcre-devel bzip2-devel

(2)解压编译haproxy支持64位系统

[root@centos03 ~]# tar zxvf haproxy-1.4.24.tar.gz -C /usr/src/

[root@centos03 ~]# cd /usr/src/haproxy-1.4.24/

[root@centos03 haproxy-1.4.24]# make TARGET=linux26

(3)安装haproxy

[root@centos03 haproxy-1.4.24]# make install

2.生成haproxy配置文件
(1)创建保存haproxy配置文件目录

[root@centos03 ~]# mkdir /etc/haproxy

(2)生成主配置文件

root@centos03 ~]# cd /usr/src/haproxy-1.4.24/

[root@centos03 haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/

(3)创建haproxy服务器控制脚本设置执行权限

[root@centos03 haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy

[root@centos03 haproxy-1.4.24]# chmod +x /etc/init.d/haproxy

(4)添加系统服务器设置开机自动启动

[root@centos03 haproxy-1.4.24]# chkconfig --add haproxy

[root@centos03 haproxy-1.4.24]# chkconfig --level 35 haproxy on

(5)优化程序执行命令

[root@centos03 haproxy-1.4.24]# cp haproxy /usr/sbin/

(6)创建服务运行的临时目录

[root@centos03 haproxy-1.4.24]# mkdir -p /usr/share/haproxy

3.配置haproxy群集
修改主配置文件

[root@centos03 ~]# vim /etc/haproxy/haproxy.cfg
contimeout 10 连接超时时间

   clitimeout      10 客户端超时时间

   srvtimeout      10 服务器超时时间

listen
nginx 192.168.100.30:80

   balance roundrobin

   server  web01 192.168.100.10:80check inter 2000 fall 3

   server  web02 192.168.100.20:80check inter 2000 fall 3

启动haproxy服务

[root@centos03 ~]# /etc/init.d/haproxy start

客户端访问测试

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值