搜索
首页后端开发php教程Keepalived+nginx实现双主高可用负载均衡

Nginx+keepalived高可用有两种配置方案:
1、Nginx+keepalived 主从配置
这种方案,使用一个vip地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备份机器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠,所以本次不予采用。
2、Nginx+keepalived 双主配置

这种方案,使用两个vip地址,前端使用2台机器,互为主备,同时有两台机器工作,当其中一台机器出现故障,两台机器的请求转移到一台机器负担,非常适合于当前架构环境,故本次采用此方案对网站进行高可用架构。

下面我们以第二种配置演示实现双主高可用负载均衡。


第一组

VIP1:192.168.36.100

主nginx:192.168.36.99

备nginx:192.168.36.86

第二组

VIP2:192.168.36.100

主nginx:192.168.36.86

备nginx:192.168.36.99

web服务:

web1:192.168.36.215

web2:192.168.36.80

一、nginx配置(安装省略)

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    upstream web1 {
        server 192.168.36.215:80 max_fails=3 fail_timeout=3s;
    }
    upstream web2 {
        server 192.168.36.80:80 max_fails=3 fail_timeout=3s;
    }
    server {
            listen         88;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
                proxy_pass http://web1;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
   }
   server {
            listen         99;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
                proxy_pass http://web2;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
   }
}
配置并启动192.168.36.99和192.168.36.86的nginx。检查访问是否正常.

web1

http://192.168.36.86:88/

http://192.168.36.99:88/

web2

http://192.168.36.86:99/

http://192.168.36.99:99/

二、安装、配置keepalived(安装省略)

1)192.168.36.86配置

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     monitor@3evip.cn
     #failover@firewall.loc
   }
   notification_email_from tianwei7518@163.com
   smtp_server smtp.163.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
        script "/etc/keepalived/chk_nginx.sh"
        interval 2
        weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.36.100
    }
}


vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 200
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }   
    virtual_ipaddress {
        192.168.36.200
    }
}
2)192.168.36.99配置

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     monitor@3evip.cn
     #failover@firewall.loc
   }
   notification_email_from tianwei7518@163.com
   smtp_server smtp.163.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
        script "/etc/keepalived/chk_nginx.sh"
        interval 2
        weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.36.100
    }
}


vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 200
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }   
    virtual_ipaddress {
        192.168.36.200
    }
}
3)两台机器都需要有nginx进程检查脚本,当检测不到进程的时候重启nginx,无法重启就关闭keepalived,以便让vip切换到备机。

vi /etc/keepalived/chk_nginx.sh

#!/bin/sh
#description: # 如果启动失败,则停止keepalived
status=$( ps -C nginx --no-heading| wc -l)
if [ "${status}" = "0" ]; then
        /usr/local/nginx/sbin/nginx
        status2=$( ps -C nginx --no-heading| wc -l)
        if [ "${status2}" = "0" ]; then
                service keepalived stop
        fi
fi
设置执行权限:chmod +x /etc/keepalived/chk_nginx.sh

三、启动、测试

分别启动nginx和keepalived:

 /usr/local/nginx/sbin/nginx

service keepalived start

访问:

web1

http://192.168.36.100:88/

http://192.168.36.200:88/

web2

http://192.168.36.100:99/

http://192.168.36.200:99/

可以正常访问。

我们可以试一下,关闭一个keepalived。关闭192.168.36.86上的keepalived

service keepalived stop

查看日志:

tail -f /var/log/messages 

192.168.36.86日志:

Feb  7 00:39:05 slave-d Keepalived[5738]: Stopping Keepalived v1.2.15 (02/07,2015)
Feb  7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) sending 0 priority
Feb  7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) removing protocol VIPs.
Feb  7 00:39:05 slave-d Keepalived_healthcheckers[5740]: Netlink reflector reports IP 192.168.36.200 removed
Feb  7 00:39:05 slave-d avahi-daemon[1823]: Withdrawing address record for 192.168.36.200 on eth0.
192.168.36.99日志:
Feb  7 00:39:11 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Transition to MASTER STATE
Feb  7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Entering MASTER STATE
Feb  7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) setting protocol VIPs.
Feb  7 00:39:12 slave-c Keepalived_healthcheckers[25102]: Netlink reflector reports IP 192.168.36.200 added
Feb  7 00:39:12 slave-c avahi-daemon[1832]: Registering new address record for 192.168.36.200 on eth0.IPv4.
Feb  7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
Feb  7 00:39:17 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
再开启192.168.36.86上的keepalived:

192.168.36.86日志:

Feb  7 00:40:42 slave-d Keepalived[6004]: Starting Keepalived v1.2.15 (02/07,2015)
Feb  7 00:40:42 slave-d Keepalived[6005]: Starting Healthcheck child process, pid=6007
Feb  7 00:40:42 slave-d Keepalived[6005]: Starting VRRP child process, pid=6008
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: Netlink reflector reports IP 192.168.36.86 added
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: Netlink reflector reports IP fe80::20c:29ff:fe6a:66ff added
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering Kernel netlink reflector
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering Kernel netlink command channel
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering gratuitous ARP shared channel
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: Opening file '/etc/keepalived/keepalived.conf'.
Feb  7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP 192.168.36.86 added
Feb  7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP fe80::20c:29ff:fe6a:66ff added
Feb  7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Registering Kernel netlink reflector
Feb  7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Registering Kernel netlink command channel
Feb  7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Opening file '/etc/keepalived/keepalived.conf'.
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: Configuration is using : 44182 Bytes
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: Using LinkWatch kernel netlink reflector...
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_1) Entering BACKUP STATE
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Feb  7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Configuration is using : 7257 Bytes
Feb  7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Using LinkWatch kernel netlink reflector...
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Transition to MASTER STATE
Feb  7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Received lower prio advert, forcing new election
Feb  7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Entering MASTER STATE
Feb  7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) setting protocol VIPs.
Feb  7 00:40:43 slave-d avahi-daemon[1823]: Registering new address record for 192.168.36.200 on eth0.IPv4.
Feb  7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
Feb  7 00:40:43 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP 192.168.36.200 added
Feb  7 00:40:48 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200
192.168.36.99日志:

Feb  7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Received higher prio advert
Feb  7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Entering BACKUP STATE
Feb  7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) removing protocol VIPs.
Feb  7 00:40:47 slave-c Keepalived_healthcheckers[25102]: Netlink reflector reports IP 192.168.36.200 removed
Feb  7 00:40:47 slave-c avahi-daemon[1832]: Withdrawing address record for 192.168.36.200 on eth0.
关闭后依旧可以正常访问。

关闭nginx后,可以看到nginx立即有启动。

参考文章:

CentOS6.5 keepalived详解及实现Nginx服务的高可用性

用NginX+keepalived实现高可用的负载均衡

Keepalived+Nginx实现高可用和双主节点负载均衡

nginx+keepalived实现nginx双主高可用的负载均衡

以上就介绍了Keepalived+nginx实现双主高可用负载均衡,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境