Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize highly available Master-Slave configuration with nginx + keepAlive

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use nginx + keepAlive to achieve highly available master-slave configuration". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use nginx + keepAlive to achieve highly available master-slave configuration".

A brief introduction to keepAlived

At first, Keepalived software is designed for LVS load balancing software, which is used to manage and monitor the status of each service node in LVS cluster system. Later, it adds the function of VRRP (Virtual Router Redundancy Protocol, Virtual Router redundancy Protocol), which can achieve high availability. Therefore, in addition to managing LVS software, Keepalived can also be used as a highly available solution software for other services (such as Nginx, Haproxy, MySQL, etc.)

Second, how to achieve failover

Failover between Keepalived highly available service pairs is achieved through VRRP. When the Keepalived service is working normally, the primary Master node will constantly send heartbeat messages to the standby node to tell the standby Backup node that it is still alive, and when the primary Master node fails, it will not be able to send heartbeat messages, so the standby node can no longer detect the heartbeat of the incoming autonomous Master node, so it calls its own takeover program to take over the IP resources and services of the primary Master node. When the primary Master node is restored, the standby Backup node will release the IP resources and services that it takes over when the primary node fails, and return to the original standby role.

III. KeepAlive configuration

There is no description of how to install it here, but first configure the master node

! Configuration File for keepalivedglobal_defs {router_id ip address # Router ID, preferably with hostname or IP address, used to identify} vrrp_script chk_nginx {# configuration check method Chk_nginx is the custom name script "/ etc/keepalived/nginx_check.sh" # the file address of the script interval 2 weight 20 # this value needs to be greater than MASTER's priority minus BACKUP's priority} vrrp_instance VI_1 {state MASTER # master node is: MASTER The slave node is: BACKUP interface eth0 # current virtual machine network card virtual_router_id 51 # virtual route number. The higher the priority value of the master-slave priority 100 #, the higher the priority of obtaining and processing the request. Advert_int 1 authentication {# this is the authentication method MASTER and BACKUP unified auth_type PASS auth_pass 1314} track_script {# call the above check script chk_nginx} virtual_ipaddress {# set the VIP address 18.50.129.74and24}}

Continue to configure the slave node. The slave node backup configuration is the same as the master node, except that the state should be changed to backup, and the priority value should be lower than master.

! Configuration File for keepalivedglobal_defs {router_id ip address} vrrp_script chk_nginx {script "/ etc/keepalived/nginx_check.sh" interval 2 weight 20} vrrp_instance VI_1 {state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication {auth_type PASS auth_pass 1314} track_script {chk_nginx} virtual_ipaddress { 18.50.129.74/24}}

Finally, write a script nginx_check.sh

#! / bin/bashA= `ps-C nginx-- no-header | wc-l`if [$A-eq 0]; then / usr/local/nginx/sbin/nginx sleep 2 if [`ps-C nginx-- no-header | wc-l`-eq 0]; then pkill keepalived fifi

Script description:

Script logic is to detect the process if there is no nginx to try to start once, and then wait for two seconds, and then detect, if not on the process to end the keepalived process. So the VIP will be moved to the backup node

Here is the Nginx installed by yum, so you can start nginx by using the nginx command directly, if you are installing with source code, or if you need to configure the environment variables or change the startup command and absolute path, and test whether it can be started normally.

If you think the waiting time of two seconds is too long, you can change it to 1 second.

Test:

Check whether the Nginx service of the two nodes is started successfully. If the keepalived starts successfully, the startup Nginx will be called. It is necessary to ensure that the Nginx can start normally.

Deliberately change the wrong Nginx configuration file in Master, and stop the Nginx service after a few seconds to see if VIP has been moved to BACKUP, normally it will move to the BACKUP node

Restore the Master node to check whether VIP returns to Master. Normal VIP will bind to Master.

Thank you for reading, the above is "how to use nginx + keepAlive to achieve high availability master-slave configuration" content, after the study of this article, I believe you on how to use nginx + keepAlive to achieve high availability master-slave configuration of this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report