搭建LNMP平台实现负载均衡与高可用

目录

环境要求

安装服务

搭建服务

1. nfs主机操作

2. web1主机操作

3. web2主机操作

4. php主机操作

5. mysql主机操作

6. 验证1

7. lb1主机操作

8. lb2主机操作

9. 验证2

测试1

测试2

网站故障解决

验证


环境要求

        实验目标:搭建LNMP平台实现负载均衡与高可用。

拓扑图如下:

 搭建LNMP平台实现负载均衡与高可用_第1张图片

        打开七台centos服务器,根据各个主机命名并配置IP,安装所需软件。

安装服务

        下面将进行安装服务,注意看清除主机名。

1. lb1安装

[root@lb1 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force

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

[root@lb1 ~]# systemctl start nginx keepalived

[root@lb1 ~]# systemctl enable nginx keepalived

2. lb2安装

[root@lb2 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force

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

[root@lb2 ~]# systemctl start nginx keepalived

[root@lb2 ~]# systemctl enable nginx keepalived

3. web1安装

[root@web1 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force

[root@web1 ~]# systemctl start nginx

[root@web1 ~]# systemctl enable nginx

4. web2安装

[root@web2 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force 

[root@web2 ~]# systemctl start nginx

[root@web2 ~]# systemctl enable nginx

5. php安装

[root@php ~]# rpm -ivh /media/php-rpm/* --nodeps --force

[root@php ~]# systemctl start php-fpm

[root@php ~]# systemctl enable php-fpm

6. mysql安装

[root@mysql ~]# rpm -ivh /media/mysql5.6-rpm/* --nodeps --force

[root@mysql ~]# systemctl start mysqld

[root@mysql ~]# systemctl enable mysqld

7. nfs安装

[root@nfs ~]# yum -y install nfs-utils rpcbind

[root@nfs ~]# systemctl start rpcbind nfs

[root@nfs ~]# systemctl enable rpcbind nfs

搭建服务

          经过上面安装服务后的操作,就可以进行下面的搭建操作了,本次将进行对每个主机的操作,分多次验证。注意:区分主机名称!

1. nfs主机操作

[root@nfs ~]# cp -rp /media/wordpress-4.9.4-zh_CN.zip /

[root@nfs ~]# cd /

[root@nfs /]# unzip wordpress-4.9.4-zh_CN.zip 

[root@nfs /]# chmod -R 777 /wordpress

[root@nfs /]# vim /etc/exports

/wordpress 192.168.1.0/24(rw,sync,no_root_squash)

[root@nfs /]# systemctl restart rpcbind nfs

2. web1主机操作

[root@web1 ~]# showmount -e 192.168.1.10 //查看nfs是否成功
Export list for 192.168.1.10:
/wordpress 192.168.1.0/24
[root@web1 ~]# cd /etc/nginx/conf.d/
[root@web1 conf.d]# rm -rf *
[root@web1 conf.d]# vim web.conf
server {
        listen 80;
        server_name www.web.com;
        root /wordpress;
        index index.php index.html;

        location ~ \.php$ {
                root /wordpress;
                fastcgi_pass 192.168.1.8:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                }
        }
[root@web1 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web1 conf.d]# systemctl restart nginx
[root@web1 conf.d]# mkdir /wordpress
[root@web1 conf.d]# mount -t nfs 192.168.1.10:/wordpress /wordpress/
[root@web1 conf.d]# echo "192.168.1.10:/wordpress /wordpress nfs defaults 0 0" >> /etc/fstab

3. web2主机操作

[root@web2 ~]# showmount -e 192.168.1.10

Export list for 192.168.1.10:

/wordpress 192.168.1.0/24

[root@web2 ~]# scp -rp [email protected]:/etc/nginx/* /etc/nginx/

[root@web2 ~]# cd /etc/nginx/conf.d/

[root@web2 conf.d]# rm -rf default.conf

[root@web2 conf.d]# systemctl restart nginx

[root@web2 conf.d]# mkdir /wordpress

[root@web2 conf.d]# mount -t nfs 192.168.1.10:/wordpress/ /wordpress/

[root@web2 conf.d]# echo "192.168.1.10:/wordpress /wordpress nfs defaults 0 0" >> /etc/fstab

4. php主机操作

[root@php ~]# showmount -e 192.168.1.10

Export list for 192.168.1.10:

/wordpress 192.168.1.0/24

[root@php ~]# vim /etc/php-fpm.d/www.conf  //修改下面两行内容

listen = 192.168.1.8:9000 //监听php本机

listen.allowed_clients = 192.168.1.6,192.168.1.7 //允许web1和web2主机访问

[root@php ~]# systemctl restart php-fpm

[root@php ~]# mkdir /wordpress

[root@php ~]# mount -t nfs 192.168.1.10:/wordpress/ /wordpress/

[root@php ~]# echo "192.168.1.10:/wordpress /wordpress nfs defaults 0 0" >> /etc/fstab

5. mysql主机操作

[root@mysql ~]# mysqladmin -uroot password

New password:

Confirm new password:

[root@mysql ~]# mysql -uroot -p123456

//省略部分内容

mysql> create database blog;

Query OK, 1 row affected (0.00 sec)



mysql> grant all on blog.* to lisi@'%' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

6. 验证1

        访问http://192.168.1.6或者http://192.168.1.7都可以查看到论坛安装页面,点击“现在就开始!”。

搭建LNMP平台实现负载均衡与高可用_第2张图片

        输入数据库名称,用户名密码以及mysql服务器IP地址。完成后点击提交。

 搭建LNMP平台实现负载均衡与高可用_第3张图片

        连接数据库后,点击现在安装。

搭建LNMP平台实现负载均衡与高可用_第4张图片

        创建站点标题,新建管理员用户名称密码及邮箱号后点击安装。

搭建LNMP平台实现负载均衡与高可用_第5张图片

        此时就可以看到完成页面了,点击登录按钮输入登录信息。

搭建LNMP平台实现负载均衡与高可用_第6张图片  

        输入管理员用户密码后就可以登录到论坛首页了。

搭建LNMP平台实现负载均衡与高可用_第7张图片

搭建LNMP平台实现负载均衡与高可用_第8张图片

        以上便是LNMP平台搭建论坛的全部过程了。下面将继续进行搭建负载均衡及高可用功能。

7. lb1主机操作

[root@lb1 ~]# 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;
[root@lb1 ~]# cd /etc/nginx/conf.d/
[root@lb1 conf.d]# rm -rf default.conf 
[root@lb1 conf.d]# vim lb.conf
upstream web {
        server 192.168.1.6:80;
        server 192.168.1.7:80;
        }
server {
        listen 80;
        server_name www.blog.com;
        location / {
                proxy_pass http://web;
                include nginx_params;
        }
}

[root@lb1 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb1 conf.d]# systemctl restart nginx
[root@lb1 ~]# vim /etc/keepalived/keepalived.conf  //修改下面配置文件
global_defs {
   router_id lb1 //修改路由名称
   vrrp_strict //删除本行内容,路由器中vrrp的一些协议,linux系统中部分没有所以需要删掉
}
vrrp_instance VI_1 {
    state MASTER //路由身份,MASTER 主要,BACKUP备份
    interface ens33 //修改为本机网卡
    virtual_router_id 51
    priority 100 //优先级,最高为100
    advert_int 1
    authentication { //验证模块不需要修改,主备需相同
        auth_type PASS
        auth_pass 1111
    }   
    virtual_ipaddress {
        192.168.1.200 //虚拟路由IP(漂移地址)
    }   
}
[root@lb1 ~]# systemctl restart keepalived

        重启路由后可以通过命令查看漂移地址

搭建LNMP平台实现负载均衡与高可用_第9张图片

8. lb2主机操作

[root@lb2 ~]# scp -rp [email protected]:/etc/nginx/* /etc/nginx
[root@lb2 ~]# rm -rf /etc/nginx/conf.d/default.conf
[root@lb2 ~]# systemctl restart nginx
[root@lb2 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
   router_id lb2 //修改路由名称
   vrrp_strict //删掉此行内容
}


vrrp_instance VI_1 {
    state BACKUP //修改为备份状态
    interface ens33 //修改为本机网卡
    virtual_router_id 51
    priority 99 //优先级修改为99,要低于主服务器
    advert_int 1
    authentication { //验证模块信息不需要修改
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.200 //漂移地址
    }
}
[root@lb2 ~]# systemctl restart keepalived

9. 验证2

        此刻配置已经差不多了,下面验证一下漂移地址是否可用。

访问http://192.168.1.200,显示是正常的。

搭建LNMP平台实现负载均衡与高可用_第10张图片

        测试关闭主服务器的keepalived是否显示漂移地址,浏览器访问是否能成功。

搭建LNMP平台实现负载均衡与高可用_第11张图片

 搭建LNMP平台实现负载均衡与高可用_第12张图片

        上面可以看到lb1已经没有漂移地址,但是依旧可以访问论坛,现在查看lb2服务器是否存在漂移地址。下面经过验证,漂移地址已经在lb2服务器上了。

搭建LNMP平台实现负载均衡与高可用_第13张图片

测试1

        重新开启lb1的keepalived服务,并关闭lb1服务器的nginx服务,验证是否可以访问网站并确定漂移地址位置。

[root@lb1 ~]# systemctl start keepalived

[root@lb1 ~]# systemctl stop nginx

搭建LNMP平台实现负载均衡与高可用_第14张图片

搭建LNMP平台实现负载均衡与高可用_第15张图片 

        访问http://192.168.1.200

搭建LNMP平台实现负载均衡与高可用_第16张图片 

        上面可以看到,虽然漂移地址还在主服务器中,但是无法访问网站内容。

测试2

        开启lb1服务器nginx服务,关闭lb2服务器nginx服务,验证是否可以访问网站。

[root@lb1 ~]# systemctl start nginx

[root@lb2 ~]# systemctl stop nginx

搭建LNMP平台实现负载均衡与高可用_第17张图片

        发现可以访问网站,下面将进行lb1服务器nginx关闭无法访问网站问题。

网站故障解决

        开启lb2的网站服务。

[root@lb2 ~]# systemctl start nginx

        下面在lb1服务器操作,编辑监控脚本。

[root@lb1 ~]# vim /sh/check_nginx_proxy.sh
#!/bin/bash
killall  -0  nginx
if  [ $? -ne 0 ];then
  systemctl stop keepalived
fi
[root@lb1 ~]# chmod +x /sh/check_nginx_proxy.sh
[root@lb1 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
 //省略部分内容
}
vrrp_script check_nginx_proxy { //添加脚本追踪模块
        script "/sh/check_nginx_proxy.sh"
        interval 2
        weight 5
}
vrrp_instance VI_1 {
//省略部分内容
    }   
    virtual_ipaddress {
        192.168.1.200
    }   
        track_script { //此模块在实例内添加
        check_nginx_proxy
    }   
}
[root@lb1 ~]# systemctl restart keepalived

验证

        下面将进行最后的验证,关闭lb1的nginx网站服务,查看漂移地址。查看是否可以查看论坛内容。

搭建LNMP平台实现负载均衡与高可用_第18张图片

搭建LNMP平台实现负载均衡与高可用_第19张图片 

        经过网站故障解决操作,已经解决了lb1主机nginx故障不能访问网站的问题。最后可以看到漂移地址已经到lb2上了。

搭建LNMP平台实现负载均衡与高可用_第20张图片  

以上就是今天的实验全过程了,如果没看明白请点击【传送门】。

你可能感兴趣的:(群集,LNMP,nginx,服务器,运维)