trhhrth 发表于 2014-9-23 09:03:20

haproxy实现动静分离

                      实验环境:
    三台虚拟机:node1:eth0:172.16.103.1(模拟为外网IP),eth1:192.168.1.1(内网IP,与后端RealServer通信使用)

                node2:eth0:192.168.1.2 (提供动态页面内容)

                node3:eth0:192.168.1.3 (提供静态页面内容)

实验简易拓扑:

   
实验过程:
前端HAProxy代理服务器上安装haproxy程序包

1
# yum -y install haproxy




为了能在后端服务器均出现故障时,能提供提示性信息,在haproxy所在主机启动httpd服务,并提供主页文件,内容编辑为:
# vim /etc/httpd/conf/httpd.conf
Listen 8080   #修改一个默认的监听地址,与后端的RealServer不一致,因为haproxy地址监听的通常设置为80,设置这个监听地址的目的就是为了避免在本机不同进程使用同一端口,而产生冲突。
# vim /var/www/html/index.html
Site under Maintaince
RS1主机提供动态页面的访问,安装有httpd和php程序包并提供主页文件


1
2
3
4
5
6
7
8
9
# yum -y install httpd php
# vim /var/www/html/index.php
php Page
   phpinfo();
?>
# vim /var/www/html/index.html#为了之后测试可以在node2这台提供动态页面访问服务的主机上创建一个静态页面文件,与node3上的文件名称相同,但内容不同,在访问相同名称的文件时,可以测试是否能始终将静态内容调度到静态内容服务器node3上。
# vim /var/www/html/index.html
node2.cluster.com




RS2上仅提供静态页面内容,只安装httpd程序

1
2
3
# yum -y install httpd
# vim /var/www/html/index.html
node3.cluster.com




配置前端的haproxy,使得用户请求时,动态页面内容调度至RS1,请求静态内容时会调度到RS2,在配置文件中的定义内容为:

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
    #    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
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
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
    maxconn 6000
    bind :80
    acl url_static path_beg -i /images /stylesheets /videos /css /javascript   #定义访问控制列表
    acl url_static path_end -i .jpg .html .css .js .png .gif#定义访问控制列表,以便在条件匹配时调用
    use_backend static if url_static    #调用之前定义的访问控制列表,之前定义的访问控制列表指定的内容是开头以图片,样式表,视频,css文件及java脚本开头的和以.jpg.html等文件结尾的文件时会使用后端以static定义的服务器组。
    default_backend webservers   #定义默认的访问调度服务器
backend static
    balance roundrobin   
    server static1 192.168.1.3 check port 80#提供静态页面内容的服务器定义在static组中
    server b1 127.0.0.1:8080 backup check port 8080
backend webservers
    balance roundrobin
    server s1 192.168.1.2 check port 80 # 提供动态页面内容的服务器定义在默认的服务器组中
    server b1 127.0.0.1:8080 backup check port 8080   #定义了一个backup server,当后端的各RealServer都出现故障时,会使用haproxy所在的主机提供的web服务来响应,显示服务器在维护中的信息。
    stats enable    #这段内容是定义stats页面,可以方便使用web浏览器以图形化的方式查看后端服务器的工作状态,并可以进行调整
    stats hide-version
    stats uri /hap?stats
    stats scope .
    stats realm HAProxy Statistics
    stats auth admin:admin
    stats admin if TRUE




启动haproxy服务:
# service haproxy start
# netstat -tnlp

配置过程中的日志的启用方式说明:

记录日志:使用配置文件中定义的默认日志记录方式,不过需要启用rsyslog.conf配置文件中的UDP协议,同时要指定haproxy配置文件中定义的默认日志记录文件的位置:

1
2
3
4
5
6
7
# vim /etc/rsyslog.conf #启用下面的内容,同时添加日志记录的位置及facility。
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
local2.*                   /var/log/haproxy.log
# service rsyslog start
# chkconfig rsyslog on




查看rsyslog服务监听的端口,rsyslog工作在514端口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# netstat -unlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
udp      0      0 0.0.0.0:111               0.0.0.0:*                               1304/rpcbind      
udp      0      0 0.0.0.0:631               0.0.0.0:*                               1379/cupsd         
udp      0      0 0.0.0.0:632               0.0.0.0:*                               1304/rpcbind      
udp      0      0 0.0.0.0:514               0.0.0.0:*                               3529/rsyslogd      
udp      0      0 0.0.0.0:44815               0.0.0.0:*                               3306/haproxy      
udp      0      0 0.0.0.0:680               0.0.0.0:*                               1352/rpc.statd      
udp      0      0 0.0.0.0:48834               0.0.0.0:*                               1352/rpc.statd      
udp      0      0 :::111                      :::*                                    1304/rpcbind      
udp      0      0 :::631                      :::*                                    1304/rpcbind      
udp      0      0 :::514                      :::*                                    3529/rsyslogd      
udp      0      0 :::51898                  :::*                                    1352/rpc.statd




查看日志文件中记录的内容:


1
2
3
4
# tail /var/log/haproxy.log
Sep8 13:05:53 localhost haproxy: Proxy main started.
Sep8 13:05:53 localhost haproxy: Proxy static started.
Sep8 13:05:53 localhost haproxy: Proxy webservers started.




实现动静分离时定义的效果:当访问默认主页时,使用默认的backend定义的webserver服务器响应,这个服务器上提供的是php页面文件,也是动态页面的内容:


多次刷新测试后访问的还是同一个页面的内容,如果访问的是静态文件的内容,比如index.html,即使在node2上有相同名称的页面文件,也不会调度到node2上,而是只显示node3上的文件:

实现了动态页面和静态页面分离的效果。
                  

页: [1]
查看完整版本: haproxy实现动静分离