0
点赞
收藏
分享

微信扫一扫

[000-01-008].第08节:Sentinel 环境搭建

才德的女子 2024-09-18 阅读 17

主要内容:

网站架构演变、LNP+Mariadb数据库分离、Web服务器集群(部署Nginx后端web服务器、部署NFS共享存储服务器、部署Haproxy代理服务器、部署DNS域名解析服务器)

一、网站架构演变:

随着网站访问量和业务复杂度的增加,网站架构需要不断演变以应对更高的性能、可用性和扩展性需求。以下是常见的网站架构演变过程:

1、单机版LNMP

2、独立数据库服务器(LNMP+Mariadb)

该架构将网站静态文件、代码文件等资料与数据库分离LNMP架构,当访问用户量增加时单机的处理能力及资源有限,PHP或JAVA代码的解析和执行需要消耗大量CPU资源,数据库的增删改查需要调用大量的内存资源,将两者分离可以减轻服务器的压力;Web服务器和数据库服务器的压力得到有效改善的同时,但依然存在单点故障问题;

3、Web服务器集群(代理)与Session保持

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

但是如果只有一台调度器依然会导致单点故障的问题,因此还需要使用Keepalived或Heartbeat之类的软件进行高可用配置;

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

4、动静分离、数据库集群

随着服务器的增加,虽然性能与并发量得到了明显的提升,但是数据的一致性、管理的便利性成为了新的问题,因此就需要增加统一的存储服务器,实现数据的同步一致,可以使用NFS,GlusterFS、Ceph等软件实现该功能;此时所有应用服务器都连接一台数据库服务器进行读写操作,而且后期随着数据库中的数据不断增加,会导致数据库成为整个网站的瓶颈!这就需要我们对数据进行分库分表,创建数据库主从或者数据库集群,实现读写分离;

5、缓存服务器与业务模型

对于静态数据我们可以通过varnish、squid或者nginx进行缓存,将数据缓存到距离用户更近的位置,构建CDN(内容分发网络)架构;对于传统的SQL数据库而言,我们也可以通过增加NoSQL数据库,实现数据缓存的功能,提升数据库的访问速度;


二、网站架构进阶项目案例

案例1:LNP+Mariadb数据库分离

部署LNP+Mariadb实现数据库与Web服务器分离,实现以下目标:

  • 1)将旧的数据库备份,迁移到新的服务器(延续DAY1案例)
  • 2)修改配置调用新的数据库服务器

实验拓扑图:

主机配置表:

步骤1:部署数据库服务器(database主机操作)

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

[root@database ~]# yum -y install mariadb mariadb-server mariadb-devel
[root@database ~]# systemctl start mariadb
[root@database ~]# systemctl enable mariadb
[root@database ~]# firewall-cmd --set-default-zone=trusted
[root@database ~]# setenforce  0
[root@database ~]# sed -i  '/SELINUX/s/enforcing/permissive/'  /etc/selinux/config

2)将web1单机版的LNMP网站中的数据库迁移到新的数据库服务器

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

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

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

[root@database ~]# mysql
MariaDB [(none)]> create database wordpress character set utf8mb4;   //创建数据库wordpress,该数据库支持中文
MariaDB [(none)]> exit

使用备份文件还原数据:

[root@database ~]# mysql wordpress < wordpress.bak    //使用备份文件导入数据到wordpress数据库

重新创建账户并授权访问:

[root@database ~]# mysql
MariaDB [(none)]> grant all on wordpress.* to wordpress@'%' identified by 'wordpress';
MariaDB [(none)]> flush privileges;     //刷新权限
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> exit

解释说明:

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

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

[root@web1 ~]# vim /usr/local/nginx/html/wp-config.php
# 修改前内容如下:
define('DB_HOST', '192.168.2.11');   //将原指定2.11本机配置修改为DB主机2.21
# 修改后内容如下:
define('DB_HOST', '192.168.2.21');
...

步骤2:客户端(真机)验证测试

1)客户端使用浏览器访问wordpress网站:http://192.168.2.11 

 


案例2:Web服务器集群

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

  • 1)部署三台Web服务器(包含DAY1的web1,部署相同web2、web3)
  • 2)迁移网站数据,使用NFS实现数据共享
  • 3)部署HAProxy代理服务器实现负载均衡
  • 4)部署DNS域名解析服务器

实验拓扑图:

主机配置表:

步骤1:部署LNP架构

1)安装LNP软件包(web2、web3操作)

[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 --user=nginx --prefix=/usr/local/nginx --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 --user=nginx --prefix=/usr/local/nginx --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

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

web2修改默认首页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修改默认首页index.php,配置两个location实现动静分离。

[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;
        }

3)启动LNP所有相关服务、设置防火墙和SELinux

