关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

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

发布时间:2023-06-30 19:01:00

环境要求

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

拓扑图如下:

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

安装服务

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

1. lb1安装

1. [root@lb1 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force 2. 3. [root@lb1 ~]# yum -y install keepalived 4. 5. [root@lb1 ~]# systemctl start nginx keepalived 6. 7. [root@lb1 ~]# systemctl enable nginx keepalived

   

2. lb2安装

1. [root@lb2 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force 2. 3. [root@lb2 ~]# yum -y install keepalived 4. 5. [root@lb2 ~]# systemctl start nginx keepalived 6. 7. [root@lb2 ~]# systemctl enable nginx keepalived

   

3. web1安装

1. [root@web1 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force 2. 3. [root@web1 ~]# systemctl start nginx 4. 5. [root@web1 ~]# systemctl enable nginx

   

4. web2安装

1. [root@web2 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force 2. 3. [root@web2 ~]# systemctl start nginx 4. 5. [root@web2 ~]# systemctl enable nginx

   

5. php安装

1. [root@php ~]# rpm -ivh /media/php-rpm/* --nodeps --force 2. 3. [root@php ~]# systemctl start php-fpm 4. 5. [root@php ~]# systemctl enable php-fpm

   

6. mysql安装

1. [root@mysql ~]# rpm -ivh /media/mysql5.6-rpm/* --nodeps --force 2. 3. [root@mysql ~]# systemctl start mysqld 4. 5. [root@mysql ~]# systemctl enable mysqld

   

7. nfs安装

1. [root@nfs ~]# yum -y install nfs-utils rpcbind 2. 3. [root@nfs ~]# systemctl start rpcbind nfs 4. 5. [root@nfs ~]# systemctl enable rpcbind nfs

   

搭建服务

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

1. nfs主机操作

1. [root@nfs ~]# cp -rp /media/wordpress-4.9.4-zh_CN.zip / 2. 3. [root@nfs ~]# cd / 4. 5. [root@nfs /]# unzip wordpress-4.9.4-zh_CN.zip 6. 7. [root@nfs /]# chmod -R 777 /wordpress 8. 9. [root@nfs /]# vim /etc/exports 10. 11. /wordpress 192.168.1.0/24(rw,sync,no_root_squash) 12. 13. [root@nfs /]# systemctl restart rpcbind nfs

   

2. web1主机操作

1. [root@web1 ~]# showmount -e 192.168.1.10 //查看nfs是否成功 2. Export list for 192.168.1.10: 3. /wordpress 192.168.1.0/24 4. [root@web1 ~]# cd /etc/nginx/conf.d/ 5. [root@web1 conf.d]# rm -rf * 6. [root@web1 conf.d]# vim web.conf 7. server { 8. listen 80; 9. server_name www.web.com; 10. root /wordpress; 11. index index.php index.html; 12. 13. location ~ \.php$ { 14. root /wordpress; 15. fastcgi_pass 192.168.1.8:9000; 16. fastcgi_index index.php; 17. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 18. include fastcgi_params; 19. } 20. } 21. [root@web1 conf.d]# nginx -t 22. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 23. nginx: configuration file /etc/nginx/nginx.conf test is successful 24. [root@web1 conf.d]# systemctl restart nginx 25. [root@web1 conf.d]# mkdir /wordpress 26. [root@web1 conf.d]# mount -t nfs 192.168.1.10:/wordpress /wordpress/ 27. [root@web1 conf.d]# echo "192.168.1.10:/wordpress /wordpress nfs defaults 0 0" >> /etc/fstab

   

3. web2主机操作

1. [root@web2 ~]# showmount -e 192.168.1.10 2. 3. Export list for 192.168.1.10: 4. 5. /wordpress 192.168.1.0/24 6. 7. [root@web2 ~]# scp -rp root@192.168.1.6:/etc/nginx/* /etc/nginx/ 8. 9. [root@web2 ~]# cd /etc/nginx/conf.d/ 10. 11. [root@web2 conf.d]# rm -rf default.conf 12. 13. [root@web2 conf.d]# systemctl restart nginx 14. 15. [root@web2 conf.d]# mkdir /wordpress 16. 17. [root@web2 conf.d]# mount -t nfs 192.168.1.10:/wordpress/ /wordpress/ 18. 19. [root@web2 conf.d]# echo "192.168.1.10:/wordpress /wordpress nfs defaults 0 0" >> /etc/fstab

   

4. php主机操作

1. [root@php ~]# showmount -e 192.168.1.10 2. 3. Export list for 192.168.1.10: 4. 5. /wordpress 192.168.1.0/24 6. 7. [root@php ~]# vim /etc/php-fpm.d/www.conf //修改下面两行内容 8. 9. listen = 192.168.1.8:9000 //监听php本机 10. 11. listen.allowed_clients = 192.168.1.6,192.168.1.7 //允许web1和web2主机访问 12. 13. [root@php ~]# systemctl restart php-fpm 14. 15. [root@php ~]# mkdir /wordpress 16. 17. [root@php ~]# mount -t nfs 192.168.1.10:/wordpress/ /wordpress/ 18. 19. [root@php ~]# echo "192.168.1.10:/wordpress /wordpress nfs defaults 0 0" >> /etc/fstab

   

5. mysql主机操作

1. [root@mysql ~]# mysqladmin -uroot password 2. 3. New password: 4. 5. Confirm new password: 6. 7. [root@mysql ~]# mysql -uroot -p123456 8. 9. //省略部分内容 10. 11. mysql> create database blog; 12. 13. Query OK, 1 row affected (0.00 sec) 14. 15. 16. 17. mysql> grant all on blog.* to lisi@'%' identified by '123456'; 18. 19. Query OK, 0 rows affected (0.00 sec) 20. 21. mysql> exit 22. 23. Bye

   

6. 验证1

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

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

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

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

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

 

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

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

7. lb1主机操作

1. [root@lb1 ~]# vim /etc/nginx/nginx_params 2. proxy_set_header Host $http_host; 3. proxy_set_header X-Real-IP $remote_addr; 4. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 5. proxy_connect_timeout 30; 6. proxy_send_timeout 60; 7. proxy_read_timeout 60; 8. proxy_buffering on; 9. proxy_buffer_size 32k; 10. proxy_buffers 4 128k; 11. [root@lb1 ~]# cd /etc/nginx/conf.d/ 12. [root@lb1 conf.d]# rm -rf default.conf 13. [root@lb1 conf.d]# vim lb.conf 14. upstream web { 15. server 192.168.1.6:80; 16. server 192.168.1.7:80; 17. } 18. server { 19. listen 80; 20. server_name www.blog.com; 21. location / { 22. proxy_pass http://web; 23. include nginx_params; 24. } 25. } 26. 27. [root@lb1 conf.d]# nginx -t 28. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 29. nginx: configuration file /etc/nginx/nginx.conf test is successful 30. [root@lb1 conf.d]# systemctl restart nginx 31. [root@lb1 ~]# vim /etc/keepalived/keepalived.conf //修改下面配置文件 32. global_defs { 33. router_id lb1 //修改路由名称 34. vrrp_strict //删除本行内容,路由器中vrrp的一些协议,linux系统中部分没有所以需要删掉 35. } 36. vrrp_instance VI_1 { 37. state MASTER //路由身份,MASTER 主要,BACKUP备份 38. interface ens33 //修改为本机网卡 39. virtual_router_id 51 40. priority 100 //优先级,最高为100 41. advert_int 1 42. authentication { //验证模块不需要修改,主备需相同 43. auth_type PASS 44. auth_pass 1111 45. } 46. virtual_ipaddress { 47. 192.168.1.200 //虚拟路由IP(漂移地址) 48. } 49. } 50. [root@lb1 ~]# systemctl restart keepalived

   

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

8. lb2主机操作

1. [root@lb2 ~]# scp -rp root@192.168.1.4:/etc/nginx/* /etc/nginx 2. [root@lb2 ~]# rm -rf /etc/nginx/conf.d/default.conf 3. [root@lb2 ~]# systemctl restart nginx 4. [root@lb2 ~]# vim /etc/keepalived/keepalived.conf 5. global_defs { 6. router_id lb2 //修改路由名称 7. vrrp_strict //删掉此行内容 8. } 9. 10. 11. vrrp_instance VI_1 { 12. state BACKUP //修改为备份状态 13. interface ens33 //修改为本机网卡 14. virtual_router_id 51 15. priority 99 //优先级修改为99,要低于主服务器 16. advert_int 1 17. authentication { //验证模块信息不需要修改 18. auth_type PASS 19. auth_pass 1111 20. } 21. virtual_ipaddress { 22. 192.168.1.200 //漂移地址 23. } 24. } 25. [root@lb2 ~]# systemctl restart keepalived

   

9. 验证2

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

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

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

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

测试1

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

1. [root@lb1 ~]# systemctl start keepalived 2. 3. [root@lb1 ~]# systemctl stop nginx

   

        访问http://192.168.1.200

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

测试2

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

1. [root@lb1 ~]# systemctl start nginx 2. 3. [root@lb2 ~]# systemctl stop nginx

   

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

网站故障解决

        开启lb2的网站服务。

[root@lb2 ~]# systemctl start nginx

   

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

1. [root@lb1 ~]# vim /sh/check_nginx_proxy.sh 2. #!/bin/bash 3. killall -0 nginx 4. if [ $? -ne 0 ];then 5. systemctl stop keepalived 6. fi 7. [root@lb1 ~]# chmod +x /sh/check_nginx_proxy.sh 8. [root@lb1 ~]# vim /etc/keepalived/keepalived.conf 9. global_defs { 10. //省略部分内容 11. } 12. vrrp_script check_nginx_proxy { //添加脚本追踪模块 13. script "/sh/check_nginx_proxy.sh" 14. interval 2 15. weight 5 16. } 17. vrrp_instance VI_1 { 18. //省略部分内容 19. } 20. virtual_ipaddress { 21. 192.168.1.200 22. } 23. track_script { //此模块在实例内添加 24. check_nginx_proxy 25. } 26. } 27. [root@lb1 ~]# systemctl restart keepalived

   

验证

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

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

 

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



/template/Home/leiyu/PC/Static