文章目录
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.在微服务架构中的位置
2.配置前提
3.主从模式架构图
注意:这里由于tomcat1在主Nginx上,一旦主Nginx崩溃,则这个tomcat就不可用,所以才需要在从Nginx也搭建一份,如果部署在独立主机上就不需要,就像这个tomcat22.启动主Nginx和两个Tomcat
1.启动linux的tomcat
2.启动win的tomcat
3.启动主Nginx,进入安装目录 ./sbin/nginx -c nginx.conf
4.windows访问 http://look.sunxiansheng.cn:7777/search/cal.jsp
3.配置从Nginx
1.将主虚拟机复制一份作为从虚拟机
2.打开从虚拟机测试
3.保证新虚拟机的ip和win在同一网段
1.查看新虚拟机ip ifconfig
2.查看win的ip ipconfig
3.互相ping一下保证网络通畅
4.保证两台虚拟机之间可以ping通
5.Xshell远程登录两个虚拟机
6.配置域名解析到 192.168.200.129
7.修改nginx.conf(修改主机名即可)
1.http全局块
2.server块
8.测试从nginx访问
1.启动win的tomcat
2.启动linux的tomcat
3.启动从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的tomcat
3.win访问主Nginx http://look.sunxiansheng.cn:7777/search/cal.jsp
)
4.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_strict
2.关闭keepalived killall keepalived
或者 kill -9 进程号
3.再次启动 /usr/local/sbin/keepalived
4.总体测试
1.win访问 http://192.168.200.18:7777/search/cal.jsp 会一直访问win和主Nginx
2.关闭主的keepalived来模拟主Nginx挂掉
3.win再次访问 http://192.168.200.18:7777/search/cal.jsp
6.编写自动检测Nginx异常脚本kill keepalived
1.进入keeplived配置文件目录 cd /etc/keepalived/
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
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
这里是因为一旦停止主nginx,我们的脚本就会检测出异常,然后杀死keepalived的进程,然后将权重-20这时再访问,就只会访问到从Nginx了7.注意事项和细节
1.注意事项
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}}