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

Analysis of the implementation process of Nginx High availability Scheme in production Environment

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

Share

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

Preparatory work:

192.168.16.128

192.168.16.129

Two virtual machines. Install Nginx

Install Nginx

Update the yum source file:

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

Yum-y install nginx

Operation command:

Systemctl 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. it usually refers to reducing the time when the system can not 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. today we use keepalived to implement Nginx.

The high availability of.

Dual-computer hot standby scheme

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 certain 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.

Realization process

Install keepalived

You can install it directly in yum, which automatically installs dependencies:

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

Vi keepalived.conf

Keepalived.conf:

# 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, mainly MASTER 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 route number, master and slave always priority 100 # priority, the higher the value, the higher the priority of processing requests, the higher the advert_int 1 # check interval Default is 1s (seconds 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 normally 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:

# 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) 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 route number, master and slave always priority 99 # priority, the higher the value, the higher the priority of processing requests, the higher the advert_int 1 # check interval Default is 1s (seconds 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 normally 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:

#! / bin/bash# detects whether nginx starts A = `ps-C nginx-- no-header | wc-l`if [$A-eq 0]; then # if nginx is not started, restart nginx systemctl start nginx # restart nginx if [`ps-C nginx-- no-header | wc-l`-eq 0]; if then # nginx fails to restart, stop keepalived service and transfer killall keepalived fifi by 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:

Systemctl 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.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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

Servers

Wechat

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

12
Report