前段時間幫一箇中型企業做了一個集羣方案配置,目前已經安全運行了一個月,而且速度非常快,穩定性也很強,作爲一般中型企業負載均衡的方案,非常的合適。但是由於資本有限,沒有做到高可用性方案。
一,拓撲圖如下:
二,架構說明及配置
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.
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版本即可
- #wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
- #wget http://www.monkey.org/~provos/libevent-1.3.tar.gz
(2)先安裝libevent:
- # tar zxvf libevent-1.3.tar.gz
- # cd libevent-1.3
- # ./configure --prefix=/usr
- # make
- # make install
(3)安裝memcached,同時需要安裝中指定libevent的安裝位置:
- # cd /tmp
- # tar zxvf memcached-1.4.13.tar.gz
- # cd memcached-1.4.13
- # ./configure --with-libevent=/usr
- # make
- # make install
安裝完成後會把memcached放到 /usr/local/bin/memcached
(4)fastcgi 安裝memcache擴展模塊,讓php支持memcached
1.在http://pecl.php.net/package/memcache 選擇相應想要下載的memcache版本。
2.安裝PHP的memcache擴展
- #tar vxzf memcache-2.2.1.tgz
- #cd memcache-2.2.1
- #/usr/local/webserver/php/bin/phpize
- #./configure--enable-memcache --with-php-config=/usr/local/webserver/php/bin/php-config --with-zlib-dir
- #make
- #make install
3.上述安裝完後會有類似這樣的提示:
- Installing shared extensions: /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/
4,在php.ini 添加一行來載入memcache擴展:
- #extension = "memcache.so"
如下圖
5,重新加載php 配置文件
啓動memcached
- # /usr/local/bin/memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 10240 -P /tmp/memcached.pid
端口已正常啓動。
- memcache啓動參數備註:
- -d選項是啓動一個守護進程,
- -m是分配給Memcache使用的內存數量,單位是MB,我這裏是10MB,
- -u是運行Memcache的用戶,我這裏是root,
- -l是監聽的服務器IP地址,爲了安全我這裏指定了 127.0.0.1,
- -p是設置Memcache監聽的端口,我這裏設置了11211,最好是1024以上的端口,
- -c選項是最大運行的併發連接數,默認是1024,我這裏設置10240,按照你服務器的負載量來設定
- -P是設置保存Memcache的pid文件,我這裏是保存在 /tmp/memcached.pid,
Memcached安裝完畢。我們來測試一下是否能緩存mysql數據
我們在此服務器上架設一個php網站,此處我先用的是佔用資源比較大的magento程序網站,調試好配置文件,綁定一下host文件。看網站是否能正常打開。
好,網站打開正常,接下來我們看一下,memcached是否發揮了他的作用
這裏我們用一個軟件 memadmin (網上可以下載到) 來監控一下memcached的緩存命中率,如下圖:
命中率還是挺高的。
4,varnish服務器安裝
以下在192.168.1.20 和 192.168.1.21 上操作
下載varnish軟件 (此處下載3.0版本)
- #wget http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz
- #tar -xvf varnish-3.0.0.tar.gz
- #cd varnish-3.0.0
- #./configure --prefix=/usr/local/varnish
- #make &&make install
至此安裝完成。
我們來配置一下
- #vim /usr/local/varnish/etc/varnish/vcl.conf
以下爲配置代碼:(除backend 及vcl_recv外,其他地方無需更改,這裏是更改其訪問的主機名,及定義後端服務器地址,字數有限,其他代碼這裏就不解釋了)
- #Cache for linuxtone sites
- #backend vhost
- backend wwwyaozhibingceshicom {
- .host = "www.yaozhibingceshi.com";
- .port = "80";
- }
- #acl
- acl purge {
- "localhost";
- "127.0.0.1";
- "192.168.0.0"/24;
- }
- sub vcl_recv {
- if (req.http.Accept-Encoding) {
- if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) {
- remove req.http.Accept-Encoding;
- remove req.http.Cookie;
- } else if (req.http.Accept-Encoding ~ "gzip") {
- set req.http.Accept-Encoding = "gzip";
- } else if (req.http.Accept-Encoding ~ "deflate") {
- set req.http.Accept-Encoding = "deflate";
- } else {
- remove req.http.Accept-Encoding;
- }
- }
- if (req.http.host ~ "(.*)yaozhibingceshi.com") {
- set req.backend = wwwyaozhibingceshicom;
- }
- else {
- error 404 "This website is maintaining or not exist!";
- }
- if (req.request == "PURGE") {
- if (!client.ip ~purge) {
- error 405 "Not Allowed";
- }
- #.dd.....
- return(lookup);
- }
- #...GET...url...jpg,png,gif. ..cookie
- 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)$") {
- unset req.http.cookie;
- }
- #..GET...url.php....cache....
- if (req.request =="GET"&&req.url ~ "\.php($|\?)"){
- return (pass);
- }
- # if (req.restarts == 0) {
- if (req.http.x-forwarded-for) {
- set req.http.X-Forwarded-For =
- req.http.X-Forwarded-For + ", " + client.ip;
- } else {
- set req.http.X-Forwarded-For = client.ip;
- }
- # }
- #........pipe..
- if (req.request != "GET" &&
- req.request != "HEAD" &&
- req.request != "PUT" &&
- req.request != "POST" &&
- req.request != "TRACE" &&
- req.request != "OPTIONS" &&
- req.request != "DELETE") {
- return (pipe);
- }
- #..GET .HEAD.....
- if (req.request != "GET" && req.request != "HEAD") {
- return (pass);
- }
- if (req.http.Authorization) {
- return (pass);
- }
- return (lookup);
- }
- #..url+host hash......
- sub vcl_hash {
- hash_data(req.url);
- if (req.http.host) {
- hash_data(req.http.host);
- } else {
- hash_data(server.ip);
- }
- return (hash);
- }
- # .....purge .....
- sub vcl_hit {
- if (req.request == "PURGE") {
- set obj.ttl = 0s;
- error 200 "Purged";
- }
- return (deliver);
- }
- sub vcl_fetch {
- if (req.url ~ "\.(jpeg|jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico|swf|flv|dmg|js|css|html|htm)$") {
- set beresp.ttl = 2d;
- set beresp.http.expires = beresp.ttl;
- set beresp.http.Cache-Control = "max-age=172800";
- unset beresp.http.set-cookie;
- }
- if (req.url ~ "\.(dmg|js|css|html|htm)$") {
- set beresp.do_gzip = true;
- }
- if (beresp.status == 503) {
- set beresp.saintmode = 15s;
- }
- }
- sub vcl_deliver {
- set resp.http.x-hits = obj.hits ;
- if (obj.hits > 0) {
- set resp.http.X-Cache = "HIT You!";
- } else {
- set resp.http.X-Cache = "MISS Me!";
- }
- }
配置文件保存退出 ,並啓動 varnish,如下圖
由於我這域名沒有註冊,所以我們在這兩臺機器的hosts文件裏綁定一下
另外一臺機器我們同樣配置
Varnish 已經成功啓動了,我們來分別把域名在本地綁定一下,直接訪問varnish服務器,看是否能正常打開網站,並查看緩存閤中率
從上圖可以看出來,綁定varnish 服務器網站是可以正常打開,而且從varnish自帶的命令 varnishstat 來看,平均緩存命中率能達到 92%,命中率也是非常的高!另一臺同樣的測試方法
至此,varnish 服務器集羣安裝配置完成。
5,Haproxy 服務器安裝配置
以下配置在 192.168.1.5;192.168.1.6 服務器上運行
下載 haproxy 軟件 (目前最新版本爲1.4.19)
- wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.19.tar.gz
- #tar -xvf haproxy-1.4.19.tar.gz
- #cd haproxy-1.4.19
- #make TARGET=linux26 PREFIX=/usr/local/haproxy
- #make install PREFIX=/usr/local/haproxy
安裝完成,接下來配置一下 我解析幾個比較重要的參數
- # cd /usr/local/haproxy
- # vim haproxy.conf
- global
- maxconn 5120
- chroot /usr/local/haproxy
- uid 99
- gid 99
- daemon
- quiet
- nbproc 2
- pidfile /usr/local/haproxy/haproxy.pid
- defaults
- log global
- mode http
- option httplog
- option dontlognull
- log 127.0.0.1 local3
- retries 3
- option redispatch
- maxconn 20000
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
- listen www.yaozhibingceshi.com 0.0.0.0:80
- mode http
- stats uri /status (後端服務器狀態查看地址)
- stats realm Haproxy\ statistics
- stats auth admin:admin (狀態查看頁面登陸帳號密碼)
- balance source (調度算法,調度算法有很多,我這裏用source是和nginx的ip_hash同理,解決session問題)
- option httpclose
- option forwardfor
- server app1_1 192.168.1.20:80 cookie app1inst1 check inter 2000 rise 2 fall 5
- 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