第十八周作業

1、為LNMP架構添加memcached支持,并完成對緩存效果的測試報告;

第十八周作業

環境準備:

1)LNMP(php-fpm)環境已搭建完畢

2)Wordpress已部署完成

1.在memcache服務器上安裝memcached包并啟動服務

]# yum install memcached -y
]# systemctl start memcached.service

2.查看memcached狀態信息

]# telnet 192.168.0.24 11211                #memcached默認端口是11211,使用telnet進行連接管理
Trying 192.168.0.24...
Connected to 192.168.0.24.
Escape character is '^]'.
stats            #stats命令用來查看memcahced狀態
STAT pid 2852    #memcache服務器的進程ID  
STAT uptime 23   #服務器已經運行的秒數
STAT time 1497241012    #服務器當前的unix時間戳
STAT version 1.4.15     #memcache版本
STAT libevent 2.0.21-stable    #libevent版本
STAT pointer_size 64    #當前操作系統的指針大小(32位系統一般是32bit,64就是64位操作系統)
STAT rusage_user 0.001610    #進程的累計用戶時間
STAT rusage_system 0.012883   #進程的累計系統時間
STAT curr_connections 10    #服務器當前存儲的items數量
STAT total_connections 11    #從服務器啟動以后存儲的items總數量
STAT connection_structures 11    #服務器分配的連接構造數
STAT reserved_fds 20    
STAT cmd_get 0   #get命令(獲?。┛傉埱蟠螖?STAT cmd_set 0    #set命令(保存)總請求次數  
STAT cmd_flush 0    #flush命令請求次數
STAT cmd_touch 0    #touch命令請求次數
STAT get_hits 0     #總命中次數
STAT get_misses 0    #總未命中次數
STAT delete_misses 0    #delete命令未命中次數
STAT delete_hits 0    #delete命令命中次數
STAT incr_misses 0    #incr命令未命中次數
STAT incr_hits 0    #incr命令命中次數
STAT decr_misses 0    #decr命令未命中次數
STAT decr_hits 0    #decr命令命中次數
STAT cas_misses 0    #cas命令未命中次數
STAT cas_hits 0        #cas命令命中次數
STAT cas_badval 0    #使用擦拭次數
STAT touch_hits 0    #touch命令未命中次數
STAT touch_misses 0    #touch命令命中次數
STAT auth_cmds 0    #認證命令處理的次數
STAT auth_errors 0    #認證失敗數目
STAT bytes_read 7        #總讀取字節數(請求字節數)
STAT bytes_written 0    #總發送字節數(結果字節數)
STAT limit_maxbytes 67108864      #分配給memcache的內存大小(字節)
STAT accepting_conns 1            #服務器是否達到過最大連接(0/1)
STAT listen_disabled_num 0    #失效的監聽數
STAT threads 4        #當前線程數
STAT conn_yields 0    #連接操作主動放棄數目
STAT hash_power_level 16    
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0   //當前存儲占用的字節數
STAT curr_items 0    #當前存儲的數據總數
STAT total_items 0    #啟動以來存儲的數據總數
STAT evictions 0    #為獲取空閑內存而刪除的items數(分配給memcache的空間用滿后需要刪除舊的items來得到空間分配給新的items)
STAT reclaimed 0    #已過期的數據條目來存儲新數據的數目
END

3.在php服務器上安裝php-pecl-memcache

]# yum install php-pecl-memcache -y 
]# php -m|grep memcache                #查看memcache模塊是否被應用到php
memcache

4.測試php到memcached的連接

]# vim index.php
<?php
$memcache = new Memcache;             //創建一個memcache對象
$memcache->connect('192.168.0.24',11211) or die ("Could not connect"); //連接Memcached服務器
$memcache->set('key','test');        //設置一個變量到內存中,名稱是key 值是test
$get_value = $memcache->get('key');   //從內存中取出key的值
echo $get_value;
?>

瀏覽器訪問http://192.168.0.22,返回字符”test”即為正常

5.為Wordpress配置memcached

#在wordpress官網下載memcahced插件,放到cp-content目錄下
]# cp object-cache.php /data/www/wordpress/wp-content
]# vim object-cache.php +418               #修改418行中memcached默認地址為實際地址
...
                  $buckets = array('192.168.0.24:11211');
...

