第9章 nginx+lb负载均衡+keepalived双机热备+nfs文件共享

nginx基础架构实验

实验环境:

关闭防火墙和selinux

web1:192.168.8.5

web2:192.168.8.6

mysql:192.168.8.7

PHP:192.168.8.8

nfs:192.168.8.9

lb1:192.168.8.10

lb2:192.168.8.11

实验步骤:

web1和web2上安装nginx

  1. yum安装nginx

yum -y install nginx

  1. 启动nginx服务并设置开机自启

[root@node13 ~]# systemctl start nginx

[root@node13 ~]# systemctl enable nginx

  1. 在8.7上安装MySQL

将MySQL 

下的所有安装包拖进/mysql目录下

yum -y remove mariadb-libs

yum -y localinstall *.rpm

  1. 重启mysql并设置开机自启

systemctl start mysqld

systemctl enable mysqld

  1. 8.8上安装PHP

将php包

下的所有rpm拖进虚拟机

yum -y remove php-common

yum -y localinstall *.rpm

  1. 启动PHP并设置开机自启

systemctl start php-fpm

systemctl enable php-fpm

  1. web1上安装wordpress

复制wordpress 

安装包,到虚拟机/,解压并赋权

unzip wordpress-4.9.4-zh_CN.zip

mv wordpress /

chmod -R 777 /wordpress

  1. 创建虚拟主机配置文件

vim /etc/nginx/conf.d/blog.conf

添加:

server {

        listen 80;

        server_name blog.benet.com;

        root /wordpress;

        index index.php index.html;

        location ~ \.php$ {

                root /wordpress;

                fastcgi_pass 192.168.8.8:9000;

                fastcgi_index index.php;

                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

                include fastcgi_params;

        }

}

nginx -t

  1. 在mysql服务器上创建blog数据库和用户

初始化密码:mysql_secure_installation

登录数据库:mysql -uroot -p123.com

创建数据库:create database blog;

创建远程管理用户:grant all on blog.* to whh@'%' identified by '123.com';

  1. 在web1复制wordpress目录到php和web2的根目录

scp -rp /wordpress root@192.168.8.8:/

scp -rp /wordpress root@192.168.8.6:/

scp -rp /etc/nginx/conf.d/blog.conf root@192.168.8.6:/etc/nginx/conf.d/

  1. 修改PHP服务器的配置文件

vim /etc/php-fpm.d/www.conf

定位并修改为:

listen = 192.168.8.8:9000

listen.allowed_clients = 192.168.8.5,192.168.8.6

  1. 重启php:

systemctl restart php-fpm

  1. 在客户端添加域名解析

echo "192.168.8.5 blog.benet.com" >>/etc/hosts

  1. 在客户端域名访问

firefox blog.benet.com

数据库名:blog

用户名:whh

密码:123

数据库主机:192.168.8.7 //mysql的IP地址

在web1上安装知乎

  1. 创建一个文件夹/zh,将软件包拖拽到此文件夹中:

[root@node13 ~]# mkdir /zh

[root@node13 ~]# cd /zh

  1. 解压软件包并赋予权限

[root@node13 zh]# unzip WeCenter_3-3-4.zip

[root@node13 zh]# chmod -R 777 /zh

  1. 编辑主配置文件

[root@node13 zh]# vim /etc/nginx/conf.d/zh.conf

添加:

server {

        listen 80;

        server_name zh.benet.com;

        root /zh;

        index index.php index.html;

        location ~ \.php$ {

                root /zh;                fastcgi_pass 192.168.8.8:9000;

                fastcgi_index index.php;

                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

                include fastcgi_params;

        }

        }

  1. 重载nginx

[root@node13 zh]# nginx -s reload

  1. 在mysql服务器上创建zh数据库;创建本地用户并设置密码

[root@node13 ~]# mysql -uroot -p123.com

mysql> create database zh;

mysql>  grant all on zh.* to wangwu@'%' identified by '123.com';将web1上的wecenter复制到web2和PHP服务器上

[root@node13 zh]# scp -rp /zh root@192.168.8.6:/

[root@node13 zh]# scp -rp /etc/nginx/conf.d/zh.conf root@192.168.8.6:/etc/nginx/conf.d/

scp -rp /zh root@192.168.8.8:/

  1. 添加域名解析,客户端上用 域名访问

echo "192.168.8.5 zh.benet.com" >>/etc/hosts

  1. 修改客户端配置文件:测试web2是否有博客和知乎

[root@node13 zh]# echo "192.168.8.6 zh.benet.com" >>/etc/hosts

[root@node13 zh]# echo "192.168.8.6 blog.benet.com" >>/etc/hosts

  1. 客户端使用域名访问

http://blog.benet.com/

http://zh.benet.com/

  1. 在8.10和8.11上配置负载均衡服务器

[root@node13 ~]# rm -rf /var/run/yum.pid

[root@node13 ~]# yum -y install nginx

安装失败的话执行以下步骤

  1. 配置nginx镜像

[root@node13 ~]# cd /etc/yum.repos.d/

[root@node13 yum.repos.d]# ls

CentOS-Base.repo  epel.repo

[root@node13 yum.repos.d]# vim /etc/yum.repos.d/nginx.repo

添加:

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/7/$basearch/

gpgcheck=0

enabled=1

  1. 添加优化项

[root@node13 yum.repos.d]# vim /etc/nginx/nginx_params

添加:

proxy_set_header Host $http_host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 30;

