nginx+keepalive high availability construction solution

1. What is nginx and what does it do?

nginx is a very widely used web server that provides high performance and scalability. It is developed by Google and is a replacement for Apache HTTP Server. Here are some of the main features of nginx:

  1. Lightweight: nginx is lighter than Apache. It does not require as many configuration files and modules, making it easier to install and manage.
  2. Scalable: nginx can handle a large number of concurrent requests. It supports functions such as load balancing, proxy, caching and reverse proxy, making the website more scalable.
  3. Customizable: The configuration of nginx is very flexible, and various settings of the website can be customized through simple configuration files, such as caching policies, logging, etc.
  4. Security: nginx can be set up to prevent common security issues, such as cache attacks and cross-site scripting attacks.
  5. Open source: nginx is open source software and its source code is freely available on GitHub, which makes it easier to extend and modify.

2. What is keepalive and what does it do?

keepalive is a plug-in for maintaining long connections, which can improve the efficiency and stability of the server. It maintains the connection through heartbeat packets and reconnects within the connection timeout period. Here are some of the key features of keepalive:

  1. Connection maintenance: keepalive will maintain long connections and reconnect within the connection timeout period to ensure that the server is always running.
  2. Improve efficiency: By maintaining long connections, you can reduce the time required to establish and close connections, thereby improving server efficiency.
  3. Save bandwidth: Because keepalive can maintain long connections, it reduces the network traffic required to establish and close connections, thus saving bandwidth.
  4. Stability: Maintaining connections through heartbeat packets ensures that the server is always running, thereby improving the stability and availability of the website.

3. The principle of keepalive to achieve seamless connection and nginx to seamlessly replace the host

There are at least two machines in the keepalived cluster.
Two machines: one master and one
slave. Three machines: one master and two slaves...and so on.
Set a unique virtual IP for the keepalived cluster. The virtual IP will be bound to the master machine by default,
which is the master machine. There will be two IP addresses, one is your own source IP, and the other is the virtual IP address given by keepalived

In non-preemptive mode:
When the master machine fails, keepalived takes back the virtual IP address immediately and assigns it to the slave machine. This is equivalent to upgrading the slave machine to the master machine. At this time, the user only needs to remember the virtual IP address of keepalived. But
if the master machine returns to normal, it will not be assigned a virtual IP address. It will not be assigned a virtual IP address until the slave machine fails.
4. Build a Keepalived+Nginx high-availability load balancing server

Prepare 2 Nginx (one master and one slave) and install keepalived respectively.

hostname vip ip
nginx1 172.21.161.100 172.21.161.*
nginx2 172.21.161.100 172.21.161.*

Install keepalived

  View network card

Create a new check nginx script (both hosts need to be created)

#!/bin/bash

A=`ps -C nginx --no-header |wc -l`        
    if [ $A -eq 0 ];then                            
        /opt/nginx1.23.1/sbin/nginx                
        if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
            exit 1
        else
            exit 0
        fi
    else
        exit 0
    fi

Make keepalive configuration modifications

Start keepalived and get the virtual IP address (the backup machine is set up in the same way)

 Start on both servers:
service keepalived start

Execute the ip addr command respectively, and you can see the virtual IP on the first started machine.

 

 

 

Guess you like

Origin blog.csdn.net/qq_41118173/article/details/130987476