美文网首页Project--WordPress网站搭建
WordPress网站-3·搭建负载均衡集群

WordPress网站-3·搭建负载均衡集群

作者: 技术老男孩 | 来源:发表于2022-12-10 08:29 被阅读0次

一、集群拓扑图:

拓扑图.png

二、环境配置:

主机名 地址
客户端机 client 192.168.88.10
代理服务器机 proxy 网卡1:192.168.88.5
代理服务器机 proxy 网卡2:192.168.99.5
web集群机 web1 192.168.99.11
web集群机 web2 192.168.99.12
web集群机 web3 192.168.99.13
数据存储服务器 nfs 192.168.99.21
数据库服务器 database 192.168.99.31

三、搭建思路:

  1. 配置web集群服务器部署nginx
  2. 配置NFS服务器
  3. 配置代理服务器
  4. 配置域名名称解析

四、实操:

第一步:配置web集群服务器部署nginx

  • 由于在上一个实操过程已经给web1搭建好nginx的环境,直接拿来主义
  • 将web1的nginx部署到web2和web3
[root@web1 ~]# cd /usr/local/
# 打包nginx
[root@web1 local]# tar czf /root/nginx.tar.gz nginx

# scp发送压缩包
[root@web1 local]# cd
[root@web1 ~]# scp nginx.tar.gz 192.168.99.12:/root/
[root@web1 ~]# ^12^13   # 将上一条命令的12换成13执行

# 在web2和web3上解压,实现nginx部署
[root@web2 ~]# tar xf nginx.tar.gz -C /usr/local/
[root@web3 ~]# tar xf nginx.tar.gz -C /usr/local/
  • 给web2,web3配置server文件并启动nginx
# 把web1上的service文件拷贝到web2上
[root@web1 ~]# scp /usr/lib/systemd/system/nginx.service 192.168.99.12:/usr/lib/systemd/system/
# 把web1上的service文件拷贝到web3上
[root@web1 ~]# ^12^13

# 在web2上启服务
[root@web2 ~]# systemctl daemon-reload 
[root@web2 ~]# systemctl enable nginx.service --now
[root@web2 ~]# ss -tlnp | grep :80
LISTEN     0      128          *:80

# 在web3上启服务
[root@web3 ~]# systemctl daemon-reload 
[root@web3 ~]# systemctl enable nginx.service --now
[root@web3 ~]# ss -tlnp | grep :80
LISTEN     0      128          *:80
  • 配置web2和web3支持php
# web2安装php
[root@web2 ~]# yum install -y php php-fpm php-mysql
# web2启动php-fpm
[root@web2 ~]# systemctl enable php-fpm --now

# web3安装php
[root@web3 ~]# yum install -y php php-fpm php-mysql
# web3启动php-fpm
[root@web3 ~]# systemctl enable php-fpm --now
  • 测试访问web1、web2和web3页面,再任意新建文章,如果其他二台服务器可以同步内容,就视为成功。


    wordpress首页.png

第二步:配置NFS服务器

  • 配置nfs并启动服务
# 安装nfs
[root@nfs ~]# yum install -y nfs-utils.x86_64 

# 创建共享目录
[root@nfs ~]# mkdir /web_share

# 修改nfs的配置文件export
# 添加共享目录及访问主机IP地址
[root@nfs ~]# vim /etc/exports
/web_share      192.168.99.0/24(rw,no_root_squash)
# rw表示读写权限
# no_root_squash,表示远程主机root创建的文件
# 属主属组就是root。默认会变成nfsnobody

# 启动rpcbind服务
# 需要注意的是NFS服务依赖rpcbind服务
[root@nfs ~]# systemctl enable rpcbind --now
# 检查rpcbind服务(端口111)
[root@nfs ~]# ss -tlnp | grep :111
LISTEN     0      128          *:111

# 启动nfs服务
[root@nfs ~]# systemctl enable nfs --now
# 检查nfs服务(端口2049)
[root@nfs ~]# ss -tlnp | grep :2049
LISTEN     0      64           *:2049

# 查看nfs共享列表
[root@nfs ~]# showmount -e
Export list for nfs:
/web_share 192.168.99.0/24
  • 将html目录迁移
# 进入web1的nginx目录
[root@web1 ~]# cd /usr/local/nginx/
# 将网页目录保留权限,打压缩包
[root@web1 nginx]# tar cpzf /root/html.tar.gz html

# 拷贝文件至nfs服务器
[root@web1 ~]# scp html.tar.gz 192.168.99.31:/root/

