HAproxy實現從入門到進階

HAproxy 

LB Cluster (負載均衡 集羣)

四層

lvs、nginx(stream模塊)、haproxy

七層

http:nginx(http、ngx_http_upstream)、haproxy(mode http),httpd,ats、perlbal,pound

官方文檔:http://cbonte.github.io/haproxy-dconv/

使用yum安裝 haproxy 光盤收錄的版本即可

以HAProxy 1.5爲例


主程序:/usr/sbin/haproxy

主配置文件:/etc/haproxy/haproxy.cfg


配置端分爲2段·

global:全局配置段

進程及安全配置相關的參數

性能調整相關參數

Debug參數

proxies:代理配置段

default:frontend,listen,backend提供默認配置:

其中

frontend:前端,相當於nginx,server{ }

backend :後端,相當於nginx,upstream { }

listen:  同時用於前端和後端


實現1.1使用haproxy配製簡單的負載均衡集羣


這裏使用三臺主機

172.18.10.10,使用yum安裝httpd,並配置基本網頁內容,啓動httpd服務

[root@localhost ~]# yum install -y httpd

[root@localhost ~]# vim /var/www/html/index.html

<h1>Server1 172.18.10.10</h1>

root@localhost ~]# ss -tnl

State       Recv-Q Send-Q                               Local Address:Port                                 Peer Address:Port 

LISTEN      0      128                                             :::80                                             :::*     

LISTEN      0      128                                             :::22                                             :::*     

LISTEN      0      128                                              *:22                                              *:*     

LISTEN      0      100                                            ::1:25                                             :::*     

LISTEN      0      100                                      127.0.0.1:25

172.18.10.11,使用yum安裝httpd,並配置基本網頁內容,啓動httpd服務

[root@localhost ~]# yum install -y httpd

[root@localhost ~]# vim /var/www/html/index.html

<h1>Server1 172.18.10.11</h1>

root@localhost ~]# ss -tnl

State       Recv-Q Send-Q                               Local Address:Port                                 Peer Address:Port 

LISTEN      0      128                                             :::80                                             :::*     

LISTEN      0      128                                             :::22                                             :::*     

LISTEN      0      128                                              *:22                                              *:*     

LISTEN      0      100                                            ::1:25                                             :::*     

LISTEN      0      100                                      127.0.0.1:25


172.18.200.100,使用yum安裝haproxy,編輯haproxy.cfg文件

[root@localhost haproxy]# vim haproxy.cfg

將frontend斷和backend段做如下修改

frontend  web

        bind *:80

        default_backend         websrvs

backend  websrvs

        balance roundrobin

        server srv1 172.18.10.10:80 check

        server srv2 172.18.10.11:80 check

啓動haproxy服務

[root@localhost haproxy]# service haproxy start


使用curl命令訪問測試

