• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

集群部署解决方案

服务器 开心洋葱 2843次浏览 0个评论

服务器说明(3web机情况,其他类似)

  • 本文档仅供参考,达到集群部署目的即可,不同运维都有自己的方案,生产环境的要求要复杂于以下文档,具体环境具体对待
  • 软件部署安装具体请参考 linux单机部署,集群部署可能不需按照单机部署那样所有软件都需要安装,根据服务器实际需要安装
  • 集群部署机器分配,由于机器有限,部分服务器同时用做多个服务
  • 实际情况下请根据自己的实际ip进行调整,包括各种配置中的都要根据您真实情况调整
    机器 功能 安装软件 说明
    192.168.10.228 负载均衡 haproxy 负载均衡服务器
    192.168.10.229 web1 nginx+php+rsync+inotify 主web机,并且推送代码到从web机
    192.168.10.230 web2 nginx+php+rsync 从web机
    192.168.10.231 web3 nginx+php+rsync 从web机
    192.168.10.232 memcached memcached 缓存服务器
    192.168.10.232 redis redis redis服务器
    192.168.10.232 NFS nginx+nfs-utils 资源,图片服务器
    192.168.10.233 mysql主 mysql 主数据库
    192.168.10.234 mysql从 mysql 从数据库
  • 关于一些配置文件以及相关目录说明
    名称 本次版本(2016.07) 相关说明
    nginx openresty/1.9.7.4 配置文件 /usr/local/nginx/conf/nginx.conf
    vhost配置文件 /usr/local/nginx/conf/vhosts/*.conf
    php 5.6.19 配置文件 /usr/local/php56/etc/php.ini
    php 扩展配置文件目录 /usr/local/php56/etc/php.d/
    zend 配置文件 /usr/local/php56/etc/php.d/Zend.ini
    memcached 配置文件 /usr/local/php56/etc/php.d/memcached.ini
    mysql 5.6.22 basedir /usr/local/mysql
    datadir /data/mysql/3306
    配置文件 /usr/local/mysql/my.cnf
    redis 3.0.3 配置文件 /etc/redis.conf
    memcached 1.4.4 配置文件 /etc/sysconfig/memcached
    rsync+inotify 1.4.4 配置文件 /etc/rsyncd.conf
    密码文件 /etc/rsyncd.passwd
    配合inotify监控脚本位置 /root/rsync.sh
    代码目录 /data/httpd

MySQL主从安装配置

MySQL主192.168.10.233, MySQL从192.168.10.234

服务器和服务器都安装MySQL

初始化yum源,将shopex-lnmp源加入到系统中,如果已经参照单机部署初始化过yum源了则省略此步骤

<code class="hljs sql">yum <span class="hljs-keyword">install</span> wget -y
cd /etc/yum.repos.d/
wget <span class="hljs-keyword">http</span>://mirrors.shopex.cn/shopex/shopex-lnmp/shopex-lnmp.repo
</code>

安装mysql

<code class="hljs sql">yum <span class="hljs-keyword">install</span> mysql -y
</code>

启动MySQL

<code class="hljs sql">/etc/init.d/mysqld <span class="hljs-keyword">start</span>
</code>

登录MySQL并且修改密码并且删除空用户

<code class="hljs ruby"><span class="hljs-comment">#这里设置 shopex123 为msyql登录密码</span>
mysql> UPDATE mysql.user SET password = PASSWORD(<span class="hljs-string">'shopex123'</span>) WHERE user = <span class="hljs-string">'root'</span>;
Query OK, <span class="hljs-number">4</span> rows affected (<span class="hljs-number">0</span>.<span class="hljs-number">00</span> sec)
Rows <span class="hljs-symbol">matched:</span> <span class="hljs-number">4</span>  <span class="hljs-symbol">Changed:</span> <span class="hljs-number">4</span>  <span class="hljs-symbol">Warnings:</span> <span class="hljs-number">0</span>

mysql> SELECT user,host,password FROM mysql.user;
+------+-----------------------+-------------------------------------------+
<span class="hljs-params">| user |</span> host                  <span class="hljs-params">| password                                  |</span>
+------+-----------------------+-------------------------------------------+
<span class="hljs-params">| root |</span> localhost             <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> localhost.localdomain <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> <span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span>             <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span>                   <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">|      |</span> localhost             <span class="hljs-params">|                                           |</span>
<span class="hljs-params">|      |</span> localhost.localdomain <span class="hljs-params">|                                           |</span>
+------+-----------------------+-------------------------------------------+
<span class="hljs-number">6</span> rows <span class="hljs-keyword">in</span> set (<span class="hljs-number">0</span>.<span class="hljs-number">00</span> sec)

mysql> DROP user <span class="hljs-string">''</span>@localhost;
Query OK, <span class="hljs-number">0</span> rows affected (<span class="hljs-number">0</span>.<span class="hljs-number">00</span> sec)

mysql> DROP user <span class="hljs-string">''</span>@localhost.localdomain;
Query OK, <span class="hljs-number">0</span> rows affected (<span class="hljs-number">0</span>.<span class="hljs-number">00</span> sec)

mysql> SELECT user,host,password FROM mysql.user;
+------+-----------------------+-------------------------------------------+
<span class="hljs-params">| user |</span> host                  <span class="hljs-params">| password                                  |</span>
+------+-----------------------+-------------------------------------------+
<span class="hljs-params">| root |</span> localhost             <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> localhost.localdomain <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> <span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span>             <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span>                   <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
+------+-----------------------+-------------------------------------------+
<span class="hljs-number">4</span> rows <span class="hljs-keyword">in</span> set (<span class="hljs-number">0</span>.<span class="hljs-number">00</span> sec)
</code>

配置

修改主服务器master192.168.10.233 vim /usr/local/mysql/my.cnf

<code class="hljs ini"><span class="hljs-section">[mysqld]</span>
<span class="hljs-attr">log-bin</span>=mysql-bin   #[必须]启用二进制日志
<span class="hljs-attr">server-id</span>=<span class="hljs-number">1</span>      #[必须]服务器唯一ID,默认是<span class="hljs-number">1</span>,一般取IP最后一段
</code>

修改从服务器slave192.168.10.234 vim /usr/local/mysql/my.cnf

<code class="hljs ini"><span class="hljs-section">[mysqld]</span>
<span class="hljs-attr">log-bin</span>=mysql-bin   #[不是必须]启用二进制日志
<span class="hljs-attr">server-id</span>=<span class="hljs-number">2</span>      #[必须]服务器唯一ID,默认是<span class="hljs-number">1</span>,一般取IP最后一段
</code>

重启两台服务器/etc/init.d/mysql restart

登录master服务器,给Slave服务器授权

<code class="hljs ruby">mysql>  GRANT REPLICATION SLAVE ON *.* to <span class="hljs-string">'root'</span>@<span class="hljs-string">'192.168.10.234'</span> identified by <span class="hljs-string">'shopex123'</span>;
Query OK, <span class="hljs-number">0</span> rows affected (<span class="hljs-number">0</span>.<span class="hljs-number">00</span> sec)

mysql>  SELECT user,host,password FROM mysql.user;
+------+-----------------------+-------------------------------------------+
<span class="hljs-params">| user |</span> host                  <span class="hljs-params">| password                                  |</span>
+------+-----------------------+-------------------------------------------+
<span class="hljs-params">| root |</span> localhost             <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> localhost.localdomain <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> <span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span>             <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span>                   <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
<span class="hljs-params">| root |</span> <span class="hljs-number">192.168</span>.<span class="hljs-number">10.234</span>        <span class="hljs-params">| *13EB6A5E957F69117458EF4E95776C9B0BB9FA75 |</span>
+------+-----------------------+-------------------------------------------+
<span class="hljs-number">6</span> rows <span class="hljs-keyword">in</span> set (<span class="hljs-number">0</span>.<span class="hljs-number">00</span> sec)
</code>

查询主数据库状态

<code class="hljs ruby">mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
<span class="hljs-params">| File             |</span> Position <span class="hljs-params">| Binlog_Do_DB |</span> Binlog_Ignore_DB <span class="hljs-params">| Executed_Gtid_Set |</span>
+------------------+----------+--------------+------------------+-------------------+
<span class="hljs-params">| mysql-bin.000001 |</span>      <span class="hljs-number">640</span> <span class="hljs-params">|              |</span>                  <span class="hljs-params">|                   |</span>
+------------------+----------+--------------+------------------+-------------------+
<span class="hljs-number">1</span> row <span class="hljs-keyword">in</span> set (<span class="hljs-number">0</span>.<span class="hljs-number">00</span> sec)

<span class="hljs-comment">#这里需要记录 File 以及 Position 的值,在操作从服务器时会用到</span>
</code>

配置Slave 服务器

<code class="hljs bash"><span class="hljs-comment"># 登录mysql Slave 服务器</span>
[root@localhost ~]<span class="hljs-comment"># mysql -uroot -p</span>
Enter password:

<span class="hljs-comment"># 执行同步SQL语句</span>
mysql> change master to master_host=<span class="hljs-string">'192.168.10.233'</span>,master_user=<span class="hljs-string">'root'</span>,master_password=<span class="hljs-string">'shopex123'</span>,master_<span class="hljs-built_in">log</span>_file=<span class="hljs-string">'mysql-bin.000001'</span>,master_<span class="hljs-built_in">log</span>_pos=640;
Query OK, 0 rows affected (0.03 sec)

<span class="hljs-comment"># 启动同步进程</span>
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

<span class="hljs-comment"># 主从同步检查</span>
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting <span class="hljs-keyword">for</span> master to send event
                  Master_Host: 192.168.10.233
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 640
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 640
              Relay_Log_Space: 460
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 4bbab569-4cb1-11e6-86f5<span class="hljs-_">-d</span>4bed9a810af
             Master_Info_File: /data/mysql/3306/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has <span class="hljs-built_in">read</span> all relay <span class="hljs-built_in">log</span>; waiting <span class="hljs-keyword">for</span> the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row <span class="hljs-keyword">in</span> <span class="hljs-built_in">set</span> (0.00 sec)
<span class="hljs-comment"># 可以看到:Slave_IO_Running | Slave_SQL_Running两个值都是YES,说明配置成功了</span>
</code>

主从服务器测试

<code class="hljs bash"><span class="hljs-comment"># 主服务器Mysql,建立数据库,并在这个库中建表插入一条数据</span>
mysql> create database <span class="hljs-built_in">test</span>_db;
Query OK, 1 row affected (0.00 sec)

mysql> use <span class="hljs-built_in">test</span>_db;
Database changed

mysql>  create table <span class="hljs-built_in">test</span>_tb(id int(3),name char(10));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into <span class="hljs-built_in">test</span>_tb values(001,<span class="hljs-string">'test'</span>);
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| <span class="hljs-built_in">test</span>_db                |
| mysql                |
| <span class="hljs-built_in">test</span>                 |
+--------------------+
4 rows <span class="hljs-keyword">in</span> <span class="hljs-built_in">set</span> (0.00 sec)

<span class="hljs-comment"># 从服务器查看</span>
mysql>  show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| <span class="hljs-built_in">test</span>               |
| <span class="hljs-built_in">test</span>_db            |
+--------------------+
5 rows <span class="hljs-keyword">in</span> <span class="hljs-built_in">set</span> (0.00 sec)

mysql> use <span class="hljs-built_in">test</span>_db;
Database changed

mysql> show tables;
+-------------------+
| Tables_<span class="hljs-keyword">in</span>_<span class="hljs-built_in">test</span>_db |
+-------------------+
| <span class="hljs-built_in">test</span>_tb           |
+-------------------+
1 row <span class="hljs-keyword">in</span> <span class="hljs-built_in">set</span> (0.00 sec)

mysql> select * from <span class="hljs-built_in">test</span>_tb;
+------+------+
| id   | name |
+------+------+
|    1 | <span class="hljs-built_in">test</span> |
+------+------+
1 row <span class="hljs-keyword">in</span> <span class="hljs-built_in">set</span> (0.00 sec)
</code>

主web配置Mysql主从

MySQL主服务器给web服务器授权访问

<code class="hljs sql"><span class="hljs-keyword">GRANT</span> ALL <span class="hljs-keyword">PRIVILEGES</span>  <span class="hljs-keyword">ON</span> *.* <span class="hljs-keyword">to</span> <span class="hljs-string">'root'</span>@<span class="hljs-string">'192.168.10.229'</span> <span class="hljs-keyword">identified</span> <span class="hljs-keyword">by</span> <span class="hljs-string">'shopex123'</span>;
</code>

web服务器配置

修改数据库配置文件vim /data/httpd/config/production/database.php

<code class="hljs php"><span class="hljs-comment">/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
| 对应原系统: DB_*
|
*/</span>