# 在nfs服务器上解压
[root@nfs ~]# tar xf html.tar.gz -C /web_share/

# 删除web服务器html目录中的内容
[root@web1 ~]# rm -rf /usr/local/nginx/html/*
[root@web2 ~]# rm -rf /usr/local/nginx/html/*
[root@web3 ~]# rm -rf /usr/local/nginx/html/*
  • 挂载共享目录
# web1安装nfs-utils
[root@web1 ~]# yum install -y nfs-utils
# web1配置开机挂载共享目录
[root@web1 ~]# echo '192.168.99.31:/web_share/html /usr/local/nginx/html nfs defaults 0 0' >> /etc/fstab 
[root@web1 ~]# mount -a
# 查看挂载列表,确认nfs是否在web1上挂载成功
[root@web1 ~]# df -h /usr/local/nginx/html/
文件系统                      容量  已用  可用 已用% 挂载点
192.168.99.31:/web_share/html   30G  1.4G   29G    5% /usr/local/nginx/html


# web2安装nfs-utils
[root@web2 ~]# yum install -y nfs-utils
# web2配置开机挂载共享目录
[root@web2 ~]# echo '192.168.99.31:/web_share/html usr/local/nginx/html
 nfs defaults 0 0' >> /etc/fstab 
[root@web2 ~]# mount -a
# 查看挂载列表,确认nfs是否在web2上挂载成功
[root@web2 ~]# df -h /usr/local/nginx/html/
文件系统                      容量  已用  可用 已用% 挂载点
192.168.99.31:/web_share/html   30G  1.4G   29G    5% /usr/local/nginx/html


# web3安装nfs-utils
[root@web3 ~]# yum install -y nfs-utils
# web3配置开机挂载共享目录
[root@web3 ~]# echo '192.168.99.31:/web_share/html /usr/local/nginx/html
 nfs defaults 0 0' >> /etc/fstab 
[root@web3 ~]# mount -a
# 查看挂载列表,确认nfs是否在web3上挂载成功
[root@web3 ~]# df -h /usr/local/nginx/html/
文件系统                      容量  已用  可用 已用% 挂载点
192.168.99.31:/web_share/html   30G  1.4G   29G    5% /usr/local/nginx/html

第三步:配置代理服务器

  • 安装并配置haproxy
# 安装haproxy
[root@proxy ~]# yum install -y haproxy

# 配置负载均衡web集群
[root@proxy ~]# vim /etc/haproxy/haproxy.cfg 
# 把63行到最后一行删除,然后追加以下内容
listen wordpress *:80
    balance roundrobin
    server web1 192.168.99.11:80 check inter 2000 rise 2 fall 3
    server web2 192.168.99.12:80 check inter 2000 rise 2 fall 3
    server web3 192.168.99.13:80 check inter 2000 rise 2 fall 3

# 为HAProxy配置监控页面
# refresh 30s:刷新时间为30秒
# auth admin:admin:监控用户名密码是admin和admin
# uri /mon:监控页访问的页面
[root@proxy ~]# vim /etc/haproxy/haproxy.cfg 
# 在结尾追加以下内容
listen mon *:1080
    stats refresh 30s
    stats uri /mon
    stats auth admin:admin
  • 启动haproxy服务
[root@proxy ~]# systemctl enable haproxy.service --now
[root@proxy ~]# ss -tlnp | grep :80
LISTEN     0      128          *:80
  • 查看监控页:访问http://192.168.88.5:1080/mon。不断访问http://192.168.88.5,在监控页可以看到不同的服务器有连接数。
    监控页.png

第四步:配置域名名称解析

  • 通过本机hosts文件实现名称解析
# 如果客户端是windows主机,则使用记事本程序打开
# 路径:C:\windows\System32\drivers\etc\hosts添加名称解析
# linux主机,直接添加hosts
[root@localhost~]# echo -e "192.168.99.5\twww.lab.com" >> /etc/hosts
  • 当点击http://www.lab.com页面中任意链接时,地址栏上的地址,都会变成192.168.99.11。通过以下方式修复它:
# 在nfs服务器上修改wordpress网站的配置文件
[root@nfs ~]# vim /web_share/html/wp-config.php 
# 在"define('DB_NAME', 'wordpress')"它的上方添加以下两行:
define('WP_SITEURL', 'http://www.lab.com');
define('WP_HOME', 'http://www.lab.com');

相关文章

网友评论

    本文标题:WordPress网站-3·搭建负载均衡集群

    本文链接:https://www.haomeiwen.com/subject/dttwfdtx.html