Haproxy配置http模式负载均衡

原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://jerry12356.blog.51cto.com/4308715/1858239

环境准备:

主机名 角色 IP地址
mylinux1.contoso.com haproxy服务器

eth0:192.168.100.121

eth1:172.16.100.121

mylinux3.contoso.com web服务器1 eth0:192.168.100.181
mylinux4.contoso.com web服务器2 eth0:192.168.100.182

一、准备工作

在mylinux3和mylinux4上安装http服务,并启动。

1
2
yum -y  install  httpd
/etc/init .d /httpd  start

使用浏览器访问,保证服务正常。

wKiom1fuhQmh288NAADy93YhLng059.png-wh_50

wKiom1fuhQuxrI8JAADzYvilmMU794.png-wh_50

二、修改haproxy配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[root@mylinux1 conf] # cp haproxy.cfg haproxy.cfg.bak
[root@mylinux1 conf] # vi haproxy.cfg
[root@mylinux1 conf] # cat 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:514  local0  warning
         pidfile  /usr/local/haproxy/var/run/haproxy .pid
         daemon
         maxconn 4096
         chroot  /usr/local/haproxy/var/chroot
         user haproxy
         group haproxy
         nbproc 1
         
defaults
         log     global
         mode    http     #默认的模式{tcp|http|health},tcp是4层,http是7层,health只会返回OK  
         retries 3
         option  httplog    #日志类别,采用httplog
         option  httpclose      #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现  
         option  dontlognull    #不记录健康检查日志信息
         option  forwardfor     #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip  
         option  redispatch     #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
         maxconn 2000
         balance roundrobin     #设置默认负载均衡方式,轮询方式
         timeout connect 5000
         timeout client  50000
         timeout server  50000
         
listen  haproxy_stats
         bind   *:8000     #绑定到本地所有IP地址的8000端口上
         mode   http       #http的7层模式
         option httplog    #采用http日志格式
         maxconn 20        #默认的最大连接数
         stats  enable      #启用状态监控
         stats refresh 30s             #监控页面自动刷新时间
         stats uri  /haproxy_status     #监控页面url
         stats auth admin:123456       #设置监控页面的用户名和密码,可以设置多个用户名
         stats hide-version            #隐藏监控页面上的Haproxy版本信息
         
listen  websites
         bind  192.168.100.121:80      #绑定到192.168.100.121上的80端口
         timeout  server  15s
         timeout  connect 30s
         server  mylinux3  192.168.100.181:80 check port 80 inter 2000 fall 3  #检测健康端口80,检测心跳频率是2000ms,失败3次则认为服务器不可用
         server  mylinux4  192.168.100.182:80 check port 80 inter 2000 fall 3

三、启动haproxy并测试

1、启动haproxy

1
2
3
4
5
6
[root@mylinux1 conf] # /usr/local/haproxy/sbin/haproxy -f haproxy.cfg -c
Configuration  file  is valid
[root@mylinux1 conf] # /usr/local/haproxy/sbin/haproxy -f haproxy.cfg -D
[root@mylinux1 conf] # ps -ef|grep haproxy
haproxy    2575      1  0 23:24 ?        00:00:00  /usr/local/haproxy/sbin/haproxy  -f  /usr/local/haproxy/conf/haproxy .cfg
root       2584   1015  0 23:45 pts /1     00:00:00  grep  haproxy

2、测试haproxy监控页面

wKioL1fuiWvBVDPpAAEtqdh0S0k694.png-wh_50

wKiom1fuiXHyE2qhAAIfxrAK7KU014.png-wh_50

3、web访问测试

wKiom1fujHnQT3jOAAD9Y4Whgq0724.png-wh_50

wKioL1fujHywU8FkAAECdwUV8HM915.png-wh_50

4、后端节点负载测试

首先使用curl测试一下获取网页内容:

[root@mylinux1 conf]# curl -s http://192.168.100.121

<html>

<head>

   <title>Web1</title>

</head>


<body>

   <p align="center">

      <span style="color:blue;font-size:24px">Web Site 1</span>

   </p>

   <hr />

   <h1 align="center">mylinux3.contoso.com</h1>

</body>


</html>

[root@mylinux1 conf]# curl -s http://192.168.100.121

