HAProxy实现负载均衡

一.关于HAProxy:

1.什么是HAProxy:

HAProxy是一个特别适用于高可用性环境的TCP/HTTP开源的反向代理和负载均衡软件。

在7层负载均衡方面的功能很强大(支持cookie track, header rewrite等等),支持双机热备,支持虚拟主机,支持健康检查(通过patch可以支持ECV),同时还提供直观的监控页面,可以清晰实时的监控服务集群的运行状况。同时支持Linux 2.6内核中System Epoll,通过简化系统调用,大幅的提高了网络I/O性能。

 

2.HAProxy的特点:

1)支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机;

2)能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作

3)支持url检测后端的服务器出问题的检测会有很好的帮助。

4)更多的负载均衡策略比如:动态加权轮循(Dynamic Round Robin),加权源地址哈希(Weighted Source Hash),加权URL哈希和加权参数哈希(Weighted Parameter Hash)已经实现

5)单纯从效率上来讲HAProxy更会比Nginx有更出色的负载均衡速度。

6)HAProxy可以对Mysql进行负载均衡,对后端的DB节点进行检测和负载均衡。

9)支持负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie)

10)不能做Web服务器即Cache。

 

二.实验:

 

实验环境:

Server1,2,3三台虚拟机。都需要关闭selinux,关闭iptables。

server3做为后台服务器,需要开启http服务,监听端口80。

server1,4作为代理服务器。

 

Server3:

1下载HAProxy:haproxy-1.6.11

2解压安装包:tar xf haproxy-1.6.11.tar.gz 

3安装rpmbuild服务:yum install -y rpm-build

rpmbuild -tb haproxy-1.6.11.tar.gz 

##执行此命令需要虚拟机已经安装gcc,pcre-devel

4.安装haproxy服务:rpm -ivh haproxy-1.6.11-1.x86_64.rpm

##执行此命令需要进入/rpmbuild/RPMS/x86_64/

##建议检查haproxy的脚本文件,防止被写入恶意代码rpm -qpl haproxy-1.6.11-1.x86_64.rpm --scripts

5.添加新用户:groupadd -g 200 haproxy

useradd -g 200 -u 200 haproxy

6编辑haprocy配置文件:vim /etc/haproxy/haproxy.cfg

#

# This is a sample configuration. It illustrates how to separate static objects

# traffic from dynamic traffic, and how to dynamically regulate the server load.

#

# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or

# URIs starting with /img or /css to a dedicated group of servers. URIs

# starting with /admin/stats deliver the stats page.

#

 

#全局配置, 用于设定义全局参数, 属于进程级的配置, 通常与操作系统配置有关.

global

        maxconn         10000

#定义统计信息保存位置

        stats socket    /var/run/haproxy.stat mode 600 level admin

        log             127.0.0.1 local0

        uid             200

        gid             200

#运行路径

        chroot          /var/empty

#后台运行haproxy

        daemon

 

#默认配置

defaults

#默认的模式【tcp:4层; http:7层; health:只返回OK】

        mode            http

        log             global

#日志类别

        option          httplog

        option          dontlognull

        monitor-uri     /monitoruri

    #默认的最大连接数

        maxconn         8000

        timeout client  30s

 

        stats uri       /admin/stats

 

#2次连接失败就认为是服务器不可用

        retries         2

#当与后端服务器的会话失败(服务器故障或其他原因)时, 把会话重新分发到其他健康的服务器上;

        option redispatch

    #haproxy与后端服务器连接超时时间.

        timeout connect 5s

        timeout server  30s

        timeout queue   30s

 

# The public 'www' address in the DMZ

frontend public

        bind            172.25.25.3:80 name clear

        #use_backend     static if { hdr_beg(host) -i img }

        #use_backend     static if { path_beg /img /css   }

        default_backend dynamic

 

backend dynamic

#设置默认的负载均衡方式

        balance         roundrobin

#后端服务器定义

        server          dynsrv1 172.25.25.1:80 check inter 1000

        server          dynsrv2 172.25.25.4:80 check inter 1000

7修改健康检查文件:vim /etc/security/limits.conf 

##增加文件允许的最大值,保证服务正常运行

##可su - haproxy后ulimit -a检查

8开启服务:/etc/init.d/haproxy start

 

测试:

物理机:

for i in {1..10}; do curl 172.25.152.3;done

网页访问172.25.152.3

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值