搭建小型LAMP架构-进阶一

1. 架构设置如下

14399383-f746fdafbd1d92d2.png
image.png
14399383-1ebe094545e6b191.png
image.png

2. 搭建dns服务器

  1. 安装dns包bind
[root@dns ~]#yum -y install bind
  1. 修改dns注配置文件
[root@dns-server ~]#vim /etc/named.conf
listen-on port 53 { 192.168.18.17;  };  注:监听端口写192.168.18.17就可以了
//  allow-query     { localhost; };   
 dnssec-enable no;
 dnssec-validation no;
注:定义区域
zone "testwpress.com" IN {
    type master;
    file "testwpress.com.zone";                                                              
};
  1. 建立区域数据库文件
[root@dns ~]#vim /etc/named.conf 
[root@dns ~]#vim /var/named/testwpress.com.zone 

$TTL    3600
$ORIGIN testwpress.com.
@   IN      SOA     ns1.testwpress.com.     admin.testwpress.com (
            2019051702                                                                             
            1H
            10M
            3D
            1D)
    IN      NS      ns1
    IN      MX  10  mx1
ns1 IN      A       192.168.18.17
mx1 IN      A       192.168.18.107
www IN      A       192.168.18.27
www IN      A       192.168.18.37

  1. 语法检查
[root@dns-server ~]# named-checkconf
[root@dns-server ~]#named-checkzone "testwpress.com" /var/named/testwpress.com.zone
[root@dns-server ~]#rndc reload  让服务器重载配置文件和区域数据文件
server reload successful
  1. 测试是否能解析
[root@dns ~]#dig www.testwpress.com @192.168.18.17

; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> www.testwpress.com @192.168.18.17
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65405
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.testwpress.com.        IN  A

;; ANSWER SECTION:
www.testwpress.com. 3600    IN  A   192.168.18.27
www.testwpress.com. 3600    IN  A   192.168.18.37

;; AUTHORITY SECTION:
testwpress.com.     3600    IN  NS  ns1.testwpress.com.

;; ADDITIONAL SECTION:
ns1.testwpress.com. 3600    IN  A   192.168.18.17

;; Query time: 1 msec
;; SERVER: 192.168.18.17#53(192.168.18.17)
;; WHEN: Sat May 25 16:41:08 CST 2019
;; MSG SIZE  rcvd: 113


3. 搭建mysql读写分离

3.1 主节点mas-mysql

[root@master-sql ~]#yum -y install mariadb-server

[root@master-sql ~]#vim /etc/my.cnf

[mysqld]
server_id=77
binlog_format=row
log_bin=/data/bin/mysql-bin

[root@master-sql ~]#mkdir /data/bin/
[root@master-sql ~]#chown mysql.mysql /data/bin
[root@master-sql ~]#systemctl start mariadb

安全加固
[root@master-sql ~]#mysql_secure_installation

[root@master-sql ~]#ll /data/bin/
total 940
-rw-rw---- 1 mysql mysql  26765 May 25 16:51 mysql-bin.000001
-rw-rw---- 1 mysql mysql 921736 May 25 16:51 mysql-bin.000002
-rw-rw---- 1 mysql mysql   2835 May 25 16:54 mysql-bin.000003
      注:2835起点位置在后面配置从节点要用
-rw-rw---- 1 mysql mysql     81 May 25 16:51 mysql-bin.index

