1、概述:javascript
HAProxy是一個用於4層或7層的高性能負載均衡軟件,在大型網站的大型Web服務器羣集中,HAProxy可用來替代專業的硬件負載均衡設備,節省大量的開支。php
一般狀況下,爲了不整個體系中出現單點故障,在相當重要的架構中,都須要部署備份設備,一樣,負載均衡設備也不能部署單臺,一旦主設備出現問題以後,備份設備可對主設備進行接管。實現不間斷的服務,這即是Keepalived的做用。css
因而,HAProxy和Keepalived的組合便成了省錢高效的Web服務器負載均衡架構。html
拓撲圖:前端
2、前端負載均衡層配置:java
1.ha_1配置<172.16.41.1>:mysql
<1>配置keepalivedlinux
[root@ha_1 ~]# yum install -y keepalived [root@ha_1 ~]# cd /etc/keepalived/ [root@ha_1 keepalived]# cp keepalived.conf keepalived.conf.bak [root@ha_1 keepalived]# vim keepalived.conf ! Configuration File forkeepalived global_defs { notification_email { #郵件通知機制 root@localhost maoqiuguo@localhost } notification_email_from kaadmin@localhost smtp_server 127.0.0.1 #使用本機郵件服務 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_haproxy { #檢測haprox服務狀態 script "killall -0 haproxy" interval 1 weight 2 #權重 } ###########VRRP_INSTANCE VI_1###########實例1的配置 vrrp_instance VI_1 { state MASTER #在ha_1上面是主,對端ha_2上面是備 interface eth0 virtual_router_id 100 #路由ID priority 100 #優先級 advert_int 1 authentication { #路由之間認證 auth_type PASS auth_pass 123.com } virtual_ipaddress { #VIP配置 172.16.41.100/16dev eth0 label eth0:0 } track_script { #追蹤腳本 chk_haproxy } track_interface { #追蹤端口 eth0 } #通知腳本 notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } ##########VRRP_INSTANCE VI_2############實例2的配置 vrrp_instance VI_2 { state BACKUP #在ha_1上面是被,對端ha_2上面是主 interface eth0 virtual_router_id 200 #路由ID priority 199 #優先級 advert_int 1 authentication { #路由間認證 auth_type PASS auth_pass 123.com } virtual_ipaddress { #VIP配置 172.16.41.101/16dev eth0 label eth0:1 } track_interface { #追蹤端口 eth0 } track_script { #追蹤腳本 chk_haproxy } } ###################################### 爲ha_1的keepalived提供腳本文件: [root@ha_1 ~]# vim /etc/keepalived/notify.sh #!/bin/bash # Author: MageEdu <linuxedu@foxmail.com> 腳本使用請註明出處 # description: An example of notify script # vip=172.16.41.100 contact='root@localhost' notify() { mailsubject="`hostname` to be $1: $vip floating" mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1" echo$mailbody | mail -s "$mailsubject"$contact } case"$1"in master) notify master /etc/rc.d/init.d/haproxystart exit0 ;; backup) notify backup /etc/rc.d/init.d/haproxystop exit0 ;; fault) notify fault /etc/rc.d/init.d/haproxystop exit0 ;; *) echo'Usage: `basename $0` {master|backup|fault}' exit1 ;; esac #賦予執行權限: [root@ha_1 ~]# chmod +x /etc/keepalived/notify.sh |
<2>配置haproxy.web
[root@ha_1 haproxy]# yum install haproxy -y [root@ha_1 ~]# cd /etc/haproxy/ [root@ha_1 haproxy]# cp haproxy.cfg haproxy.cfg.bak [root@ha_1 haproxy]# vim haproxy.cfg global #全局配置 log 127.0.0.1 local2 #日誌功能 chroot /var/lib/haproxy#修改haproxy的工做目錄至指定的目錄並在放棄權限以前執行chroo t()操做,能夠提高haproxy的安全級別,不過須要注意的是要確保指定的目錄爲空 目錄且任何用戶均不能有寫權限; pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon #讓haproxy以守護進程的方式工做於後臺 defaults mode http #指定haproxy的工做模式 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 listen stats mode http bind 0.0.0.0:1080 #綁定1080端口 stats enable#開啓stats功能 stats hide-version #隱藏haproxy版本信息 stats uri /myadmin?stats #在瀏覽器中經過什麼樣的URI訪問stats頁面 stats realm Haproxy\ Statistics #認證註釋信息 stats auth maoqiu:123.com #認證機制(User:Password) stats admin ifTRUE #若是認證成功,則賦予管理權限 acl allow src 172.16.0.0/16#訪問控制,只容許是這個網段的客戶端訪問 tcp-request content accept ifallow tcp-request content reject frontend proxy #前端代理 bind *:80 #監聽80port mode http log global option httpclose option logasap option dontlognull capture request header Host len 20 capture request header Referer len 60 acl url_static path_beg -i /static/p_w_picpaths/javascript/stylesheets acl url_static path_end -i .jpg .gif .png .css .js .html use_backend static_servers ifurl_static default_backend dynamic_servers backend static_servers #後端靜態server balance source#基於source算法調度 server imgsrv1 192.168.100.2:80 check maxconn 6000 backend dynamic_servers #後端動態server balance source#基於source算法調度 server websrv1 192.168.100.1:80 check maxconn 6000 |
2.ha_2配置<172.16.41.2>:redis
<1>配置keepalived:
! Configuration File forkeepalived global_defs { notification_email { root@localhost maoqiuguo@localhost } notification_email_from kaadmin@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_haproxy { script "killall -0 haproxy" interval 1 weight 2 } ###########VRRP_INSTANCE VI_1########### vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 100 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123.com } virtual_ipaddress { 172.16.41.100/16dev eth0 label eth0:0 } track_script { chk_haproxy } track_interface { eth0 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } ##########VRRP_INSTANCE VI_2############ vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 200 priority 200 advert_int 1 authentication { auth_type PASS auth_pass 123.com } virtual_ipaddress { 172.16.41.101/16dev eth0 label eth0:1 } track_interface { eth0 } track_script { chk_haproxy } } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } #####ha_2上面的腳本文件同ha_1,須將VIP修改成172.16.41.101,再賦予權限便可! |
<2>配置kehaproxy:
由於在前端的haproxy功能都是將服務代理至至後端的Real Server,每項配置都是同樣的,因此在ha_2上安裝好haproxy以後將ha_1上面的配置文件copy過來便可!
[root@ha_2 keepalived]# scp root@172.16.41.1:/etc/haproxy/haproxy.cfg /etc/haproxy/ |
3.啓動keepalived測試:
<1>當兩個前端節點的服務正常狀態時: |
<2>當把某個前端節點的haproxy服務中止後的狀態: |
目前keepalived爲haporxy提供高可用已經達到目的,下面繼續關於haproxy的動靜分離機制和haproxy 統計信息輸出機制的實現.
3、後端Web Server(RS1/RS2,意爲Real Server)配置
在拓撲圖中規劃RS1爲客戶端請求的動態內容提供服務,RS2爲客戶端請求靜態內容提供服務
1.爲RS1提供動態內容頁面(我這裏使用直接使用一個php的測試頁)
[root@RealServer1 ~]# yum install -y php php-mysql [root@RealServer1 ~]# vim /var/www/html/index.php <h1>Real Server1</h1> <?php phpinfo(); ?> [root@RealServer1 ~]# service httpd start Starting httpd: [ OK ] [root@RealServer1 ~]#
2.RS2提供圖片或者html網頁文檔
#放個html網頁文檔 [root@RealServer2 ~]# vim /var/www/html/index.html <h1>Real Server2</h1> #放張圖片 [root@RealServer2 ~]# cd /var/www/html/ [root@RealServer2 html]# ls index.html tux_windows.jpg [root@RealServer2 html]#
4、測試:
1.靜態內容測試:
2.動態內容測試:
3.haproxy統計頁面輸出機制:
haproxy的動靜分離以及統計信息的輸出機制在兩個代理節點上都正常的狀況下沒有問題,最後再次將某一代理服務關閉後仍是同樣訪問正常;基於keepalived的haproxy高可用實驗成功!