LNP&Mariadb数据库分离|web服务器集群


前序文章: https://blog.csdn.net/shengweiit/article/details/135160979

网站架构演变

单机版LNMP

用户量少时使用,简单,成本低
但是存在单点故障
在这里插入图片描述

独立数据库服务器

独立数据库服务器是将网站静态文件、代码文件等资料与数据库分离的架构,当用户量增加时单机的处理能力有限,数据库的增删改需要大量的内存资源,将两者分离可以减轻服务器的压力。
在这里插入图片描述

web服务器集群与Session保持

可以通过Nginx、Haproxy代理服务器实现Web负载均衡集群,也可以使用LVS调度器实现Web负载均衡集群。部署完Web集群后还需要考虑如何进行Session会话保持,方法很多,如:根据源IP保持,代理服务器重写Cookie信息,共享文件系统保存session,使用数据库共享session等等。
在这里插入图片描述

对于网站内容而言可以分离为动态页面和静态页面,静态页面就需要数据文件,动态页面则需要CPU解析代码,需要消耗大量的CPU资源,因此可以将静态和动态分离为两组服务器,动态页面有脚本代码组成,是一种基于网页的应用程序,因此这一组服务器也称为应用服务器
在这里插入图片描述

LNP与数据库分离

在这里插入图片描述
首先要关掉防火墙和SELinux

[root@database ~]# firewall-cmd --set-default-zone=trusted
[root@database ~]# setenforce  0
[root@database ~]# sed -i  '/SELINUX/s/enforcing/permissive/'  /etc/selinux/config

1. 准备一台独立的服务器,安装数据库软件包

[root@database ~]# yum -y install mariadb mariadb-server mariadb-devel
[root@database ~]# systemctl start mariadb
[root@database ~]# systemctl enable mariadb

在这里插入图片描述

2. 将之前的LNMP网站中的数据库迁移到新的数据库服务器

登陆192.168.2.11主机 备份数据库并拷贝给新的服务器,关闭旧的数据库服务

#备份数据库到文件(备份的文件名和扩展名任意)
[root@centos7 ~]# mysqldump wordpress > wordpress.bak
[root@centos7 ~]# scp wordpress.bak 192.168.2.21:/root/ # 拷贝文件到远程主机
[root@centos7 ~]# systemctl stop mariadb
[root@centos7 ~]# systemctl disable mariadb

登陆192.168.2.21主机 创建空数据库 使用备份文件还原数据库

[root@database ~]# mysql
MariaDB [(none)]> create database wordpress character set utf8mb4;
#创建数据库wordpress,该数据库支持中文
MariaDB [(none)]> exit
#使用备份文件导入数据到wordpress数据库
[root@database ~]# mysql wordpress < wordpress.bak        

并在192.168.2.21创建账户并授权访问

[root@database ~]# mysql
MariaDB [(none)]> grant all on wordpress.* to wordpress@'%' identified by 'wordpress';
#语法格式:grant 权限 on 数据库名.表名  to 用户名@客户端主机 identified by 密码
#创建用户并授权,用户名为wordpress,该用户对wordpress数据库下的所有表有所有权限
#wordpress用户的密码是wordpress,授权该用户可以从localhost主机登录数据库服务器
#all代表所有权限(wordpress用户可以对wordpress数据库中所有表有所有权限)
#wordpress.*代表wordpress数据库中的所有表
MariaDB [(none)]> flush privileges;
#刷新权限
MariaDB [(none)]> exit

备注:在MySQL和MariaDB中%代表所有,这里是授权任何主机都可以连接数据库。
验证:能连就可以用
在这里插入图片描述

3. 修改wordpress网站配置文件,调用新的数据库服务器。

Wordpress在第一次初始化操作时会自动生产配置文件:wp-config.php,登陆192.168.2.11修改该文件即可调用新的数据库服务。

[root@centos7 ~]# vim /usr/local/nginx/html/wp-config.php
修改前内容如下:
define('DB_HOST', '192.168.2.11');
修改后内容如下:
define('DB_HOST', '192.168.2.21');

web服务器集群

使用HAProxy部署web服务器集群,实现以下目标:

  • 部署三台web服务器
  • 迁移网站数据,使用NFS实现数据共享
  • 部署HAProxy代理服务器实现负载均衡
  • 部署DNS域名解析服务器
    在这里插入图片描述
    在这里插入图片描述

