双主模式nginx + keepalived配置(双机互为主备)

2024-01-07 09:18

本文主要是介绍双主模式nginx + keepalived配置(双机互为主备),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

配置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配置(双机互为主备)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/579447

相关文章

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信