设为首页 收藏本站
查看: 2574|回复: 0

[经验分享] 日均百万PV架构第一弹(基本架构搭建)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-5-19 14:51:42 | 显示全部楼层 |阅读模式
规划图:
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 安装
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

4. mysql-proxy(mmm) 实现读写分离 (slave4.king.com)

5. 实现双主keepalived HA的haproxy (slave2.king.com, slave1.king.com)

wKiom1NmKjzQfuChAAGxPMdjHEc044.jpg
wKioL1NmKiXj4YfBAAYUtAZp7yw923.jpg




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-19311-1-1.html 上篇帖子: 基于corosync+packmaker对http做高可用集群 下篇帖子: 日均百万PV架构第二弹(缓存时代来临)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表