In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to achieve the high availability of Nginx". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to achieve the high availability of Nginx.
Preparatory work:
192.168.16.128
192.168.16.129
Two virtual machines, or a server. Install Nginx
Install Nginx
Update the yum source file:
Copyrpm-ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmwget-O / etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
Install Nginx:
Copyyum-y install nginx
Operation command:
Copysystemctl start nginx; # start Nginxsystemctl 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, which usually refers to reducing the time when the system cannot provide services through design. If a system can provide services all the time, then the availability is 100%, but there are unexpected events. So we can only reduce service failures as much as possible.
To solve the problem? # in the production environment, Nginx is often used as a reverse proxy to provide services, but one day Nginx will inevitably encounter failures, such as server downtime. When Nginx goes down, all interfaces provided to the outside world will make it inaccessible.
Although we can't guarantee that the server is 100% available, we have to find a way to avoid this tragedy, and today we use keepalived to achieve high availability of Nginx.
Dual-server hot backup solution # this scheme is the most common high-availability solution in domestic enterprises. Dual-server hot backup actually means that one server is providing services, and the other is the standby state of a service. When one server is not available, the other will be replaced.
What is 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.)
Failover mechanism
Failover between Keepalived highly available services 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.
Implementation process # install keepalived
You can install it directly in yum, which automatically installs dependencies:
Copyyum-y install keepalived
Modify the host (192.168.16.128) keepalived configuration file
The configuration files installed in yum mode will be produced under / etc/keepalived:
Copyvi keepalived.conf
Keepalived.conf:
Copy# detect script vrrp_script chk_http_port {script "/ usr/local/src/check_nginx_pid.sh" # script executed by heartbeat, detect whether nginx starts interval 2 # (detect the interval between script execution (in seconds) weight 2 # weight} # vrrp instance definition part vrrp_instance VI_1 {state MASTER # specifies the role of keepalived, MASTER is the main, BACKUP is the standby interface ens33 # the network interface card (the network card of the current centos) that currently communicates with centos, use ifconfig to view your specific network card virtual_router_id 66 # virtual route number The higher the value, the higher the priority of processing requests. Advert_int 1 # check interval defaults to 1s (number of vrrp Multicast cycles) # authorize access to authentication {auth_type PASS # set authentication type and password MASTER and BACKUP must use the same password to communicate properly auth_pass 1111} track_script {chk_http_port # (call detection script)} virtual_ipaddress {192.168.16.130 # define virtual ip (VIP), one more per line}}
Vip can be configured in virtual_ipaddress, and the service can be accessed online through vip.
Interface needs to be set according to the server network card, and the usual viewing method ip addr
The same configuration is required for authentication configuration to authorize access to the backup.
Modify the standby (192.168.16.129) keepalived configuration file
Keepalived.conf:
Copy# detection script vrrp_script chk_http_port {script "/ usr/local/src/check_nginx_pid.sh" # script executed by heartbeat Detect whether nginx starts interval 2 # (detection script execution interval) weight 2 # weight} # vrrp instance definition part vrrp_instance VI_1 {state BACKUP # specifies the role of keepalived, MASTER is the main BACKUP for standby interface ens33 # current network interface card for vrrp communication (current centos network card) use ifconfig to view your specific network card virtual_router_id 66 # virtual routing number, the master and slave should always be priority 99 # priority, the higher the value The higher the priority of processing the request, the higher the advert_int 1 # check interval, which defaults to 1s (seconds of vrrp Multicast cycles) # authorized access authentication {auth_type PASS # set authentication type and password MASTER and BACKUP must use the same password to communicate properly auth_pass 1111} track_script {chk_http_port # (call detection script)} virtual_ipaddress {192.168.16.130 # define virtual ip (VIP), one more per line}}
Test script:
Then # detects whether nginx starts A = `ps-C nginx-- no-header | wc-l` if [$A-eq 0]; then # starts nginx systemctl start nginx # restart nginx if if the nginx if is not started [`Copies-C nginx-- no-header | wc-l`-eq 0] If the restart of then # nginx fails, stop the keepalived service and transfer killall keepalived fifi to VIP
Script authorization: chmod 775 check_nginx_pid.sh
Note: the script must be authorized, otherwise there is no permission to access, ah, here we have two servers to execute, VIP (virtual_ipaddress:192.168.16.130), we access the service directly through vip in the production environment.
Simulate a nginx failure:
Modify the html page of the Nginx accessed by the two servers by default as a difference.
First visit 192.168.16.130 and access it through vip, and the page displays 192.168.16.128, indicating that it is currently a service provided by the primary server.
At this time, the 192.168.16.128 master server executes the command:
Copysystemctl stop nginx; # stop nginx
Visit vip again (192.168.16.130) and find that the page is still displayed at this time: 192.168.16.128, which is automatically restarted in the script.
Now turn off the 192.168.16.128 server directly, visit the vip (192.168.16.130) and now find that the page shows 192.168.16.129 when the keepalived automatically fails over, and a high-availability solution for an enterprise production environment is set up.
There are many functions in keepalived, such as: email reminder and so on, so you can go to the official website to see the documents.
At this point, I believe you have a deeper understanding of "how to achieve high availability of Nginx". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.