当前位置: 首页 > news >正文

nginx+keepalived实现负载均衡和高可用

环境准备

IPVIP环境
客户端192.168.134.174
Master192.168.134.170192.168.134.100需要配置nginx负载均衡
Backup192.168.134.172192.168.134.100需要配置nginx负载均衡
web1服务器192.168.134.171

web2服务器

192.168.134.173

1、首先安装nginx服务器(此处采用yum安装)

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum install nginx -y

2、修改nginx的配置文件(配置负载均衡)

cd /etc/nginx/conf.d/
vim web.confupstream webPools {server 192.168.134.171;server 192.168.134.173;
}
server {location / {proxy_pass http://webPools;}
}

3、启动nginx

systemctl start nginx

4、修改keepalived配置文件

主节点:
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVEL1
}vrrp_instance VI_1 {state MASTER# nopreemptinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.134.100}
}
备节点
修改:state MASTERpriority 80

此时可以实现keepalived的故障切换和nginx负载均衡,但是如果nginx的主服务器出现故障,那么此时无法实现客户端的正常访问,即需要新的配置来实现高可用,因此,利用·vrrp-script(利用VIP漂移实现服务的可用)去监控集群资源。

5、重新修改keepalived配置文件,添加vrrp-script,在实例中还要调用。

主备节点做一样的修改
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVEL1
}
vrrp_script check_nginx {  # 自定义资源监控脚本script "killall -0 nginx"interval 2
}
vrrp_instance VI_1 {state MASTER# nopreemptinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {check__nginx}virtual_ipaddress {192.168.134.100}
}

6、还可以用脚本检测服务的方法

cat check_#!/bin/bashtotal=$(ps -C nginx --no-header | wc -l)if [ $total -eq 0 ]
thennginx_status=1
elsenginx_status=0
fiexit $nginx_status在keepalived文件中也进行修改vrrp_script check_nginx {#script "killall -0 nginx"script "/etc/keepalived/check_nginx.sh"interval 2
}

7、还有一种使用notify的·方法

写notify.sh脚本#!/bin/bashcase "$1" inmaster)nmap localhost -p 80 | grep "80/tcp open"if [ $? -ne 0 ];thensystemctl start nginxfi;;
backup)nginx_psr=`ps -C nginx --no-header | wc -l`if [ $nginx_psr -ne 0 ];thensystemctl stop nginxfi;;
*)echo "Usage:$0 master|backup";;
esacchmod +x notify.sh  # 给脚本增加执行权限然后在keepalived文件中修改就行,修改下面几个地方
vrrp_script check_nginx {#script "killall -0 nginx"script "/etc/keepalived/check_nginx.sh"interval 2weight -30
}track_script {check_nginx}virtual_ipaddress {192.168.134.100}notify_master "/etc/keepalived/notify.sh master"notify_backup "/etc/keepalived/notify.sh backup"
}

8、测试如下:

在停掉nginx后,仍旧可以正常访问systemctl stop nginx
结果如下:
web test page ,ip is 192.168.134.173 192.168.122.1 
web test page ,ip is 192.168.134.171 192.168.122.1 
web test page ,ip is 192.168.134.173 192.168.122.1 
web test page ,ip is 192.168.134.173 192.168.122.1 
web test page ,ip is 192.168.134.171 192.168.122.1 
web test page ,ip is 192.168.134.171 192.168.122.1
http://www.lryc.cn/news/121769.html

相关文章:

  • 微信小程序实现图片多点裁剪
  • 计算图片的均值和方差用图片的归一化取值
  • 预测算法|改进粒子群算法优化极限学习机IDM-PSO-ELM
  • 小黑子—JavaWeb:第六章 - Filter、Listener、AJAX与JSON
  • STM32 LL库开发
  • 标记垃圾,有三种色彩:四千长文带你深入了解三色标记算法
  • 277/300 React+react-router-dom+Vite 二级页面刷新时,白屏问题解决
  • 如何做线上监控
  • 饥荒开服教程——游戏
  • 查询 npm/yarn 安装依赖的全局路径及路径修改
  • 掌握Python的X篇_35_用Python为美女打码_图像库Pillow
  • SpringBoot 异步、邮件任务
  • 【LeetCode】45. 跳跃游戏 II - 贪婪算法
  • [C初阶笔记]P1
  • 外企面试题
  • 【目标检测系列】YOLOV1解读
  • Sentieon | 每周文献-Multi-omics(多组学)-第九期
  • CSDN竞赛70期
  • mac安装vscode 配置git
  • UI自动化环境的搭建(python+pycharm+selenium+chrome)
  • AbstractQueuedSynchronizer
  • 谈谈什么是云计算?以及它的应用
  • 【BASH】回顾与知识点梳理(十六)
  • docsify gitee 搭建个人博客
  • SpringBoot2-Tomcat部署
  • Docker查看、创建、进入容器相关的命令
  • leetcode1. 两数之和
  • 温室花卉种植系统springboot框架jsp鲜花养殖智能管理java源代码
  • 测试老鸟经验总结,Jmeter性能测试-重要指标与性能结果分析(超细)
  • IDEA设置Maven自动编译model