日期:2014-05-16  浏览次数:20662 次

haproxy(一)linux下安装和基本设置
haproxy(一)linux下安装和基本设置

首页
http://haproxy.1wt.eu/

中文资料
http://cn.haproxy.org/download/1.3/doc/configuration-cn.txt

参考WIKI
http://blog.csdn.net/cenwenchu79/archive/2009/08/04/4409343.aspx

一、haproxy的安装
下载得到文件
haproxy-1.3.20.tar.gz
解开压缩
tar zxvf haproxy-1.3.20.tar.gz
移动到工作目录
mv /usr/tmp/haproxy-1.3.20 /usr/local/haprox
转到工作目录
cd /usr/local/haproxy
编译
make TARGET=linux26
安装
make install
执行
haproxy
返回:
HA-Proxy version 1.3.20 2009/08/09
Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
证明安装已经成功

二、haproxy的简单配置
vi haproxy.cfg
文件内容如下:

global
        log 127.0.0.1   local0
        maxconn 4096
        chroot /usr/local/haproxy
        uid 99
        gid 99
        daemon
        nbproc 1
        pidfile /usr/local/haproxy/logs/haproxy.pid
        debug
        #quiet

defaults
        log     127.0.0.1       local3
        mode    http
        option httplog
        option httpclose
        option dontlognull
        option forwardfor
        option redispatch
        retries 2
        maxconn 2000
        balance roundrobin
        stats   uri     /haproxy-stats
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen web_proxy 0.0.0.0:80
        option httpchk GET /demo/ping.jsp

        server s1 127.0.0.1:8080 weight 3 check
        server s2 127.0.0.1:8081 weight 3 check

启动命令
haproxy -f haproxy.cfg
报错:
[root@localhost haproxy]# haproxy -f haproxy.cfg
[ALERT] 236/112258 (5538) : [haproxy.main()] Cannot create pidfile /usr/local/haproxy/logs/haproxy.pid
mkdir /usr/local/haproxy/logs 就可以了

访问:
http://192.168.95.129:80/haproxy-stats

调试启动命令:
haproxy -f haproxy.cfg -d

可以访问页面,但是页面显示两个S1,S2都是DOWN的。

三、配置TOMCAT测试应用
启动TOMCAT6.0-1 打开端口8080
启动TOMCAT6.0-2 打开端口8081
都部署上应用demo.war,里面只有两个文件,一个index.jsp,一个ping.jsp
index就显示各当前是哪个SERVER
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>test demo 1</title>
</head>
<body>
test demo 1
</body>
</html>

PING.JSP就是一个空白页面,让haproxy知道后台服务器还活着就行了

访问http://192.168.95.129/demo就可以随机访问TOMCAT6.0-1和TOMCA