HAproxy的负载均衡,以及基于TCP和HTTP的应用程序代理

本文详细介绍了如何使用HAproxy实现负载均衡,包括配置过程、测试以及基于TCP和HTTP的应用程序代理。在负载均衡部分,通过配置后端服务器和负载均衡算法,实现了服务器间的轮调。在应用程序代理部分,讨论了动态页面与静态页面的分离、日志路径修改、访问控制以及读写分离的实现,确保了服务的高效稳定运行。
摘要由CSDN通过智能技术生成
HAProxy是一个使用C语言编写的自由及开放源代码软件[1],其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。

一.Haproxy实现负载均衡

配置过程及测试:
1.解压haproxy压缩包,安装源码编译软件,源码编译haproxy软件

[root@server1 ~]# ls
haproxy-1.7.3.tar.gz
[root@server1 ~]# tar zxf haproxy-1.7.3.tar.gz 
[root@server1 ~]# cd haproxy-1.7.3
[root@server1 haproxy-1.7.3]# find . -name *.spec
./examples/haproxy.spec
[root@server1 haproxy-1.7.3]# yum install -y rpm-build
[root@server1 ~]# rpmbuild -tb haproxy-1.7.3.tar.gz 

2.找到haproxty的配置文件

[root@server1 ~]# ls
haproxy-1.7.3  haproxy-1.7.3.tar.gz  rpmbuild
[root@server1 ~]# cd rpmbuild/RPMS/x86_64/
[root@server1 x86_64]# ls
haproxy-1.7.3-1.x86_64.rpm
[root@server1 x86_64]# rpm -ivh haproxy-1.7.3-1.x86_64.rpm   安装
Preparing...                ########################################### [100%]
   1:haproxy                ########################################### [100%]
[root@server1 x86_64]# rpm -qa | grep haproxy
haproxy-1.7.3-1.x86_64
[root@server1 x86_64]# rpm -ql haproxy-1.7.3-1.x86_64
/etc/haproxy
/etc/rc.d/init.d/haproxy
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.7.3
/usr/share/doc/haproxy-1.7.3/CHANGELOG
/usr/share/doc/haproxy-1.7.3/README
/usr/share/doc/haproxy-1.7.3/architecture.txt
/usr/share/doc/haproxy-1.7.3/configuration.txt
/usr/share/doc/haproxy-1.7.3/intro.txt
/usr/share/doc/haproxy-1.7.3/management.txt
/usr/share/doc/haproxy-1.7.3/proxy-protocol.txt
/usr/share/man/man1/haproxy.1.gz
[root@server1 x86_64]# cd
[root@server1 ~]# cd haproxy-1.7.3 
[root@server1 haproxy-1.7.3]# cd examples/
[root@server1 examples]# ls
acl-content-sw.cfg     debug2ansi    haproxy.spec           ssl.cfg
auth.cfg               debug2html    haproxy.vim            stats_haproxy.sh
check                  debugfind     init.haproxy           transparent_proxy.cfg
check.conf             errorfiles    option-http_proxy.cfg  wurfl-example.cfg
content-sw-sample.cfg  haproxy.init  seamless_reload.txt
[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/  将配置文件拷贝到/etc/haproxy
[root@server1 examples]# cd /etc/haproxy/
[root@server1 haproxy]# ls
content-sw-sample.cfg
[root@server1 haproxy]# mv content-sw-sample.cfg haproxy.cfg 给配置文件重新命名
[root@server1 haproxy]# ls
haproxy.cfg

3.建立haproxy用户和组。利用linux对haporxy用户的访问控制实现对haporxy服务的控制

4.修改配置文件,添加后端真实服务器和负载均衡算法

[root@server1 haproxy]# vim 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                # haproxy用户的uid
        gid             200              
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值