haproxy 七层 负载均衡

系统:ubuntu12.04

环境:

   client/haproxy 192.168.30.138

   web1        192.168.30.140

   web2        192.168.30.141

软件安装:
一、haproxy/client

1 、下载haproxy软件进行安装

#tar zxvfhaproxy-1.4.22.tar.gz

#makeTARGET=linux26

#make installPREFIX=/usr/local/haproxy

#cd/usr/local/haproxy/

#mkdir etc

#touchhaproxy.cfg

#vimhaproxy.cfg

global

       log 127.0.0.1   local0 info

       maxconn 409600

       chroot /usr/local/haproxy

       uid 99

       gid 99

       daemon

       #nbproc 2

       pidfile /usr/local/haproxy/haproxy.pid

       stats socket    /tmp/haproxy.sock

defaults

       timeout server  150s

       timeout connect 150s

       timeout client  150s

       timeout queue   150s

       option httpclose

       retries 3

       option redispatch

       stats uri /status

       stats auth root:root

       stats refresh 60s

frontend http

       mode http

       #bind-process 4

       maxconn 1000000

       bind *:80

       acl test hdr(Host) -i test.com

       use_backend test.com if test

       acl nihao hdr(Host) -i nihao.com

       use_backend nihao.com if nihao

       #option httplog

       #option dontlognull

backendtest.com

       balance roundrobin

       option httpchk HEAD /index.htmlHTTP/1.0

       mode http

       cookie SERVERID insert indirect

       option abortonclose

               server keep2 192.168.30.140:80

               server web 192.168.30.141:80

backendnihao.com

       balance roundrobin

       option httpchk HEAD /index.htmlHTTP/1.0

       mode http

       cookie SERVERID insert indirect

       option abortonclose

               server keep2192.168.30.140:8080

               server web 192.168.30.141:8080

2、启动haproxy

/usr/local/haproxy/sbin/haproxy�Cf  /usr/local/haproxy/etc/haproxy.cfg

二、web1 web2   安装naginx tomcat

1)、安装配置naginx

#tar zxvfnginx-1.2.4.tar.gz

#cdnginx-1.2.4/

# ./configure--prefix=/usr/local/nginx

#make&& make install

# cd/usr/local/nginx/conf/

# mkdirsites-enabled

#vim/usr/local/nginx/conf/nginx.conf

添加如下行:

include /usr/local/nginx/conf/sites-enabled/*.conf;   //以后多个项目的配置文件可放在如下目录(配置文件要求以conf为结尾)

#修改nginx主页面

#echo"test 140------test------140" > /usr/local/nginx/html/index.html

2)、安装配置tomcat

1安装JDK  java环境
#chmod 755 jdk-6u20-linux-x64.bin
#mv jdk-6u20-linux-x64.bin /usr/local/
#cd /usr/local
#./jdk-6u20-linux-x64.bin
2
修改环境变量
Vim /etc/profile
JAVA_HOME=/usr/local/jdk1.6.0_20
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
3
、解压tomcat
4
、配置/etc/ environment
修改为以下
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
CLASSPATH=$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
JAVA_HOME=/usr/local/jdk1.6.0_20


5
启动tomcat


# tar -zxvf apache-tomcat-5.5.28.tar.gz -C /data/tomcat
# /data/tomcat/bin/startup.sh


6、修改tomcat主页面

        #cd /usr/local/tomcat/webapps/ROOT

        #rm �Crf *

        #echo “testtomcat-----140------------tomcat”  >index.html

重启即可


web2  配置与web1 相同web 网管需要指向 haproxy服务器


三、客户端进行测试

  1 配置hosts

   #vim /etc/hosts

添加如下两行

   192.168.30.138  test.com

192.168.30.138 nihao.com


测试结果 elinks �Cdump http://test.com显示140 141 nginx

             elinks �Cdumphttp://nihao.com显示 140 141 tomcat


你可能感兴趣的:(haproxy,负载,七层)