首页 > 其他分享 >LNMP集群架构

LNMP集群架构

时间:2023-08-21 14:35:22浏览次数:43  
标签:web code 架构 wecenter LNMP 集群 wordpress MariaDB root

网站集群拆分

上一节我们是部署了单机的LNMP,再往下,要进行拆分了,无论是性能、还是安全性,都务必要拆分。

拆分的内容有

  • nginx集群
  • mysql
  • nfs共享存储

 

拆分思路

情况1

  • 当前的单机环境已经装好了,数据也都有了,需要拆分到多个机器
  • 需要考虑数据迁移

情况2

  • 初试环境直接以集群模式部署,较为简单

必要因素

应用拆分为集群后,需要考虑的问题

  • 数据的备份与恢复
  • LNMP的每一个组件调用,从本地连接,改为了远程连接。
  • mysql的远程连接授权。

拆分步骤

  • 部署db-51机器,mariadb程序
  • web-7的数据导出,发给db-51
  • db-51导入数据
    • 设置远程连接权限
  • web-7测试远程连接db-51
    • 修改php配置文件,连接远程的mysql地址db-51
  • 访问网站,测试数据读写情况

一、拆分数据库

拆分背景

由于单台服务器运行LNMP,单机性能较差、安全性较低,且多个程序运行,抢夺内存资源;

  • 内存吃满后,系统容易crash崩溃,导致进程异常退出等故障
  • 单机若出问题,那就是毁灭性打击。

拆分解决了什么问题

1.增强数据安全性
2.增强数据库的处理能力
3.降低172.16.1.7服务器的压力
4.提升用户访问体验

 

机器环境准备

web-7  172.16.1.7 nginx+php (应用服务器)

db-51  172.16.1.51 mysql (数据库服务器)

备份web-7上的数据库

1.导出web-7的mysql数据库,里面包含了几个产品的数据
[root@web-7 ~]#!mysql
mysql -uroot -pyuchaoit.cn
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 155
Server version: 5.5.68-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)]> 
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wecenter           |
| wordpress          |
+--------------------+
6 rows in set (0.00 sec)



2.数据库备份
# -A 所有数据库  
# --single-transaction 保证数据完整且一致

[root@web-7 ~]#mysqldump -uroot -p'yuchaoit.cn' -A --single-transaction > /opt/alldb.sql


3.查看备份数据
[root@web-7 ~]#ll /opt/alldb.sql -h
-rw-r--r-- 1 root root 2.0M May 13 18:34 /opt/alldb.sql

数据发给备份服务器

[root@web-7 /opt]#scp /opt/alldb.sql [email protected]:/opt/

二、部署db-51

安装、导入数据

[root@db-51 ~]#yum install mariadb mariadb-server -y 
[root@db-51 ~]#systemctl start mariadb
[root@db-51 ~]#
[root@db-51 ~]#systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@db-51 ~]#

设置密码
[root@db-51 ~]#mysqladmin password 'yuchaoit.cn'


导入数据
[root@db-51 /opt]#mysql -uroot -pyuchaoit.cn < /opt/alldb.sql 


查看数据库
[root@db-51 /opt]#mysql -uroot -pyuchaoit.cn -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wecenter           |
| wordpress          |
+--------------------+

授权远程可访问

[root@db-51 /opt]#mysql -uroot -pyuchaoit.cn
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.68-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 all privileges on *.* to 'yuchao01'@'%' identified by 'yuchaoit.cn';
Query OK, 0 rows affected (0.00 sec)

# 刷新权限表
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

# 参数解释

授予所有权限 grant all privileges 
所有库、所有表  *.*
授予访问的用户@所有主机  'yuchao01'@'%'
该授权用户的访问密码  identified by 'yuchaoit.cn';

测试该用户登录,读取数据

[root@db-51 /opt]#mysql -uyuchao01 -p'yuchaoit.cn' -h 10.0.0.51
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-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 |
| mysql              |
| performance_schema |
| test               |
| wecenter           |
| wordpress          |
+--------------------+
6 rows in set (0.00 sec)

三、修改web-7远程连接数据库

 

修改wordpress代码

该网站产品后端是php,数据也都是通过程序读写的数据库。
[root@web-7 /code/wordpress]#vim /code/wordpress/wp-config.php

 

修改wecenter代码

[root@web-7 /code/wecenter]#cat  system/config/database.php 
<?php

$config['charset'] = 'utf8mb4';
$config['prefix'] = 'aws_';
$config['driver'] = 'MySQLi';
$config['master'] = array (
  'charset' => 'utf8mb4',
  'host' => '172.16.1.51',
  'username' => 'yuchao01',
  'password' => 'yuchaoit.cn',
  'dbname' => 'wecenter',
  'port' => '3306',
);
$config['slave'] = false;
$config['port'] = '3306';

 

