1.HTML-A & HTML-B
[root@localhost ~]# yum install httpd -->分別創建測試頁面index.html,開啓服務
2.PHP-A & php-B
[root@localhost ~]# yum install httpd php -->分別創建測試頁面 index.php ,開啓服務
3.安裝HAproxy
[root@localhost ~]# tar xf haproxy-1.4.20.tar.gz
[root@localhost ~]# cd haproxy-1.4.20
[root@localhost haproxy-1.4.20]# make TARGET=linux26 PREFIX=/usr/local/haproxy install
4.生成HAproxy配置文件
[root@localhost haproxy-1.4.20]# cd /usr/local/haproxy/
[root@localhost haproxy]# mkdir conf logs
[root@localhost haproxy]# cd conf/
[root@localhost conf]# vim haproxy.cfg
global
log 127.0.0.1 local3 info -->日誌服務器
maxconn 4096 -->單個進程的最大併發連接數
uid nobody -->用戶身份
gid nobody -->組身份
daemon -->守護進程方式後臺運行
nbproc 1 -->工作進程數量
defaults
log global
mode http -->工作模式 http ,tcp 是 4 層,http是 7 層
maxconn 2048 -->最大連接數
retries 3 -->3次連接失敗就認爲服務器不可用
optionredispatch -->如果 cookie 寫入了 serverId 而客戶端不會刷新 cookie,當serverId 對應的服務器掛掉後,強制定向到其他健康的服務器
statsuri /haproxy -->使用瀏覽器訪問http://192.168.122.254/haproxy,可以看到服務器狀態
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend http-in
bind 0.0.0.0:80
mode http
log global
option httplog
option httpclose -->打開支持主動關閉功能
acl php url_reg -i \.php$ -->acl <ACL名字> <類型> <大小寫> <規則>
acl html url_reg -i \.html$
use_backend php-server if php -->use_backend <服務器組> if <ACL名字>
use_backend html-server if html
default_backend html-server -->默認使用的服務器組
backend php-server
mode http
balance roundrobin -->負載均衡的方式
option httpchk GET /index.php -->健康檢查
cookie SERVERID insert indirect nocache -->客戶端的 cookie 信息
server php-A 192.168.122.30:80 weight 1 cookie 1 check inter 2000 rise 2 fall 5
server php-B 192.168.122.40:80 weight 1 cookie 2 check inter 2000 rise 2 fall 5
-->cookie 1 標識 serverid 爲 1
-->check inter 2000 檢測心跳頻率
-->rise 2 2 次正確認爲服務器可用
-->fall 5 5 次失敗認爲服務器不可用
backend html-server
mode http
balance roundrobin
option httpchk GET /index.html
cookie SERVERID insert indirect nocache
server html-A 192.168.122.10:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
server html-B 192.168.122.20:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
5.啓動HAproxy
[root@localhost conf]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
6.查看HAproxy狀態
[root@localhost conf]# firefox http://localhost/haproxy
7.在客戶端訪問 HAproxy 測試
[root@localhost ~]# elinks –dump http:// 192.168.122.254
[root@localhost ~]# elinks –dump http:// 192.168.122.254/index.html
[root@localhost ~]# elinks –dump http:// 192.168.122.254/index.php