网站架构的演变

单机版LNMP

  • 用户量少时使用,简单、成本低、存在单点故障

1.png

独立数据库服务器

  • 解耦
  • 将数据库分类
  • PHP或JAVA代码的执行需要消耗大量CPU资源
  • 数据库的增删改查 需要调用大量的内存资源
  • 将两者分离可以减轻服务器的压力

2.png

高可用服务器集群

  • 我们可以通过Nginx、Haproxy代理服务器实现Web负载均衡集群,也可以使用LVS调度器实现Web负载均衡集群

3.png

动静分离

  • 动态网站与静态网站分离

4.png

数据库集群

  • 主从数据库、分库分表、读写分离
  • 共享存储

5.png

缓存服务器与业务模型

  • 对于静态数据我们可以通过varnish、squid或者nginx进行缓存,将数据缓存到距离用户更近的位置,构建CDN(内容分发服务)架构

6.png

综合实战项目

项目整体拓扑

7.png

主机角色 主机名 ip
代理服务器1 proxy1 ens33(192.168.4.5/24)
ens34(192.168.2.5/24)
代理服务器2 proxy2 ens33(192.168.4.6/24)
ens34(192.168.2.6/24)
web1服务器 web1 ens33(192.168.2.11/24)
web2服务器 web2 ens33(192.168.2.12/24)
web3服务器 web3 ens33(192.168.2.13/24)
数据库服务器 database ens33(192.168.2.21/24)
ceph-node1 node1 ens33(192.168.2.31/24)
ceph-node2 node2 ens33(192.168.2.31/24)
ceph-node3 node3 ens33(192.168.2.31/24)

项目一期

8.png

部署LNP服务器

  • Linux+Nginx+PHP(仅先部署一台web)
  • 安装软件
1
2
3
4
5
6
7
8
[root@web1 ~]# yum install -y gcc openssl-devel pcre-devel make
[root@web1 ~]# tar xf nginx-1.22.1.tar.gz
[root@web1 ~]# cd nginx-1.22.1/
[root@web1 nginx-1.22.1]# ./configure \
--with-http_ssl_module \
--with-http_stub_status_module
[root@web1 nginx-1.22.1]# make && make install
[root@web1 ~]# yum install -y php php-fpm php-mysql
  • 修改配置实现动静分离
1
2
3
4
5
6
7
8
9
10
11
[root@web1 ~]# 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;
}
  • 启动服务
1
2
3
4
5
[root@web1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@web1 ~]# nginx
[root@web1 ~]# systemctl start php-fpm
[root@web1 ~]# systemctl enable php-fpm

  • 关闭防火墙
1
2
3
4
[root@web1 ~]# systemctl disable --now firewalld
[root@web1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@web1 ~]# setenforce 0

systemd

  • 使用systemd管理nginx
  • 可拷贝sshd服务文件修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@web1 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server #描述信息
#启动顺序
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking #服务类型
PIDFile=/usr/local/nginx/logs/nginx.pid #记录进程号
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 enable nginx.service

部署Maria DB数据库

  • database主机安装数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@database ~]# yum -y install mariadb mariadb-server mariadb-devel
[root@database ~]# mysql -uroot -p000000

MariaDB [(none)]> create database wordpress character set utf8mb4;

MariaDB [(none)]> grant all on wordpress.* to wordpress@'%' identified by 'wordpress';

MariaDB [(none)]> flush privileges;

MariaDB [(none)]> exit

[root@database ~]# systemctl disable --now firewalld
[root@database ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@database ~]# setenforce 0

上线Word Press

  • 将代码上线到LNMP平台
1
2
3
4
[root@web1 ~]# tar xf wordpress-4.7.3-zh_CN.tar.gz 
[root@web1 ~]# cp -r wordpress/* /usr/local/nginx/html/
[root@web1 ~]# chown -R apache.apache /usr/local/nginx/html/

初始化wordpress

9.png

项目二期

10.png

部署ceph存储集群

  • 准备三台服务器(每台虚拟机台再加2块50磁盘)
  • 一个缓存盘,一个数据盘
  • 配置主机名、IP地址、yum源、修改hosts解析文件、防火墙和SELinux
  • node1创建ssh密钥,远程其他主机无需密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@node1 ~]# cat /etc/hosts
192.168.2.31 node1
192.168.2.32 node2
192.168.2.33 node3

[root@node1 ~]# cat /etc/yum.repos.d/ceph.repo
[MON]
name=MON
baseurl=ftp://192.168.2.31/ceph/MON
gpgcheck=0
[OSD]
name=OSD
baseurl=ftp://192.168.2.31/ceph/OSD
gpgcheck=0
[Tools]
name=Tools
baseurl=ftp://192.168.2.31/ceph/Tools
gpgcheck=0