<html>

<head>

   <title>Web2</title>

</head>


<body>

   <p align="center">

      <span style="color:blue;font-size:24px">Web Site 2</span>

   </p>

   <hr />

   <h1 align="center">mylinux4.contoso.com</h1>

</body>


</html>

然后使用for循环测试100次,并将100次的结果全部追加到一个文件中去,最后检查每个节点出现的次数:

1
2
3
4
5
[root@mylinux1 conf] # for i in {1..100};do curl http://192.168.100.121/ >>/tmp/webtest.txt;done
[root@mylinux1 conf] # grep mylinux3.contoso.com /tmp/webtest.txt |wc -l
50
[root@mylinux1 conf] # grep mylinux4.contoso.com /tmp/webtest.txt |wc -l
50

结果是mylinux3和mylinux4都访问了50次,证明轮询方式下负载是一样的。

四、haproxy启动停止脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
#
###################################################
#chkconfig: 2345 20 70
#description: Start and stop haproxy service.
#Author: Jerry Zhao  QQ:1217406852
#Date: 2016-09-30 19:46:42 Friday
###################################################
 
BASE= /usr/local/haproxy
PROG=$BASE /sbin/haproxy
PIDFILE=$BASE /var/run/haproxy .pid
CONFFILE=$BASE /conf/haproxy .cfg
RUNNING_STATUS=` ps  -ef| grep  "haproxy -f" | egrep  - v  grep | wc  -l`
start(){ 
     RUNNING_STATUS=` ps  -ef| grep  "haproxy -f" | egrep  - v  grep | wc  -l`
     if  [ $RUNNING_STATUS - ge  1 ]; then
         echo  "Haproxy is already running! Exit now."
         exit  1
     else
         $PROG -f $CONFFILE
            [ $? - eq  0 ] &&  echo  "Start haproxy successful."  ||  echo  "Start haproxy failed."
     fi
}
stop(){
     if  [ $RUNNING_STATUS -lt 1 ]; then
         echo  "Haproxy is not running. Stop haproxy failed!"
     else
         kill  -9 $( cat  $PIDFILE)
            [ $? - eq  0 ] &&  echo  "Stop haproxy successful."  ||  echo  "Stop haproxy failed."
         rm  -rf $PIDFILE
     fi
}
reload(){
      if  [ ! -f $PIDFILE ]; then
         echo  "No pid file found. Maybe you need to check haproxy status first."
         exit  1
      else
         $PROG -f $CONFFILE -sf $( cat  $PIDFILE)
      fi
      
}
status(){
      if  [ $RUNNING_STATUS - ge  1 ]; then
         PID_NUM=` cat  $PIDFILE`
         echo  "Haproxy (pid  $PID_NUM) is running..."
      else
         echo  "Haproxy is stopped."
      fi
}
check(){
      $PROG -f $CONFFILE -c
}
case  $1  in        
      start)
          start
      ;;
      stop)
          stop
      ;;
      restart)
          stop
          start
      ;;
      reload)
          reload
      ;;
      status)
          status
      ;;
      check)
          check
      ;;
      *)
          echo  "USAGE: $0 start|stop|restart|reload|check ."
          exit  1
      ;;
esac


测试一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@mylinux1 ~] # cp haproxy /etc/init.d/
[root@mylinux1 ~] # ll /etc/init.d/haproxy 
-rwxr-xr-x 1 root root 1567 Oct  1 00:06  /etc/init .d /haproxy
[root@mylinux1 ~] # service haproxy status
Haproxy (pid  2575) is running...
[root@mylinux1 ~] # service haproxy stop
Stop haproxy successful.
[root@mylinux1 ~] # service haproxy start
Start haproxy successful.
[root@mylinux1 ~] # service haproxy restart
Stop haproxy successful.
Start haproxy successful.
[root@mylinux1 ~] # service haproxy status
Haproxy (pid  2796) is running...
[root@mylinux1 ~] # service haproxy reload
[root@mylinux1 ~] # service haproxy status
Haproxy (pid  2822) is running...
[root@mylinux1 ~] # service haproxy check
Configuration  file  is valid


本文出自 “IT小二郎” 博客,请务必保留此出处http://jerry12356.blog.51cto.com/4308715/1858239

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值