1. 配置web2和web3服务器

安装LNP软件包

[root@web2 ~]# yum -y install gcc pcre-devel openssl-devel 
[root@web2 lnmp_soft]# tar -xf nginx-1.12.2.tar.gz
[root@web2 lnmp_soft]# cd nginx-1.12.2/
[root@web2 nginx-1.12.2]# ./configure \
--with-http_ssl_module \
--with-http_stub_status_module
[root@web2 nginx-1.12.2]# make && make install
[root@web2 ~]# yum -y install php php-fpm php-mysql mariadb-devel

[root@web3 ~]# yum -y install gcc pcre-devel openssl-devel
[root@web3 lnmp_soft]# tar -xf nginx-1.12.2.tar.gz
[root@web3 lnmp_soft]# cd nginx-1.12.2/
[root@web3 nginx-1.12.2]# ./configure \
--with-http_ssl_module \
--with-http_stub_status_module
[root@web3 nginx-1.12.2]# make && make install
[root@web3 ~]# yum -y install php php-fpm php-mysql mariadb-devel

修改nginx配置实现动静分离(web2 和 web3操作)

web2修改默认首页index.php,配置两个location实现动静分离。
web3修改默认首页index.php,配置两个location实现动静分离。

[root@web2 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
    
    
            root   html;
            index  index.php index.html index.htm;
        }


location ~ \.php$ {
    
    
            root            html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include         fastcgi.conf;
        }

web3修改

[root@web3 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
    
    
            root   html;
            index  index.php index.html index.htm;
        }
location ~ \.php$ {
    
    
            root            html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include         fastcgi.conf;
        }

在这里插入图片描述
设置开机自启

