1、keepalived和haproxy
javascript
一、keepalivedphp
Keepalived的做用是檢測服務器的健康狀態,在全部可能出現單點故障的地方爲其提供高可用。若是有一臺服務器死機,或工做出現故障,Keepalived將檢測到,並將有故障的服務器從系統中剔除,當服務器工做正常後Keepalived自動將服務器加入到服務器羣中,這些工做所有自動完成,不須要人工干涉,須要人工作的只是修復故障的服務器。css
keepalived的核心是vrrp,它是經過腳原本調用服務的,因此在keepalived的使用中,僅需關心兩點:配置文件(/etc/keepalived/keepalived.conf)和服務腳本(/etc/rc.d/init.d/keepalived)
html
二、haproxy前端
haproxy是一個七層的負載均衡高度器,和nginx是屬於一個層次上的,而lvs是一個四層的負載均衡高度器,它最多隻能工做在TCP\IP協議棧上,因此對於代理轉發,haproxy作的能夠比lvs更細膩
java
HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代理,支持虛擬主機,它是免費、快速而且可靠的一種解決方案。HAProxy特別適用於那些負載特大的web站點,這些站點一般又須要會話保持或七層處理。HAProxy運行在當前的硬件上,徹底能夠支持數以萬計的併發鏈接。而且它的運行模式使得它能夠很簡單安全的整合進您當前的架構中,同時能夠保護你的web服務器不被暴露到網絡上。linux
2、拓撲圖nginx
3、前端配置
web
一、ha1配置(172.16.7.10)redis
(1)配置keepalived
[root@ha1 ~]# yum -y install keepalived #安裝keepalived [root@ha1 ~]# vim /etc/keepalived/keepalived.conf #修改配置文件 ! Configuration File for keepalived global_defs { notification_email { #通知郵件地址 root@localhost shuishui@localhost } notification_email_from warning@localhost smtp_server 127.0.0.1 #郵件服務器地址 smtp_connect_timeout 30 router_id LVS_DEVEL_shuishui } # vrrp_script chk_haproxy { script "killall -0 haproxy" #服務探測,返回0說明服務是正常的 interval 1 #每隔1秒探測一次 weight 2 #haproxy上線,權重加2;下線,權重減2 } # vrrp_instance VI_1 { #雙主實例1 state MASTER #ha1(172.16.7.10)爲主,ha2(172.16.7.100)爲備 interface eth0 virtual_router_id 88 #實例1的VRID爲88 garp_master_delay 1 priority 100 #主(172.16.7.10)的優先級爲100,從的(172.16.7.100)優先級爲99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } # virtual_ipaddress { 172.16.7.88/16 dev eth0 #實例1的VIP } 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" } vrrp_instance VI_2 { state BACKUP #實例2在ha1(172.16.7.10)上是備,在ha2(172.16.7.100)上是主 interface eth0 virtual_router_id 188 #實例2的VRID是188 garp_master_delay 1 priority 200 #實例2在ha1上的優先級是200,在ha2上的優先級是201 advert_int 1 authentication { auth_type PASS auth_pass 123456 } # virtual_ipaddress { 172.16.7.188/16 dev eth0 #實例2的VIP } track_interface { eth0 } # track_script { #腳本追蹤 chk_haproxy } } |
爲ha1的keepalived提供腳本文件:
[root@ha1 keepalived]# pwd /etc/keepalived [root@ha1 keepalived]# vim notify.sh #!/bin/bash # Author: MageEdu <linuxedu@foxmail.com> 腳本使用請清明出處 # description: An example of notify script # vip=172.16.7.88 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/haproxy start exit 0 ;; backup) notify backup /etc/rc.d/init.d/haproxy stop exit 0 ;; fault) notify fault /etc/rc.d/init.d/haproxy stop exit 0 ;; *) echo 'Usage: `basename $0` {master|backup|fault}' exit 1 ;; esac #給腳本執行權限 [root@ha1 keepalived]# chmod +x notify.sh |
(2)配置haproxy
[root@ha1 ~]# yum -y install haproxy #安裝haproxy [root@ha1 ~]# vim /etc/haproxy/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 #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode http #指定haproxy的工做模式爲http log global option httplog option dontlognull option http-server-close #當客戶端超時時,容許服務器關閉鏈接 option forwardfor except 127.0.0.0/8 #在響應頭部加入forwardfor option redispatch #在使用了基於cookie的會話保持的時候,一般須要 #加這麼一項,一旦後端某一server宕機時,可以將 #其會話從新派發到其它的upstream servers 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 10000 #最大併發鏈接數 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend proxy *:80 #前端代理 acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets acl url_static path_end -i .html .jpg .gif .png .css .js acl dynamic_content path_end -i .php use_backend static if url_static default_backend dynamic #--------------------------------------------------------------------- # static backend for serving up p_w_picpaths, stylesheets and such #--------------------------------------------------------------------- backend static #後端靜態服務器 balance roundrobin server web1 172.16.7.201:80 inter 3000 rise 2 fall 3 check maxconn 5000 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- backend dynamic #後端動態服務器 balance roundrobin server web2 172.16.7.202:80 inter 3000 rise 2 fall 3 check maxconn 5000 server web3 172.16.7.200:80 inter 3000 rise 2 fall 3 check maxconn 5000 listen statistics mode http bind *:8080 #把stats頁面綁定到8080端口 stats enable #開啓stats功能 stats auth admin:admin #認證的用戶名和密碼 stats uri /admin?stats #指定uri訪問路徑 stats hide-version #爲了安全(版本bug),隱藏版本信息 stats admin if TRUE #若是認證經過了就容許管理 stats refresh 5s #頁面5秒刷新一次 acl allow src 172.16.0.0/16 #定義訪問控制列表 tcp-request content accept if allow tcp-request content reject |
二、ha2配置(172.16.7.100)
(1)配置keepalived
[root@ha2 ~]# yum -y install keepalived #安裝keepalived [root@ha2 ~]# vim /etc/keepalived/keepalived.conf #修改配置文件 ! Configuration File for keepalived global_defs { notification_email { root@localhost shuishui@localhost } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL_shuishui } # vrrp_script chk_haproxy { script "killall -0 haproxy" interval 1 weight 2 } # vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 88 garp_master_delay 1 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } # virtual_ipaddress { 172.16.7.88/16 dev eth0 } track_interface { eth0 } # track_script { chk_haproxy } } # vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 188 garp_master_delay 1 priority 201 advert_int 1 authentication { auth_type PASS auth_pass 123456 } # virtual_ipaddress { 172.16.7.188/16 dev eth0 } 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" } # # #腳本同ha1的(腳本里的vip改成172.16.7.188),最後給執行權限 |
(2)配置haproxy
由於ha1的haproxy與ha2的haproxy是相同的,都是將服務代理至後端服務器,因此直接scp就能夠
[root@ha1 ~]# scp /etc/haproxy/haproxy.cfg root@172.16.7.100:/etc/haproxy/ |
三、啓動keepalived並測試
(1)ha1
(2)ha2
(3)關閉ha1的haproxy服務測試VIP飄移
(4)查看ha2,是否接收到了ha1飄過來的VIP
4、後端配置
一、配置web1(172.16.7.201),靜態的
[root@web1 ~]# yum -y install httpd [root@web1 ~]# cd /var/www/html/ [root@web1 html]# vim index.html <h1>Welcome to web1(172.16.7.201)</h1> [root@web1 html]# service httpd start |
二、配置web2(172.16.7.202),動態的
[root@web2 ~]# yum -y install httpd php [root@web2 ~]# cd /var/www/html/ [root@web2 html]# vim index.php <h1>Welcome to web2(172.16.7.202)</h1> <?php phpinfo(); ?> [root@web2 html]# service httpd start |
三、配置web3(172.16.7.200),動態的
[root@web3 ~]# yum -y install httpd php [root@web3 ~]# cd /var/www/html/ [root@web3 html]# vim index.php <h1>Welcome to web3(172.16.7.200)</h1> <?php phpinfo(); ?> [root@web3 html]# service httpd start |
5、測試
一、keepalived的高可用
上面測試過了,當haproxy服務掛掉的時候,VIP能夠飄走;當keepalived服務掛掉的時候,VIP也能夠飄走,高可用功能實現
二、haproxy動靜分離機制
(1)請求靜態內容
首先在web1(172.16.7.201)的網頁目錄下放入1.jpg
(2)請求動態內容
(3)haproxy統計頁面的輸出
①、URI及安全驗證
②、haproxy統計頁面