wewe22 发表于 2015-1-12 09:31:42

haproxy的web服务负载均衡、动静分离、 MySQL服务负载均衡、状态监控

实验环境:基于centos6.6
          haproxy-Server:172.16.249.98hostname:node1
          upsteram server1:172.16.249.100 hostname:node2
          upstream server2:172.16.249.99hostname:node3

web服务的负载均衡以及状态监控:
设置记录haproxy日志的文件位置:
node1:

1
2
3
4
5
6
7
8
9
10
#vim /etc/rsyslog.conf
(1)启用UDP:
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
(2)添加记录日志的文件位置
# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
local2.*                                             /var/log/haproxy.log
#service rsyslog restart




node1:

1
2
3
4
#yum install haproxy -y
#cd /etc/haproxy/
#cp haproxy.cfg{,.bak}
#vim haproxy.cfg





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
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                     /var/log/haproxy.log
    #
    log         127.0.0.1 local2   

    chroot      /var/lib/haproxy
    pidfile   /var/run/haproxy.pid
    maxconn   4000
    user      haproxy
    group       haproxy
    daemon

defaults
    mode                  http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries               3
    timeout http-request    10s
    timeout queue         1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check         10s
    maxconn               30000

    listen stats               #此段为设置状态监控
    mode http
    bind :8888               #生产环境中为保证安全性,为设置特殊端口,例如:8888
    stats enable                  
    stats hide-version                           
    stats uri   /haproxyadmin?stats
    stats realm   Haproxy\ Statistics
    stats auth    admin:admin      #设置用户名以及密码
    stats admin if TRUE

frontend http-in
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture requestheader Host len 20
    capture requestheader Referer len 60
    default_backend servers

frontend healthcheck
    bind :1099
    mode http
    option httpclose
    option forwardfor
    default_backend servers

backend servers
balance roundrobin
    server node2 172.16.249.100:80 check maxconn 2000
    server node3 172.16.249.99:80 check maxconn 2000





1
#serivce haproxy start




node2,node3节点分别建立测试页面,并启动httpd服务;


1
2
3
4
5
6
node2:#vim /var/www/html/index.html
      <h1> httpd on node2 </h1>
   #service httpd start
node3:#vim /var/www/html/index.html
          <h1> httpd on node3 </h1>
   #service httpd start




打开浏览器:172.16.249.98:



打开浏览器:172.16.249.98:8888/haproxy?stats



动静分离配置:
#vim /etc/haproxy/haproxy.cfg


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
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                     /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile   /var/run/haproxy.pid
    maxconn   4000
    user      haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats


defaults
    mode                  http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries               3
    timeout http-request    10s
    timeout queue         1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check         10s
    maxconn               3000

frontendmain *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js .html .txt .htm

    use_backend static          if url_static
    default_backend             app

backend static
    balance   roundrobin
    server      node2 172.16.249.100:80 check

backend app
    balance   roundrobin
    server node3 172.16.249.99:80 check





1
#serivce haproxy start    #启动haproxy服务




所有静态页面请求代理至node2节点;所有动态页面请求代理至node3节点。

1
2
3
4
5
6
7
8
node2:#vim /var/www/html/index.html
       <h1> httpd on node2 </h1>
   #service httpd start
node3:#vim /var/www/html/index.php
       <?php
          phpinfo();   
       ?>
   #service httpd start




打开浏览器:172.16.249.98/index.html

打开浏览器:172.16.249.98/index.php

负载均衡mysql服务:
node1:#yum install mysql -y
       #vim /etc/haproxy/haproxy.cfg

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
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                     /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile   /var/run/haproxy.pid
    maxconn   4000
    user      haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

defaults
    mode                  tcp
    log                     global
    option                  dontlognull
    option http-server-close
    #option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries               3
    timeout http-request    10s
    timeout queue         1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check         10s
    maxconn               3000

frontend bd
      bind :3306
      default_backend dbsrvs

backend dbsrvs
      balance leastconn
      server node2 172.16.249.100:3306 check
      server node3 172.16.249.99:3306 check
listen stats
   bind :80
   mode http
   stats enable
   stats uri /haproxy?stats
   stats realm haproxy\ stats
   stats auth admin:admin   
   stats admin if TRUE




分别在node2、node3节点建立数据库,并授权可远程连接:
node2:#yum install mysql-server -y
   #serivce mysqld start
   #mysql

node3:#yum install mysql-server -y
   #serivce mysqld start
   #mysql

node1:#service haproxy restart   #重启服务,配置文件才能生效

   #mysql -uroot -h172.16.249.98 -pmageedu


检测:
node2:#mysql -uroot -pmageedu


node3:#mysql -uroot -pmageedu;
      
说明:由于我们在node1节点调用了数据库并创建了表,node2节点查看有此表,node3节点无此表,因此我们刚才的测试中调用了node2节点的mysql数据库而非node3节点。
页: [1]
查看完整版本: haproxy的web服务负载均衡、动静分离、 MySQL服务负载均衡、状态监控