<span class="hljs-string">'connections'</span> => <span class="hljs-keyword">array</span>(
    <span class="hljs-comment">#'default' => array(</span>
    <span class="hljs-comment">#    'driver'    => 'mysqli',</span>
    <span class="hljs-comment">#    'host'      => '192.168.10.233',</span>
    <span class="hljs-comment">#    'dbname'  => 'b2b2c',</span>
    <span class="hljs-comment">#    'user'  => 'root',</span>
    <span class="hljs-comment">#    'password'  => 'shopex123',</span>
    <span class="hljs-comment">#    'charset'   => 'utf8',</span>
    <span class="hljs-comment">#    //'collation' => 'utf8_general_ci',</span>
    <span class="hljs-comment">#),</span>
    <span class="hljs-string">'default'</span> => <span class="hljs-keyword">array</span>(
        <span class="hljs-string">'master'</span> => <span class="hljs-keyword">array</span>(<span class="hljs-string">'user'</span> => <span class="hljs-string">'root'</span>, <span class="hljs-string">'password'</span> => <span class="hljs-string">'shopex123'</span>, <span class="hljs-string">'host'</span> => <span class="hljs-string">'192.168.10.233'</span>, <span class="hljs-string">'dbname'</span> => <span class="hljs-string">'b2b2c'</span>,<span class="hljs-string">'charset'</span>   => <span class="hljs-string">'utf8'</span>),
        <span class="hljs-string">'slaves'</span> => <span class="hljs-keyword">array</span>(
            <span class="hljs-keyword">array</span>(<span class="hljs-string">'user'</span> => <span class="hljs-string">'root'</span>, <span class="hljs-string">'password'</span>=><span class="hljs-string">'shopex123'</span>, <span class="hljs-string">'host'</span> => <span class="hljs-string">'192.168.10.234'</span>, <span class="hljs-string">'dbname'</span> => <span class="hljs-string">'b2b2c'</span>,<span class="hljs-string">'charset'</span>   => <span class="hljs-string">'utf8'</span>),
         ),
        <span class="hljs-string">'driver'</span>    => <span class="hljs-string">'mysqli'</span>,
    ),
),
</code>

