Preface
In high availability services on the Internet, a single service node often cannot meet the business's requirements for stability and availability. Dual-machine hot standby is a common high availability solution that runs the same service simultaneously by two servers and uses Keepalived to fail over, ensuring that the standby server can immediately take over the service when the primary server fails, thus ensuring service continuity. This article will explain in detail how to deploy a dual-machine hot standby solution in an Nginx environment.
Environmental preparation
- Two Linux servers (this article takes CentOS 7 as an example)
- Nginx server software
- Keepalived Software
- A available IPv4 address segment (assuming this article is 192.168.1.0/24)
- Two different virtual IP addresses (VIP, assumed in this article is 192.168.1.100 and 192.168.1.101)
Steps Overview
- Install Nginx and Keepalived on both servers.
- Configure Nginx to provide the same service.
- Configure Keepalived for failover.
- Test the dual-machine hot standby function.
Install Nginx and Keepalived
# Install Nginxyum install nginx -y # Start Nginx and set up power-onsystemctl start nginx systemctl enable nginx # Install Keepalivedyum install keepalived -y
Configure Nginx
Configure the same Nginx service on both servers, for example, provide a simple HTTP service:
# user nginx; worker_processes 1; error_log /var/log/nginx/ warn; pid /var/run/; events { worker_connections 1024; } http { server { listen 80; server_name _; location / { root /usr/share/nginx/html; index ; try_files $uri $uri/ =404; } } }
Make sure that the Nginx configuration files of the two servers are exactly the same, including file paths and configuration items.
Configure Keepalived
Keepalived implements failover through VRRP (Virtual Router Redundancy Protocol). Here is a configuration example for Keepalived:
# ! Configuration File for keepalived # Define two virtual routers, corresponding to two VIPsvrrp_instance VI_1 { state MASTER # Set the master server to MASTER interface eth0 #Network card interface virtual_router_id 51 # Virtual router ID, both servers must be the same priority 100 # Priority, the priority of the primary server should be higher than the backup server advert_int 1 # Announcement interval, unit seconds virtual_ipaddress { 192.168.1.100 # First VIP } } vrrp_instance VI_2 { state BACKUP # Set the backup server to BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 virtual_ipaddress { 192.168.1.101 # The second VIP } }
On the main server,state MASTER
Set tostate BACKUP
, and willpriority
The value is reduced to indicate that it is a standby server.
Start Keepalived
# Start Keepalived and set the power-on self-startsystemctl start keepalived systemctl enable keepalived
Test dual-machine hot backup
During testing, you can manually set it in the Keepalived configuration of the master server state
forBACKUP
to simulate failover. If everything is configured correctly, the standby server should be able to take over the VIP and provide services when the primary server fails.
Summarize
Through the combination of Nginx and Keepalived, we can use Nginx as an HTTP and reverse proxy server in actual network services and load balancing scenarios, while keepalived is used to implement high availability (HA) solutions. The following is a simple example showing how to implement dual-machine hot standby with the combination of Nginx and keepalived.
First, you need to install Nginx and keepalived services. This example assumes that you already have two servers, we call Server A and Server B, both running Nginx, and you want to implement their dual-machine hot standby with keepalived.
Nginx configuration
Nginx is very simple to configure, you just need to make sure Nginx is running properly on both servers. Here is a basic example of Nginx configuration:
http { server { listen 80; location / { # Here is your application logic # For example, return a simple "Hello World" return 200 'Hello World'; } } }
Keepalived configuration
Keepalived is a little more complicated to configure, you need to configure different roles (master or backup in vrrp_instance) on both servers and make sure they can communicate over VIP (Virtual IP Address). Here is a simple keepalived configuration example:
Keepalived configuration for Server A (as master)
# Make sure the configuration file paths are the same on both servers[root@server-a ~]# vim /etc/keepalived/ # Configuration file content! Configuration File for keepalived # Define a virtual router ID (VRID), for example 10vrrp_instance VI_1 { state MASTER # Set as master interface eth0 # Specify the interface virtual_router_id 10 # Virtual router ID priority 200 # Priority, determines which node becomes the master node advert_int 1 # Announcement interval, unit seconds # Virtual IP address, this is the IP accessed by the client virtual_ipaddress { 192.168.1.100 # Assume this is your VIP } } # Start keepalivedsystemctl start keepalived
Keepalived configuration for Server B (as backup)
# Make sure the configuration file paths are the same on both servers[root@server-b ~]# vim /etc/keepalived/ # Configuration file content! Configuration File for keepalived # Define a virtual router ID (VRID), for example 10vrrp_instance VI_1 { state BACKUP # Set as backup interface eth0 # Specify the interface virtual_router_id 10 # Virtual router ID priority 100 # Priority, configuration lower than Server A advert_int 1 # Announcement interval, unit seconds # Virtual IP address, this is the IP accessed by the client virtual_ipaddress { 192.168.1.100 # Assume this is your VIP } } # Start keepalivedsystemctl start keepalived
Configuration instructions
- In keepalived, the master node will hold the VIP until it is unavailable or downgraded to the backup node.
- The backup node monitors the master node, and if the master node is unavailable, the backup node takes over the VIP and becomes the new master.
- The priority value determines the priority of the node. The higher the value, the greater the possibility of becoming a master.
- The virtual_ipaddress segment defines the VIP, which is the IP address the client actually accesses.
Things to note
- Ensure that the files of both servers are updated synchronously, especially after modifying the configuration.
- Make sure Nginx is running properly on both servers and is configured the same so that services can be switched seamlessly when failover.
- Test failover regularly to ensure that keepalived is configured correctly and can be switched smoothly when needed.
Note that this is just a basic example, and actual deployments may need to be adjusted to your network environment, load balancing requirements, and security requirements. In a production environment, you may also need to consider factors such as configuration health checks, security certification, and logging. In the dual-machine hot standby architecture of Nginx+Keepalived, you need to configure two Nginx servers and a Keepalived instance to manage them. Here is a simplified configuration example, including Nginx configuration and Keepalived configuration.
Nginx configuration
The configuration of Nginx usually includes the following parts:
- Main configuration file:, it contains the global configuration of all Nginx servers.
- Virtual Host Configuration File: Usually located in or in the site-available directory, each file corresponds to a virtual host.
Here is a simple Nginx virtual host configuration example:
In this configuration, we define an upstream server group called backend, which includes two Nginx servers. We then define a virtual host listening on port 80, which proxies all requests to the backend server group.
Keepalived configuration
Keepalived is a high availability solution based on VRRP (Virtual Router Redundancy Protocol). In the dual-machine hot standby of Nginx+Keepalived, Keepalived is used to manage the health status of the Nginx server and ensure that only one Nginx server accepts client requests as the primary server and the other as the backup server waiting for the switch.
Here is a simple Keepalived configuration example:
# Assume this configuration file is located in /etc/keepalived/ vrrp_instance VI_1 { state MASTER; # Set the current node to Master virtual_router_id 51; priority 101; # Set priority, higher than standby node advert_int 1; virtual_ipaddress { 192.168.1.10; # Virtual IP address, that is, the IP accessed by the client } }
In this configuration, we define a VRRP instance named VI_1 that configures the current node as a Master. We set up a virtual IP address 192.168.1.10, which is the IP address accessed by the client, which is actually assigned to the Master node.
Location and permissions of configuration files
Make sure that the Nginx configuration file is located in the Nginx configuration directory, such as /etc/nginx//, and has the correct permissions (usually root:root). Keepalived configuration files are usually located in /etc/keepalived/, and also require correct permissions.
Start and monitor
After the configuration is complete, you need to restart the Nginx and Keepalived services. Use the following command to start and stop the service:
systemctl start nginx systemctl start keepalived systemctl stop nginx systemctl stop keepalived systemctl status nginx systemctl status keepalived
Use the systemctl status command to check the running status of the service.
Health checks and failovers
Keepalived determines the health status of the node by sending VRRP notices. If the Master node fails, Keepalived will automatically promote the backup node to Master. You can add health check scripts in Keepalived configuration to make sure only healthy Nginx servers can become masters.
# Assume this configuration file is located in /etc/keepalived/ ... check_script chk_nginx { script "killall -0 nginx || exit 1"; interval 2; weight 2; } vrrp_instance VI_1 { ... track_script { chk_nginx; } }
In this example, we define a name calledchk_nginx
The health check script, it passes
This is the article about the practice of Nginx+keepalived dual-machine hot backup technology. For more related content on Nginx keepalived dual-machine hot backup, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!