[root@web2 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
[root@web2 ~]# chmod +x /etc/rc.local
[root@web2 ~]# /usr/local/nginx/sbin/nginx
[root@web2 ~]# systemctl start  php-fpm                   #启动php-fpm服务
[root@web2 ~]# systemctl enable php-fpm                   #设置服务开启自启

[root@web3 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
[root@web3 ~]# chmod +x /etc/rc.local
[root@web3 ~]# /usr/local/nginx/sbin/nginx
[root@web3 ~]# systemctl start  php-fpm                   #启动php-fpm服务
[root@web3 ~]# systemctl enable php-fpm                #设置服务开机自启

源码安装的软件默认无法使用systemd管理,如果需要使用systemd管理源码安装的软件需要手动编写服务的service文件(编写是可以参考其他服务的模板文件)。以下是nginx服务最终编辑好的模板。
Service文件存储路径为/usr/lib/systemd/system/目录。

[root@centos7 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
#描述信息
After=network.target remote-fs.target nss-lookup.target
#指定启动nginx之前需要其他的其他服务,如network.target等
[Service]
Type=forking
#Type为服务的类型,仅启动一个主进程的服务为simple,需要启动若干子进程的服务为forking
ExecStart=/usr/local/nginx/sbin/nginx
#设置执行systemctl start nginx后需要启动的具体命令.
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#设置执行systemctl reload nginx后需要执行的具体命令.
ExecStop=/bin/kill -s QUIT ${MAINPID}
#设置执行systemctl stop nginx后需要执行的具体命令.
[Install]
WantedBy=multi-user.target

2. 部署NFS,将网站数据迁移至NFS共享服务器

部署NFS共享服务器

[root@nfs ~]# yum install nfs-utils
[root@nfs ~]# mkdir /web_share
[root@nfs ~]# vim /etc/exports
/web_share  192.168.2.0/24(rw,no_root_squash)

# NFS使用的是随机端口,每次启动NFS都需要将自己的随机端口注册到rpcbind服务,这样客户端访问NFS时先到rpcbind查询端口信息,得到端口信息后再访问NFS服务。
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl enable rpcbind

[root@nfs ~]# systemctl restart nfs
[root@nfs ~]# systemctl enable nfs

迁移旧网站数据到NFS共享服务器

将web1(192.168.2.11)上的wordpress代码拷贝到NFS共享。

[root@web1 ~]# cd /usr/local/nginx/
[root@web1 nginx]# tar -czpf html.tar.gz html/  # -p代表打包时保留文件的权限 
[root@web1 nginx]# scp html.tar.gz 192.168.2.31:/web_share/

登陆nfs服务器,将压缩包解压

[root@nfs ~]# cd /web_share/
[root@nfs web_share]# tar -xf html.tar.gz

所有web服务器访问挂载NFS共享数据 关掉服务再卸载和挂载

[root@web1 ~]# rm -rf /usr/local/nginx/html/*
[root@web1 ~]# yum -y install nfs-utils
[root@web1 ~]# echo "192.168.2.31:/web_share/html /usr/local/nginx/html/ nfs defaults 0 0" >> /etc/fstab
[root@web1 ~]# mount -a

[root@web2 ~]# yum -y install nfs-utils
[root@web2 ~]# echo "192.168.2.31:/web_share/html /usr/local/nginx/html/ nfs defaults 0 0" >> /etc/fstab
[root@web2 ~]# mount -a

[root@web3 ~]# yum -y install nfs-utils
[root@web3 ~]# echo "192.168.2.31:/web_share/html /usr/local/nginx/html/ nfs defaults 0 0" >> /etc/fstab
[root@web3 ~]# mount -a

检查是否挂载上

showmount -e 192.168.2.31 # 查看是否有共享
df -h 
或者
mount | grep "192.168.2.31"

额外测试
在NFS主机的共享目录 /web_share/html下编写文件new.html (文件内容随意) 另外三台web主机上面都有。

3. 部署HAProxy代理服务器

使用haproxy软件调用三台web服务器

部署HAProxy 安装软件,手动修改配置文件,添加如下内容

[root@proxy ~]# yum -y install haproxy 
[root@proxy ~]# vim /etc/haproxy/haproxy.cfg
listen wordpress *:80        #监听80端口
  balance roundrobin         #轮询算法
  server web1 192.168.2.11:80 check inter 2000 rise 2 fall 3
  server web2 192.168.2.12:80 check inter 2000 rise 2 fall 3
  server web3 192.168.2.13:80 check inter 2000 rise 2 fall 3

在这里插入图片描述
起服务

[root@proxy ~]# systemctl start haproxy
[root@proxy ~]# systemctl enable haproxy

在这里插入图片描述
在客户端192.168.4.10连接haproxy主机访问网站服务

curl http://192.168.4.5/love.html

是否实现了负载均衡 查看三台的日志文件
在这里插入图片描述

4.部署DNS相关软件(192.168.4.5操作)

安装DNS相关软件

[root@proxy ~]# yum install -y bind bind-chroot

修改配置文件,添加zone

[root@proxy ~]# vim /etc/named.conf
options {
    
    
        listen-on port 53 {
    
     any; };           #服务监听的地址与端口
        directory       "/var/named";         #数据文件路径
        allow-query     {
    
     any; };             #允许任何主机访问DNS服务
... ...
};


zone "lab.com" IN {
    
                            #定义正向区域
        type master;
        file "lab.com.zone";
};

#include "/etc/named.rfc1912.zones";        #注释掉改行
#include "/etc/named.root.key";              #注释掉改行

[root@proxy ~]# named-checkconf /etc/named.conf            #检查语法

修改正向解析记录文件

注意保留文件权限

[root@proxy named]# cp -p /var/named/named.localhost /var/named/lab.com.zone
[root@proxy named]# vim /var/named/lab.com.zone
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
@        NS     dns.lab.com.
dns     A       192.168.4.5
www     A       192.168.4.5

# 启动服务
[root@proxy named]# systemctl start named
[root@proxy named]# systemctl enable named

在这里插入图片描述
可以用如下命令检查配置文件是否写对

[root@proxy ~]# named-checkconf /etc/named.conf            #检查语法

客户端修改DNS

如果客户端是Linux主机,则客户端修改DNS解析文件

[root@room9pc01 data]# cat /etc/resolv.conf
# Generated by NetworkManager
search tedu.cn 
nameserver 192.168.4.5 # DNS服务器地址

NFS共享目录权限 no_root_squash (生产环境不加此权限)
客户端已NFS服务器系统管理员root的身份 访问共享资源

猜你喜欢

转载自blog.csdn.net/shengweiit/article/details/135168233