中小企業web集羣方案 haproxy+varnish+LNMP+memcached配置

    前段時間幫一箇中型企業做了一個集羣方案配置,目前已經安全運行了一個月,而且速度非常快,穩定性也很強,作爲一般中型企業負載均衡的方案,非常的合適。但是由於資本有限,沒有做到高可用性方案。
一,拓撲圖如下:

 

 
二,架構說明及配置
1, haproxy 服務器: 192.168.1.5            
2, varnish服務器集羣:  192.168.1.20;192.168.1.21
3, nginx+fastcgi 服務器:  192.168.1.50
4, memcached 服務器:memcached我是裝在nginx+fastcgi服務器上的,192.168.1.50
5, mysql服務器:192.168.1.60
三,具體配置以下我們來一個個講解,我們先從後端來個個講解,
1,在192.168.1.60 服務器上安裝 mysql
2,  在192.168.1.50 服務器上安裝 nginx+fastcgi.
mysql+nginx+fastcgi 配置,這裏不講解了,網上包括我的博客裏面都有講解如何時安裝。
3,memcached安裝配置。
  這裏的memcached 我安裝在  192.168.1.50 服務器上 安裝步驟如下:
 (1)下載所需要的軟件 (memcached 目前最新版本爲1.4.13)
安裝memcached需要libevent庫支持,我們下載libevent1.3版本即可
 
  1. #wget  http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz  
  2. #wget  http://www.monkey.org/~provos/libevent-1.3.tar.gz  
(2)先安裝libevent:
 
  1. # tar zxvf libevent-1.3.tar.gz 
  2. # cd libevent-1.3 
  3. # ./configure --prefix=/usr 
  4. # make 
  5. # make install  
(3)安裝memcached,同時需要安裝中指定libevent的安裝位置:
  1. # cd /tmp 
  2. # tar zxvf memcached-1.4.13.tar.gz 
  3. # cd memcached-1.4.13 
  4. # ./configure --with-libevent=/usr 
  5. # make 
  6. # make install 
安裝完成後會把memcached放到 /usr/local/bin/memcached 
(4)fastcgi 安裝memcache擴展模塊,讓php支持memcached
 1.在http://pecl.php.net/package/memcache 選擇相應想要下載的memcache版本。
 2.安裝PHP的memcache擴展
 
  1. #tar vxzf memcache-2.2.1.tgz 
  2. #cd memcache-2.2.1 
  3. #/usr/local/webserver/php/bin/phpize 
  4. #./configure--enable-memcache --with-php-config=/usr/local/webserver/php/bin/php-config --with-zlib-dir 
  5. #make 
  6. #make install 
3.上述安裝完後會有類似這樣的提示:
 
  1. Installing shared extensions: /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/
4,在php.ini 添加一行來載入memcache擴展:
 
  1. #extension = "memcache.so" 
  如下圖


5,重新加載php 配置文件

啓動memcached 
 
  1. # /usr/local/bin/memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 10240 -P /tmp/memcached.pid  
我們來看一下是否正常啓動
端口已正常啓動。
 
  1. memcache啓動參數備註: 
  2. -d選項是啓動一個守護進程, 
  3. -m是分配給Memcache使用的內存數量,單位是MB,我這裏是10MB, 
  4. -u是運行Memcache的用戶,我這裏是root, 
  5. -l是監聽的服務器IP地址,爲了安全我這裏指定了 127.0.0.1, 
  6. -p是設置Memcache監聽的端口,我這裏設置了11211,最好是1024以上的端口, 
  7. -c選項是最大運行的併發連接數,默認是1024,我這裏設置10240,按照你服務器的負載量來設定 
  8. -P是設置保存Memcache的pid文件,我這裏是保存在 /tmp/memcached.pid, 
  9.  
Memcached安裝完畢。我們來測試一下是否能緩存mysql數據
我們在此服務器上架設一個php網站,此處我先用的是佔用資源比較大的magento程序網站,調試好配置文件,綁定一下host文件。看網站是否能正常打開。
 

好,網站打開正常,接下來我們看一下,memcached是否發揮了他的作用
這裏我們用一個軟件 memadmin (網上可以下載到) 來監控一下memcached的緩存命中率,如下圖:
命中率還是挺高的。
 
