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