安装Redis

初始化yum源,将shopex-lnmp源加入到系统中,如果已经参照单机部署初始化过yum源了则省略此步骤

<code class="hljs sql">yum <span class="hljs-keyword">install</span> wget -y
cd /etc/yum.repos.d/
wget <span class="hljs-keyword">http</span>://mirrors.shopex.cn/shopex/shopex-lnmp/shopex-lnmp.repo
yum <span class="hljs-keyword">install</span> epel-<span class="hljs-keyword">release</span> -y
</code>

安装

<code class="hljs sql">yum <span class="hljs-keyword">install</span> redis -y
</code>

配置

redis开启远程访问

<code class="hljs nginx"><span class="hljs-attribute">vim</span> /etc/redis.conf
注释掉绑定<span class="hljs-number">127.0.0.1</span>的访问,则所有局域网都能访问
<span class="hljs-comment">#bind 127.0.0.1</span>
</code>

启动

<code class="hljs sql">/etc/init.d/redis <span class="hljs-keyword">start</span>
</code>

安装Memcached

初始化yum源,将shopex-lnmp源加入到系统中,如果已经参照单机部署初始化过yum源了则省略此步骤

<code class="hljs sql">yum <span class="hljs-keyword">install</span> wget -y
cd /etc/yum.repos.d/
wget <span class="hljs-keyword">http</span>://mirrors.shopex.cn/shopex/shopex-lnmp/shopex-lnmp.repo
yum <span class="hljs-keyword">install</span> epel-<span class="hljs-keyword">release</span> -y
</code>

安装

<code class="hljs sql">yum <span class="hljs-keyword">install</span> memcached -y
</code>

配置

<code class="hljs">配置文件位置/etc/sysconfig/memcached
可以修改内存大小等配置
</code>

启动

<code class="hljs sql">/etc/init.d/memcached <span class="hljs-keyword">start</span>
</code>

静态资源服务器配置

通过NFS的方式将web服务器中的图片,模版,js登静态资源进行挂在到静态资源服务器,然后静态资源都通过该服务器进行调用

NFS安装

初始化yum源

将shopex-lnmp源加入到系统中,在线地址查看有哪些软件 http://mirrors.shopex.cn/shopex/shopex-lnmp/ 可以安装

<code class="hljs ruby">cd /etc/yum.repos.d/
wget <span class="hljs-symbol">http:</span>/<span class="hljs-regexp">/mirrors.shopex.cn/shopex</span><span class="hljs-regexp">/shopex-lnmp/shopex</span>-lnmp.repo
</code>

安装epel扩展源

<code class="hljs sql">yum <span class="hljs-keyword">install</span> epel-<span class="hljs-keyword">release</span> -y
</code>

web服务器和storage服务器都安装nfs

<code class="hljs nginx"><span class="hljs-attribute">yum</span> install nfs-utils -y