停止web-7的数据库

这也就是我们以前安装的数据库,停掉它。

[root@web-7 /code/wecenter]#systemctl stop mariadb
[root@web-7 /code/wecenter]#
[root@web-7 /code/wecenter]#ps -ef|grep mysql
root      14934  14679  0 19:06 pts/0    00:00:00 grep --color=auto mysql
[root@web-7 /code/wecenter]#netstat -tunlp|grep 3306

四、测试产品访问

本地做好dns解析
10.0.0.7 www.yuchaoit.cn wecenter.yuchaoit.cn wordpress.yuchaoit.cn

wordpress博客产品

  • 访问静态数据,首页图片等
  • 访问动态数据,登录,写博客
http://wordpress.yuchaoit.cn/wp-login 这是后台地址

试试你以前的密码还能用吗


yuchao01
chaoge666

正确登录,则数据是没问题的,以及看看文章数据还在吗

再发表一个新博客,查看数据库

 

[root@db-51 /opt]#mysql -uyuchao01 -p'yuchaoit.cn' -h 10.0.0.51
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 45
Server version: 5.5.68-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 |
| mysql              |
| performance_schema |
| test               |
| wecenter           |
| wordpress          |
+--------------------+
6 rows in set (0.00 sec)

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]> 
MariaDB [wordpress]> select * from wp_posts;

 

wecenter

http://wecenter.yuchaoit.cn/?/account/register/
试试是否可以注册新用户

cc01
chaoge666

注意,这里的域名是本地的测试域名。

登录后,数据库中找找,去db-51上找。

[root@db-51 /opt]#mysql -uyuchao01 -p'yuchaoit.cn' -h 10.0.0.51
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 92
Server version: 5.5.68-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)]> 

MariaDB [(none)]> select user_name,password from wecenter.aws_users;
+-----------+----------------------------------+
| user_name | password                         |
+-----------+----------------------------------+
| yuchao01  | *851F4EE1AF6791802ED4A520AF0611F |
| cc01      | f689087c69b60ae88bad18a1206ea557 |
+-----------+----------------------------------+
2 rows in set (0.00 sec)

五、增加web节点

在淘宝十年架构演进中,超哥讲解了单机到集群的扩展,单个web-7,再加一个web-8

 

拓展web-8解决了什么问题

  • 单机web-7能抗住的访问量是有局限性的,因此配置多个一模一样的web服务器即可,分摊这个压力,提高客户端的响应速度,请求不再集中到web-7。
  • 如果web-7故障,导致网站直接挂掉
  • 多个web节点,能保证业务稳定运行,扩展性更高了
  • 明显的,提升了用户访问的网站的速度。

部署多个web服务器思路

  • 同一个软件
  • 同一个配置文件
快捷办法,从零部署可以这样做
1. 可以通过ansible剧本,一键部署多个web服务器

2. 准备好nginx软件包,部署内网自建yum仓库,统一部署。


如果已经有了web-7环境,只能再额外加了,或者你可以基于当前状态,需要部署web8,web9,web10,依然可以用ansible

3. 按照web-7的部署笔记,再来一遍,部署好web-8

手工部署web-8

创建用户

咱们这里只加一个web8,手工再来一遍即可。

[root@web-8 ~]#groupadd www -g 666
[root@web-8 ~]#useradd www -s /sbin/nologin -M -u 666 -g 666
[root@web-8 ~]#

安装nginx、php环境

确保安装环境和web-7一样,因此所有配置文件发过来即可

[root@web-8 ~]#scp -rp [email protected]:/etc/yum.repos.d/* /etc/yum.repos.d/

[root@web-8 ~]#scp  -rp [email protected]:/etc/pki/rpm-gpg/* /etc/pki/rpm-gpg/


# 安装nginx
yum clean all

yum install nginx -y


# 安装php环境
yum install -y php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml  php71w-fpm  php71w-mysqlnd  php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb php71w-json php71w-pecl-apcu php71w-pecl-apcu-devel

拷贝web-7的nginx配置文件

[root@web-8 ~]#scp -rp [email protected]:/etc/nginx/ /etc/
[email protected]'s password: 
default.conf                                                                               100% 1072     1.8MB/s   00:00    
php.conf                                                                                   100%  344   417.1KB/s   00:00    
wecenter.conf                                                                              100%  361   560.3KB/s   00:00    
wordpress.conf                                                                             100%  372   635.8KB/s   00:00    
fastcgi_params                                                                             100% 1007     2.0MB/s   00:00    
mime.types                                                                                 100% 5231     9.2MB/s   00:00    
nginx.conf                                                                                 100%  648     1.6MB/s   00:00    
scgi_params                                                                                100%  636     1.2MB/s   00:00    
uwsgi_params                                                                               100%  664     1.4MB/s   00:00    
[root@web-8 ~]#