创建从节点访问主节点数据库账号
[root@master-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant replication slave on *.* to 'repluser'@'192.168.18.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> quit
Bye

3.2 从节点slave1-mysql

  • 配置主配置文件
安装包
[root@slave1-sql ~]#yum -y install mariadb-server
修改配置文件
[root@slave1-sql ~]#vim /etc/my.cnf

[mysqld]
server_id=87                                                                                                 
read_only=ON
启动服务
[root@slave1-sql ~]#systemctl start mariadb

数据库安全加固
[root@slave1-sql ~]#mysql_secure_installation 

  • 配置同步信息
[root@slave1-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CHANGE MASTER TO
    -> MASTER_HOST='192.168.18.77',
    -> MASTER_USER='repluser',
    -> MASTER_PASSWORD='123456',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysql-bin.000003',
    -> MASTER_LOG_POS=2835;
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> start slave; 开启从节点的2个线程
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G  注:查看从节点状态
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.18.77
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 2992
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 686
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes   注:io线程运行 
            Slave_SQL_Running: Yes注:sql线程运行

3.3 从节点slave2-mysql

  • 配置主配置文件
[root@slave2-sql ~]#yum -y install mariadb-server
[root@slave2-sql ~]#vim /etc/my.cnf

[mysqld]
server_id=97                                                                                 
read_only=ON

[root@slave2-sql ~]#vim /etc/my.cnf
[root@slave2-sql ~]#systemctl start mariadb

安全加固
[root@slave2-sql ~]#mysql_secure_installation 

  • 配置同步信息
[root@slave2-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CHANGE MASTER TO
    -> MASTER_HOST='192.168.18.77',
    -> MASTER_USER='repluser',
    -> MASTER_PASSWORD='123456',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysql-bin.000003',
    -> MASTER_LOG_POS=2835;
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)


[root@slave2-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.18.77
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 2992
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 686
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

3.4 测试主从同步
主节点创建一个数据库

[root@master-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database db1;
Query OK, 1 row affected (0.00 sec)

从节点1

[root@slave1-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

从节点2

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

3.5 配置proxySQL调度器

  • proxySQL是通过专有的yum源进行安装
[root@proxymysql ~]#cat <<EOF | tee /etc/yum.repos.d/proxysql.repo
> [proxysql_repo]
> name= ProxySQL YUM repository
> baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
> gpgcheck=1
> gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
> EOF
[proxysql_repo]
name= ProxySQL YUM repository
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/$releasever
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key

[root@proxymysql ~]#yum -y install proxysql mariadb  
注:安装proxysql同时也把mariadb客户端装好,mariadb客户端后面测试需要用

root@proxymysql ~]#rpm -ql proxysql  查看生成的文件列表
/etc/init.d/proxysql    注:表明service的服务脚本,因此用service命令启动服务
/etc/proxysql.cnf  配置文件
/usr/bin/proxysql  主要的程序
/usr/share/proxysql/tools/proxysql_galera_checker.sh
/usr/share/proxysql/tools/proxysql_galera_writer.pl

  • 启动proxysql
[root@proxymysql ~]#service proxysql start
Starting ProxySQL: 2019-05-08 21:29:16 [INFO] Using config file /etc/proxysql.cnf
DONE!

[root@proxymysql ~]#ss -tnl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      128                   *:111                               *:*                  
LISTEN     0      128                   *:6032                              *:*                  
LISTEN     0      128                   *:6033                              *:*                  
LISTEN     0      128                   *:6033                              *:*                  
LISTEN     0      128                   *:6033                              *:*                  
LISTEN     0      128                   *:6033                              *:*                  
LISTEN     0      5         192.168.122.1:53                                *:*        
注:6032:ProxySQL的管理端口
6033:ProxySQL对外提供服务的端口,6033是接收远程用户连接
的,即相当于代理人,用户修改数据,proxysql转发给主节点,用
户读数据,proxysql转发给从节点
  • 使用mysql客户端连接到ProxySQL的管理接口6032,默认管理员用户和密码都是admin
[root@proxymysql ~]#mysql -uadmin -padmin -P6032 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
注:将主从服务器ip地址,端口号填入到mysql_servers表中(此表属于main数据库),前期将主从3个主机添加到10组,
后期ProxySQL通过每个节点的read_only值来自动调整它们是属于读组还是写组
MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.18.77',3306);
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.18.87',3306);
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.18.97',3306);
Query OK, 1 row affected (0.00 sec)

  • 在master-sql主服务器上添加监控后端节点的用户
注:在主服务器上添加监控后端的用户,从服务器自动将该用户同步到从服务器上。
[root@master-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show slave status\G
Empty set (0.01 sec)

MariaDB [(none)]> grant replication client on *.* to monitor@'192.168.18.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

  • ProxySQL上添加监控用户
MySQL [(none)]> set mysql-monitor_username='monitor';
Query OK, 1 row affected (0.01 sec)

MySQL [(none)]>  set mysql-monitor_password='123456';
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> show tables;
+--------------------------------------------+
| tables                                     |
+--------------------------------------------+
| global_variables                           |
| mysql_collations                           |
| mysql_group_replication_hostgroups         |

MySQL [(none)]> select * from global_variables\G
*************************** 117. row ***************************
 variable_name: mysql-monitor_username
variable_value: monitor
*************************** 118. row ***************************
 variable_name: mysql-monitor_password
variable_value: 123456

注:修改后必须执行LOAD … TO RUNTIME才能加载到RUNTIME生效
MySQL [(none)]> load mysql variables to runtime;
Query OK, 0 rows affected (0.00 sec)


注:执行save … to disk 才将配置持久化保存到磁盘,即保存在proxysql.db文件中
MySQL [(none)]> save mysql variables to disk;
Query OK, 97 rows affected (0.01 sec)
MySQL [(none)]> select * from mysql_servers;
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname      | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 192.168.18.77 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 192.168.18.87 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 192.168.18.97 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.01 sec)

  • 查看监控连接是否正常的 (对connect指标的监控):(如果connect_error的结果为NULL则表示正常)
MySQL [(none)]> select * from mysql_server_connect_log;
  • 设置分组信息
    需要修改的是main库中的mysql_replication_hostgroups表,该表有3个字段:writer_hostgroup,reader_hostgroup,comment, 指定写组的id为10,读组的id为20
MySQL [(none)]> insert into mysql_replication_hostgroups values(10,20,"test");
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]>  select * from mysql_replication_hostgroups;
+------------------+------------------+---------+
| writer_hostgroup | reader_hostgroup | comment |
+------------------+------------------+---------+
| 10               | 20               | test    |
+------------------+------------------+---------+
1 row in set (0.00 sec)

MySQL [(none)]>  load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)

MySQL [(none)]>  save mysql servers to disk;
Query OK, 0 rows affected (0.05 sec)

注:Monitor模块监控后端的read_only值,按照read_only的值将节点自动移动到读/写组
MySQL [(none)]> select hostgroup_id,hostname,port,status,weight from mysql_servers;
+--------------+---------------+------+--------+--------+
| hostgroup_id | hostname      | port | status | weight |
+--------------+---------------+------+--------+--------+
| 10           | 192.168.18.77 | 3306 | ONLINE | 1      |
| 20           | 192.168.18.97 | 3306 | ONLINE | 1      |
| 20           | 192.168.18.87 | 3306 | ONLINE | 1      |
+--------------+---------------+------+--------+--------+
3 rows in set (0.00 sec)

MySQL [(none)]> select * from mysql_servers;
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname      | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 192.168.18.77 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 192.168.18.97 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 192.168.18.87 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+---------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.00 sec)

  • 在master主节点上创建访问用户(配置发送SQL语句的用户)(为后面的wordpress应用创建账户)
MariaDB [(none)]> grant all on wordpress.* to wpuser@'192.168.18.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

  • 在ProxySQL配置,将用户wpuser添加到mysql_users表中, default_hostgroup默认组设置为写组10,当读写分离的路由规则不符合时,会访问默认组的数据库(配置发送SQL语句的用户)
MySQL [(none)]> insert into mysql_users(username,password,default_hostgroup) values('wpuser','123456',10);
Query OK, 1 row affected (0.01 sec)

MySQL [(none)]> select * from mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
| sqluser  | magedu   | 1      | 0       | 10                | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |
| wpuser   | 123456   | 1      | 0       | 10                | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+
2 rows in set (0.00 sec)

MySQL [(none)]> load mysql users to runtime;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> save mysql users to disk;
Query OK, 0 rows affected (0.01 sec)

  • 在proxysql上配置路由规则,实现读写分离(与规则有关的表)
MySQL [(none)]> insert into mysql_query_rules
    -> (rule_id,active,match_digest,destination_hostgroup,apply)VALUES
    -> (1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);
注:定义proxysql调度器调度规则,(2,1,'^SELECT',20,1)表示查询数
据库,调度器往20组发送,未定义(表示修改数据库)的往10组发

MySQL [(none)]> select * from mysql_query_rules\G 注:查看定义规则

ySQL [(none)]> load mysql query rules to runtime;  注:规则生效
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> save mysql query rules to disk;   注:规则保存
Query OK, 0 rows affected (0.02 sec)

4. 搭建NFS服务器

4.1 在nfs-server上

[root@nfs ~]#systemctl start nfs-server   注:启动nfs服务
[root@nfs ~]#systemctl enable nfs-server   注:设置开机自启

[root@nfs ~]#vim /etc/exports   注:在/etc/exports文件中定义导出目录

/data/www 192.168.18.27(all_squash,rw)
/data/www 192.168.18.37(all_squash,rw)     

[root@nfs ~]#mkdir /data/www/  注:创建挂载目录
[root@nfs ~]#chown nfsnobody /data/www/  注:设置访问过来的用户都一nfsnobody用户身份
[root@nfs ~]#exportfs -r    注:重读配置文件,并共享目录
[root@nfs ~]#exportfs -v    注:重读配置文件,并共享目录
/data/www       192.168.18.27(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,all_squash)
/data/www       192.168.18.37(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,all_squash
   

5. 实现NFS实时同步备份

5.1 rsync服务器

  • 配置rsync服务器端配置文件
[root@rsync ~]#vim /etc/rsyncd.conf

uid = root    以谁的身份运行
gid = root
use chroot = no     是否允许切根
max connections = 0    最大并发连接数
ignore errors    错误忽略
exclude = lost+found/    忽略这个目录,即过滤这个目录
log file = /var/log/rsyncd.log    日志文件
pid file = /var/run/rsyncd.pid    进程id
lock file = /var/run/rsyncd.lock    锁文件
reverse lookup = no      不反向解析
hosts allow = 192.168.18.0/24      允许哪些主机同步,将允许的服务器的数据同步到rsync服务器上
[backup]
path = /data/backup/    用哪个文件夹存放同步数据
comment = backup    backup注释说明
read only = no    是否只读的,no代表可读可写
auth users = rsyncuser    rsync用户账号
secrets file = /etc/rsync.pass    存放账号密码的文件


  • 服务器端生成验证文件
[root@rsync ~]#echo "rsyncuser:123456" > /etc/rsync.pass
[root@rsync ~]#chmod 600 /etc/rsync.pass

  • 服务器端准备备份目录
[root@rsync ~]#mkdir /data/backup/
  • 启动服务
[root@rsync ~]#systemctl start rsyncd

5.2 nfs服务器(nfs服务器此时是rsync的客户端)

  • 客户端配置密码文件
[root@nfs ~]#echo "123456" > /etc/rsync.pass
[root@nfs ~]#chmod 600 /etc/rsync.pass

5.3 实现实时同步

  • nfs端安装inotify-tools包
[root@nfs ~]#yum -y install inotify-tools
  • 客户端创建inotify_rsync.sh脚本
[root@nfs ~]# vim inotify_rsync.sh
#!/bin/bash
SRC='/data/www'
DEST='[email protected]::backup'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do                                                   
    FILEPATH=${DIR}${FILE}
    rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done
  • 使脚本后台运行脚本
[root@nfs ~]#nohup bash inotify_rsync.sh &> /dev/null &
  • 测试
[root@nfs ~]#echo 123 > /data/www/test

[root@rsync ~]#ll /data/backup/www/
total 4
-rw-r--r-- 1 root root 4 May 25 21:29 test


[root@nfs ~]#rm -f /data/www/test 

[root@rsync ~]#ll /data/backup/www/
total 0

6. 搭建HTTPD和PHP

6.1 在lap1上搭建HTTPD和PHP服务

  • lap1安装相关包
[root@lap1 ~]#yum -y install httpd php-fpm php-mysql
  • 配置httpd配置文件
[root@lap1 ~]#vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.php 
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/ 
  • 挂载nfs网络文件系统
[root@lap1 ~]#showmount -e 192.168.18.47
Export list for 192.168.18.47:
/data/www 192.168.18.37,192.168.18.27

[root@lap1 ~]#mount 192.168.18.47:/data/www /var/www/html/
注:生产中应该挂载之后应该把挂载信息写入/etc/fstab配置文件
[root@lap1 ~]#vim /var/www/html/index.php
<?php
    phpinfo();                                                                                       
?>
  • 查看nfs服务器挂载目录下文件
[root@nfs ~]#ll /data/www/
total 4
-rw-r--r-- 1 nfsnobody nfsnobody 253 May 25 21:47 index.php
  • 查看是否nfs的/data/www是否同步到了rsync服务器上
[root@rsync ~]#ll /data/backup/www/
total 4
-rw-r--r-- 1 nfsnobody nfsnobody 253 May 25 21:47 index.php

  • 测试
[root@lap1 ~]#systemctl start httpd
[root@lap1 ~]#systemctl start php-fpm
14399383-67cd296471d9ca5e.png
image.png

6.2 在lap2上搭建HTTPD和PHP服务

  • lap2安装相关包
[root@lap2 ~]#yum -y install httpd php-fpm php-mysql
  • 配置httpd配置文件
[root@lap2 ~]#vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.php 
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/ 
  • 挂载nfs网络文件系统
[root@lap2 ~]#showmount -e 192.168.18.47
Export list for 192.168.18.47:
/data/www 192.168.18.37,192.168.18.27

[root@lap2 ~]#mount 192.168.18.47:/data/www /var/www/html/
注:生产中应该挂载之后应该把挂载信息写入/etc/fstab配置文件
  • 测试
[root@lap1 ~]#systemctl start httpd
[root@lap1 ~]#systemctl start php-fpm
14399383-de5f5a2784e8aa23.png
image.png

7. 测试安装wordpress

7.1 提前准备好wordpress压缩包

[root@lap2 ~]# rm -f /var/www/html/*
[root@lap2 ~]#cd /data/
[root@lap2 data]#ls
script36  script36.tar.gz  wordpress-5.0.3-zh_CN.tar.gz
[root@lap2 data]#tar xf wordpress-5.0.3-zh_CN.tar.gz

[root@lap2 data]#mv wordpress/* /var/www/html/
[root@lap2 data]#cd /var/www/html/
[root@lap2 html]#ls
index.php        wp-admin              wp-content         wp-load.php      wp-signup.php
license.txt      wp-blog-header.php    wp-cron.php        wp-login.php     wp-trackback.php
readme.html      wp-comments-post.php  wp-includes        wp-mail.php      xmlrpc.php
wp-activate.php  wp-config-sample.php  wp-links-opml.php  wp-settings.php

7.2 修改wordpress配置文件

[root@lap2 html]#cp wp-config-sample.php wp-config.php
[root@lap2 html]#vim wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');

/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');

/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');

/** MySQL主机 */
define('DB_HOST', '192.168.18.67:6033');


7.3 创建wordpress数据库

[root@master-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 442
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wordpress CHARACTER SET = utf8;
Query OK, 1 row affected (0.01 sec)

7.4 笔记本自带浏览器(Google Chrome)访问www.testwpress.com
修改dns服务器ip地址

14399383-744ef8f01f75a37a.png
image.png

14399383-df4caed95cbf2900.png
image.png

14399383-6436c7de446c8b06.png
image.png

14399383-dfbc0af719145960.png
image.png

14399383-6ef429f2910b01ae.png
image.png
14399383-82f1b97e3ce19cbf.png
image.png

14399383-3a9403a268c12ed0.png
image.png

14399383-fa6bdf0fcfb8bc81.png
image.png

7.5 wordpress博客安装成功之后,数据库生成一系列列表
查看数据库wordpress是否生成了表,是否主从同步了

  • 主节点mas-mysql
MariaDB [(none)]> use wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
MariaDB [wordpress]> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)

从节点slave1-sql

[root@slave1-sql ~]#mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 455
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [wordpress]> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)

搭建小型LAMP架构-进阶二

14399383-e012dee8d82e40e5.png
image.png

8. LVS-DR配置

8.1 route 设置

[root@route network-scripts]#cat ifcfg-eth0 ifcfg-eth1
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=192.168.18.100
PREFIX=24
ONBOOT=yes
DEVICE=eth1
BOOTPROTO=static
IPADDR=172.22.18.100
PREFIX=16
ONBOOT=yes

开启路由转发功能
[root@route network-scripts]#vim /etc/sysctl.conf 

net.ipv4.ip_forward = 1

[root@route network-scripts]#sysctl -p
net.ipv4.ip_forward = 1

8.2 lvs服务器设置

[root@lvs ~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=192.168.18.200
GATEWAY=192.168.18.88       注:该网关随便设置                                                                 
PREFIX=24
ONBOOT=yes

[root@lvs ~]#systemctl restart network
[root@lvs ~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.18.88   0.0.0.0         UG    100    0        0 eth0
192.168.18.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
  • lvs服务器上安装ipvsadm(调度器包)
[root@lvs ~]#yum -y install ipvsadm

  • 运行一个脚本添加集群服务管理和添加集群上的RS
[root@lvs ~]#vim lvs_dr_vs.sh 

#!/bin/bash
#Author:wangxiaochun
#Date:2017-08-13
vip='192.168.18.201'
iface='lo:1'
mask='255.255.255.255'
port='80'
rs1='192.168.18.27'
rs2='192.168.18.37'
scheduler='wrr'
type='-g'
#rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null                          
注: ipvsadm以安装,所以注释掉
case $1 in
start)
    ifconfig $iface $vip netmask $mask #broadcast $vip up
    iptables -F

    ipvsadm -A -t ${vip}:${port} -s $scheduler
    ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
    ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
    echo "The VS Server is Ready!"
    ;;
stop)
    ipvsadm -C
    ifconfig $iface down
    echo "The VS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

运行脚本
[root@lvs ~]#bash lvs_dr_vs.sh start
The VS Server is Ready!
[root@lvs ~]#ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.18.201:80 wrr
  -> 192.168.18.27:80             Route   1      0          0         
  -> 192.168.18.37:80             Route   1      0          0   

8.3 lap1服务器上设置

  • 网关设置
[root@lap1 ~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=192.168.18.27
GATEWAY=192.168.18.100                                                                               
PREFIX=24
ONBOOT=yes

[root@lap1 ~]#systemctl restart network
[root@lap1 ~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.18.100  0.0.0.0         UG    100    0        0 eth0
192.168.18.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

  • 运行脚本添加vip和相关设置
[root@lap1 ~]#vim lvs_dr_rs.sh 

#!/bin/bash
#Author:wangxiaochun
#Date:2017-08-13
vip=192.168.18.201
mask='255.255.255.255'
dev=lo:1
#rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
#service httpd start &> /dev/null && echo "The httpd Server is Ready!"
#echo "<h1>`hostname`</h1>" > /var/www/html/index.html                                               

case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

运行脚本
[root@lap1 ~]#bash lvs_dr_rs.sh start
The RS Server is Ready!

[root@lap1 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 192.168.18.201/32 scope global lo:1
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:eb:f1:42 brd ff:ff:ff:ff:ff:ff
    inet 192.168.18.27/24 brd 192.168.18.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feeb:f142/64 scope link 
       valid_lft forever preferred_lft forever

8.4 LAP2服务器上设置

  • 网关设置
[root@lap2 ~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=192.168.18.37
GATEWAY=192.168.18.100                                                                               
PREFIX=24
ONBOOT=yes


[root@lap2 ~]#systemctl restart network
[root@lap2 ~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.18.100  0.0.0.0         UG    100    0        0 eth0
192.168.18.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

  • 运行脚本添加vip和相关设置
[root@lap2 ~]#vim lvs_dr_rs.sh 

#!/bin/bash
#Author:wangxiaochun
#Date:2017-08-13
vip=192.168.18.201
mask='255.255.255.255'
dev=lo:1
#rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
#service httpd start &> /dev/null && echo "The httpd Server is Ready!"
#echo "<h1>`hostname`</h1>" > /var/www/html/index.html                                               

case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

运行脚本
[root@lap2 ~]#bash lvs_dr_rs.sh start
The RS Server is Ready!

[root@lap2 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 192.168.18.201/32 scope global lo:1
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:25:1e:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.18.37/24 brd 192.168.18.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe25:1e15/64 scope link 
       valid_lft forever preferred_lft forever

9. 重新设置dns

[root@dns ~]#ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.22.18.101  netmask 255.255.0.0  broadcast 172.22.255.255
        ether 00:0c:29:5d:45:34  txqueuelen 1000  (Ethernet)
        RX packets 9783  bytes 946732 (924.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4670  bytes 680722 (664.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions
[root@dns ~]#vim /etc/named.conf 
listen-on port 53 { 172.22.18.101;  };  注:监听端口写192.168.18.17就可以了
//  allow-query     { localhost; };   
 dnssec-enable no;
 dnssec-validation no;
注:定义区域
zone "testwpress.com" IN {
    type master;
    file "testwpress.com.zone";                                                              
};

[root@dns ~]#vim /var/named/testwpress.com.zone 

$TTL    3600
$ORIGIN testwpress.com.
@   IN      SOA     ns1.testwpress.com.     admin.testwpress.com (
            2019051701
            1H
            10M
            3D
            1D)
    IN      NS      ns1
    IN      MX  10  mx1
ns1 IN      A       172.22.18.101
mx1 IN      A       192.168.18.107
www IN      A       192.168.18.27                                                                              
www IN      A       192.168.18.37


[root@dns ~]#named-checkconf
[root@dns ~]#named-checkzone "testwpress.com" /var/named/testwpress.com.zone
zone testwpress.com/IN: loaded serial 2019051701
OK
[root@dns ~]#rndc reload
server reload successful

10. 测试
14399383-4f2a9c0441b6aa0f.png
image.png

14399383-980f764d2c28c74d.png
image.png

14399383-761a69b3b1ea310e.png
image.png

14399383-d621d6963974c260.png
image.png

14399383-3d7e870cfc8208e8.png
image.png

转载于:https://www.jianshu.com/p/e5b8a4a86fda

猜你喜欢

转载自blog.csdn.net/weixin_34319999/article/details/91067961