前言

此前已经写过一篇使用keepalived实现nginx的高可用,这种方式有一台机器一直作backup使用,有50%的资源被浪费。

  • 使用keepalived来实现nginx的高可用

下面来配置nginx+keepalived的双主机双机热备,这种配置下有两个Virtual IP,两个机器互为主备,最后我们把域名DNS服务器解析至两个Virtual IP即可。


环境介绍

两台服务器

Server1:192.168.30.61
Server2:192.168.30.62

要设置的两个虚拟IP

Virtual IP1:192.168.30.51
Virtual IP2:192.168.30.52

将DNS域名解析至两个虚拟IP: 192.168.30.51、192.168.30.52


网络扑拓图


配置第一台服务器


第一台服务器ip为192.168.30.61

vi /etc/keepalived/keepalived.conf

配置内容如下:

global_defs
{notification_email{123456@qq.com}notification_email_from 123456@qq.comsmtp_server 127.0.0.1stmp_connect_timeout 30router_id LVS_DEVEL
}vrrp_script Monitor_Nginx {script "/usr/local/keepalived/scripts/monitor_nginx.sh"interval 2weight 2
}# 虚拟IP1, 本机作为Master
vrrp_instance VI_1 {state MASTERinterface enp0s3virtual_router_id 51priority 100advert_int 1track_interface {enp0s3}authentication {auth_type PASSauth_pass 123456}virtual_ipaddress {192.168.30.51}itrack_script {Monitor_Nginx}}# 虚拟IP2, 本机作为Backup
vrrp_instance VI_2 {state BACKUPinterface enp0s3virtual_router_id 52priority 100advert_int 1track_interface {enp0s3}authentication {auth_type PASSauth_pass 123456}virtual_ipaddress {192.168.30.52}track_script {Monitor_Nginx}}

配置第二台服务器


第二台服务器ip为192.168.30.62

vi /etc/keepalived/keepalived.conf

配置内容如下:

global_defs
{notification_email{123456@qq.com}notification_email_from 123456@qq.comsmtp_server 127.0.0.1stmp_connect_timeout 30router_id LVS_DEVEL
}vrrp_script Monitor_Nginx {script "/usr/local/keepalived/scripts/monitor_nginx.sh"interval 2weight 2
}# 虚拟IP1, 本机作为BACKUP
vrrp_instance VI_1 {state BACKUPinterface enp0s3virtual_router_id 51priority 100advert_int 1track_interface {enp0s3}authentication {auth_type PASSauth_pass 123456}virtual_ipaddress {192.168.30.51}track_script {Monitor_Nginx}
}# 虚拟IP2, 本机作为Master
vrrp_instance VI_2 {state MASTERinterface enp0s3virtual_router_id 52priority 100advert_int 1track_interface {enp0s3}authentication {auth_type PASSauth_pass 123456}virtual_ipaddress {192.168.30.52}track_script {Monitor_Nginx}}

监控脚本


和之前的监控脚本一样

# 监控nginx进程,若nginx主进程不存在则启动nginx
# 若5s后nginx进程还是不存在的话kill掉keepalived进程,防止nginx没运行该主机的keepalived还接管虚拟IP
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]then/usr/local/nginx/sbin/nginxsleep 5if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]thenkillall keepalivedfi
fi

测试


修改nginx默认访问页nginx的默认页面

# nginx 默认访问页面
$NGINX_HOME/html/index.html

修改server1的页,加入IP: 192.168.30.61

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx! 192.168.30.61 </h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>

server2同理


两个机器都启动keepalived和nginx

访问虚拟ip1

在浏览器输入192.168.30.51进入访问页面

访问虚拟ip2

在浏览器输入192.168.30.52进入访问页面

证明两个VIP都起效果了


Kill掉server2的keepalived

pkill keepalived
  • 访问ip2

证明了server1为server2的备机,server2 down掉后server1接管server1的nginx访问


Kill掉server1机器的keepalived,启动server2机器的keepalived

  • 访问ip1

证明了server2为server1的备机,server1 down掉后server1接管server1的nginx访问


有关nginx和keepalived的安装可参考

  • Linux下安装nginx
  • 使用keepalived来实现nginx的高可用

