Nginx+Keepalived怎么实现双机热备
一.keepalived
keepalived是保证集群高可用的服务软件,网络中优先级高的节点为master负责响应vip的arp包,将vip和mac地址映射关系告诉网络内其他主机,还会以多播的形式向网络中发送vrrp通告,告知自己的优先级。backup节点只负责处理master发出的多播包,当发现master的优先级没自己高,或者没收到master的vrrp通告时,backup将自己切换到master状态。
二.环境搭建
环境:qemu-kvm、客户机debian(9.4.0)
虚拟机qemu-kvm安装:虚拟化技术qemu-kvm入门
1.使用临时快照创建两台虚拟机,以下操作两台虚拟机都需要执行
faramita2016@linux-l9e6:~> qemu-kvm -cpu host -m 512 -hda vdisk.img -net nic -net bridge,br=br0 -snapshot -nographic
2.设置虚拟机静态ip
root@debian:~# cat /etc/network/interfaces # the primary network interface allow-hotplug ens3 #iface ens3 inet dhcp iface ens3 inet static address 10.0.0.3 // 设置ip netmask 255.0.0.0 gateway 10.0.0.1 hwaddress ether 52:54:00:12:34:53 // 设置mac地址
3.激活网卡ip
root@debian:~# ip addr del 10.0.0.8/8 dev ens3 // 删除客户机原有ip root@debian:~# ifdown ens3 root@debian:~# ifup ens3
4.安装nginx、keepalived
root@debian:~# apt-get install -y nginx keepalived
5.修改/var/www/html/index.nginx-debian.html文件,添加当前虚拟机ip,用于标记实际响应的服务器
root@debian:~# vi /var/www/html/index.nginx-debian.html …… <h1>welcome to nginx! 10.0.0.3</h1> ……
三.软件配置
主节点虚拟机(10.0.0.3),备用节点虚拟机(10.0.0.4),虚拟ip(10.0.0.100)
1.在主节点虚拟机,编辑/etc/keepalived/keepalived.conf文件
root@debian:~# cat /etc/keepalived/keepalived.conf global_defs { router_id ri_1 // 标识当前keepalived节点 } vrrp_script chk_nginx { script "/root/chk_nginx.sh" // 定义监控脚本 interval 2 // 执行监控脚本的闹中间隔时间 } vrrp_instance vi_1 { state master // 设置节点为主节点,节点的初始状态 interface ens3 // 设置绑定虚拟ip的网络接口 virtual_router_id 50 // vrrp组名,指明节点同属一个组 priority 100 // 节点优先级,主节点应当高于备用节点 advert_int 1 // 组播信息发送间隔 #nopreempt // 禁止抢占服务,只对备用节点生效,主节点根据priority优先级进行抢占,不受nopreempty控制 authentication { auth_type pass // 设置认证方式 auth_pass 123456 // 密码 } virtual_ipaddress { 10.0.0.100/24 // 设置vip } track_script { // 调用监控脚本 chk_nginx } }
2.在备用节点虚拟机,编辑/etc/keepalived/keepalived.conf文件,除以下项,其它与主节点相同
state backup // 设置节点为主节点,节点的初始状态 priority 10 // 节点优先级
3.在所有节点执行,创建监控脚本,编辑/root/chk_nginx.sh文件
root@debian:~# vi /root/chk_nginx.sh #!/bin/bash a=`ps -c nginx --no-header | wc -l` if [ $a -eq 0 ] then /etc/init.d/keepalived stop fi
四.验证服务
1.主节点虚拟机,启动nginx和keepalived服务
root@debian:~# /etc/init.d/nginx start root@debian:~# /etc/init.d/keepalived start
执行ip a命令,主节点网卡ens3已绑定vip(10.0.0.100)
2.在备用节点虚拟机,启动nginx和keepalived服务
root@debian:~# /etc/init.d/nginx start root@debian:~# /etc/init.d/keepalived start
执行ip a命令,备用节点网卡ens3未绑定vip(10.0.0.100)
3.宿主机浏览器访问http://10.0.0.100
出现10.0.0.3的网页,说明keepalived服务已生效,vip已绑定主节点
4.在主节点虚拟机,关闭nginx服务,模拟宕机
root@debian:~# /etc/init.d/nginx stop
执行ip a命令,主节点网卡ens3未绑定vip(10.0.0.100)
5.在备用节点虚拟机,查询ip
执行ip a命令,备用节点网卡ens3已绑定vip(10.0.0.100)
6.宿主机浏览器访问http://10.0.0.100
出现10.0.0.4的网页,说明keepalived服务已生效,vip已绑定备用节点
以上是Nginx+Keepalived怎么实现双机热备的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

解决 Nginx 跨域问题有两种方法:修改跨域响应头:添加指令以允许跨域请求,指定允许的方法和头,以及设置缓存时间。使用 CORS 模块:启用模块并配置 CORS 规则,允许跨域请求、方法、头和设置缓存时间。

如何解决 Nginx 403 Forbidden 错误?检查文件或目录权限;2. 检查 .htaccess 文件;3. 检查 Nginx 配置文件;4. 重启 Nginx。其他可能原因还包括防火墙规则、SELinux 设置或应用程序问题。

启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP
