当前位置:首页 » 《我的小黑屋》 » 正文

Nginx(搭建高可用集群)

0 人参与  2024年10月06日 19:20  分类 : 《我的小黑屋》  评论

点击全文阅读


文章目录

1.基本介绍1.在微服务架构中的位置2.配置前提3.主从模式架构图 2.启动主Nginx和两个Tomcat1.启动linux的tomcat2.启动win的tomcat3.启动主Nginx,进入安装目录 `./sbin/nginx -c nginx.conf`4.windows访问 http://look.sunxiansheng.cn:7777/search/cal.jsp 3.配置从Nginx1.将主虚拟机复制一份作为从虚拟机2.打开从虚拟机测试3.保证新虚拟机的ip和win在同一网段1.查看新虚拟机ip `ifconfig`2.查看win的ip `ipconfig`3.互相ping一下保证网络通畅 4.保证两台虚拟机之间可以ping通5.Xshell远程登录两个虚拟机6.配置域名解析到 192.168.200.1297.修改nginx.conf(修改主机名即可)1.http全局块2.server块 8.测试从nginx访问1.启动win的tomcat2.启动linux的tomcat3.启动从Nginx,进入安装目录 `./sbin/nginx -c nginx.conf`4.win测试访问 http://look2.sunxiansheng.cn:7777/search/cal.jsp 9.综合测试1.由于看不出来结果的区别1.修改win的tomcat中cal.jsp的内容2.修改主从Nginx中的tomcat中的cal.jsp文件然后重启tomcat 2.重启win的tomcat3.win访问主Nginx http://look.sunxiansheng.cn:7777/search/cal.jsp4.win访问从Nginx http://look2.sunxiansheng.cn:7777/search/cal.jsp 4.在两台linux主机下载keepalived(这里只演示一个)1.将文件上传到`/opt`目录下2.在`/opt`下创建目录 /keepalived `mkdir /opt/keepalived`3.进入`/opt`目录,然后将文件解压到刚才创建的目录 `cd /opt/` `tar -zxvf keepalived-2.0.20.tar.gz -C ./keepalived`4.进入解压到的文件夹下 `cd /opt/keepalived/keepalived-2.0.20`5. 配置文件放到/etc下,安装到/usr/local下这样启动的指令就在/usr/local/sbin目录下 `./configure --sysconf=/etc --prefix=/usr/local`6.安装 `make && make install`7.如果安装成功,检查配置目录和启动指令8.在另一台linux主机也安装一下!!! 5.完成高可用集群配置1.配置主Nginx编辑keepalived.conf的配置文件 `vim /etc/keepalived/keepalived.conf` 2.配置从Nginx编辑keepalived.conf的配置文件 `vim /etc/keepalived/keepalived.conf` 3.测试1.启动 两个keepalived `/usr/local/sbin/keepalived`2.输入 ip a,看到配置的虚拟ip即成功3.使用windows来ping这个虚拟ip,如果能ping通则说明可用4.如果没有ping通,就是配置文件的vrrp_strict没注销1.注销vrrp_strict2.关闭keepalived `killall keepalived` 或者 `kill -9 进程号`3.再次启动 `/usr/local/sbin/keepalived` 4.总体测试1.win访问 http://192.168.200.18:7777/search/cal.jsp 会一直访问win和主Nginx2.关闭主的keepalived来模拟主Nginx挂掉3.win再次访问 http://192.168.200.18:7777/search/cal.jsp 6.编写自动检测Nginx异常脚本kill keepalived1.进入keeplived配置文件目录 `cd /etc/keepalived/`2.编写shell脚本 `vim ch_nginx.sh`3.为该脚本增加执行权限 `chmod 755 ch_nginx.sh`4.编写配置文件 `vim /etc/keepalived/keepalived.conf`5.重启主Nginx所在的keepalived `killall keepalived && /usr/local/sbin/keepalived`6.测试1.关闭主nginx `./sbin/nginx -s stop`2.浏览器访问 http://192.168.200.18:7777/search/cal.jsp 7.注意事项和细节1.注意事项2.keepalived.conf 详解

1.基本介绍

1.在微服务架构中的位置

image-20240407140155292

2.配置前提

image-20240407140830550

3.主从模式架构图
注意:这里由于tomcat1在主Nginx上,一旦主Nginx崩溃,则这个tomcat就不可用,所以才需要在从Nginx也搭建一份,如果部署在独立主机上就不需要,就像这个tomcat2

2.启动主Nginx和两个Tomcat

1.启动linux的tomcat

image-20240407143616337

2.启动win的tomcat

image-20240407143708044

3.启动主Nginx,进入安装目录 ./sbin/nginx -c nginx.conf
4.windows访问 http://look.sunxiansheng.cn:7777/search/cal.jsp

image-20240407145553336

3.配置从Nginx

1.将主虚拟机复制一份作为从虚拟机

image-20240407153840257

2.打开从虚拟机测试

image-20240407153943910

3.保证新虚拟机的ip和win在同一网段
1.查看新虚拟机ip ifconfig

image-20240407154456661

2.查看win的ip ipconfig

image-20240407154631237

3.互相ping一下保证网络通畅

image-20240407154651353

image-20240407154704896

4.保证两台虚拟机之间可以ping通

image-20240407155255781

image-20240407155304675

5.Xshell远程登录两个虚拟机

image-20240407160100623

6.配置域名解析到 192.168.200.129

image-20240407161158332

7.修改nginx.conf(修改主机名即可)
1.http全局块

