search
HomeBackend DevelopmentPHP TutorialKeepalived+nginx realizes dual-master high-availability load balancing

Nginx+keepalived high availability has two configuration schemes:
1. Nginx+keepalived master-slave configuration
This scheme uses a VIP address and uses 2 machines in the front end, one is the master and one is the backup, but there is only one machine at the same time. One machine works, and the other backup machine will always be wasted when the main machine does not malfunction. For websites with few servers, this solution is not economical, so it will not be adopted this time.
2. Nginx+keepalived dual-master configuration

This solution uses two VIP addresses, and the front-end uses 2 machines, each other is the master and backup. There are two machines working at the same time. When one of the machines fails, both The request from each machine is transferred to one machine, which is very suitable for the current architecture environment. Therefore, this solution is used to implement a high-availability architecture for the website.

Below we use the second configuration to demonstrate dual-master high-availability load balancing.


Group 1

VIP1: 192.168.36.100

Master nginx: 192.168.36.99

Prepare nginx: 192.168.36.86

Second Group

VIP2: 192.168.36.100

Main nginx: 192.168.36.86

Backup nginx: 192.168.36.99

web service:

web1: 192.168.36.215

web2: 192.168.36.80

1. nginx configuration (installation omitted)

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;
            }
   }
}
configure and start 192.168.36.99 and 192.168. 36.86 nginx. Check whether the access is normal.

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/

2. Install and configure keepalived (installation omitted )

1)192.168.36.86 configuration

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 configuration

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) Both machines need to have nginx process checking script. When the process cannot be detected, restart nginx. If it cannot be restarted, close keepalived so that vip can switch to Prepare machine.

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
Set execution permissions: chmod +x /etc/keepalived/chk_nginx.sh

3. Start , test

Start nginx and keepalived respectively:

/usr/local/nginx/sbin/nginx

service keepalived start

Visit:

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 /

can be accessed normally.

We can try to close a keepalived. Close keepalived on 192.168.36.86

service keepalived stop

View log:

tail - f /var/log/messages

192.168.36.86 log:

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 log:
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
Restart keepalived on 192.168.36.86:

192.168.36. 86 Log:

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 Log:

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.
You can still access it normally after closing it.

After closing nginx, you can see that nginx starts immediately.

Reference article: tCentos6.5 Keepalived detailed explanation and high availability of Nginx services


Use Nginx+Keepalived to achieve high available load balancing


KEEPALIVED+nginx to achieve high availability and high availability and Dual-master node load balancing


nginx+keepalived realizes nginx dual-master high-availability load balancing

The above introduces Keepalived+nginx to achieve dual-master high-availability load balancing, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)