拷贝web-7的php配置文件

[root@web-8 ~]#scp -rp [email protected]:/etc/php-fpm.d  /et/
[email protected]'s password: 
www.conf                                                                                   100%   18KB   8.2MB/s   00:00    
[root@web-8 ~]#

拷贝web-7的代码(网站源码)

[root@web-7 ~]#cd /code && tar -zcf code.tgz ./*

[root@web-7 /code]#scp code.tgz [email protected]:/opt/

web-8解压缩源码到指定目录

[root@web-8 ~]#mkdir /code && tar -zxf /opt/code.tgz -C /code

[root@web-8 ~]#ls /code
hello.html  mysql-test.php  test-phpinfo.php  wecenter  wordpress

启动web-8的nginx、php

systemctl start nginx php-fpm 

systemctl enable nginx php-fpm

停止web-7的所有服务

[root@web-7 /code]#systemctl stop nginx php-fpm

web-8日志检测

[root@web-8 ~]#tail -f /var/log/nginx/access.log

测试访问web-8

修改dns解析

这里需要改为8作为测试,后续我们继续学习 负载均衡服务器的部署,这里即可统一入口了。

#10.0.0.7 www.yuchaoit.cn wecenter.yuchaoit.cn wordpress.yuchaoit.cn

10.0.0.8 www.yuchaoit.cn wecenter.yuchaoit.cn wordpress.yuchaoit.cn

 

停止web-8的php服务

[root@web-8 /code/wecenter]#systemctl stop php-fpm

 

停止web-8的nginx服务

大门直接关了,你还访问个球?

[root@web-8 /code/wecenter]#systemctl stop nginx

 

六、将静态资源挂载到共享存储

1.为什么要拆分独立静态资源服务器

  • 后端的web服务器有多个,用户上传的数据、图片、视频等附件都会只传到单台一台Web服务器,导致其他服务器找不到数据。

  • 因此必须要使用到共享文件存储服务器。

2.添加NFS解决了什么问题

  • 多个web服务器的静态资源完全一致
  • 有效节省web服务器的存储空间
  • 统一管理了静态资源,便于升级CDN加速作为源站资源。

3.图解多个web节点

 

4.机器准备

web-7         nginx+php
web-8            nginx+php
db-51            mysql
nfs-31        nfs

5.部署文件服务器NFS-31



[root@nfs-31 ~]#cat /etc/exports
/web-data/wordpress/   172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/web-data/wecenter/   172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)


[root@nfs-31 ~]#mkdir -p /web-data/wordpress
[root@nfs-31 ~]#mkdir -p /web-data/wecenter


授权
[root@nfs-31 ~]#groupadd www -g 666
[root@nfs-31 ~]#useradd www -u 666 -g 666 -M -s /sbin/nologin
[root@nfs-31 ~]#chown -R www.www /web-data/

启动
[root@nfs-31 ~]#systemctl restart nfs

6.查看wordpress上传资源的目录

接下来就是修改两个web节点了。

[root@web-8 /code/wecenter]#systemctl start nginx php-fpm

发表博客,插入图片,找到图片所在的URL,查看这个资源往哪传。

 

找到目录

[root@web-8 /code/wordpress/wp-content/uploads/2022/05]#pwd
/code/wordpress/wp-content/uploads/2022/05

7.备份所有静态资源

待会我们要挂载到NFS机器了,源数据先备份好。

[root@web-8 /code/wordpress/wp-content]#
[root@web-8 /code/wordpress/wp-content]#cp -a uploads/ uploads_bak
[root@web-8 /code/wordpress/wp-content]#ls
index.php  languages  plugins  themes  uploads  uploads_bak


然后可以删除当前uploads目录的数据了,待会以NFS中数据为准。

8.挂载nfs

yum install nfs-utils -y
[root@web-8 ~]#showmount -e 172.16.1.31
Export list for 172.16.1.31:
/web-data/wecenter  172.16.1.0/24
/web-data/wordpress 172.16.1.0/24

挂载
[root@web-8 ~]#mount  -t nfs 172.16.1.31:/web-data/wordpress /code/wordpress/wp-content/uploads 

检查
[root@web-8 ~]#df -h |grep wordpress
172.16.1.31:/web-data/wordpress   17G  1.6G   16G  10% /code/wordpress/wp-content/uploads

9.恢复上传的图片数据

[root@web-8 /code/wordpress/wp-content]#cp -a  uploads_bak/* uploads/

10.挂载写入fstab

