Preparation:
192.168.16.128
192.168.16.129
Two virtual machines. Install Nginx
Install Nginx
Update yum source file:
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
Install Nginx:
yum -y install nginx
Operation command:
systemctl start nginx; #Start Nginx
systemctl stop nginx; #Stop Nginx
What is high availability?
High availability HA (High Availability) is one of the factors that must be considered in the design of distributed system architecture. It usually refers to reducing the time when the system cannot provide services through design. If a system can always provide services, then the availability is 100%, but there are unforeseen circumstances. So we can only try to reduce service failures as much as possible.
solved problem?
In production environments, Nginx is often used as a reverse proxy to provide external services. However, Nginx will inevitably encounter failures one day, such as server downtime. When Nginx goes down, all externally provided interfaces will become inaccessible.
Although we cannot guarantee that the server is 100% available, we must find ways to avoid this tragedy. Today we use keepalived to implement Nginx
High availability.
Dual-machine hot backup solution
This solution is the most common high-availability solution among domestic enterprises. Dual-server hot backup actually means that one server is providing services and the other is in standby state for a certain service. When one server is unavailable, the other one Will take his place.
What is keepalived?
Keepalived software was originally designed for LVS load balancing software to manage and monitor the status of each service node in the LVS cluster system. Later, it added VRRP (Virtual Router Redundancy Protocol) to achieve high availability. Function. Therefore, in addition to managing LVS software, Keepalived can also be used as a high-availability solution software for other services (such as Nginx, Haproxy, MySQL, etc.)
Failover mechanism
Failover transfer between Keepalived high-availability services is implemented through VRRP.
When the Keepalived service is working normally, the primary Master node will continuously send (multicast) heartbeat messages to the backup node to tell the backup Backup node that it is still alive. When the primary Master node fails, it cannot send heartbeat messages and the backup node will not be able to send heartbeat messages. Therefore, the node can no longer detect the heartbeat from the main Master node, so it calls its own takeover program to take over the IP resources and services of the main Master node. When the master node recovers, the backup node will release the IP resources and services it took over when the master node failed, and return to its original backup role.
Implementation process
Install keepalived
You can install it directly using the yum method, which will automatically install dependencies:
yum -y install keepalived
Modify the host (192.168.16.128) keepalived configuration file
Installation using yum will produce the configuration file under /etc/keepalived:
vi keepalived.conf
keepalived.conf:
#Detection script
vrrp_script chk_http_port {
Script "/usr/local/src/check_nginx_pid.sh" #Heartbeat execution script to detect whether nginx is started
interval 2 interval 2 weight 2 #weight
}
#vrrp instance definition section
vrrp_instance VI_1 {
state MASTER # Specify the role of keepalived, MASTER is the main one and BACKUP is the backup one
interface ens33 # The current network interface card for vrrp communication (current centos network card) Use ifconfig to check your specific network card
Virtual_router_id 66 # Virtual route number, master and slave must always be
Priority 100 # Priority, the larger the value, the higher the priority for processing the request
advert_int 1 # Check interval, the default is 1s (vrrp multicast cycle seconds)
#Authorized access
authentication {
auth_type PASS #Set the verification type and password. MASTER and BACKUP must use the same password for normal communication
auth_pass 1111
}
track_script {
chk_http_port #(call detection script)
}
virtual_ipaddress {
192.168.16.130 192.168.16.130 # Define virtual ip (VIP), you can set more than one, one per line
}
}
VIP can be configured in virtual_ipaddress, and services can be accessed online through VIP.
The interface needs to be set according to the server network card. The usual viewing method is ip addr
Authentication configuration authorization access to the backup machine also requires the same configuration
Modify the backup machine (192.168.16.129) keepalived configuration file
keepalived.conf:
#Detection script
vrrp_script chk_http_port {
Script "/usr/local/src/check_nginx_pid.sh" #Heartbeat execution script to detect whether nginx is started
interval 2 weight 2 #weight
}
#vrrp instance definition section
vrrp_instance VI_1 {
State Backup#Specify the role of Keepalived, Master mainly, backup as a reserve
interface ens33 # The current network interface card for vrrp communication (current centos network card) Use ifconfig to check your specific network card
virtual_router_id 66 # Virtual route number, master and slave must always be
priority 99 Advert_int 1#Check the interval, the default is 1s (VRRP broadcast cycle seconds)
#Authorized access
authentication {
auth_type PASS #Set the verification type and password. MASTER and BACKUP must use the same password for normal communication
auth_pass 1111
}
track_script {
chk_http_port #(call detection script)
}
virtual_ipaddress {
192.168.16.130 192.168.16.130 # Define virtual ip (VIP), you can set more than one, one per line
}
}
Detection script:
#!/bin/bash
#Detect whether nginx is started
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then #If nginx is not started, start nginx systemctl start nginx #Restart nginx
If [ `ps -C nginx --no-header |wc -l` -eq 0 ];then # If nginx fails to restart, stop the keepalived service and perform VIP transfer
Killall keepalived
fi
fi
Script authorization: chmod 775 check_nginx_pid.sh
Note: The script must be authorized, otherwise it will not have permission to access. Here we have two servers to execute, VIP (virtual_ipaddress:192.168.16.130). In the production environment, we access the service directly through VIP.
Simulate nginx failure:
Modify the Nginx html page that the two servers access by default as a difference.
First visit 192.168.16.130 and access it through VIP. The page displays 192.168.16.128; indicating that the service is currently provided by the main server.
At this time, the 192.168.16.128 main server executes the command:
systemctl stop nginx; #stop nginx
Visit vip (192.168.16.130) again and find that the page still displays: 192.168.16.128. This is an automatic restart in the script.
Now directly close the 192.168.16.128 server, visit VIP here (192.168.16.130) and now find that the page displays 192.168.16.129. At this time, keepalived will automatically failover, and a high-availability solution for an enterprise-level production environment has been established.
The above is the detailed content of How to achieve Nginx high availability. For more information, please follow other related articles on the PHP Chinese website!