[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
[root@web2 ~]# systemctl enable php-fpm
[root@web2 ~]# firewall-cmd --set-default-zone=trusted
[root@web2 ~]# setenforce  0
[root@web2 ~]# sed -i  '/SELINUX/s/enforcing/permissive/'  /etc/selinux/config

[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
[root@web3 ~]# systemctl enable php-fpm
[root@web3 ~]# firewall-cmd --set-default-zone=trusted
[root@web3 ~]# setenforce  0
[root@web3 ~]# sed -i  '/SELINUX/s/enforcing/permissive/'  /etc/selinux/config

扩展知识:Service文件存储路径为/usr/lib/systemd/system/目录

[root@web1 ~]# cp /usr/lib/systemd/system/httpd.service /usr/lib/systemd/system/nginx.service
[root@web1 ~]# 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
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT ${MAINPID}
 
[Install]
WantedBy=multi-user.target
[root@web1 ~]# systemctl start nginx
[root@web1 ~]# ss -nlptu | grep :80

步骤2:部署NFS,将Wordpress网站数据迁移至NFS共享服务器

1)部署NFS共享服务器

[root@nfs ~]# yum install nfs-utils rpcbind
[root@nfs ~]# mkdir /web_share    //创建NFS共享目录
[root@nfs ~]# vim /etc/exports
/web_share  192.168.2.0/24(rw,no_root_squash)
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl enable rpcbind
[root@nfs ~]# systemctl restart nfs
[root@nfs ~]# systemctl enable nfs
[root@nfs ~]# firewall-cmd --set-default-zone=trusted
[root@nfs ~]# setenforce 0
[root@nfs ~]# sed -i '/SELINUX/s/enforcing/permissive/' /etc/selinux/config
[root@nfs ~]# id nfsnobody
uid=65534(nfsnobody) gid=65534(nfsnobody) 组=65534(nfsnobody)

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

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

[root@web1 ~]# cd /usr/local/nginx/html/
[root@web1 html]# tar -czpf html.tar.gz ./*    // [-p]代表打包时保留文件的权限
[root@web1 html]# scp html.tar.gz 192.168.2.31:/web_share/
[root@web1 html]# rm -rf /usr/local/nginx/html/*     //清空本机的网页目录

登录nfs(192.168.2.31),将压缩包解压

[root@nfs ~]# cd /web_share/
[root@nfs web_share]# ls
html.tar.gz
[root@nfs web_share]# tar -xf html.tar.gz
[root@nfs web_share]# ls
html.tar.gz  wp-activate.php       wp-config.php         wp-includes        wp-mail.php       xmlrpc.php
index.php    wp-admin              wp-config-sample.php  wp-links-opml.php  wp-settings.php
license.txt  wp-blog-header.php    wp-content            wp-load.php        wp-signup.php
readme.html  wp-comments-post.php  wp-cron.php           wp-login.php       wp-trackback.php

3)所有web服务器访问挂载NFS共享数据(web1、web2、web3)

[root@web1 ~]# yum -y install nfs-utils
[root@web1 ~]# showmount -e 192.168.2.31
Export list for 192.168.2.31:
/web_share 192.168.2.0/24
[root@web1 ~]# echo "192.168.2.31:/web_share /usr/local/nginx/html/ nfs defaults 0 0" >> /etc/fstab
[root@web1 ~]# mount -a
[root@web1 ~]# ls /usr/local/nginx/html
html.tar.gz  wp-activate.php       wp-config.php         wp-includes        wp-mail.php       xmlrpc.php
index.php    wp-admin              wp-config-sample.php  wp-links-opml.php  wp-settings.php
license.txt  wp-blog-header.php    wp-content            wp-load.php        wp-signup.php
readme.html  wp-comments-post.php  wp-cron.php           wp-login.php       wp-trackback.php
 
[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

4)使用浏览器测试访问每个web服务器,查看是否有相同的网页数据结果;

步骤3:部署HAProxy代理服务器

1)部署HAProxy(80端口)

[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
[root@proxy ~]# ss -nlptu | grep :80
tcp    LISTEN     0      128       *:80                    *:*                   users:(("haproxy",pid=981,fd=7))
[root@proxy ~]# firewall-cmd --set-default-zone=trusted
[root@proxy ~]# setenforce  0
[root@proxy ~]# sed -i  '/SELINUX/s/enforcing/permissive/'  /etc/selinux/config

2)使用浏览器测试访问proxy代理服务器,查看网页数据结果;

步骤4:部署DNS域名服务器

1)安装DNS相关软件

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

2)修改主配置文件,添加zone

[root@dns ~]# 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@dns ~]# named-checkconf /etc/named.conf       //检查配置文件语法

3)修改正向解析记录文件

[root@dns ~]# cp -p /var/named/named.localhost /var/named/lab.com.zone
[root@dns ~]# 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.2.10
www     A       192.168.2.10

4)启动DNS服务

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

5)修改DNS解析文件

[root@dns ~]# vim /etc/resolv.conf
nameserver 192.168.2.10

— 验证:

[root@dns ~]# nslookup www.lab.com
Server: 192.168.2.10
Address: 192.168.2.10#53
 
Name: www.lab.com
Address: 192.168.2.10

备注:

  • 如果不做DNS,也可直接修改hosts解析文件;
  • 如果是Linux客户端,则修改/etc/hosts文件;
  • 如何客户端是Windows则需要在图形中配置网卡的DNS服务器;

步骤5:修改wordpress配置文件

1)修改wp-config.php

在define('DB_NAME', 'wordpress')这行前面添加如下两行内容:

[root@nfs ~]# vim /usr/local/nginx/html/wp-config.php
define('WP_SITEURL', 'http://www.lab.com');
define('WP_HOME', 'http://www.lab.com');
...

小结:

本篇章节为【第三阶段】PROJECT1-DAY2 的学习笔记,这篇笔记可以初步了解到 网站架构演变、LNP+Mariadb数据库分离、Web服务器集群(部署Nginx后端web服务器、部署NFS共享存储服务器、部署Haproxy代理服务器、部署DNS域名解析服务器)


Tip:毕竟两个人的智慧大于一个人的智慧,如果你不理解本章节的内容或需要相关笔记、视频,可私信小安,请不要害羞和回避,可以向他人请教,花点时间直到你真正的理解

举报

相关推荐

0 条评论