[root@web-8 /code/wordpress/wp-content]#tail -1 /etc/fstab 
172.16.1.31:/web-data/wordpress /code/wordpress/wp-content/uploads  nfs defaults 0 0

11.检查网站是否恢复

 

试试取消nfs挂载

 

重新挂载试试。。

 

七、同样的步骤,在web7再来一遍。

最终确保web-8,web-7都可以访问到wordpress,且是是拆分开的LNMP环境。

标签:web,code,架构,wecenter,LNMP,集群,wordpress,MariaDB,root
From: https://www.cnblogs.com/sxy-blog/p/17505774.html

相关文章

  • 系统架构合理性的思考 | 京东云技术团队
    最近牵头在梳理部门的系统架构合理性,开始工作之前,我首先想到的是如何定义架构合理性?从研发的角度来看如果系统上下文清晰、应用架构设计简单、应用拆分合理应该称之为架构合理。基于以上的定义可以从以下三个方面来梳理评估:1、系统的上下文清晰:明确的知道和周围系统的调用关系,数据......
  • 实验室信息管理系统(LIMS)源码,采用灵活的架构开发,支持多种应用程序和技术
    实验室信息管理系统(LIMS)是指帮助实验室组织和管理实验数据的计算机软件系统,它将实验室操作有机地组织在一起,以满足实验室工作流程的所有要求。它能以不同的方式支持实验室的工作,从简单的过程(如样品采集和入库)到复杂的流程(如教据报告和实验结果分析),完全改变实验室的工作流程,使......
  • 深入了解Elasticsearch搜索引擎篇:倒排索引、架构设计与优化策略
    什么是倒排索引?有什么好处?倒排索引是一种用于快速检索的数据结构,常用于搜索引擎和数据库中。与传统的正排索引不同,倒排索引是根据关键词来建立索引,而不是根据文档ID。倒排索引的建立过程如下:首先,将每个文档拆分成一系列的关键词或词项,然后建立一个词项到文档的映射。对每个关键......
  • Docker 搭建 LNMP 架构的 Wordpress网站
    目录一、项目环境二、服务器环境三、任务需求四、获取Linux系统基础镜像五、Nginx1.建立工作目录2.编写Dockerfile脚本3.编辑nginx的主配置文件4.生成镜像5.创建自定义网络6.启动镜像容器7.验证nginx六、MySQL1.建立工作目录2.编写Dockerfile3.编辑mysql主配......
  • ambari-hadoop集群中timeline和ams-hbase几种服务之间的调用关系
    最近经常碰到ambari集群timelineserver和ams-hbase服务的一些问题,梳理了下这些服务之间的调用关系,留作笔记方便后续查阅1.目前笔者用到的hadoop组件版本如下2.调用关系如下图3.关于timelineserver有两个版本v1.5和v2.03.1目前v1.5为过渡期版本,但是也是生产中应用用......
  • devops之Python编程-类的基础架构
    Python中,可以通过关键字class来定义一个类。类是一种自定义数据类型,它可以包含属性(变量)和方法(函数)。下面是一个示例:classMyClass:def__init__(self,name):self.name=namedefsay_hello(self):print("Hello,"+self.name+"!")在上面的......
  • 集群、分布式、微服务概念和区别
    概念:集群是个物理形态,分布式是个工作方式。1.分布式:一个业务分拆多个子业务,部署在不同的服务器上2.集群:同一个业务,部署在多个服务器上分布式是指将不同的业务分布在不同的地方。而集群指的是将几台服务器集中在一起,实现同一业务。分布式中的每一个节点,都可以做集群。而集群......
  • 鑫达惠购系统APP功能架构介绍
    鑫达惠购是一款新电商模式的购物分销系统,基于分销裂变的商业价值行为,快速地分享邀请用户注册。这个系统的模式有个特别的亮点,基于全网公排的模式快速推动用户在商城上的购买活动。1000人团队讲解,根据你下单时间的先后顺序,系统自动匹配1000人给你,他们提现,你就能拿到他们提现金额的0.......
  • x86_64/aarch64架构下ffpyplayer源码编译
    问题来源:某鱼上挂着pytorch的aarch64架构下的源码编译,遇到某网友提出的要在aarch64架构下的ubuntu上ffpyplayer源码编译,于是有了本文。  =================================================   1.下载源码ffpyplayer源码下载地址:https://github.com/matham/ffpypla......
  • 二、虚拟化架构与系统部署
    虚拟化公司:vmware、传统运行模式:硬件-->操作系统-->应用程序。虚拟机运行:1.寄居架构     2.原生架构windows系统禁用系统更新:win+r输入services.msc(服务管理窗口),停止Windowsupdate服务,并禁用,同时在恢复里改为无操作。    win+r输入gpe......