proxy_send_timeout 60;

proxy_read_timeout 60;

proxy_buffering on;

proxy_buffer_size 32k;

proxy_buffers 4 128k;

  1. 创建lb配置文件

vim /etc/nginx/conf.d/lb1.conf

添加:

upstream web_cluster {

        server 192.168.8.5:80;

        server 192.168.8.6:80;

}

server {

        listen 80;

        server_name blog.benet.com;

        location / {

                proxy_pass http://web_cluster;

                include nginx_params;

        }

}

server {

        listen 80;

        server_name zh.benet.com;

        location / {

                proxy_pass http://web_cluster;

                include nginx_params;

        }

}

  1. 检查配置文件是否有错误

[root@node13 ~]# nginx -t

  1. 重载nginx

[root@node13 ~]# nginx -s reload

  1. 修改客户机的配置文件并测试第一台负载均衡

[root@node13 ~]# echo "192.168.8.10 blog.benet.com" >>/etc/hosts

[root@node13 ~]# echo "192.168.8.10 zh.benet.com" >>/etc/hosts

http://blog.benet.com

http://zh.benet.com

  1. 拷贝配置文件到第二台lb上

[root@node13 ~]# scp -rp /etc/nginx/nginx_params root@192.168.8.11:/etc/nginx/

[root@node13 ~]# scp -rp /etc/nginx/conf.d/lb1.conf root@192.168.8.11:/etc/nginx/conf.d/

  1. 修改配置文件:测试第二台负载均衡

[root@node13 ~]# echo "192.168.8.11 zh.benet.com" >>/etc/hosts

[root@node13 ~]# echo "192.168.8.11 blog.benet.com" >>/etc/hosts

http://blog.benet.com

http://zh.benet.com

  1. 在两台负载均衡lb上安装keepalived

[root@node13 ~]# yum -y install keepalived

  1. 修改keepalived配置文件

[root@node13 ~]# vim /etc/keepalived/keepalived.conf

修改:

global_defs {

   router_id lb1

}

vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.8.254

    }

}

  1. 开启keepalived服务

[root@node13 ~]# systemctl start keepalived

  1. 修改第二台lb配置文件

[root@node13 ~]# vim /etc/keepalived/keepalived.conf

添加:

global_defs {

   router_id lb2

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.8.254

        }

}

  1. 启动keepalived服务

[root@node13 ~]# systemctl start keepalived

  1. 在lb主设备上查看漂移IP

[root@node13 ~]# ip addr show dev ens33

  1. 修改客户端配置文件:测试双击热备

[root@node13 ~]# echo "192.168.8.254 blog.benet.com" >>/etc/hosts

[root@node13 ~]# echo "192.168.8.254 zh.benet.com" >>/etc/hosts

http://blog.benet.com/

http://zh.benet.com/

  1. 在8.9上安装nfs

查看是否安装了nfs-utils、rpcbind这两个软件包:

nfs-utils:负责文件共享

rpcbind:负责网络通信

[root@node13 ~]# rpm -q nfs-utils rpcbind

  1. 创建挂载点

[root@node13 ~]# mkdir /nfs

[root@node13 ~]# mkdir /nfs/blog

[root@node13 ~]# mkdir /nfs/zh

  1. 发布共享目录

[root@node13 ~]# vim /etc/exports

添加:

/nfs/blog  192.168.8.0/24(rw,sync,no_root_squash)

/nfs/zh  192.168.8.0/24(rw,sync,no_root_squash)

  1. 重启nfs服务

[root@node13 ~]# systemctl restart rpcbind

[root@node13 ~]# systemctl restart nfs

  1. 在两台nginx服务器上(8.5和8.6)上查看nfs共享目录

[root@node13 ~]# showmount -e 192.168.8.9

  1. 把wordpress的内容目录挂载到nfs

[root@node13 ~]# cd /wordpress/

[root@node13 wordpress]# cp -rp wp-content/ wp-contentbak

[root@node13 wordpress]# mount -t nfs 192.168.8.9:/nfs  wp-content

[root@node13 wordpress]# cp -rp wp-contentbak/*  wp-content/

  1. 两台nginx上配置永久挂载

[root@node13 ~]# vim /etc/fstab

添加:

192.168.8.9:/nfs/blog   /wordpress/wp-content nfs       defaults        0 0

  1. 验证是否同步,nginx上创建文件,nfs上可以同步

[root@node13 wordpress]# cd wp-content

[root@node13 wp-content]# ls

[root@node13 wp-content]# touch 123

nfs上查看:

[root@node13 ~]# cd /nfs/blog/

[root@node13 blog]# ls

  1. 在第二台nginx上挂载知乎目录

[root@node13 ~]# showmount -e 192.168.8.9

[root@node13 ~]# cd /wordpress/

[root@node13 wordpress]# cp -rp wp-content/ wp-contentbak

[root@node13 wordpress]# mount -t nfs 192.168.8.9:/nfs wp-content

  1. nfs上创建文件

[root@node13 blog]# cd /nfs/zh

[root@node13 zh]# touch aaa

  1. 第二台nginx上查看是否同步

[root@node13 ~]# ls /wordpress/wp-content/zh

  1. 在第二台nginx服务器上创建文件

[root@node13 ~]# chmod -R 777 /wordpress/wp-content/zh

[root@node13 ~]# cd /wordpress/wp-content/zh

[root@node13 zh]# touch bbb

  1. nfs上查看是否同步

[root@node13 zh]# ls

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值