<span class="hljs-comment">#根据Centos选择安装</span>
yum install portmap -y <span class="hljs-comment">#(适用centos 5)</span>
yum install rpcbind -y <span class="hljs-comment">#(适用centos 6)</span>
</code>

启动NFS服务,启动顺序一定要先启动rpcbind,后在启动nfs

<code class="hljs sql">/etc/init.d/rpcbind <span class="hljs-keyword">start</span>
/etc/init.d/nfs <span class="hljs-keyword">start</span>
</code>

storage服务器都安装nginx

<code class="hljs sql">yum <span class="hljs-keyword">install</span> ngx_openresty -y
</code>

NFS配置

将web服务器中已有的资源复制到storage服务器

<code class="hljs ruby">scp -r /data/httpd/public/themes/ root@192.<span class="hljs-number">168.10</span>.<span class="hljs-number">232</span><span class="hljs-symbol">:/data/httpd/</span>
scp -r /data/httpd/public/images/ root@192.<span class="hljs-number">168.10</span>.<span class="hljs-number">232</span><span class="hljs-symbol">:/data/httpd/</span>
scp -r /data/httpd/public/app/ root@192.<span class="hljs-number">168.10</span>.<span class="hljs-number">232</span><span class="hljs-symbol">:/data/httpd/</span>
</code>

在storage服务器vim /etc/exports,加入如下参数

<code class="hljs markdown"><span class="hljs-section">#可以使用*不限制挂载机器</span>
<span class="hljs-section">#/data/httpd/themes  *(rw,sync,all_squash,anonuid=502,anongid=502)</span>

/data/httpd/themes  192.168.10.229/24(rw,sync,all_squash,anonuid=502,anongid=502)
/data/httpd/images  192.168.10.229/24(rw,sync,all_squash,anonuid=502,anongid=502)
/data/httpd/app     192.168.10.229/24(rw,sync,all_squash,anonuid=502,anongid=502)
</code>

修改好执行命令exportfs -rv

查看是否成功

<code class="hljs nginx"><span class="hljs-attribute">showmount</span> -e <span class="hljs-number">192.168.10.232</span>
Export list for <span class="hljs-number">192.168.10.232</span>:
/data/httpd/app    <span class="hljs-number">192.168.10.229</span>/<span class="hljs-number">24</span>
/data/httpd/images <span class="hljs-number">192.168.10.229</span>/<span class="hljs-number">24</span>
/data/httpd/themes <span class="hljs-number">192.168.10.229</span>/<span class="hljs-number">24</span>
</code>
  • web服务器中静态资源挂载到storage服务器
<code class="hljs ruby">mount <span class="hljs-number">192.168</span>.<span class="hljs-number">10.232</span><span class="hljs-symbol">:/data/httpd/themes</span> /data/httpd/public/themes
mount <span class="hljs-number">192.168</span>.<span class="hljs-number">10.232</span><span class="hljs-symbol">:/data/httpd/images</span> /data/httpd/public/images
mount <span class="hljs-number">192.168</span>.<span class="hljs-number">10.232</span><span class="hljs-symbol">:/data/httpd/app</span>    /data/httpd/public/app
</code>

解决跨域加载字体问题:firefox和IE9不支持对icon font字体的跨域访问

静态资源服务器修改nginx配置文件

<code class="hljs perl">vim /usr/<span class="hljs-keyword">local</span>/nginx/conf/vhosts/default.conf

server {
    ...
    <span class="hljs-comment">#在server中新增如果下配置</span>
    location ~* \.(eot|ttf|woff)$ {
        add_header Access-Control-Allow-Origin *;
    }
    ...
}

新增 AddType

vim /usr/<span class="hljs-keyword">local</span>/nginx/conf/mime.types

font/ttf                              ttf;
font/otf                              otf;
</code>

主web配置

复制配置文件到production cp /data/httpd/config/storager.php /data/httpd/config/production/storager.php

修改配置文件vim /data/httpd/config/production/storager.php

