haproxy和nginx都可以作為七層和四層反代服務器對外提供服務,此文通過haproxy和keealived配置varnish搭建wordpress的動靜分離站點
一、實驗環境
五臺虛擬機:
-
haproxy-1:搭建haproxy和keepalived服務,ip地址:192.168.11.176
-
haproxy-2:搭建haproxy和keepalived服務,ip地址:192.168.11.172
-
-
wordpress靜態服務器:搭建httpd,ip地址:192.168.11.177
-
wordpress動態服務器,搭建httpd+php+mariadb,ip地址:192.168.11.178
實驗目的:
通過haproxy訪問到后臺網站,并且varnish為網站提供緩存,提升網站性能。
實驗拓撲:此處虛擬機數量不夠,就不采用動靜分離集群了,而是分別采用一臺虛擬機部署:
二、實驗步驟
wordpress配置:
-
動態服務器配置:
-
安裝httpd,mariadb,php服務,此處php作為httpd的模塊進行動態php文件處理:
yum install -y httpd mariadb-server php php-mysql php-mbstring
-
上傳wordpress到/var/www/html路徑下并修改屬主屬組為apache,此處不做詳細演示;
-
-
靜態服務器配置:
-
安裝httpd服務并上傳wordpress到/var/www/html,修改屬主屬組為apache;
-
varnish配置:
-
安裝varnish服務并修改varnish的配置文件default.vcl,修改內容如下:
vcl 4.0; #聲明為varnish4.0 import directors; #調用多個后端主機做集群# probe health_check { #定義健康狀態檢測機制# .url = "/.health_check.html"; #定義檢測文件# .window = 5; #定義一共檢測幾次# .threshold = 3; #定義檢測幾次失敗則認為后端主機失效# .interval = 2s; #每隔2秒檢測一次# .timeout = 3s; #連接超時時長為3秒# } backend dynamic { #定義后端動態主機# .host = "192.168.11.178"; .port = "80"; .probe = health_check; } backend static { #定義前端主機# .host = "192.168.11.177"; .port = "80"; .probe = health_check; } sub vcl_recv { # Happens before we check if we have this in cache already. # # Typically you clean up the request here, removing cookies you don't need, # rewriting the request, etc. if (req.url ~ "(?i).*php.*") { #當訪問的url包括php,則轉到動態服務器,否則所有請求轉到靜態服務器,實現動靜分離# set req.backend_hint = dynamic; } else { set req.backend_hint = static; } if (req.url ~ "(?i)(register|login)$") { #如果請求地址結尾為register或者login,注冊和登錄頁面,則不查緩存,,直接從后端服務器獲取內容# return (pass); } } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. #當后端服務器回復給varnish的響應如果不包含公共緩存信息,而且請求為jpg等靜態資源,則卸載cookie信息并緩存資源1小時# if (beresp.http.cache-control !~ "s-maxage") { if (bereq.url ~ "(?i)\.(jpg|jpeg|png|gif|css|js|xml)$") { unset beresp.http.Set-Cookie; set beresp.ttl = 3600s; } } #當varnish請求后端服務器的url包括php,則卸載cookie信息并緩存資源1小時# if (bereq.url ~ "(?i).*php.*") { unset beresp.http.Set-Cookie; set beresp.ttl = 3600s; } } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. if (obj.hits>0) { set resp.http.X-Cache = "HIT via "+server.ip; } else { set resp.http.X-Cache = "MISS via "+server.ip; } }
注意:
varnish在探測到請求和響應報文頭部有cookie信息的時候是不緩存的,所以緩存命中率會非常低。這就是為什么要卸載php頁面和jpg等動態資源cookie的原因。
-
然后通過varnishadm登錄varnish并加載配置項,用ab進行壓測會發現啟用和不啟用緩存的性能差出數倍,而且啟用和不啟用緩存后端服務器壓力也差出數倍;
haproxy配置:
-
修改/etc/haproxy/haproxy.cfg配置文件,內容如下:
global #全局配置# log 127.0.0.1 local2 #log記錄到rsyslog服務器,此處需要在rsyslog進行配置# chroot /var/lib/haproxy #把haproxy禁錮到/var/lib/haproxy,防止haproxy被劫持后破壞系統 pidfile /var/run/haproxy.pid #pid文件位置# maxconn 4000 #每個進程最大連接數為4000# user haproxy group haproxy daemon #以服務方式運行# stats socket /var/lib/haproxy/stats defaults #默認配置# mode http #默認為http七層代理# log global #日志采用global配置# option httplog #以http方式記錄日志# option dontlognull #不記錄健康狀態檢測日志# option http-server-close option forwardfor except 127.0.0.0/8 #傳遞客戶端ip到后端服務器,需要在后端服務器修改日志樣式# option redispatch retries 3 #最大嘗試3次連接# timeout http-request 10s #等待http請求時間為10s# timeout queue 1m # timeout connect 10s #等待建立連接時間為10s# timeout client 1m #等待客戶端連接時間# timeout server 1m #等待服務端連接時間# timeout http-keep-alive 10s #長連接時長# timeout check 10s #檢測間隔# maxconn 3000 listen stats #定義狀態頁# bind *:9000 stats enable stats auth admin:admin stats uri /admin?stats #定義stats url路徑# stats refresh 30s stats realm "status-page" stats hide-version #隱藏版本信息# stats admin if TRUE #開啟后端管理功能# frontend web #定義前端服務器名為web# bind *:80 default_backend appsrvs #綁定的后端服務器# backend appsrvs #定義后端服務器名為appsrvs# server static 192.168.11.173:80 check #定義后端服務器,static為名字,check表示進行健康狀態檢測,也可以添加其他值進來,如檢測間隔等信息#
-
狀態頁面如下:
keepalived配置:
-
修改/etc/keepalived/keepalived.conf配置文件,內容如下:
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_group4 224.0.115.15 } vrrp_script chk_haproxy { script "pkill -0 haproxy && exit 0 || exit 1" interval 1 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 10 priority 96 advert_int 1 authentication { auth_type PASS auth_pass hello } virtual_ipaddress { 192.168.11.200 label ens33:0 } 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" }
-
寫notify腳本,內容如下:
#!/bin/bash # contact='root@localhost' notify() { local mailsubject="$(hostname) to be $1,vip floating." local mailbody="$(date + '%F %T'):vrrp transition,$(hostname) changed to be $1." echo "$mailbody" | mail -s "$mailsubject" $contact } case $1 in master) notify master;; backup) notify backup;; fault) notify fault;; *) echo "Usage: $(basename $0) {master|backup|fault}" exit 1;; esac
-
定義的haproxy的虛擬ip為192.168.11.200,此時任何一個haproxy主機或者haproxy服務故障都不會影響網站的正常訪問,通過192.168.11.200即可訪問到網站。
此時實驗配置成功,而且網站通過varnish可承載大量并發,但是仍有問題,前臺haproxy為高可用,但是后臺varnish和動靜服務器均是單點,為單點故障,所以還需要進一步完善。
網站訪問流程為:client –> 前端MASTER haproxy –> varnish –> 后端動靜服務器。
原創文章,作者:王子豪,如若轉載,請注明出處:http://www.www58058.com/79342