[root@node1 ~]# systemctl disable --now firewalld
[root@node1 ~]# setenforce 0
  • node1做时间服务器、其他节点与node1同步时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@node1 ~]# vim /etc/chrony.conf 
allow 192.168.0.0/16
local stratum 10
[root@node1 ~]# systemctl restart chronyd

[root@node2 ~]# vim /etc/chrony.conf
server 192.168.2.31 iburst
[root@node2 ~]# for i in node2 node3
do
scp /etc/chrony.conf $i:/chrony.conf
ssh $i "systemctl restart chronyd"
done
[root@node2 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* node1 10 6 17 18 +3012ns[ +269us] +/- 1198us

部署MON集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@node1 ~]# yum install -y ceph-deploy
[root@node1 ~]# for i in node1 node2 node3
do
ssh $i "yum install -y ceph-mon ceph-osd ceph-mds"
done

[root@node1 ~]# mkdir ceph-cluster
[root@node1 ~]# cd ceph-cluster
[root@node1 ceph-cluster]# ceph-deploy new node1 node2 node3
[root@node1 ceph-cluster]# ceph-deploy mon create-initial
[root@node1 ceph-cluster]# ceph -s
cluster 88d5875c-2fa7-44a8-928e-871e2197c3f1
health HEALTH_ERR
clock skew detected on mon.node3
no osds
Monitor clock skew detected
monmap e1: 3 mons at {node1=192.168.2.31:6789/0,node2=192.168.2.32:6789/0,node3=192.168.2.33:6789/0}
election epoch 4, quorum 0,1,2 node1,node2,node3
osdmap e1: 0 osds: 0 up, 0 in
flags sortbitwise
pgmap v2: 64 pgs, 1 pools, 0 bytes data, 0 objects
0 kB used, 0 kB / 0 kB avail
64 creating

初始化磁盘设备

1
2
[root@node1 ~]# cd ceph-cluster
[root@node1 ceph-cluster]# ceph-deploy disk zap node1:sdb node1:sdc node2:sdb node2:sdc node3:sdb node3:sdc

部署OSD集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@node1 ceph-cluster]# ceph-deploy osd create node1:sdc:/dev/sdb
[root@node1 ceph-cluster]# ceph-deploy osd create node2:sdc:/dev/sdb
[root@node1 ceph-cluster]# ceph-deploy osd create node3:sdc:/dev/sdb

[root@node1 ceph-cluster]# ceph -s
cluster 88d5875c-2fa7-44a8-928e-871e2197c3f1
health HEALTH_WARN
clock skew detected on mon.node3
Monitor clock skew detected
monmap e1: 3 mons at {node1=192.168.2.31:6789/0,node2=192.168.2.32:6789/0,node3=192.168.2.33:6789/0}
election epoch 4, quorum 0,1,2 node1,node2,node3
osdmap e14: 3 osds: 3 up, 3 in
flags sortbitwise
pgmap v26: 64 pgs, 1 pools, 0 bytes data, 0 objects
101 MB used, 61305 MB / 61406 MB avail
64 active+clean

[root@node1 ceph-cluster]# ceph osd tree
ID WEIGHT TYPE NAME UP/DOWN REWEIGHT PRIMARY-AFFINITY
-1 0.05846 root default
-2 0.01949 host node1
0 0.01949 osd.0 up 1.00000 1.00000
-3 0.01949 host node2
1 0.01949 osd.1 up 1.00000 1.00000
-4 0.01949 host node3
2 0.01949 osd.2 up 1.00000 1.00000

启动MDS服务

1
2
3
[root@node1 ~]# cd ceph-cluster/
[root@node1 ceph-cluster]# ceph-deploy mds create node3

创建存储池

1
2
[root@node1 ceph-cluster]# ceph osd pool create cephfs_data 64
[root@node1 ceph-cluster]# ceph osd pool create cephfs_metadata 64

创建文件系统

1
2
[root@node1 ceph-cluster]# ceph fs new myfs1 cephfs_metadata cephfs_data

迁移网站数据

  • 备份wordpress数据
1
2
3
[root@web1 ~]# cd /usr/local/nginx/html/
[root@web1 html]# tar -czpf /root/html.tar.gz ./*
[root@web1 html]# rm -rf ./*
  • web服务器挂载ceph文件系统(软件在ceph10.iso)
1
2
3
4
5
6
7
8
9
10
[root@web1 ~]# yum install -y libcephfs1
[root@web1 ~]# vim /etc/fstab
192.168.2.31:6789:/ /usr/local/nginx/html ceph defaults,_netdev,name=admin,secret=AQD3UT5kg/gZLhAA0otQd7xthEws9+h7jqCHkg== 0 0
[root@web1 ~]# mount -a
[root@web1 ~]# tar xf html.tar.gz -C /usr/local/nginx/html/