[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100;done

<h1>Server1 172.18.10.10</h1>

<h1>Server2 172.18.10.11</h1>

<h1>Server1 172.18.10.10</h1>

<h1>Server2 172.18.10.11</h1>

<h1>Server1 172.18.10.10</h1>

<h1>Server2 172.18.10.11</h1>

<h1>Server1 172.18.10.10</h1>

<h1>Server2 172.18.10.11</h1>

<h1>Server1 172.18.10.10</h1>

<h1>Server2 172.18.10.11</h1>

結論,實現haproxy簡單的LB Cluster。調度規則默認爲rr(輪詢)調度


實驗1.2在原來基礎上添加mysql服務,並實現負載均衡調度


首先在兩臺後端backend服務器上安裝mysql,並啓動服務,配置mysql相關參數,測試mysql鏈接是否正常

[root@BYQ ~]#service mysqld start

Starting mysqld:                                           [  OK  ]

mysql> select user();

+----------------+

| user()         |

+----------------+

| root@localhost |

+----------------+

1 row in set (0.00 sec)

mysql> grant all on mydb.* to 'test'@'%' identified by 'testpass';

Query OK, 0 rows affected (0.00 sec)


在客戶端主機上測試mysql的的鏈接是否正常

[root@localhost ~]# mysql -utest -ptestpass -h172.18.10.10

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.1.73 Source distribution


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> 


[root@localhost ~]# mysql -utest -ptestpass -h172.18.10.11

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.1.73 Source distribution


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> 


兩臺後端主機mysql連接均正常


接下來在haproxy服務器端進行配置                                                                             

[root@localhost ~]# cd /etc/haproxy/

[root@localhost haproxy]# ls

haproxy.cfg  haproxy.cfg.bak

[root@localhost haproxy]# cp haproxy.cfg{,.web}

[root@localhost haproxy]# ls

haproxy.cfg  haproxy.cfg.bak  haproxy.cfg.web

[root@localhost haproxy]# vim haproxy.cfg

frontend  web

        mode    tcp

        bind *:3306

        default_backend         websrvs

backend  websrvs

        balance leastconn

        server mysql1 172.18.10.10:3306 check

        server mysql2 172.18.10.11:3306 check

簡單配置完畢



實驗1.3配置haproxy rslog

[root@localhost ~]# vim /etc/rsyslog.conf


# Save boot messages also to boot.log

local7.*                                                /var/log/boot.log

local2.*                                                /var/log/haproxy.log


# Provides UDP syslog reception ##表示載入UDP模式,並監聽在514端口

$ModLoad imudp

$UDPServerRun 514


保存退出並重啓動rsyslog服務,查看日誌監聽端口是否啓動

[root@localhost ~]# service rsyslog restart

Shutting down system logger:                               [  OK  ]

Starting system logger:                                    [  OK  ]

[root@localhost ~]# ss -unl

State      Recv-Q Send-Q                    Local Address:Port                      Peer Address:Port 

UNCONN     0      0                                     *:514                                  *:*     

UNCONN     0      0                                     *:57263                                *:*     

UNCONN     0      0                                    :::514                                 :::*



實驗1.4配置source算法實現hash-type綁定

編輯haproxy.cfg,更改添加如下配置

backend  websrvs

        balance source

        server srv1 172.18.10.10:80 check

        server srv2 172.18.10.11:80 check

        hash-type map-based

重啓服務

[root@localhost haproxy]# service haproxy restart

Stopping haproxy:                                          [  OK  ]

Starting haproxy:                                          [  OK  ]

在客戶端測試

[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100;done

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 2 172.18.10.11</h1>


實驗1.5配置uri算法提高命中。綁定一致性hash consistent

編輯haproxy.cfg文件

backend  websrvs

        balance uri

        server srv1 172.18.10.10:80 check

        server srv2 172.18.10.11:80 check

        hash-type consistent

保存退出重啓服務,結論不管哪臺服務器,只要uri相同,便訪問同一個後端主機,若uri不同也另當別論

例如隨機在兩臺後端主機生成20個頁面文件

[root@localhost ~]# for i in {1..20};do echo "Test Page $i (BE 1)" > /var/www/html/test$i.html;done

[root@localhost ~]# for i in {1..20};do echo "Test Page $i (BE 2)" > /var/www/html/test$i.html;done

在客戶端請求,則發現test1在BE1,而test2在BE2

[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test1.html;done

Test Page 1 (BE 1)

Test Page 1 (BE 1)

Test Page 1 (BE 1)

Test Page 1 (BE 1)

Test Page 1 (BE 1)

Test Page 1 (BE 1)

Test Page 1 (BE 1)

Test Page 1 (BE 1)

Test Page 1 (BE 1)

Test Page 1 (BE 1)

[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test2.html;done

Test Page 2 (BE 1)

Test Page 2 (BE 1)

Test Page 2 (BE 1)

Test Page 2 (BE 1)

Test Page 2 (BE 1)

Test Page 2 (BE 1)

Test Page 2 (BE 1)

Test Page 2 (BE 1)

Test Page 2 (BE 1)

Test Page 2 (BE 1)

因此得出結論和綁定的客戶端沒關係,只和綁定的uri有關係,因此能夠極大的提高緩存命中率


實驗1.6採用roundrobin調度規則,且增加權重 進行測試

編輯haproxy.cfg


backend  websrvs

        balance         roundrobin

        server srv1 172.18.10.10:80 check weigth 2

        server srv2 172.18.10.11:80 check weigth 1

        hash-type       consistent

保存並退出

重啓服務

[root@localhost haproxy]# service haproxy restart

Stopping haproxy:                                          [  OK  ]

Starting haproxy:                                          [  OK  ]

在客戶端訪問同一個URL

[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test5.html;done

Test Page 5 (BE 2)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 2)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 2)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 2)

將其中一個服務器下線,等3秒 實現fall (默認三秒)

[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test5.html;done

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

再將下線的服務器上線,等2秒,實現rise (默認兩秒)恢復輪詢

[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/test5.html;done

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 2)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 2)

Test Page 5 (BE 1)

Test Page 5 (BE 1)

Test Page 5 (BE 2)

Test Page 5 (BE 1)

結論:實現健康狀態檢查

也可以自己設置 check inter(時間間隔) rise 次數 fall 次數 和 maxconn(最大併發連接數)、 以及backlog(後援隊列不指定值的話,就用maxconn的值代替),如下所示

backend  websrvs

        balance         roundrobin

        server srv1 172.18.10.10:80 check inter 1000 rise 1 fall 2 maxconn 2000 weight 2

        server srv2 172.18.10.11:80 check weight 1

        hash-type       consistent


實驗1.7,將第一個後端主機標記爲backup或者disabled,在第二個後端主機上使用redir重定向到百度的url上去


backend  websrvs

        balance         roundrobin

        server srv1 172.18.10.10:80 check inter 1000 rise 1 fall 2 maxconn 2000 weight 2 disabled

        server srv2 172.18.10.11:80 check weight 1 redir http://www.baidu.com/

        hash-type       consistent


訪問172.18.200.100.頁面跳轉至百度主頁


實驗1.8 開啓haproxy stats 頁面,並且調試各參數

開啓stats頁面,在backend 配置段添加 stats enable

backend  websrvs

        balance         roundrobin

        server srv1 172.18.10.10:80 check inter 1000 rise 1 fall 2 maxconn 2000 weight 2 backup

        server srv2 172.18.10.11:80 check weight 1 redir http://www.baidu.com/

        hash-type       consistent

        stats enable

重啓服務

這樣便開啓了stats頁面

其中stats頁面是有默認值的,默認值如下

- stats uri   : /haproxy?stats 

- stats realm : "HAProxy Statistics"

- stats auth  : no authentication


- stats scope : no restriction

在瀏覽器輸入http://172.18.200.100/haproxy?stats,則登錄狀態頁面顯示

修改stats uri的默認值

frontend  web

        bind *:80

        default_backend         websrvs


        stats enable

        stats uri /haadim?admin


backend  websrvs

        balance         roundrobin

        server srv1 172.18.10.10:80 check inter 1000 rise 1 fall 2 maxconn 2000 weight 2 

        server srv2 172.18.10.11:80 check weight 1 

        hash-type       consistent

保存退出重啓服務

在瀏覽器輸入http://172.18.200.100/haadmin?admin,才能再次顯示狀態頁面


實驗1.9 爲stats頁面添加用戶名密碼訪問認證

frontend  web

        bind *:80

        default_backend         websrvs


        stats enable

        stats uri       /haadmin?admin

        stats realm     "Stats\ Web"        ##(提示標題)

        stats auth      admin1:admin1

        stats auth      admin2:admin2

        stats auth      admin3:admin3

保存退出並重啓服務

在瀏覽器輸入http://172.18.200.100/haadmin?admin,需要輸入用戶名和密碼才能使用

而且可以啓用登錄用戶管理

frontend  web

        bind *:80

        default_backend         websrvs


        stats enable

        stats uri       /haadmin?admin

        stats realm     "Stats\ Web"

        stats auth      admin1:admin1

        stats auth      admin2:admin2

        stats auth      admin3:admin3

        stats admin if TRUE

表示如果驗證成功,就可以一直設置相應的參數

保存,並且退出

重啓haproxy服務

刷新剛纔的頁面,則會現在後端的模塊下面顯示可操作的會話框

Choose the action to perform on the checked servers :     Apply


在Apply的下拉框中會有許多選項,可以控制後端的服務器狀態


也可以直接專門定義一個端口,讓stats監聽在一個端口上,以後只能通過該端口訪問stats頁面

frontend  web

        bind *:80

        default_backend         websrvs

listen stats :10086

        stats enable

        stats uri       /haadmin?admin

        stats realm     "Stats\ Web"

        stats auth      admin1:admin1

        stats auth      admin2:admin2

        stats auth      admin3:admin3

        stats admin if TRUE

在瀏覽器輸入http://172.18.200.100:10086/haadmin?admin 通過用戶驗證,訪問


還可以隱藏haproxy版本信息

stats hide-version

還可以自動刷新,以及顯示具體信息

stats refresh

stats show-desc

stats show-legends

stats show-node


實驗2.0,顯示前端最大連接數maxconn

frontend  web

        bind *:80

        maxconn 4000

        default_backend         websrvs


實驗2.1,代理ssh服務 實現mode tcp模式的驗證

backend app

    balance     roundrobin

    server  app1 127.0.0.1:5001 check

    server  app2 127.0.0.1:5002 check

    server  app3 127.0.0.1:5003 check

    server  app4 127.0.0.1:5004 check

listen sshsrvs :10088

        mode            tcp

        maxconn         20

        balance         leastconn

        server          sshsrv1 172.18.10.10:22 check

        server          sshsrv2 172.18.10.11:22 check

在客戶端使用[root@localhost ~]# ssh -p 10088 [email protected]

刷新頁面

發現在頁面中sshsrvs欄中,sshsrv1頁面有連接處理顯示,說明代理成功


實驗2.2,代理mysql,實現mode tcp模式的驗證

listen sshsrvs :3306

        mode            tcp

        maxconn         20

        balance         leastconn

        server          sshsrv1 172.18.10.10:3306 check

        server          sshsrv2 172.18.10.11:3306 check

查看端口是否監聽

[root@localhost haproxy]# ss -tnl

State      Recv-Q Send-Q                      Local Address:Port                        Peer Address:Port 

LISTEN     0      128                                     *:10086                                  *:*     

LISTEN     0      20                                      *:3306                                   *:*     

LISTEN     0      128                                     *:80                                     *:*     

LISTEN     0      128                                    :::22                                    :::*     

LISTEN     0      128                                     *:22                                     *:*     

LISTEN     0      100                                   ::1:25                                    :::*     

LISTEN     0      100                             127.0.0.1:25     

在客戶端測試連接mysql

[root@localhost ~]# mysql -utest -ptestpass -h172.18.200.100

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.1.73 Source distribution


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> 

結論:代理成功 ,實現mode tcp模式的驗證



實驗2.3 實現基於ookie的粘性處理,即基於cookie的session sticky的實現:


backend  websrvs

        balance         roundrobin

        cookie          WEBSRV insert nocache indirect

        server          srv1 172.18.10.10:80 check cookie web1

        server          srv2 172.18.10.11:80 check cookie web2

        hash-type       consistent

使用瀏覽器訪問http://172.18.200.100/,開啓F12,查看瀏覽器記錄的Request Headers,如下

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Accept-Encoding:gzip, deflate, sdch

Accept-Language:zh-CN,zh;q=0.8

Authorization:Basic YWRtaW4zOmFkbWluMw==

Cache-Control:max-age=0

Connection:keep-alive

Cookie:WEBSRV=web1

Host:172.18.200.100

If-Modified-Since:Wed, 03 May 2017 06:23:57 GMT

If-None-Match:"1e0599-27-54e98b3828057"

Upgrade-Insecure-Requests:1

User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36


可以看出Cookie:WEBSRV=web1,已經基於cookie黏性綁定web1服務器,因此再次不敢訪問多少次都是web1


實驗2.4 實現option forward發往後端主機的請求報文中添加“X-Forwarded-For”首部


首先進入後端服務器,修改httpd關於日誌段的配置

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

從啓httpd服務,再次在客戶端訪問

[root@localhost ~]# for i in {1..10};do curl http://172.18.200.100/index.html;done

<h1>Backend Server 1 172.18.10.10</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 1 172.18.10.10</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 1 172.18.10.10</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 1 172.18.10.10</h1>

<h1>Backend Server 2 172.18.10.11</h1>

<h1>Backend Server 1 172.18.10.10</h1>

<h1>Backend Server 2 172.18.10.11</h1>


並查看訪問日誌,

[root@localhost ~]# tail /var/log/httpd/access_log 

172.18.249.57 - - [10/May/2017:08:08:22 +0800] "GET /index.html HTTP/1.1" 200 39 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

172.18.249.57 - - [10/May/2017:08:08:22 +0800] "GET /index.html HTTP/1.1" 200 39 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"


則在日誌中會顯示真實訪問的客戶端地址


實驗2.5 實現rspadd <string> [{if | unless} <cond>]  向響應報文首部添加一個指定的首部

frontend  web

        bind *:80

        rsadd X-Via:\ HAProxy

        maxconn 4000

        default_backend         websrvs

從啓haproxy服務

刷新瀏覽器

在響應報文首部發現添加的指定首部

Response Headers

Date:Wed, 03 May 2017 16:24:04 GMT

ETag:"1e0599-27-54e98b3828057"

Server:Apache/2.2.15 (CentOS)

Vary:Accept-Encoding

X-Via:HAProxy


實驗2.6 實現rspidel <search> [{if | unless} <cond>]  (ignore case) 不區分大小寫

frontend  web

        bind *:80

        rspadd X-Via:\ HAProxy

        rspidel Server.*

        maxconn 4000

        default_backend         websrvs


Date:Wed, 03 May 2017 16:28:59 GMT

ETag:"1e0599-27-54e98b3828057"

Vary:Accept-Encoding

X-Via:HAProxy


對比之前,server已經被刪除


實驗2.7 log

[root@localhost haproxy]# tail /var/log/haproxy.log 

May 10 12:46:47 localhost haproxy[6628]: 172.18.254.240:59220 [10/May/2017:12:46:47.769] web websrvs/srv1 0/0/0/1/1 304 173 - - --VN 1/1/0/1/0 0/0 "GET / HTTP/1.1"



實驗2.8 實現內容 類型壓縮compression

frontend  web

        bind *:80

        rspadd X-Via:\ HAProxy

        rspidel Server.*

        maxconn 4000

        default_backend         websrvs

        compression type text/html

        compression algo gzip

瀏覽器訪問http://172.18.200.100/test12.html

在響應報文首部response headers

Accept-Ranges:bytes

Content-Encoding:gzip

Content-Length:40

Content-Type:text/html; charset=UTF-8

Date:Wed, 03 May 2017 16:46:10 GMT

ETag:"1e059d-14-54e9c272c1b5f"

Last-Modified:Wed, 03 May 2017 10:31:02 GMT

Vary:Accept-Encoding

X-Via:HAProxy

發現內容壓縮,且格式爲gzip


實驗2.9 對後端服務器http協議的健康狀態監測

backend  websrvs

        balance         roundrobin

        cookie          WEBSRV insert nocache indirect

        server          srv1 172.18.10.10:80 check cookie web1

        server          srv2 172.18.10.11:80 check cookie web2

        option httpchk /test20.html

        hash-type       consistent

在stats頁面可以查看,正常通過檢測


實驗3.0 http-check檢測

backend  websrvs

        balance         roundrobin

        cookie          WEBSRV insert nocache indirect

        server          srv1 172.18.10.10:80 check cookie web1

        server          srv2 172.18.10.11:80 check cookie web2

        option           httpchk /test20.html

        http-check      expect rstatus ^2

        hash-type       consistent

在stats頁面可以查看,正常通過檢測



實驗3.1 實現基於acl的各種訪問控制

acl只檢查不控制,控制還需其他條件實現

acl要先配置,再調用


frontend  web

        acl invalid_src src 172.18.254.240

        block if invalid_src

保存退出重啓服務,使用瀏覽器訪問http://172.18.200.100/

403 Forbidden


Request forbidden by administrative rules.


客戶端地址正好爲172.18.254.240,因此訪問被拒絕


而且403頁面可以重定向到我們自己定義的錯誤頁面去,操作如下

[root@localhost haproxy]# mkdir /etc/haproxy/errorfiles

[root@localhost haproxy]# vim /etc/haproxy/errorfiles/403.html

<h1>OoOo,VIP Source</h1>


編輯haproxy配置文件

frontend  web

        acl invalid_src src 172.18.254.240

        block if invalid_src

        errorfile 403 /etc/haproxy/errorfiles/403.html

保存退出,並重啓haproxy服務

使用瀏覽器訪問http://172.18.200.100/,顯示內容如下

OoOo,VIP Source


或者使用errorloc重定向uri到百度

frontend  web

        acl invalid_src src 172.18.254.240

        block if invalid_src

        errorloc 403 http://www.baidu.com

使用瀏覽器訪問http://172.18.200.100/,顯示內容如下


百度主頁。。。。。


如果acl是 curl_agent,則使用如下的服務器172.18.10.11:8080

frontend  web

        acl invalid_src src 172.18.254.240

        block if invalid_src

        errorloc 403 http://www.baidu.com

        acl curl_agent hdr_sub(User-Agent) -i curl

        use_backend curlbe if curl_agent

        bind *:80

        rspadd X-Via:\ HAProxy

        rspidel Server.*

        maxconn 4000

        default_backend         websrvs

        compression type text/html

        compression algo gzip


backend curlbe

        balance roundrobin

        server curlsrv1 172.18.249.57:80 check

並在後端172.18.249.57上開啓80端口

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 

[root@localhost ~]# service httpd restart


打開瀏覽器,出輸入http://172.18.200.100:10086/haadmin?admin,輸入用戶名和密碼驗證

發現curlbe模塊正常運行,並且通過四層檢測

使用curl命令

[root@localhost ~]# curl http://172.18.249.57

<h1>Curl server 172.18.249.57</h1>



實驗3.2實現簡單的動靜分離

在172.18.249.57端。使用yum安裝php

在將編輯index.php頁面

<h1>Curl server 172.18.249.57</h1>

<?php

        phpinfo();

?>


使用瀏覽側訪問測試頁面,成功顯示php信息

在haproxy端配置haproxy.cfg文件。添加如下內容

frontend  web

#       acl invalid_src src 172.18.254.240

#       block if invalid_src

        errorloc 403 http://www.baidu.com

#       acl curl_agent hdr_sub(User-Agent) -i curl

#       use_backend curlbe if curl_agent

        acl phpapp path_end -i .php ##定義acl條件

        use_backend dynsrvs if phpapp ##定義使用的後端服務標識

        bind *:80

        rspadd X-Via:\ HAProxy

        rspidel Server.*

        maxconn 4000

        default_backend         websrvs

        compression type text/html

        compression algo gzip


#backend curlbe

#       balance roundrobin

#       server curlsrv1 172.18.249.57:80 check

backend dynsrvs ##定義後端的代理服務及標識

        balance source

        server dynsrv1 172.18.249.57:80 check

保存並退出,重啓服務,

使用瀏覽器分別訪問

http://172.18.200.100/index.php

Curl server 172.18.249.57


PHP Logo

PHP Version 5.3.3


http://172.18.200.100/test1.html


Test Page 1 (BE 1)


實現簡單的動靜分離





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章