haproxy的负载均衡及应用

Haproxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机,它是免费、快速并且可
靠的一种解决方案。HAProxy 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层
处理。HAProxy 运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很
简单安全的整 合进您当前的架构中, 同时可以保护你的 web 服务器不被暴露到网络上

一、haproxy 实现负载均衡

实验环境:rhel6.5    selinux and iptables disabled
实验主机: 172.25.45.1 (server1)   haproxy
          172.25.45.2 (server2)   web1       
          172.25.45.3 (server3)   web2
          172.25.45.4(server4)    haproxy
HAproxy安装及配置:

1.解压haproxy压缩包,安装源码编译软件,源码编译haproxy软件

 

tar zxf haproxy-1.4.24.tar.gz   ##解压源码包

yum install rpm-build -y             ###安装rpm-build软件


rpmbuild -tb haproxy-1.4.23.tar.gz   ###源码编译,制作rpm包

yum install -y gcc pcre-devel        ##解决依赖性包安装

2.找到haproxy的配置文件

cd rpmbuild/RPMS/x86_64/

ls
rpm -qpl haproxy-1.4.23.1.x86_64.rpm    ###查看安装后有那些文件


rpm -ivh haproxy-1.4.23-1.x86_64.rpm    ###安装制作好的rpm包

cd haproxy-1.4.23/examples/
cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
find -name *.spec
cd /etc/haproxy/
ls

3.

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

groupadd -g 200 haproxy            ##建立一个haproxy组
useradd -u 200 -g 200 -M haproxy

id haproxy


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

vim /etc/security/limits.conf
aproxy      -    nofile      10000


cd /etc/haproxy
vim haproxy.cfg

修改配置文件:

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    # haproxy用户的gid
        chroot          /var/empty
        daemon
defaults    # 默认条件
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
        stats uri       /admin/stats
option prefer-last-server
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  5s
# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        default_backend  static   ###默认后端设置为静态的
# The static backend backend for 'Host: img', /img and /css.
backend static
        balance         roundrobin   # 负载均衡算法rr
        server          statsrv1 172.25.70.2:80 check inter 1000   ###添加两个后端
        server          statsrv2 172.25.70.3:80 check inter 1000

5.打开server1的haproxy服务和后端服务器的httpd服务

/etc/init.d/haproxy start ##打开服务
在server2和server3中分别打开httpd
/etc/init.d/httpd start
测试:在浏览器访问:172.25.45.1出现server2和server3的论调

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值