[root@web1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
192.168.2.31:6789:/ 60G 244M 60G 1% /usr/local/nginx/html

web服务器集群环境

  • 部署服务器(web2与web3做相同操作)
1
2
3
4
5
6
7
8
[root@web2 ~]# yum install -y gcc openssl-devel pcre-devel make 
[root@web2 ~]# tar xf nginx-1.22.1.tar.gz
[root@web2 ~]# cd nginx-1.22.1/
[root@web2 nginx-1.22.1]# ./configure \
--with-http_ssl_module \
--with-http_stub_status_module
[root@web2 nginx-1.22.1]# make && make install
[root@web2 nginx-1.22.1]# yum install -y php php-fpm php-mysql
  • 修改配置
1
2
3
4
5
6
7
8
9
10
11
[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;
}
  • 启动服务
1
2
3
4
5
6
[root@web2 ~]# scp web1:/usr/lib/systemd/system/nginx.service /usr/lib/systemd/system/
[root@web2 ~]# systemctl start nginx
[root@web2 ~]# systemctl enable nginx
[root@web2 ~]# systemctl start php-fpm
[root@web2 ~]# systemctl enable php-fpm

  • 关闭防火墙
1
2
3
4
[root@web2 ~]# systemctl disable --now firewalld
[root@web2 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@web2 ~]# setenforce 0

  • web2服务器挂载ceph文件系统
1
2
3
4
5
6
[root@web2 ~]# yum install -y libcephfs1
[root@web2 ~]# vim /etc/fstab
192.168.2.31:6789:/ /usr/local/nginx/html ceph defaults,_netdev,name=admin,secret=AQD3UT5kg/gZLhAA0otQd7xthEws9+h7jqCHkg== 0 0

[root@web2 ~]# mount -a

  • 可以通过浏览器访问
  • 192.168.2.12
  • 192.168.2.13

项目三期

11.png

部署代理服务器一

  • 部署代理服务
1
2
3
4
5
6
7
8
9
[root@proxy1 ~]# yum install -y haproxy
[root@proxy1 ~]# vim /etc/haproxy/haproxy.cfg
#手动添加
listen websrv *:80
server web1 192.168.2.11:80 check
server web2 192.168.2.12:80 check
server web3 192.168.2.13:80 check

[root@proxy1 ~]# systemctl enable --now haproxy.service
  • 配置keepalived实现高可用(主服务器)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@proxy1 ~]# yum install -y keepalived  
[root@proxy1 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id proxy1
vrrp_iptables
}

vrrp_instance VI_1 {
state MASTER
interface ens33 #4网段网卡
virtual_router_id 51
priority 100
virtual_ipaddress {
192.168.4.80
}
}
#后面可删除
[root@proxy1 ~]# systemctl enable --now keepalived.service
[root@proxy1 ~]# ip a s ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f3:4b:59 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.5/24 brd 192.168.4.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.4.80/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::2e84:2496:fa91:d96a/64 scope link noprefixroute
valid_lft forever preferred_lft forever

部署代理服务器二

  • 部署代理服务
1
2
3
4
5
6
7
8
9
[root@proxy2 ~]# yum install -y haproxy
[root@proxy2 ~]# vim /etc/haproxy/haproxy.cfg
#手动添加
listen websrv *:80
server web1 192.168.2.11:80 check
server web2 192.168.2.12:80 check
server web3 192.168.2.13:80 check

[root@proxy2 ~]# systemctl enable --now haproxy.service
  • 配置keepalived实现高可用(备用服务器)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@proxy2 ~]# yum install keepalived -y 
[root@proxy2 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id proxy2
vrrp_iptables
}

vrrp_instance VI_1 {
state BACKUP
interface ens33 #4网段网卡
virtual_router_id 51
priority 80
virtual_ipaddress {
192.168.4.80
}
}
#后面可删除
[root@proxy2 ~]# systemctl enable --now keepalived.service

客户端解析

  • 正常情况下,我们应该部署一台DNS服务器完成域名解析

  • 这里因为我们使用真实主机作为客户端,所以不再修改真机DNS

    否则,可能导致真机无法上网

  • 这里我们直接修改hosts解析即可(域名labx.tedu.cn–192.168.4.80)

    Windows:C:\Windows\System32\drivers\etc\hosts

    Linux:/etc/hosts

  • 如果使用域名访问还需要修改wordpress配置文件

1
2
3
[root@web1 ~]# vim  /usr/local/nginx/html/wp-config.php
define('WP_SITEURL','http://labx.tedu.cn');
define('WP_HOME','http://labx.tedu.cn');
  • 通过labx.tedu.cn或192.168.4.80访问