HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理。现在2个web端的ip分别是192.168.232.167 , 192.168.232.168 , haproxy的机器ip为192.168.232.163。
1、 首先下载haproxy源码包
[root@localhost ~]# iptables –F //关闭防火墙
[root@localhost ~]# setenforce 0 //关闭selinux
[root@localhost ~]# cd /usr/local/src/
[root@localhost ~]# wget http://pan.baidu.com/s/1pJiJvq7 //下载地址
2、 编译安装
[root@localhost ~]# tar –xzvf haproxy-1.4.24.tar.gz
[root@localhost ~]# cd haproxy-1.4.24
[root@localhost ~]# uname -a //查看linux内核版本
[root@localhost ~]# make TARGET=linux26 PREFIX=/usr/local/haproxy
[root@localhost ~]# make install PREFIX=/usr/local/haproxy
[root@localhost ~]# cd /usr/local/haproxy/
[root@localhost ~]# mkdir etc
[root@localhost ~]# cp /usr/local/src/haproxy-1.4.24/ examples/haproxy.cfg /usr/local/haproxy/etc/
3、 配置Haproxy
[root@localhost ~]# vim /usr/local/haproxy/etc/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
Global
#日志配置
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
#最大连接数
maxconn 4096
#haproxy安装地址
chroot /usr/local/haproxy
uid 99
gid 99
#Haproxy进程进入后台运行
daemon
#debug
#quiet
defaults
log global
#默认的模式mode{tcp|http|health},tcp是4层,http是7层,health只返回OK。
mode http
#采用http日志格式
option httplog
#当服务器负载很高的时候,自动结束当前队列处理比较久的连接。
option abortonclose
option dontlognull
#尝试连接三次就认为无服务器
retries 3
#默认的最大连接数
maxconn 2000
#连接超时
contimeout 5000
#客户端超时
clitimeout 50000
#服务器超时
srvtimeout 50000
#心跳检测超时
timeout check 1000
listen web_proxy 192.168.232.163:80
mode http
#代理的Web连接
server web1 192.168.232.167:80 cookie web1ck check inter 2000 rise 2 fall 5
server web2 192.168.232.168:80 cookie web2ck check inter 2000 rise 2 fall 5
#下面是统计页面的配置:
listen admin_stats
#监听端口
bind 0.0.0.0:1080
#http的7层模式
mode http
option httplog
maxconn 10
#统计页面自动刷新时间
stats refresh 30s
#统计页面的URL
stats uri /stats
stats realm smister
#统计页面用户名和密码设置
stats auth admin:admin
#隐藏统计页面上HAProxy的版本信息
stats hide-version
4、 开始haproxy服务
[root@localhost ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
#查看端口是否开启
[root@localhost ~]# netstat -tunpl | grep 1080
#用客户机测试,输入192.168.232.163 。看是否能代理访问到192.168.232.167 , 192.168.232.168 。例如当前访问到了192.168.232.167 , 到192.168.232.167的机器 , 将其web服务关掉 , 再次访问192.168.232.163 , 看是否顺利转移到192.168.232.168
5、 haproxy配置文件详解
global #log 127.0.0.1 local0 maxconn 65535 chroot /usr/local/haproxy uid 99 gid 99 #maxconn 4096 spread-checks 3 daemon nbproc 1 pidfile /usr/local/haproxy/haproxy.pid defaults log 127.0.0.1 local3 mode http option httplog option httpclose option dontlognull option forwardfor option redispatch retries 10 maxconn 2000 stats uri /haproxy-stats stats auth admin:admin contimeout 5000 clitimeout 50000 srvtimeout 50000 frontend HAProxy bind *:80 mode http option httplog acl cache_domain path_end .css .js .gif .png .swf .jpg .jpeg .ico acl cache_dir path_reg /apping acl cache_jpg path_reg /theme acl bugfree_domain path_reg /bugfree use_backend varnish.offer99.com if cache_domain use_backend varnish.offer99.com if cache_dir use_backend varnish.offer99.com if cache_jpg use_backend bugfree.offer99.com if bugfree_domain default_backend www.offer99.com backend bugfree.offer99.com server bugfree 222.35.135.151:80 weight 5 check inter 2000 rise 2 fall 3 backend varnish.offer99.com server varnish 222.35.135.152:81 weight 5 check inter 2000 rise 2 fall 3 backend www.offer99.com balance source option httpchk HEAD /index.php HTTP/1.0 server web1 222.35.135.154:80 weight 5 check inter 2000 rise 2 fall 3 server web2 222.35.135.155:80 weight 5 check inter 2000 rise 2 fall 3 #我将HAProxy的配置文件正则情况跟大家稍作说明: acl cache_domain path_end .css .js .gif .png .swf .jpg .jpeg .ico #以上语句的作用是将.css和.js以及图片类型文件定义成cache_domain。 acl cache_dir path_reg /apping acl cache_jpg path_reg /theme #以上语句的作用是定义静态页面路径,cache_dir和cache_jpg是自定义的名字。 use_backend varnish.offer99.com if cache_domain use_backend varnish.offer99.com if cache_dir use_backend varnish.offer99.com if cache_jpg #如果满足以上文件后缀名或目录名,则HAProxy将客户端请求定向到后端的varnish缓存服务器varnish.offer99.com上。 acl bugfree_domain path_reg /bugfree use_backend bugfree.offer99.com if bugfree_domain #以上两行代码的作用是将/bugfree定义成名字为bugfree-domain的静态域,即客户端有bugfree的请求,则定向到后端的222.35.135.151机器上。 default_backend www.offer99.com #如果客户端的请求都不满足以上条件,则分发到后端的两台Apache服务器上。
版权声明:未经博主允许不得转载。http://www.smister.com/post-27.html