6.登錄wordpress進行一些操作后,發現memcached中已經緩存了一些信息

]# telnet 192.168.0.24 11211
Trying 192.168.0.24...
Connected to 192.168.0.24.
Escape character is '^]'.
stats
STAT pid 3164
STAT uptime 7178
STAT time 1497257007
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.060055
STAT rusage_system 1.233451
STAT curr_connections 19
STAT total_connections 23
STAT connection_structures 21
STAT reserved_fds 20
STAT cmd_get 611
STAT cmd_set 56
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 515            #已命中緩存515次
STAT get_misses 96
STAT delete_misses 2
STAT delete_hits 1
STAT incr_misses 0
STAT incr_hits 2
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 91663
STAT bytes_written 478934
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 21105
STAT curr_items 48
STAT total_items 58 STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END

7.性能測試

1)這里使用開源web壓力測試工具siege進行測試,并發度為5,首先在不使用memcached的情況下進行測試,經過多次測試,發現Elapsed time始終在17~19s左右

]# siege -r10 -c1 http://192.168.0.22/wordpress
Transactions:                    500 hits
Availability:                 100.00 %
Elapsed time:                  19.49 secs
Data transferred:              17.78 MB
Response time:                  0.17 secs
Transaction rate:              25.65 trans/sec
Throughput:                     0.91 MB/sec
Concurrency:                    4.28
Successful transactions:         500
Failed transactions:               0
Longest transaction:            2.89
Shortest transaction:           0.00

2)使用memcached后進行測試,發現性能與不使用memcached時差不多,甚至還略差,但從memcached狀態變化來看,緩存確實是生效了,所以可能跟我虛擬機性能以及并發數不夠大有關系。

]# siege -r10 -c5 http://192.168.0.22/wordpress
Transactions:                    500 hits
Availability:                 100.00 %
Elapsed time:                  18.43 secs
Data transferred:              17.78 MB
Response time:                  0.15 secs
Transaction rate:              27.13 trans/sec
Throughput:                     0.96 MB/sec
Concurrency:                    4.19
Successful transactions:         500
Failed transactions:               0
Longest transaction:            1.76
Shortest transaction:           0.00

2、部署配置haproxy,能夠實現將來自用戶的80端口的http請求轉發至后端8000上的server服務,寫出其配置過程。

第十八周作業

環境準備:

后端web服務器已配置完成,并對外以8000端口提供web服務;

1.安裝haproxy

]# yum install haproxy -y

2.編輯haproxy配置文件

]# vim /etc/haproxy/haproxy.cfg
...
listen websrvs                                      #定義一個代理服務器websrvs
    bind        *:80                                #指定代理監聽端口為80
    server      websrv  192.168.0.22:8000 check     #定義一個后端websrv,注意:后端服務端口如果與監聽端口不一致,需要在地址后指明端口號

3.檢查配置無誤后啟動haproxy服務

]# haproxy -f /etc/haproxy/haproxy.cfg -c
Configuration file is valid
]# systemctl start haproxy.service

4.客戶端訪問測試

]# curl 192.168.0.22:8000
<h1>This is Backend Server</h1>
]# curl 192.168.0.20                                #通過代理能夠正常訪問后端webserver <h1>This is Backend Server</h1>

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.0.23上配置動態web服務######
~]# yum install nginx php-fpm php-mysql php-mbstring php-gd php-xml -y
~]# mkdir -p /data/www
~]# vim /etc/nginx/nginx.conf
location / {
                root    /data/www;
                index   index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           /data/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
~]# systemctl start nginx.service
~]# systemctl start php-fpm.service
######在192.168.0.22上配置靜態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.0.%' 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.0.22";
    .port = "80";
######配置健康狀態監測######
    .probe = {
        .url = "/";
        .interval = 2s;
        .window = 5;
        .threshold = 4;
    }

}
######配置后端動態web服務器######
backend appsrv {
    .host = "192.168.0.23";
    .port = "80";
######配置健康狀態監測######
    .probe = {
        .url = "/";
        .interval = 2s;
        .window = 5;
        .threshold = 4;
    }

}
######定義Purge-ACL控制######
acl purgers {
    "127.0.0.1";
    "192.168.0.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.0.23上創建動態探測頁######
~]# vim /data/www/index.php <?php
        $conn = mysql_connect('192.168.0.25','root','magedu');
        if ($conn)
                echo "Dynamic webserver to Mariadb is OK!";
        else
                echo "Failure";
