日均百萬PV架構第一彈(基本架構搭建)

規劃圖:

wKioL1NmHSSBBkvAAALvpfoClG8279.jpg

以上架構可在生產環境中可以做出多樣性的拆分與組合,文中四臺主機限於虛擬機資源受限情況


規劃如下: (結合圖片四臺主機的區分應該不難)

   slave1.king.com
       172.16.43.1
       DNS輪詢 -> slave1.king.com , slave2.king.com
       haproxy七層代理流量(文件類型)分離 -> imgs , text , dynamic
       keepalived 爲haproxy HA

   slave2.king.com
       172.16.43.2
       haproxy七層代理流量(文件類型)分離 -> imgs , text , dynamic
       keepalived 爲haproxy HA

   slave3.king.com
       172.16.43.3
       nginx虛擬主機組 -> imgs1.king.com , imgs2.king.com
                        text1.king.com , text2.king.com
                        dynamic1.king.com
       php-fpm模塊
       mysql數據庫
       nfs文件共享  /nfsshared

   slave4.king.com
       172.16.43.4
       nginx虛擬主機 -> dynamic2.king.com
       php-fpm模塊
       mysql-proxy(mmm)
       mysql數據庫


過程如下:
   1. 一臺haproxy(lvs)分離數據, dns解析雙haproxy    (slave1)
   2. 四個虛擬主機分離imgs,text 靜態測試                  (slave3)
   3. nginx , php-fpm , mysql 構建,測試動態代理        (slave3 , slave4)
   4. mysql-proxy 實現讀寫分離                                  (slave4)
   5. 實現雙主keepalived HA的haproxy                      (slave2 , slave1)


1. 一臺haproxy(lvs)分離數據, dns解析雙haproxy (slave1.king.com)

i) dns解析
yum -y install named
.
vim /etc/named.rfc1912.zones
    zone "king.com" IN {
        type master;
        file "king.com.zone";
    };
.
vim /var/named/king.com.zone
$TTL 600
@       IN      SOA     dns.king.com.   adminmail.king.com. (
                        2014050401
                        1H
                        5M
                        3D
                        12H )
        IN      NS      dns
dns     IN      A       172.16.43.1
www     IN      A       172.16.43.88
www     IN      A       172.16.43.188
.
# 啓動named
service named start
ii) haproxy配置
#安裝 yum -y install haproxy
#
vim /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
#
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 30000
#
listen stats
    mode http
    bind 0.0.0.0:8080
    stats enable
    stats hide-version
    stats uri     /haproxyadmin?stats
    stats realm   Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE
#
frontend http-in
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request  header Host len 20
    capture request  header Referer len 60
    acl img_static       path_beg       -i /images /imgs
    acl img_static       path_end       -i .jpg .jpeg .gif .png
    acl text_static      path_beg       -i / /static /js /css
    acl text_static      path_end       -i .html .shtml .js .css
#
    use_backend img_servers             if img_static
    use_backend text_servers            if text_static
    default_backend dynamic_servers
#
backend img_servers
    balance roundrobin
    server imgsrv1 imgs1.king.com:80 check maxconn 4000
    server imgsrv2 imgs2.king.com:80 check maxconn 4000
#
backend text_servers
    balance roundrobin
    server textsrv1 text1.king.com:80 check maxconn 4000
    server textsrv2 text2.king.com:80 check maxconn 4000
#
backend dynamic_servers
    balance roundrobin
    server websrv1 dynamic1.king.com:80 check maxconn 1000
    server websrv2 dynamic2.king.com:80 check maxconn 1000
iii) hosts配置
#vim /etc/hosts
172.16.43.1 slave1.king.com
172.16.43.2 slave2.king.com
172.16.43.3 slave3.king.com
172.16.43.4 slave4.king.com
#
172.16.43.3 imgs1.king.com
172.16.43.3 imgs2.king.com
172.16.43.3 text1.king.com
172.16.43.3 text2.king.com
172.16.43.3 dynamic1.king.com
172.16.43.4 dynamic2.king.com
#
#
# 啓動haproxy
service haproxy start