配置nginx + keepalived双主模式(双机互为主备)相关推荐

  1. Nginx+keepalived双主配置(双机双主热备)

    简介 这种方案,使用两个VIP地址,前端使用2台机器,互为主备,同时有两台机器工作,当其中一台机器出现故障,两台机器的请求转移到一台机器负担,非常适合于生产架构环境. 一.网络拓扑 四台虚拟机如下所示 ...

  2. OpenResty+Keepalived组建高可用集群+双主模式

    * 环境: system version:CentOS Linux release 7.5.1804 (Core) 3.10.0-862.9.1.el7.x86_64 nginx version:op ...

  3. Nginx+keepalived 高可用双机热备(主从模式/双主模式)

    基础介绍 负载均衡技术对于一个网站尤其是大型网站的web服务器集群来说是至关重要的!做好负载均衡架构,可以实现故障转移和高可用环境,避免单点故障,保证网站健康持续运行. 关于负载均衡介绍,可以参考:l ...

  4. keepalived+nginx/mysql双主

    目录 keepalived+nginx 编译安装nginx 编译安装keepalived 配置keepalived+nginx主从模式 keepalived配置文件介绍 主从配置 keepalived ...

  5. 双机热备(双主模式)——实现数据库的高可用

    1.搭建双主机 <总说明:实现此功能需要用到mycat> 问题:如果根据如下的配置实现数据库的代理,如果数据库主库宕机,则直接影响整个程序的执行.所以需要实现高可用机制. 高可用实现的问题 ...

  6. Mariadb互为主从(双主模式)配置

    前言 这段时间 一直在Cloudera Manager集群上配置高可用的Mariadb服务.今天花一点记录一下自己的配置过程 希望后续的读者看到后 在遇到相同业务需求时具有参考价值. 以往我们在配置M ...

  7. nginx+keepalived构建主备负载均衡代理服务器

    一.架构 二.唠叨一会原理: 1.nginx Nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永 ...

  8. MySQL数据库Keepalived双主

    目录 1.环境设置 2.配置my.cnf配置文件 3.授权允许同步 4.开启slave同步 5.验证互为主从 6.安装keepalived 7.配置keepalived 8.验证 1.环境设置 10. ...

  9. mysql复制模式第二部分-----双主模式

    双主配置 我在配置主从服务器时,使用了两台服务器:10.19.34.126和10.19.34.91. 1.首先需要在这两台上搭建单独的mysql服务masterA和masterB. 2.配置数据库ma ...

最新文章

  1. ​DeepMind Nature发文:AI首次实现数学领域的重大进展
  2. 万字干货:教新手从0到1搭建完整的增长数据体系(30图)
  3. log4jdbc mysql_[简单]log4jdbc-log4j2配置简记_MySQL
  4. Angular Chart.js第三方库ng-chartjs基础使用
  5. 百面机器学习——python实现二分类逻辑回归
  6. 7. Document write() 方法
  7. 谜题40:不情愿的构造器
  8. 特洛伊木马与计算机病毒有什么区别,特洛伊木马Vs病毒Vs蠕虫, 有什么区别?...
  9. java jbutton 禁用_java – 如何禁用JButton在禁用时变灰?
  10. POJO和po,vo,bo
  11. Android 13中的 Open Mobile API
  12. Ctrl、Alt、Shift常用的电脑快捷键大全(收藏级)
  13. 国庆必看9大区块链电影!建议收藏!
  14. Springboot:整合DubboProvider的配置以及Comsumer的配置
  15. 火狐浏览器不兼容event问题
  16. 电压放大器的作用原理是什么
  17. 李宏毅2022机器学习HW5解析
  18. 极客时间和极客学院_极客拔掉
  19. 计算机专业大学生应该怎么规划未来?
  20. python项目实战——银行取款机系统(一)

热门文章

  1. Android启动流程:上电到启动第一个APP的详细流程,
  2. 国务院办公厅:多处强调“电子签名、电子印章、电子合同”的应用
  3. 纵观Android Adapter适配器。比较三种常用Adapter(SimpleAdapter,ArrayAdapter,BaseAdapter)。
  4. 数据库中对存储过程的理解
  5. (大厂必备)厂长熬夜爆肝万字之多线程高并发JUC编程(一)⭐学妹已收藏
  6. 选点问题(贪心算法)
  7. 数码管:3位6脚的数码管分析和编码
  8. rpm提示:XXX conflicts with file form package XXX
  9. 【电脑使用技巧】1TB的硬盘只有931G 硬盘容量去哪儿了?
  10. 使用c++实现复数类的运算