<code class="hljs php">    <span class="hljs-comment">/*
    |--------------------------------------------------------------------------
    | 静态资源映象站地址(js css)
    |--------------------------------------------------------------------------
    |
    | 资源映像站地址
    |
    */</span>
    <span class="hljs-string">'host_mirrors'</span> => <span class="hljs-keyword">array</span>(
        <span class="hljs-string">'http://192.168.10.232'</span>,
        <span class="hljs-comment">//'http://img2.example.com',</span>
    ),

    <span class="hljs-comment">/*
    |--------------------------------------------------------------------------
    | 图片映象站地址
    |--------------------------------------------------------------------------
    |
    | 图片资源映像站地址
    | 一个域名标识对应一个域名,在替换的时候域名的时候替换对应的值就可以了
    | 图片域名标识不可变,添加后慎重删除,除非确保改标识下不存在图片了
    | 如果未配置则默认使用 defaultHostMirrors 作为图片标识
    */</span>
    <span class="hljs-string">'host_mirrors_img'</span> => [
        <span class="hljs-string">'img0'</span>=><span class="hljs-string">'http://192.168.10.232'</span>,
        <span class="hljs-comment">//'img1'=>'http://img2.example.com',</span>
    ],
</code>

web机代码同步,采取 rsync+inotify方式

说明:这里的配置是采取单向同步,采取 主web机 向 从web机 推送代码,如要双向同步,请自行配置

安装配置 rsync (主web机和从web机 都需要安装)

  • 安装 rsync
    <code class="shell hljs sql">yum <span class="hljs-keyword">install</span> rsync -y
    </code>
  • 编辑 rsyncd.conf 配置文件,如果没有则创建一个,主web服务器也可以不配置此文档,但是为了统一也可以配置一下
  • 并且约定从web机中的rsyncd配置的用户名和密码全部一样,如rsyncd.passwd 中为rsync:123456,并且模块名也一样,如rsyncd.conf中为salve_web
  • 同步的时候注意防火墙问题可能导致无法同步
    <code class="hljs nginx"><span class="hljs-attribute">vim</span> /etc/rsyncd.conf
    
    <span class="hljs-comment">#添加以下内容</span>
    </code>
    <code class="hljs makefile"><span class="hljs-comment">#rsync通用配置文件,配置的注释不要写在配置后面,否则会有问题</span>
    
    uid = root
    gid = root
    use chroot = 0
    port = 873
    <span class="hljs-comment">#允许ip访问设置,请根据实际需要进行配置,这里为了方便设为全网段 *,生产环境下为了安全请指定ip或ip段</span>
    <span class="hljs-comment"># hosts allow = 192.168.0.1/255.255.255.0 198.162.145.1 10.0.1.0/255.255.255.0</span>
    hosts allow = *
    max connections = 0
    timeout = 300
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    log file = /var/log/rsyncd.log
    log format = %t %a %m %f %b
    transfer logging = yes
    syslog facility = local3
    
    <span class="hljs-comment">#方括号中为模块声明,对应命名,这里master_web对应了主web机配置,从服务器可都为[slave_web],方便inotify脚本配置</span>
    [master_web]
    <span class="hljs-comment">#指定当前模块在rsync服务器上的同步路径,该参数是必须指定的</span>
    path = /data/httpd/
    <span class="hljs-comment">#注释,可以同模块名一样,从服务器可都为slave_web</span>
    comment = master_web
    ignore errors
    <span class="hljs-comment">#是否允许客户端上传文件</span>
    read only = no
    list = no
    <span class="hljs-comment">#指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块</span>
    auth users = rsync
    <span class="hljs-comment">#保存密码和用户名文件,需要自己生成</span>
    secrets file = /etc/rsyncd.passwd
    </code>
  • 编辑 rsyncd.passwd 密码配置文件,如果没有则创建一个
    <code class="hljs nginx"><span class="hljs-attribute">vim</span> /etc/rsyncd.passwd
    </code>

    注意:添加以下内容的时候,千万要看清楚,不要全部复制粘贴,分主web服务器和从web服务器

    <code class="hljs markdown"><span class="hljs-section">#从web服务器配置内容,需要填写 用户名:密码 ,多个用户名密码则每添加多行,建议从web机都是一样的用户名和密码</span>
    
    rsync:123456
    
    <span class="hljs-section">#主web服务器配置内容,只需要填写从服务器的密码,例如这里从服务器配的用户名密码都是rsync:123456,这里主服务器则写123456一个就可以了</span>
    
    123456
    </code>
  • 修改密码文件 /etc/rsyncd.passwd 的权限为600
    <code class="hljs perl"><span class="hljs-comment">#修改密码文件权限为600</span>
    <span class="hljs-keyword">chmod</span> <span class="hljs-number">600</span> /etc/rsyncd.passwd
    </code>
  • 启动 rsync 服务并设置开机自启动。下面两行分别执行,不要全部复制粘贴
    <code class="hljs ruby"><span class="hljs-comment">#以守护进程方式启动rsync服务</span>
    /usr/bin/rsync --daemon --config=<span class="hljs-regexp">/etc/rsyncd</span>.conf
    
    <span class="hljs-comment">#添加开机自启动</span>
    echo <span class="hljs-string">"/usr/bin/rsync --daemon --config=/etc/rsyncd.conf"</span><span class="hljs-meta">>></span>/etc/rc.local
    </code>
  • 验证代码是否能够同步, 在web服务器上执行下面命令,如有报错,请自行查找原因,
    <code class="shell hljs ruby"><span class="hljs-comment"># 主web服务器推送代码到从web服务器,命令中的</span>
    <span class="hljs-comment"># rsync@192.168.10.230::slave_web</span>
    <span class="hljs-comment"># 192.168.10.230 为从web服务器的ip,</span>
    <span class="hljs-comment"># slave_web 为从web服务器的 rsyncd.conf 中配置的模块名,</span>
    <span class="hljs-comment"># rsync 为为从web服务器的中 rsyncd.passwd 中配置的用户名</span>
    rsync -vzrtopg --delete --progress /data/httpd/ rsync@192.<span class="hljs-number">168.10</span>.<span class="hljs-number">230</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:slave_web</span>  --password-file=<span class="hljs-regexp">/etc/rsyncd</span>.passwd
    </code>