2. 四個虛擬主機分離imgs,text 靜態測試 (slave3.king.com)

i) nginx 安裝
   參見 http://apprentice.blog.51cto.com/2214645/1403422#nginx_install
ii) 配置 hosts 文件實現解析與 slave1.king.com hosts文件一致
iii) 配置nginx

#vim /etc/nginx/nginx.conf
#
#user  nobody;
worker_processes  1;
#
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#
#pid        logs/nginx.pid;
#
events {
    worker_connections  1024;
}
#
http {
    include       mime.types;
    default_type  application/octet-stream;
#
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$st,,atus $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
#
    #access_log  logs/access.log  main;
#
    sendfile        on;
    #tcp_nopush     on;
#
    #keepalive_timeout  0;
    keepalive_timeout  5;
#
    gzip  on;
#
    open_file_cache          max=10000 inactive=60s;
    open_file_cache_valid    60s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;
#
    proxy_cache_path /var/log/cache levels=1:2 keys_zone=web:100m max_size=1g inactive=12h;
#
    server {
        listen  80;
        server_name dynamic1.king.com;
#
        access_log  /var/log/nginx/dynamic1.access.log;
#
        location ~ \.php$ {
            root        /nfsshared/html;
            fastcgi_pass    172.16.43.3:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include     fastcgi_params;
        }
#
        location / {
            root    /nfsshared/html;
            proxy_cache web;
            index   index.php index.html index.htm;
        }
    }
#
    server {
        listen       80;
        server_name  imgs1.king.com;
#
        access_log  /var/log/nginx/imgs1.access.log;
#
        location ~* \.(jpg|png|gif|jpeg)$ {
            root /nfsshared/html/imgs;
        }
#
        location ~* \.(jpg|png|gif|jpeg)$ {
            root /nfsshared/html/images;
        }
#
            error_page  404              /404.html;
#
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
#
        server {
            listen       80;
            server_name  imgs2.king.com;
#
            access_log  /var/log/nginx/imgs2.access.log;
#
        location ~* \.(jpg|png|gif|jpeg)$ {
            root /nfsshared/html/imgs;
        }
#
        location ~* \.(jpg|png|gif|jpeg)$ {
            root /nfsshared/html/images;
        }
#
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
#
    server {
        listen       80;
        server_name  text1.king.com;
#
        access_log  /var/log/nginx/text1.access.log;
#
        location ~* \.html$ {
            root /nfsshared/html/static;
        }
#
        location ~* \.css$ {
            root /nfsshared/html/css;
        }
#
        location ~* \.js$ {
            root /nfsshared/html/js;
        }
#
            location / {
                root   /nfsshared/html/static;
                index  index.html index.htm;
            }
#
            error_page  404              /404.html;
#
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
#
    server {
        listen       80;
        server_name  text2.king.com;
#
        access_log  /var/log/nginx/text2.access.log;
#
        location ~* \.html$ {
            root /nfsshared/html/static;
        }
#
        location ~* \.css$ {
            root /nfsshared/html/css;
        }
#
        location ~* \.js$ {
            root /nfsshared/html/js;
        }
#
        location / {
            root   /nfsshared/html/static;
            index  index.html index.htm;
        }
#
        error_page  404              /404.html;
#
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

iv) 測試文件

vim /nfsshared/static/index.html
<html>
<head>
<link rel="stylesheet" href="../css/test.css">
<script src="../js/test.js"></script>
</head>
<body>
        <div id="testid" >測試文字</div>
        <img src="../imgs/2.jpg" height=200 width=200 />
        <img src="../images/1.jpg" height=200 width=200 />
</body>
</html>
#
vim /nfsshared/css/test.css
#testid {
    font-size: 40px;
    border: 10px red dashed;
}
#
vim /nfsshared/js/test.js
window.onload = function() {
    alert("加載頁面...使用js..");
}

v) 測試

訪問 imgs2.king.com/2.jpg  text1.king.com/test.css 均無問題

wKiom1NmKa-QkTvsAATlubnugH4870.jpg

wKioL1NmKYfidTEDAAFTRfxSLBo888.jpg


3. nginx , php-fpm , mysql 構建 (slave3.king.com , slave4.king.com)

#i) nginx安裝及配置
#
vim /etc/nginx/nginx.conf
server {
    listen       80;
    server_name  dynamic2.king.com;
#
    access_log  /var/log/nginx/dynamic2.access.log;
    open_file_cache          max=10000 inactive=60s;
    open_file_cache_valid    60s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;
#
    proxy_cache_path /var/log/cache levels=1:2 keys_zone=web:100m max_size=1g inactive=12h;
    location ~ \.php$ {
        root           /nfsshared/html;
        fastcgi_pass   172.16.43.4:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
#
    location / {
        root   /nfsshared/html;
        proxy_cache web;
        index  index.php index.html index.htm;
    }
}
ii) 解決php安裝依賴
yum -y groupinstall "Desktop Platform Development"
yum -y install libmcrypt-devel
yum -y install bzip2-devel
#
ii) 安裝php with fpm
tar xf php-5.4.19.tar.bz2
cd php-5.4.19
./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
make && make install
#
ii) 爲php提供配置文件:
cp php.ini-production /etc/php.ini
#
ii) 配置php-fpm
# 爲php-fpm提供SysV init腳本,並將其添加至服務列表:sapi在源碼包下
cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
#
# 爲php-fpm提供配置文件:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# 編輯php-fpm的配置文件:
vim /usr/local/php/etc/php-fpm.conf
# 配置fpm的相關選項爲你所需要的值,並啓用pid文件:
pid = /usr/local/php/var/run/php-fpm.pid
listen = 172.16.43.2:9000
iii) MariaDB安裝與配置(創建mysql的數據目錄)
mkdir /data
groupadd -r mysql
useradd -g mysql -r -s /sbin/nologin -M -d /data mysql
chown -R mysql:mysql /data
#
iii) 安裝二進制mysql
tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mysql-5.5.33-linux2.6-x86_64 mysql
cd mysql
chown -R mysql:mysql  .
mysql/scripts/mysql_install_db --user=mysql --datadir=/data
chown -R root  .
# 提供mysql的配置文件
cp support-files/my-large.cnf  /etc/my.cnf
# 需要添加如下行指定mysql數據文件的存放位置:
datadir = /data
#
iii) 爲mysql提供sysv服務腳本:
cd /usr/local/mysql
cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
添加至服務列表:
chkconfig --add mysqld
chkconfig mysqld on
echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
#
iii) 啓動服務並授權php服務器賬號訪問
service mysqld restart
mysql
grant all on *.* to 'root'@'172.16.%.%' identified by '123456';
flush privileges;

slave4.king.com也是如上的配置 nginx , php-fpm , mysql

   其他配置參見 http://apprentice.blog.51cto.com/2214645/1403422#php_install

4. mysql-proxy(mmm) 實現讀寫分離 (slave4.king.com)
   其他配置參見 http://apprentice.blog.51cto.com/2214645/1399141#mysql_mmm


5. 實現雙主keepalived HA的haproxy (slave2.king.com, slave1.king.com)
   其他配置參見 http://apprentice.blog.51cto.com/2214645/1404853#keepalivedHA


可用性測試,在slave2.king.com癱瘓後,vip轉移至slave1.king.com一個節點上

wKiom1N8YbmhlhqWAAbJBKbUu7I174.jpg

wKiom1NmKjzQfuChAAGxPMdjHEc044.jpg

wKioL1NmKiXj4YfBAAYUtAZp7yw923.jpg


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