寂寞大萝卜 发表于 2015-11-20 11:18:10

HAproxy七层负载均衡

haproxy七层负载均衡  实验环境:192.168.1.27 haproxy
  192.168.1.3 WEB1
  192.168.1.4 WEB2
  1. haproxy下载安装
  #wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.19.tar.gz
  #tar zxvfhaproxy-1.4.19.tar.gz
  # cd haproxy-1.4.19
  # make TARGET=linux26
  # make install
  2. 配置
  Vi /etc/haproxy.cfg
  global
  log 127.0.0.1 local0 info #日志
  maxconn 4096 #单个进程的最大连接数
  pidfile /var/run/haproxy.pid
  user haproxy
  group haproxy
  daemon #守护进程运行
  #nbproc 2 #作为守护进程运行时,创建多少个进程
  defaults
  log global
  mode http
  option dontlognull #不记录空连接
  option log-health-checks #记录健康检测日志
  retries 3 #连接失败后的重连次数
  option redispatch #在连接失败或断开情况下,允许当前会话被重新分发
  maxconn 2000 #最大连接数
  timeout connect 5000ms
  timeout client 30000ms
  timeout server 30000ms
  listen stats 192.168.1.27:8888 #haproxy统计页面
  stats refresh 30s
  stats hide-version
  mode http
  transparent
  stats uri / haproxy
  stats realm Haproxy \ statistic
  listen web 0.0.0.0:80
  mode http #http七层负载模式
  balance roundrobin #服务器分配算法
  option tcpka
  option httpchk GET /123.php #用于检查服务是否正常的页面
  cookie SERVERID insert indirect #允许插入serverid到cookie中serverid后面可自定义
  server web1 192.168.1.3:80 weight 1 check inter 5s rise 2 fall 2 cookie A
  server web2 192.168.1.4:80 weight 1 check inter 5s rise 2 fall 2 cookie B
  #后台服务器
  #weight 服务器权重
  #check port 检测端口
  #inter 设置连续的两次健康检测间的时间
  #rise 指定多少次连接成功的健康检测后,可认定该服务可用
  #fall 指定多少次失败的健康检测后,可认定该服务故障
  3. haproxy七层负载功能测试
  以下是网上找的一个session检测页面
  <?php
  session_start();
  $_SESSION['time'] =date(&quot;Y:m:d:H:s&quot;,time());
  echo &quot;time&quot;.&quot;&lt;font color=red>&quot;.$_SESSION['time'].&quot;</font>&quot;.&quot;<br>&quot;;
  echo &quot;ip adress&quot;.&quot;<font color=red>&quot;.$_SERVER['SERVER_ADDR'].&quot;</font>&quot;.&quot;<br>&quot;;
  echo &quot;local adress&quot;.&quot;<font color=red>&quot;.$_SERVER['SERVER_NAME'].&quot;</font>&quot;.&quot;<br>&quot;;
  echo &quot;SESSIONNAME&quot;.&quot;<font color=red>&quot;.session_name().&quot;</font>&quot;.&quot;<br>&quot;;
  echo &quot;SESSIONID&quot;.&quot;<font color=red>&quot;.session_id().&quot;</font>&quot;.&quot;<br>&quot;;
  ?&gt;
  在192.168.1.3和192.168.1.4的web目录中各上传一个该PHP页面后访问:
http://www.linuxso.com/uploads/allimg/120117/2309303000-0.png
  可以看到三次访问结果,访问的真实服务器IP都是192.168.1.4 ,SESSIONID都是dm22o79o2r28guir5loap95ba1。 可以说明haproxy已成功通过cookie保存了SessionID,使得同一客户端每次访问都是同一个Session。
  
  
haproxy通过页面检测服务器和统计  1. 状态查看
  在/etc/haproxy.cfg配置文件中的option httpchk GET /phpmyadmin/index.php这一行即为通过某一页面来检测服务器是否正常。还可以打开http://192.168.1.27:8888/统计面可以看到当前连接数、最大连接数、发送/接收字节、服务器状态等信息。
http://www.linuxso.com/uploads/allimg/120117/23093015B-1.jpg
  2. 测试服务器故障检测功能
  更改检测页面名称,模拟页面无法访问
http://www.linuxso.com/uploads/allimg/120117/23093035b-2.jpg
  统计页面WEB1的Status显示为DOWN,并呈红色预警
http://www.linuxso.com/uploads/allimg/120117/230930M07-3.jpg
  更改检测页面回原名
http://www.linuxso.com/uploads/allimg/120117/230930I52-4.jpg
  统计页面显示WEB1的Status显示为UP
http://www.linuxso.com/uploads/allimg/120117/2309302U7-5.jpg
页: [1]
查看完整版本: HAproxy七层负载均衡