安装配置 inotify (主web机需要安装,从web机不需要)

  • 安装 inotify
    <code class="hljs sql">yum <span class="hljs-keyword">install</span> inotify-tools
    </code>
  • 配置rsync.sh同步监控脚本
    <code class="hljs nginx"><span class="hljs-attribute">vim</span> /root/rsync.sh
    </code>

    添加以下内容

    <code class="hljs bash"><span class="hljs-meta">#!/bin/bash</span>
    src=/data/httpd/
    des=web2
    user=rsync
    <span class="hljs-comment">#多个ip就用空格间断,每个IP用双引号包起来</span>
    host=<span class="hljs-string">"192.168.10.230"</span>
    /usr/bin/inotifywait -mrq --exclude=public/images --exclude=public/themes --exclude=data/logs --timefmt <span class="hljs-string">'%d/%m/%y %H:%M'</span> --format <span class="hljs-string">'%T %w%f'</span> <span class="hljs-_">-e</span> modify,delete,create,attrib <span class="hljs-variable">$src</span> | <span class="hljs-keyword">while</span> <span class="hljs-built_in">read</span> file
    <span class="hljs-keyword">do</span>
        <span class="hljs-keyword">for</span> hostip <span class="hljs-keyword">in</span> <span class="hljs-variable">$host</span>
        <span class="hljs-keyword">do</span>
            rsync -vzrtopg --delete --progress --exclude=public/images --exclude=public/themes --exclude=data/logs <span class="hljs-variable">${src}</span> <span class="hljs-variable">${user}</span>@<span class="hljs-variable">${hostip}</span>::<span class="hljs-variable">${des}</span>  --password-file=/etc/rsyncd.passwd
            <span class="hljs-built_in">echo</span> <span class="hljs-string">"<span class="hljs-variable">${file}</span> was rsynced"</span> >> /tmp/rsync.log 2>&1
        <span class="hljs-keyword">done</span>
    <span class="hljs-keyword">done</span>
    </code>
  • 配置rsync.sh脚本的执行权限
    <code class="hljs perl"><span class="hljs-keyword">chmod</span> +<span class="hljs-keyword">x</span> /root/rsync.sh
    </code>
  • 运行rsync.sh同步监控脚本和配置守护进程
    <code class="hljs ruby"><span class="hljs-comment">#主web服务器 推送代码到 从web服务器 的时候主web服务器的rsync.passwd只需要配置密码就可以,不用配置用户名,从服务器最好配置同一个用户名和密码)</span>
    nohup sh /root/rsync.sh &
    
    <span class="hljs-comment">#建立守护进程运行rsync.sh脚本</span>
    echo <span class="hljs-string">"nohup sh /root/rsync.sh &"</span> <span class="hljs-meta">>> </span>/etc/rc.local
    </code>
  • 注意:如果inotify报错upper limit on inotify watches reached,要修改inotify的max_user_watches参数
    <code class="hljs nginx"><span class="hljs-comment">#编辑 /etc/sysctl.conf 文件,配置fs.inotify.max_user_watches,防止句柄数过多报错</span>
    <span class="hljs-attribute">vim</span> /etc/sysctl.conf
    <span class="hljs-comment">#加上下面这行,这个不是脚本,不要执行,复制内容到 /etc/sysctl.conf</span>
    fs.inotify.max_user_watches=<span class="hljs-number">8192000</span>
    <span class="hljs-comment">#配置好后,执行下面命令使配置生效</span>
    sysctl -p
    </code>

