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

Highly available methods for Nginx

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 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.rpm

Wget-O/etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

Install Nginx:

Yum-yinstallnginx

Operation command:

Systemctlstartnginx;# starts Nginx

Systemctlstopnginx;# stop Nginx

What is high availability?

High availability HA (HighAvailability) 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 (VirtualRouterRedundancyProtocol, 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-yinstallkeepalived

Modify the host (192.168.16.128) keepalived configuration file

The configuration files installed in yum mode will be produced under / etc/keepalived:

Vikeepalived.conf

Keepalived.conf:

# Test script

Vrrp_scriptchk_http_port {

Script "/ usr/local/src/check_nginx_pid.sh" # script executed by heartbeat to detect whether nginx is started

Interval2# (detect the interval between script execution, in seconds)

Weight2# weight

}

# vrrp instance definition section

Vrrp_instanceVI_1 {

StateMASTER# specifies the role of keepalived, with MASTER as the primary and BACKUP as the backup

Interfaceens33# current network interface card for vrrp communication (current centos network card) use ifconfig to view your specific network card

Virtual_router_id66# virtual route number, master and slave all the time

Priority100# priority. The higher the value, the higher the priority of getting and processing the request.

Advert_int1# check interval. Default is 1s (number of seconds of vrrp Multicast cycles)

# authorizing access

Authentication {

Auth_typePASS# sets the authentication type and password. MASTER and BACKUP must use the same password to communicate normally.

Auth_pass1111

}

Track_script {

Chk_http_port# (call detection script)

}

Virtual_ipaddress {

192.168.16.13 Virtual ip (VIP) is defined in 192.168.16.13, which can be configured with one 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 ipaddr

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:

# Test script

Vrrp_scriptchk_http_port {

Script "/ usr/local/src/check_nginx_pid.sh" # script executed by heartbeat to detect whether nginx is started

Interval2# (detect the interval between script execution)

Weight2# weight

}

# vrrp instance definition section

Vrrp_instanceVI_1 {

StateBACKUP# specifies the role of keepalived, with MASTER as the primary and BACKUP as the backup

Interfaceens33# current network interface card for vrrp communication (current centos network card) use ifconfig to view your specific network card

Virtual_router_id66# virtual route number, master and slave all the time

Priority99# priority. The higher the value, the higher the priority of getting and processing the request.

Advert_int1# check interval. Default is 1s (number of seconds of vrrp Multicast cycles)

# authorizing access

Authentication {

Auth_typePASS# sets the authentication type and password. MASTER and BACKUP must use the same password to communicate normally.

Auth_pass1111

}

Track_script {

Chk_http_port# (call detection script)

}

Virtual_ipaddress {

192.168.16.13 Virtual ip (VIP) is defined in 192.168.16.13, which can be configured with one per line.

}

}

Test script:

#! / bin/bash

# check whether nginx is started

A = `ps-Cnginx--no-header | wc- l`

If [$A-eq0]; then# start nginx if nginx is not started

Systemctlstartnginx# restart nginx

If [`ps-Cnginx--no-header | wc- l`-eq0]; if then#nginx restart fails, stop the keepalived service and transfer VIP

Killallkeepalived

Fi

Fi

Script authorization: chmod775check_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:

Systemctlstopnginx;# 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.

Thank you for your reading, the above is the content of "Nginx highly available methods", after the study of this article, I believe you have a deeper understanding of the problem of Nginx highly available methods, and the specific use 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

Development

Wechat

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

12
Report