實驗拓撲圖:php
操做流程:html
Real Server : 192.168.4.53 pc53 192.168.4.54 pc54
web
配置WEB 服務器redis
HAProxy調度器 : 192.168.4.50 pc50
vim
安裝並啓用HAProxy後端
修改配置文件配置負載平衡
bash
Clinet :192.168.4.253 pc253
服務器
鏈接測試 cookie
具體步驟:app
環境準備:
配置yum源
# service iptables stop //關閉防火牆
# chkconfig iptables off //關閉開機自啓
# setenforce 0 //設置SELinux 爲寬鬆模式
配置WEB服務器 pc53 / pc54
#yum -y install httpd php
#service httpd start
#chkconfig httpd on
[root@pc53 ~] #echo " <?php echo 'web53' ; ?> " > /var/www/html/test.php
[root@pc54 ~] #echo " <?php echo 'web54' ; ?> " > /var/www/html/test.php
配置HAProxy分發器 pc50
安裝並啓動HAProxy
# mount /dev/cdrom /mnt/
RHEL6 光盤文件的LoadBalancer目錄中含有HAProxy的RPM包
在已有的yum源配置文件 上添加以下
[LoadBalancer]
name=LoadBalancer
baseurl=file:///mnt/LoadBalancer
gpgcheck=0
# yum -y install haproxy
# rpm -qa haproxy
haproxy-1.5.4-2.el6.x86_64
# rpm -qc haproxy
/etc/haproxy/haproxy.cfg //haproxy配置文件
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
# cp /etc/haproxy/haproxy.cfg /root/ //備份haproxy配置文件
# chkconfig haproxy on//設置開機自啓
# chkconfig --list haproxy
haproxy 0:關閉1:關閉2:啓用3:啓用4:啓用5:啓用6:關閉
修改HAProxy配置文件 進行配置
HAProxy配置文件 說明
— 命令行:老是具備最高優先級
— global 部分:全局設置進程級別參數
— 代理聲明部分
來自於 default, listen, frontend 和 backend
— default 爲後續的其餘部分設置缺省參數,缺省參數能夠被後續部分重置
— frontend 描述接受客戶端偵聽套接字(socket)集
— backend 描述轉發連接的服務器集
— listen 把frontend 和 backend 結合到一塊兒的完整聲明
不作業務區分 修改配置文件以下
# vim /etc/haproxy/haproxy.cfg
global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid //haproxy的pid存放路徑 maxconn 4000 //最大連級數 默認4000 user haproxy group haproxy daemon // 建立1個進程進程入deamon模式運行 # turn on stats unix socket stats socket /var/lib/haproxy/stats defaults mode http #默認的模式 mode{tcp|http|health} log global # 採用全局定義的日誌 option httplog # 日誌類別http日誌格式 option dontlognull #不記錄健康檢查的日誌信息 option http-server-close option forwardfor except 127.0.0.0/8 #後端服務器能夠從Http Header中得到客戶端IP option redispatch #serverid 服務器掛掉後強制指定向到其餘健康服務器 retries 3 #3次鏈接失敗就認爲u服務不可用,也能夠經過後面設置 timeout http-request 10s timeout queue 1m timeout connect 10s #若是backend 沒有指示,默認爲10s timeout client 1m #客戶端鏈接超時 timeout server 1m #服務器鏈接超時 timeout http-keep-alive 10s timeout check 10s maxconn 3000 #最大鏈接數 stats uri /admin //定義監控頁面 uri listen weblb 0.0.0.0:80 cookie SERVERID rewrite balance roundrobin server weba 192.168.4.53:80 cookie app1inst1 check inter 2000 rise 2 fall 5 server webb 192.168.4.54:80 cookie app1inst2 check inter 2000 rise 2 fall 5
# service haproxy start //啓動服務
# netstat -pantu | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3192/haproxy
客戶端訪問
# elinks --dump 192.168.4.50/test.php
web53
# elinks --dump 192.168.4.50/test.php
web54
# elinks --dump 192.168.4.50/test.php
web53
# elinks --dump 192.168.4.50/test.php
web54
# elinks --dump 192.168.4.50/test.php
web53
# elinks --dump 192.168.4.50/test.php
web54
# firefox 192.168.4.50/admin //查看監控頁面
部署基於業務區分HAProxy負載平衡集羣
實驗拓撲圖:
操做流程:
Real Server :
配置WEB 服務器 使用HTML網頁文件 192.168.4.51 pc51 192.168.4.52 pc52
使用php 網頁文件 192.168.4.53 pc53 192.168.4.54 pc54
HAProxy調度器 : 192.168.4.50 pc50
安裝並啓用HAProxy
修改配置文件配置負載平衡
Clinet :192.168.4.253 pc253
鏈接測試
具體步驟:
環境準備:
配置yum源
# service iptables stop //關閉防火牆
# chkconfig iptables off //關閉開機自啓
# setenforce 0 //設置SELinux 爲寬鬆模式
配置web服務端 pc 51 pc 52 pc 53 pc54
部署基本的httpd 服務
# yum -y install httpd
在pc 53 和 pc 54 上下載 php 軟件包
#yum -y install php
#service httpd start
#chkconfig httpd on
# cd /var/www/html/
[root@pc51 html]# echo 192.168.4.51 > index.html
[root@pc52 html]# echo 192.168.4.52 > index.html
[root@pc53 html]# echo '<?php echo "192.168.4.53";?>' > test.php
[root@pc54 html]# echo '<?php echo "192.168.4.54";?>' > test.php
配置HAProxy分發器 pc50
安裝並啓動HAProxy
# mount /dev/cdrom /mnt/
RHEL6 光盤文件的LoadBalancer目錄中含有HAProxy的RPM包
在已有的yum源配置文件 上添加以下
[LoadBalancer]
name=LoadBalancer
baseurl=file:///mnt/LoadBalancer
gpgcheck=0
# yum -y install haproxy
# rpm -qa haproxy
haproxy-1.5.4-2.el6.x86_64
# rpm -qc haproxy
/etc/haproxy/haproxy.cfg //haproxy配置文件
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
# cp /etc/haproxy/haproxy.cfg /root/ //備份haproxy配置文件
# chkconfig haproxy on//設置開機自啓
# chkconfig --list haproxy
haproxy 0:關閉1:關閉2:啓用3:啓用4:啓用5:啓用6:關閉
基於業務區分 修改配置文件
# cd /etc/haproxy/
修改配置文件
# vim haproxy.cfg
global 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 stats uri /admin frontend weblb *:80 acl urlhtml path_end -i .html // acl 名字 匹配路徑結尾 不區分大小寫 .html acl urlphp path_end -i .php use_backend htmlgrp if urlhtml //若是接受到與urlhtml這個ACL 匹配時 去找htmlgrp 組 # use_backend phpgrp if urlphp default_backend htmlgrp // 默認去找htmlgrp backend htmlgrp balance roundrobin server web51 192.168.4.51:80 check //check 後不寫參數 默認用 defaults 定義的參數 server web52 192.168.4.52:80 check backend phpgrp balance roundrobin server web53 192.168.4.53:80 check server web52 192.168.4.54:80 check
客戶端訪問
# firefox 192.168.4.50/admin //查看監控頁面
//測試html
# elinks --dump 192.168.4.50
192.168.4.51
# elinks --dump 192.168.4.50
192.168.4.52
# elinks --dump 192.168.4.50
192.168.4.51
# elinks --dump 192.168.4.50
192.168.4.52
# firefox 192.168.4.50/admin //查看監控頁面
健康性檢查
模擬 51 服務器 故障
[root@pc51 ~]# service httpd stop
# elinks --dump 192.168.4.50
192.168.4.52
# elinks --dump 192.168.4.50
192.168.4.52
# elinks --dump 192.168.4.50
192.168.4.52
# firefox 192.168.4.50/admin //查看監控頁面
模擬 51 服務器 故障已經解決
[root@pc51 ~]# service httpd start
# elinks --dump 192.168.4.50
192.168.4.52
# elinks --dump 192.168.4.50
192.168.4.51
# elinks --dump 192.168.4.50
192.168.4.52
# elinks --dump 192.168.4.50
192.168.4.51
# firefox 192.168.4.50/admin //查看監控頁面
//測試php
# elinks --dump 192.168.4.50/test.php
192.168.4.53
# elinks --dump 192.168.4.50/test.php
192.168.4.54
# elinks --dump 192.168.4.50/test.php
192.168.4.53
# elinks --dump 192.168.4.50/test.php
192.168.4.54
# firefox 192.168.4.50/admin //查看監控頁面