image-20240407161719532

2.server块

image-20240407161729776

8.测试从nginx访问
1.启动win的tomcat
2.启动linux的tomcat

image-20240407162359458

3.启动从Nginx,进入安装目录 ./sbin/nginx -c nginx.conf
4.win测试访问 http://look2.sunxiansheng.cn:7777/search/cal.jsp

image-20240407162720142

9.综合测试
1.由于看不出来结果的区别
1.修改win的tomcat中cal.jsp的内容

image-20240407163631586

2.修改主从Nginx中的tomcat中的cal.jsp文件然后重启tomcat

image-20240407164731044

image-20240407164417923

2.重启win的tomcat
3.win访问主Nginx http://look.sunxiansheng.cn:7777/search/cal.jsp

image-20240407165147179)

image-20240407163759075

4.win访问从Nginx http://look2.sunxiansheng.cn:7777/search/cal.jsp

image-20240407165217634

image-20240407163831881

4.在两台linux主机下载keepalived(这里只演示一个)

1.将文件上传到/opt目录下

image-20240407170123038

2.在/opt下创建目录 /keepalived mkdir /opt/keepalived
3.进入/opt目录,然后将文件解压到刚才创建的目录 cd /opt/ tar -zxvf keepalived-2.0.20.tar.gz -C ./keepalived

image-20240407170915846

4.进入解压到的文件夹下 cd /opt/keepalived/keepalived-2.0.20

image-20240407171105717

5. 配置文件放到/etc下,安装到/usr/local下这样启动的指令就在/usr/local/sbin目录下 ./configure --sysconf=/etc --prefix=/usr/local

image-20240407171518409

image-20240407171614106

6.安装 make && make install

image-20240407171738617

7.如果安装成功,检查配置目录和启动指令

image-20240407171817048

image-20240407171926121

8.在另一台linux主机也安装一下!!!

5.完成高可用集群配置

1.配置主Nginx
编辑keepalived.conf的配置文件 vim /etc/keepalived/keepalived.conf

image-20240407173602570

image-20240407173735851

2.配置从Nginx
编辑keepalived.conf的配置文件 vim /etc/keepalived/keepalived.conf

image-20240407174036816

image-20240407181312292

3.测试
1.启动 两个keepalived /usr/local/sbin/keepalived

image-20240407174550886

image-20240407174543313

2.输入 ip a,看到配置的虚拟ip即成功

image-20240407174829097

3.使用windows来ping这个虚拟ip,如果能ping通则说明可用

image-20240407175007813

4.如果没有ping通,就是配置文件的vrrp_strict没注销
1.注销vrrp_strict

image-20240407175140213

2.关闭keepalived killall keepalived 或者 kill -9 进程号

image-20240407175610064

3.再次启动 /usr/local/sbin/keepalived
4.总体测试
1.win访问 http://192.168.200.18:7777/search/cal.jsp 会一直访问win和主Nginx

image-20240407180247589

image-20240407180253631

2.关闭主的keepalived来模拟主Nginx挂掉

image-20240407180642129

3.win再次访问 http://192.168.200.18:7777/search/cal.jsp

image-20240407181847419

image-20240407181840010

6.编写自动检测Nginx异常脚本kill keepalived

1.进入keeplived配置文件目录 cd /etc/keepalived/

image-20240407193313910

2.编写shell脚本 vim ch_nginx.sh
#!/bin/bashnum=`ps -C nginx --no-header | wc -l`if [ $num -eq 0 ];thenkillall keepalivedfi
3.为该脚本增加执行权限 chmod 755 ch_nginx.sh

image-20240407193259347

4.编写配置文件 vim /etc/keepalived/keepalived.conf

image-20240407194118625

5.重启主Nginx所在的keepalived killall keepalived && /usr/local/sbin/keepalived

image-20240407194304900

6.测试
1.关闭主nginx ./sbin/nginx -s stop

image-20240407194659907

2.浏览器访问 http://192.168.200.18:7777/search/cal.jsp
这里是因为一旦停止主nginx,我们的脚本就会检测出异常,然后杀死keepalived的进程,然后将权重-20这时再访问,就只会访问到从Nginx了

image-20240407194730364

7.注意事项和细节
1.注意事项

image-20240407195240269

2.keepalived.conf 详解
#这里只注释要修改的地方global_defs {notification_email {test@foxmail.com #接收通知的邮件地址}notification_email_from Alexandre.Cassen@firewall.loc #发送邮件的邮箱smtp_server 192.168.200.1 #smtp server 地址smtp_connect_timeout 30router_id Node132 #Node132 为主机标识vrrp_skip_check_adv_addr#vrrp_strict #这里需要注释,避免虚拟 ip 无法 ping 通vrrp_garp_interval 0vrrp_gna_interval 0}vrrp_instance VI_1 {state MASTER #主节点 MASTER 备用节点为 BACKUPinterface ens33 #网卡名称virtual_router_id 51 #VRRP 组名,两个节点的设置必须一样,指明属于同一 VRRP 组priority 100 #主节点的优先级(1-254 之间),备用节点必须比主节点优先级低advert_int 1 #组播信息发送间隔,两个节点设置必须一样authentication { #设置验证信息,两个节点必须一致auth_type PASSauth_pass 1111}virtual_ipaddress { #指定虚拟 IP, 两个节点设置必须一样192.168.200.16}}

点击全文阅读


本文链接:http://zhangshiyu.com/post/168380.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1