4,varnish服務器安裝
以下在192.168.1.20 和 192.168.1.21 上操作
下載varnish軟件  (此處下載3.0版本)
 
  1. #wget   http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz  
  2. #tar -xvf varnish-3.0.0.tar.gz 
  3. #cd varnish-3.0.0 
  4. #./configure --prefix=/usr/local/varnish 
  5. #make &&make install 
至此安裝完成。
我們來配置一下
 
  1. #vim /usr/local/varnish/etc/varnish/vcl.conf 
以下爲配置代碼:(除backend 及vcl_recv外,其他地方無需更改,這裏是更改其訪問的主機名,及定義後端服務器地址,字數有限,其他代碼這裏就不解釋了)
 
  1. #Cache for linuxtone sites 
  2. #backend vhost 
  3. backend  wwwyaozhibingceshicom { 
  4. .host = "www.yaozhibingceshi.com"
  5. .port = "80"
  6. #acl 
  7. acl purge { 
  8.   "localhost"
  9.   "127.0.0.1"
  10.   "192.168.0.0"/24; 
  11. sub vcl_recv { 
  12.         if (req.http.Accept-Encoding) { 
  13.             if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) { 
  14.                 remove req.http.Accept-Encoding; 
  15.                 remove req.http.Cookie; 
  16.             } else if (req.http.Accept-Encoding ~ "gzip") { 
  17.                 set req.http.Accept-Encoding = "gzip"
  18.             } else if (req.http.Accept-Encoding ~ "deflate") { 
  19.                 set req.http.Accept-Encoding = "deflate"
  20.             } else { 
  21.                 remove req.http.Accept-Encoding; 
  22.             } 
  23.         } 
  24.            if (req.http.host ~  "(.*)yaozhibingceshi.com") { 
  25.                        set req.backend = wwwyaozhibingceshicom; 
  26.                  } 
  27.             else { 
  28.                         error 404 "This website is maintaining or not exist!"
  29.                 } 
  30.   if (req.request == "PURGE") { 
  31.      if (!client.ip ~purge) { 
  32.        error 405 "Not Allowed"
  33.    } 
  34. #.dd..... 
  35.    return(lookup); 
  36.   } 
  37. #...GET...url...jpg,png,gif. ..cookie 
  38.   if (req.request == "GET"&& req.url ~ "\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm|gz|tgz|bz2|tbz|mp3|ogg|mp4|flv|f4v|pdf)$") { 
  39.         unset req.http.cookie; 
  40.   } 
  41. #..GET...url.php....cache.... 
  42.   if (req.request =="GET"&&req.url ~ "\.php($|\?)"){ 
  43.         return (pass); 
  44.   } 
  45. #     if (req.restarts == 0) { 
  46.         if (req.http.x-forwarded-for) { 
  47.             set req.http.X-Forwarded-For = 
  48.                 req.http.X-Forwarded-For + ", " + client.ip; 
  49.         } else { 
  50.             set req.http.X-Forwarded-For = client.ip; 
  51.         } 
  52. #   } 
  53. #........pipe.. 
  54.     if (req.request != "GET" && 
  55.       req.request != "HEAD" && 
  56.       req.request != "PUT" && 
  57.       req.request != "POST" && 
  58.       req.request != "TRACE" && 
  59.       req.request != "OPTIONS" && 
  60.       req.request != "DELETE") { 
  61.         return (pipe); 
  62.     } 
  63. #..GET .HEAD..... 
  64.     if (req.request != "GET" && req.request != "HEAD") { 
  65.         return (pass); 
  66.     } 
  67.     if (req.http.Authorization) { 
  68.         return (pass); 
  69.     } 
  70.     return (lookup); 
  71. #..url+host hash...... 
  72. sub vcl_hash { 
  73.     hash_data(req.url); 
  74.     if (req.http.host) { 
  75.         hash_data(req.http.host); 
  76.     } else { 
  77.         hash_data(server.ip); 
  78.     } 
  79.     return (hash); 
  80. # .....purge ..... 
  81. sub vcl_hit { 
  82.    if (req.request == "PURGE") { 
  83.        set obj.ttl = 0s; 
  84.        error 200 "Purged"
  85.     } 
  86.     return (deliver); 
  87. sub vcl_fetch { 
  88.           if (req.url ~ "\.(jpeg|jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico|swf|flv|dmg|js|css|html|htm)$") { 
  89.                    set beresp.ttl = 2d; 
  90.                    set beresp.http.expires = beresp.ttl; 
  91.                    set beresp.http.Cache-Control = "max-age=172800"
  92.                    unset beresp.http.set-cookie; 
  93.           } 
  94.           if (req.url ~ "\.(dmg|js|css|html|htm)$") { 
  95.                    set beresp.do_gzip = true
  96.           } 
  97.           if (beresp.status == 503) { 
  98.                          set beresp.saintmode = 15s; 
  99.           } 
  100. sub vcl_deliver { 
  101.         set resp.http.x-hits = obj.hits ; 
  102.         if (obj.hits > 0) { 
  103.                 set resp.http.X-Cache = "HIT You!"
  104.         } else { 
  105.                 set resp.http.X-Cache = "MISS Me!"
  106.         } 
配置文件保存退出 ,並啓動 varnish,如下圖

由於我這域名沒有註冊,所以我們在這兩臺機器的hosts文件裏綁定一下
 

另外一臺機器我們同樣配置
Varnish 已經成功啓動了,我們來分別把域名在本地綁定一下,直接訪問varnish服務器,看是否能正常打開網站,並查看緩存閤中率
從上圖可以看出來,綁定varnish 服務器網站是可以正常打開,而且從varnish自帶的命令 varnishstat 來看,平均緩存命中率能達到 92%,命中率也是非常的高!另一臺同樣的測試方法
至此,varnish 服務器集羣安裝配置完成。
5,Haproxy 服務器安裝配置
以下配置在 192.168.1.5;192.168.1.6 服務器上運行
下載 haproxy 軟件 (目前最新版本爲1.4.19)
 
  1. wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.19.tar.gz   
  2. #tar -xvf haproxy-1.4.19.tar.gz 
  3. #cd haproxy-1.4.19 
  4. #make TARGET=linux26 PREFIX=/usr/local/haproxy 
  5. #make install PREFIX=/usr/local/haproxy 
安裝完成,接下來配置一下 我解析幾個比較重要的參數
 
  1. # cd /usr/local/haproxy 
  2. # vim haproxy.conf 
  3. global 
  4.         maxconn 5120 
  5.         chroot /usr/local/haproxy 
  6.         uid 99 
  7.         gid 99 
  8.         daemon 
  9.         quiet 
  10.         nbproc  2 
  11.         pidfile /usr/local/haproxy/haproxy.pid 
  12. defaults 
  13.         log     global 
  14.         mode    http 
  15.         option  httplog 
  16.         option  dontlognull 
  17.         log 127.0.0.1 local3 
  18.         retries 3 
  19.         option redispatch 
  20.         maxconn 20000 
  21.         contimeout      5000 
  22.         clitimeout      50000 
  23.         srvtimeout      50000 
  24. listen www.yaozhibingceshi.com 0.0.0.0:80 
  25.        mode http 
  26.        stats uri /status                     (後端服務器狀態查看地址)   
  27.        stats realm Haproxy\ statistics 
  28.        stats auth admin:admin              (狀態查看頁面登陸帳號密碼) 
  29.        balance source    (調度算法,調度算法有很多,我這裏用source是和nginx的ip_hash同理,解決session問題) 
  30.        option httpclose 
  31.        option forwardfor 
  32. server app1_1 192.168.1.20:80 cookie app1inst1 check inter 2000 rise 2 fall 5 
  33. server app1_2 192.168.1.21:80 cookie app1inst2 check inter 2000 rise 2 fall 5 
最下面兩行,定義了後端的兩臺服務器,如果有更多,再另起一行,根據上面的格式寫上即可。
配置好後,啓動haproxy  並查看是否啓動成功

這樣看,是啓動成功了的
好的,我們來把域名。綁定到這臺haproxy服務器上看網站是否能正常打開。

我們發現,網站也是可以正常打開的,
haproxy 有宕機檢測功能,當後端服務器宕機後,他能自動檢測,並禁止鏈接再發往宕機的機器.
注:haproxy 後端服務器狀態查看方法
配置文件裏註明了   stats uri /status  即訪問 www.域名.com/status 即可查看後端服務器狀態
如圖:
 

 

這次方案由於資本有限,所以在服務器這塊,大大的減少了。如果做到高可用性,
應該要做到 haproxy 集羣,nginx+fastcgi集羣,以及數據庫這塊的集羣。
但是道理都是一樣的,只是多加一些服務器,要達到千萬級PV的網站,借用老男孩的一句來說,就是“把所有的用戶訪問請求都儘量往前推 ”。
QQ:410018348

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章