KeepAlived實現HAProxy雙主并實現資源的動靜分離
前言
我們這次使用
HAProxy
作為負載均衡調度器來實現后端httpd
服務的負載均衡和動靜分離,并使用KeepAlived
讓HAproxy
實現雙主高可用, 再使用DNS
輪詢將用戶對www.anyisalin.com
的訪問負載均衡至兩個HAProxy
實現的負載均衡調度器上
HAProxy介紹
HAProxy的是一個免費的,非??焖俸涂煽康慕鉀Q方案,提供高可用性,負載均衡和代理對TCP和HTTP的應用程序。它特別適用于非常高流量網站。多年來,它已成為標準的開源的負載均衡程序,現在隨最主流的Linux發行版,并且通常默認的云平臺部署。其運作模式使得其集成到現有的架構非常容易,無風險,同時還提供了可能性不暴露脆弱的Web服務器到網絡
__轉自HAProxy官方站點我們今天只演示
HAProxy
作為負載均衡器的場景
實驗拓撲
實驗環境
VIP1: 172.16.1.10
VIP2: 172.16.1.11
主機 | IP | 功用 |
---|---|---|
node1.anyisalin.com | 172.16.1.2, VIP | HAproxy, KeepAlived Node |
node2.anyisalin.com | 172.16.1.3, VIP | HAproxy, KeepAlived Node |
node3.anyisalin.com | 172.16.1.4 | httpd, Image資源 |
node4.anyisalin.com | 172.16.1.5 | httpd, 動態網頁 |
node5.anyisalin.com | 172.16.1.8 | dns |
注意: 本文實驗中所有主機SElinux和iptables都是關閉的
實驗步驟
配置后端httpd服務器
node3
存放圖片
[root@node3 ~]# yum install httpd -y #安裝httpd [root@node3 ~]# cd /var/www/html/ [root@node3 html]# ls mage.jpg #這里有一張實現存好的圖片 [root@node3 html]# service httpd start #啟動httpd服務
node4
存放php文件
[root@node4 ~]# yum install httpd php [root@node4 ~]# ls /var/www/html/ #查看目錄下的文件, 什么都沒有 [root@node4 ~]# cat >> /var/www/html/index.php << "EOF" #創建網頁文件 > <html> > <head><title>Welcome to magedu.com</title></head> > <body> > <img src="/mage.jpg"/> #我們的網頁目錄下并沒有這張圖片 > <?php > echo "<h1>Welcome to MageEdu</h1>"; > ?> > </body> > </html> > EOF [root@node3 html]# service httpd start #啟動httpd服務
配置HAProxy實現backend負載均衡
[root@node1 ~]# yum install -y haproxy #安裝haproxy [root@node1 ~]# vim /etc/haproxy/haproxy.cfg #配置文件如下 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 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 frontend main bind *:80 ##開啟stats界面 stats enable stats hide-version stats uri /haproxyadmin default_backend dynamic #默認backend為dynamic acl url_static path_end -i .jpg #訪問控制列表, 匹配結尾為.jpg的資源 use_backend static if url_static #如果結尾為.jpg, 則使用backend為static backend dynamic balance roundrobin #這里使用roundrobin算法 server dynamic 172.16.1.5:80 check backend static balance uri #這里使用uri算法 server static 172.16.1.4:80 check
測試動靜分離效果
我們訪問
172.16.1.2
當我們將node3的
httpd
服務停止
再次將node3的
httpd
服務啟動
我們打開了
stats
頁面, 可以通過設置的URI
進行訪問
配置KeepAlived
KeepAlived
的配置不做解釋, 有興趣的請看官方文檔
node1和node2上都需要一個腳本文件監控haproxy的運行狀態 [root@node2 keepalived]# vim /etc/keepalived/haproxytest.sh #!/bin/bash if [ $(ps -C haproxy --no-header | wc -l ) -eq 0 ]; then /etc/init.d/haproxy start fi sleep 2 if [ $(ps -C haproxy --no-header | wc -l ) -eq 0 ]; then /etc/init.d/haproxy start fi node1的配置文件如下 [root@node1 ~]# vim /etc/keepalived/keepalived.conf global_defs { notification_email { } notification_email_from Alexandre.Cassen@firewall.loc smtp_server localhost smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_haproxy { script /etc/keepalived/haproxytest.sh interval 2 weight 2 } vrrp_script chk_down { script "[ -f /etc/keepalived/down ] && exit 1 || exit 0" interval 2 weight -20 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_haproxy } track_script { chk_down } virtual_ipaddress { 172.16.1.10/24 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 2222 } track_script { chk_haproxy } track_script { chk_down } virtual_ipaddress { 172.16.1.11/24 } } node2的配置文件如下 [root@node2 keepalived]# cat keepalived.conf global_defs { notification_email { } notification_email_from Alexandre.Cassen@firewall.loc smtp_server localhost smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_haproxy { script /etc/keepalived/haproxytest.sh interval 2 weight 2 } vrrp_script chk_down { script "[ -f /etc/keepalived/down ] && exit 1 || exit 0" interval 2 weight -20 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_haproxy } track_script { chk_down } virtual_ipaddress { 172.16.1.10/24 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 2222 } track_script { chk_haproxy } track_script { chk_down } virtual_ipaddress { 172.16.1.11/24 } } 兩個節點啟動Keepalived并查看狀態 [root@node1 ~]# service keepalived start [root@node1 ~]# ssh node2.anyisalin.com -- service keepalived start [root@node1 ~]# ip a | grep "inet\>" #ip地址為172.16.1.10 inet 127.0.0.1/8 scope host lo inet 172.16.1.2/24 brd 172.16.1.255 scope global eth0 inet 172.16.1.10/24 scope global secondary eth0 [root@node2 keepalived]# ip a | grep "inet\>" #ip地址為172.16.1.11 inet 127.0.0.1/8 scope host lo inet 172.16.1.3/24 brd 172.16.1.255 scope global eth0 inet 172.16.1.11/24 scope global secondary eth0 [root@node1 ~]# scp /etc/haproxy/haproxy.cfg node2.anyisalin.com:/etc/haproxy/ #同步文件到node2 我們這樣配置haproxy會自動啟動在兩個節點上
測試KeepAlived
我們分別對
172.16.1.10
和172.16.1.11
進行測試, 都能夠正常調度
我們強行停止
node1
的KeepAlived
,IP
已經轉移, 再次進行測試
配置DNS
過程沒什么好說的, 不懂可以看我以前的文章DNS and BIND 配置指南
[root@node5 ~]# yum install bind bind-utils [root@node5 ~]# vim /etc/named.conf #編輯配置文件 options { directory "/var/named"; }; logging { channel default_debug { file "data/named.run"; severity dynamic; }; }; zone "." IN { type hint; file "named.ca"; }; include "/etc/named.rfc1912.zones"; include "/etc/named.root.key"; [root@node5 ~]# vim /etc/named.rfc1912.zones #添加以下字段 zone "anyisalin.com" IN { type master; file "anyisalin.com.zone"; }; [root@node5 ~]# vim /var/named/anyisalin.com.zone #創建區域解析庫文件 $TTL 600 $ORIGIN anyisalin.com. @ IN SOA ns.anyisalin.com admin.anyisalin.com ( 20160416 1D 5M 7D 1D ) IN NS ns ns IN A 172.16.1.8 www IN A 172.16.1.10 www IN A 172.16.1.11 [root@node5 ~]# service named start #啟動dns
DNS測試
[root@node5 ~]# nslookup #測試能達到以下效果 > www.anyisalin.com Server: 172.16.1.1 Address: 172.16.1.1#53 Name: www.anyisalin.com Address: 172.16.1.11 Name: www.anyisalin.com Address: 172.16.1.10 > www.anyisalin.com Server: 172.16.1.1 Address: 172.16.1.1#53 Name: www.anyisalin.com Address: 172.16.1.10 Name: www.anyisalin.com Address: 172.16.1.11
最終測試
直接訪問
www.anyisalin.com
將
node2
的服務停止, 再次進行測試, 能夠正常訪問
總結
我們輕松地通過
HAProxy
實現資源的動靜分離和后端httpd
主機的負載均衡,也通過KeepAlived
實現HAProxy
的高可用, 最后在再通過DNS
輪詢實現HAProxy
的負載均衡, 整套架構還是很完整的, 如果有小伙伴有興趣可以多放點資源上去, 哈哈, 我這算不算侵犯馬哥的肖像權啊, 如果侵犯了, 請告知, 我會刪除的!作者水平很低, 如果有錯誤及時指出, 如果你覺得本文寫的好請點一波贊~(≧▽≦)/~
作者: AnyISaIln QQ: 1449472454
感謝: MageEdu
原創文章,作者:Net18-AnyISalIn,如若轉載,請注明出處:http://www.www58058.com/14951