?>
######在192.168.0.22上創建靜態探測頁######
~]# 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.0.22,,80)      2      probe      Healthy 5/5
appsrv(192.168.0.23,,80)       2      probe      Healthy 5/5

######第一次訪問靜態頁面,MISS######
~]# curl -I 192.168.0.21/index.html
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Thu, 22 Jun 2017 13:32:56 GMT
Content-Type: text/html
Content-Length: 23
Last-Modified: Wed, 21 Jun 2017 14:03:45 GMT
ETag: "594a7cc1-17"
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS                     
Connection: keep-alive
######第二次訪問靜態頁面,HIT!######
[root@dbserver ~]# curl -I 192.168.0.21/index.html
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Thu, 22 Jun 2017 13:32:56 GMT
Content-Type: text/html
Content-Length: 23
Last-Modified: Wed, 21 Jun 2017 14:03:45 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.0.21/index.php
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 21 Jun 2017 15: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.0.21/index.php
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 21 Jun 2017 15:51:49 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.0.21/index.html
<h1>I‘m Static Server</h1>
~]# curl 192.168.0.21/index.php
Dynamic Server to Mariadb is OK! 

原創文章,作者:N26-西安-方老喵,如若轉載,請注明出處:http://www.www58058.com/77956

(0)
N26-西安-方老喵N26-西安-方老喵
上一篇 2017-06-22
下一篇 2017-06-23

相關推薦

  • 自定義命令提示符

     命令提示符的格式放在變量PS1中         命令提示符特殊字符定義             \u  當前用戶      &…

    Linux干貨 2017-05-02
  • 通過webGUI顯示日志信息

         日志信息通常對于我們維護人員有著極為重要的意義,能夠為我們提供故障排查、運維監控、歷史操作記錄等等很多必要的信息。為了方便此類信息的獲取,我們可以搭建日志服務器,并實現webGUI顯示以便更直觀形象的觀察收集它們。     一、拓撲圖         &nbs…

    Linux干貨 2015-07-15
  • 我與Linux的第一次親密接觸

        作為一個新手,經過一周的學習,我對Linux目前有了些許的了解,從Linux的發展史中,開源共享精神深深的感染了我,也使得我對Linux的興趣更加濃厚。對于一個一直翹計算機課的我來說,入門和基礎是一項非常艱巨的任務,初期為了搭建學習的環境,也是費了不小的功夫,相信許多萌也對此有很大的感觸,在這里,我來和大家分享一下如何去構建環境?!?/p>

    Linux干貨 2017-03-26
  • N22-北京-追求極境之人-RSYNC差分編碼同步原理

    1客戶端發送自己的filelist,filelist包含文件hashindexid(alert32)和關聯的文件chunkChecksum(MD5)和hashindexID以便進行快速查找,chunksize根據文件大小進行等分,不足均值部分直接發送不進行MD5校驗值生成 發送進程一次從生成器讀取一個文件索引號和關聯的塊校驗集合 2對于生成器發送的每個文件I…

    Linux干貨 2016-08-22
  • rpm包管理

    一、概述 RPM 是RPM Package Manager(RPM軟件包管理器)的縮寫。由redhat公司的Redhat package manager改名而來,成了RedHat的工業標準 二、rpm的命名格式: rpm包的命名格式:name-version-relase.arch.rpm version: major.minor.release,同源代碼 …

    Linux干貨 2015-10-07
  • Function函數實例

    函數:function     把一段獨立功能的代碼當做一個整體,而后為之取一個名字,命令的代碼段,即為函數。 注意:     定義函數的代碼段不會自動執行,在調用時執行;所謂調用函數,在代碼中給定函數名即可     函數名出現的任何位置,…

    Linux干貨 2016-08-21

評論列表(3條)

  • ch368087977
    ch368087977 2017-06-22 23:01

    想問一下圖式用什么軟件畫的呢

    • N26-西安-方老喵
      N26-西安-方老喵 2017-06-23 08:10

      @ch368087977
      Edraw

  • luoweiro
    luoweiro 2017-06-26 22:27

    圖文并茂,尤其是還是用了siege做壓測,這點是非常贊的,如果能配合一點性能壓測效果會更好,加油。

欧美性久久久久