1.LNMP架構添加Memcached支持,并驗證其緩存結果
Memcache是一個高性能的分布式的內存對象緩存系統,通過在內存里維護一個統一的巨大的hash表,它能夠用來存儲各種格式的數據,包括圖像、視頻、文件以及數據庫檢索的結果等。簡單的說就是將數據調用到內存中,然后從內存中讀取,從而大大提高讀取速度。
Memcached是以守護程序(監聽)方式運行于一個或多個服務器中,隨時會接收客戶端的連接和操作。
配置:
Nginx, PHP-FPM和MariaDB安裝在此忽略。
(1) memcached安裝
yum -y install memcached
(2) 啟動memcached
memcached -d -m 1024 -u memcache
(3)連接并測試
telnet 192.168.31.11 11211
(3) 安裝PHP的memcached的擴展
php連接memcached服務的模塊有兩個,php-pecl-memcache和php-pecl-memcached.若要安裝php-pecl-memcached需要依賴libmemcached程序包,可以提供相應操作查看memcached的工具。在這里為方便演示就直接使用php-pecl-memcache擴展模塊。
yum -y install php-pecl-memcache
(4) 測試PHP是否已支持Memcached
啟動nginx 和php-fpm
service nginx start
service php-fpm start
編輯配置頁 vim index.php
<?php
phpinfo();
?>
(5) 測試memcached緩存
編寫一個測試頁
測試完成已生效
2.HAProxy實現端口轉發
用HAProxy作為負載均衡器,實現把前端請求調度到后端,前端監聽80端口,轉發至后端的8000端口,并會對訪問資源進行判斷實現不同的訪問內容轉發至相對應的服務器
HAPROXY: 192.168.31.233
RS1: 192.168.31.26
RS2: 192.168.31.23
配置如下
RS1 RS2 安裝apache 頁面 修改端口為8000
在RS1 /var/www/html目錄下建立一個 .php文件
在RS2 /var/www/html目錄下建立一個 .html文件
分別測試訪問
yum -y install haproxy
# vim /etc/haproxy/haproxy.cfg
stats refresh 30s #統計頁面自動刷新時間
stats enable 開啟狀態頁
stats hide-version #隱藏統計頁面上HAProxy的版本信息
stats admin if TRUE 認證成功啟動管理接口
backend dynamic
balance roundrobin
server web1 192.168.31.26:8000 check
backend static
balance roundrobin
server web2 192.168.31.23:8000 check
use_backend dynamic if url_dynamic use_backend 使用后端主機 ,條件符合條件才調用
default_backend static # 默認請求轉入后端靜態服務器
啟動haproxy
systemctl start haproxy.service
查看HAProxy的狀態頁
測試負載均衡效果
HAProxy 實現了把用戶來自80端口的請求轉發至后端8000端口上
3.闡述varnish的功能及應用場景,并通過實際案例調試、配置、測試過程
Web緩存是指一個Web資源(html,js,css,images…)存在于Web服務器和客戶端(瀏覽器),緩存會根據進來的請求報文做出響應,后緩存一份到本地的緩存中;當下一個請求到來的時候,如果是相同的URL,緩存會根據緩存機制決定是直接使用從緩存中響應訪問請求還是向后端服務器再次發送請求,取決于緩存是否過期及其請求的內容是否發生改變。有效的緩存能減少后端主機的壓力,實現快速響應用戶的請求,提高用戶體驗。
varnish就是一種實現上述web緩存功能(通常針對于靜態資源提供頁面緩存)的一款開源工具,通常它也被稱為http或web加速器,同時它也可以做為http反向代理工具,實現負載均衡和動靜分離的功能。此外,據官網介紹,Varnish的設計不僅僅是定位于反向代理服務器,根據使用方式的不同,Varnish可扮演的角色也豐富多樣,其它可實現的功能如下:
1)WEB應用防火墻;
2)DDoS攻擊防護;
3)網站防盜鏈;
4)負載均衡;
5)integration point;
6)單點登錄網關;
7)認證和認證授權;
8)后端主機快速修復;
9)HTTP路由
但在實際生產環境中varnish更多是在HAProxy、Nginx等七層負載均衡器后充當靜態資源緩存的反向代理,下圖是一個簡化的varnish應用場景,主要用來實現靜態資源緩存和動靜分離
1.部署web服務
######在192.168.31.26上配置動態web服務######,這里我用apache ~]# yum -y install gd php-gd gd-devel php-xml php-common php-mbstring php-ldap php-pear php-xmlrpc php-imap php php-mysql
######在192.168.31.23上配置靜態web服務###### ~]# yum install nginx -y ~]# mkdir -p /data/www ~]# vim /etc/nginx/nginx.conf location / { root /data/www; index index.html index.htm; } ~]# systemctl start nginx.service
2.部署mariadb服務器
~]# yum install mariadb-server -y ~]# vim /etc/my.cnf [mysqld] ... innodb_file_per_table = ON skip_name_resolve = ON ~]# systemctl start mariadb.service ~]# mysql > grant all on *.* to root@'192.168.31.%' identified by 'magedu'; > flush privileges;
3.部署varnish
~]# yum install varnish -y ######將varnish的監聽端口設置為80###### ~]# vim /etc/varnish/varnish.params VARNISH_LISTEN_PORT=80 ######配置varnish參數文件###### ~]# vim /etc/varnish/default.vcl ######設置默認的后端靜態服務器ip和端口###### backend default { .host = "192.168.31.23"; .port = "80"; ######配置健康狀態監測###### .probe = { .url = "/"; .interval = 2s; .window = 5; .threshold = 4; } } ######配置后端動態web服務器###### backend appsrv { .host = "192.168.31.26"; .port = "80"; ######配置健康狀態監測###### .probe = { .url = "/"; .interval = 2s; .window = 5; .threshold = 4; } } ######定義Purge-ACL控制###### acl purgers { "127.0.0.1"; "192.168.31.0"/24; } ######定義purge操作###### sub vcl_purge { return(synth(200,"Purged")); } sub vcl_recv { ######動靜分離###### if (req.url ~ "(?i)\.php$") { set req.backend_hint = appsrv; } else { set req.backend_hint = default; } ######如果請求方法為PURGE,且客戶端IP滿足acl,則執行purge操作,否則返回405頁面并提示###### if (req.method == "PURGE") { if (!client.ip ~ purgers) { return(synth(405,"Purging not allowed for" + client.ip)); return(purge); } } } ######記錄緩存命中狀態###### sub vcl_deliver { if (obj.hits>0) { set resp.http.X-Cache="HIT"; } else { set resp.http.X-Cache="MISS"; } } ######啟動服務使配置生效###### ~]# systemctl start varnish.service
4.創建探測頁
######在192.168.31.26上創建動態探測頁###### ~]# vim /data/www/index.php <?php $conn = mysql_connect('192.168.31.72','root','magedu'); if ($conn) echo "Dynamic webserver to Mariadb is OK!"; else echo "Failure"; ?> ######在192.168.31.23上創建靜態探測頁###### ~]# vim /data/www/index.html <h1>I'm Static Server!</h1>
5.varnish緩存效果測試
######在varnish CLI命令接口下創建并啟用vcl###### ~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ######加載默認vcl配置文件,并命名為test1###### varnish> vcl.load test1 default.vcl 200 VCL compiled. ######激活test1###### varnish> vcl.use test1 200 VCL 'test1' now active ######通過健康狀態監測可以看到后端服務器都正常###### varnish> backend.list 200 Backend name Refs Admin Probe default(192.168.31.23,,80) 2 probe Healthy 5/5 appsrv(192.168.31.26,,80) 2 probe Healthy 5/5 ######第一次訪問靜態頁面,MISS###### ~]# curl -I 192.168.31.233/index.html HTTP/1.1 200 OK Server: nginx/1.10.2 Date: Thu, 6 Jul 2017 22:18:03 GMT Content-Type: text/html Content-Length: 23Last-Modified: Mon, 31 Oct 2016 12:37:02 GMTETag: "58173aee-e74"X-Varnish: 2 Age: 0 Via: 1.1 varnish-v4 X-Cache: MISS Connection: keep-alive ######第二次訪問靜態頁面,HIT!###### [root@dbserver ~]# curl -I 192.168.31.233/index.html HTTP/1.1 200 OK Server: nginx/1.10.2 Date: Thu, 6 Jul 2017 22:18:09 GMT Content-Type: text/html Content-Length: 23 Last-Modified: Mon, 31 Oct 2016 12:37:02 GMT ETag: "594a7cc1-17" X-Varnish: 32770 3 Age: 2 Via: 1.1 varnish-v4 X-Cache: HIT #第二次訪問狀態為HIT,說明緩存生效 Connection: keep-alive ######第一次訪問動態頁面,MISS###### ~]# curl -I 192.168.31.233/index.php HTTP/1.1 200 OK Server: nginx/1.10.2 Date: Wed, 5 Jul 2017 21:51:49 GMT Content-Type: text/html X-Powered-By: PHP/5.4.16 X-Varnish: 5 Age: 0 Via: 1.1 varnish-v4 X-Cache: MISS Content-Length: 4 Connection: keep-alive ######第二次訪問動態頁面,HIT!###### ~]# curl -I 192.168.31.233/index.php HTTP/1.1 200 OK Server: nginx/1.10.2 Date: Wed, 5 Jul 2017 23:11:13 GMT Content-Type: text/html X-Powered-By: PHP/5.4.16 X-Varnish: 32772 6 Age: 2 Via: 1.1 varnish-v4 X-Cache: HIT Content-Length: 4 Connection: keep-alive ######分別訪問動靜資源均正常,說明動靜分離實現成功###### ~]# curl 192.168.31.233/index.html <h1>I‘m Static Server</h1> ~]# curl 192.168.31.233/index.php Dynamic Server to Mariadb is OK!
原創文章,作者:N27_Vicent,如若轉載,請注明出處:http://www.www58058.com/79433
總結的很好,架構的思路清晰,給出了詳細的操作過程和解釋,請繼續保持,加油?。?!