负载均衡

安装 haproxy

<code class="hljs sql">yum <span class="hljs-keyword">install</span> haproxy -y
</code>

配置

集群部署机器分配,具体请根据实际情况配置

机器 功能 端口
192.168.10.228 haproxy 80
192.168.10.229 web 1 80
192.168.10.230 web 2 80
192.168.10.231 web 3 80

192.168.10.228(负载均衡服务器) 编辑/etc/haproxy/haproxy.cfg文件

<code class="hljs nginx"><span class="hljs-attribute">vim</span> /etc/haproxy/haproxy.cfg(此配置不分前后台)
<span class="hljs-comment">#以下为配置内容,根据实际情况调整</span>
</code>
<code class="hljs sql">global
    log         127.0.0.1 local3
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     30000
    user        haproxy
    group       haproxy
    daemon

    stats socket /var/lib/haproxy/stats

defaults
    option http-keep-alive
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option accept-invalid-http-request
    option http-server-close
    option                  redispatch
    retries                 3
    timeout http-request    60s
    timeout queue           1m
    timeout http-keep-alive 60s
    timeout connect 60s
    timeout client 1m
    timeout server 1m
    timeout <span class="hljs-keyword">check</span> <span class="hljs-number">2000</span>
    maxconn                 <span class="hljs-number">30000</span>
    stats <span class="hljs-keyword">enable</span>
    stats uri /shopexadminss
    stats auth <span class="hljs-keyword">admin</span>:r4b7x3m7vVN3
frontend  <span class="hljs-keyword">main</span> *:<span class="hljs-number">80</span>
    default_backend             app

backend <span class="hljs-keyword">static</span>
    balance     roundrobin
    <span class="hljs-keyword">server</span>      <span class="hljs-keyword">static</span> <span class="hljs-number">192.168</span><span class="hljs-number">.10</span><span class="hljs-number">.229</span>:<span class="hljs-number">80</span> <span class="hljs-keyword">check</span>


backend app
    balance     roundrobin
    <span class="hljs-keyword">mode</span> <span class="hljs-keyword">http</span>
    cookie SERVERID <span class="hljs-keyword">insert</span> nocache indirect
    <span class="hljs-keyword">server</span> web1 <span class="hljs-number">192.168</span><span class="hljs-number">.10</span><span class="hljs-number">.229</span>:<span class="hljs-number">80</span>  <span class="hljs-keyword">check</span> inter <span class="hljs-number">1500</span> rise <span class="hljs-number">1</span> fall <span class="hljs-number">3</span> weight <span class="hljs-number">1</span>
    <span class="hljs-keyword">server</span> web2 <span class="hljs-number">192.168</span><span class="hljs-number">.10</span><span class="hljs-number">.230</span>:<span class="hljs-number">80</span>  <span class="hljs-keyword">check</span> inter <span class="hljs-number">1500</span> rise <span class="hljs-number">1</span> fall <span class="hljs-number">3</span> weight <span class="hljs-number">1</span>
    <span class="hljs-keyword">server</span> web3 <span class="hljs-number">192.168</span><span class="hljs-number">.10</span><span class="hljs-number">.231</span>:<span class="hljs-number">80</span>  <span class="hljs-keyword">check</span> inter <span class="hljs-number">1500</span> rise <span class="hljs-number">1</span> fall <span class="hljs-number">3</span> weight <span class="hljs-number">1</span>

</code>

启动 haproxy

<code class="hljs sql">/etc/init.d/haproxy <span class="hljs-keyword">start</span>
</code>

访问负载均衡服务器,检验效果

FAQ

1、同步的目录必须是已有的目录,并且权限正确

2、同步代码的配置中,请检查好模块名,密码等,主web服务器的密码文件中不需要写用户名,本文档中从web服务器之间的rsync配置的用户名和密码都是一致的


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明集群部署解决方案
喜欢 (0)

您必须 登录 才能发表评论!

加载中……