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

How to achieve High availability of Web Server by Nginx+Keepalived

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

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you how Nginx+Keepalived to achieve the high availability of Web servers, I hope you will learn something after reading this article, let's discuss it together!

1 、 Nginx

Business background

Now the company needs to quickly build web servers to provide web services to users.

Demand split

Need software based on http protocol to build service implementation

Introduction

Common usage:

1) web server software httpd http protocol

Similar web server software: apache (veteran) nginx (Russia) IIS (Microsoft)

2) proxy server reverse proxy

3) Mailbox proxy server IMAP POP3 SMTP

4) load balancing function LB loadblance

Features of the Nginx architecture:

① high reliability: stable master process management scheduling request to which worker= > worker process response request-master multi-worker ② hot deployment: (1) smooth upgrade (2) can quickly reload configuration ③ high concurrency: can simultaneously respond to more request events epoll model tens of thousands of ④ response fast: especially in dealing with static files Fast response time sendfile ⑤ low consumption: cpu and memory 1w requests memory 2-3MB ⑥ distributed support: reverse proxy seven-tier load balancing

Official website: http://nginx.org/

1.2. Installation

Common installation methods:

① yum installation and configuration, which needs to be compiled using Nginx official source or EPEL source ② source

# add and run user shell > useradd-s/sbin/nologin-M www # installation dependency shell > yum-y install pcre-devel zlib-devel openssl-devel # compile and install shell > cd / root/soft shell > tar xvf nginx-1.14.2.tar.gz shell > cd nginx-1.14.2 shell >. / configure-- prefix=/usr/local/nginx-- user=www-- group=www-- with-http_ssl_module-- with-http_stub_status_module-- with-http_realip_module & & make & & make install

Compilation parameter description

1.3. Catalogue introduction

Check the installation directory / usr/local/nginx

1.4. Software operation parameters

View the relevant parameters of the binary executable file of nginx

Shell > cd / usr/local/nginx/sbin shell >. / nginx-h

Display after execution

Nginx version: nginx/1.14.2 Usage: nginx [-? hvVtTq] [- s signal] [- c filename] [- p prefix] [- g directives] Options: # View help -? -h: this help # View version and exit-v: show version and exit # View version and configuration options and exit-V: show version and configure options then exit # detect profile syntax and exit-t: test configuration and exit # detect profile syntax print it and exit-T: test configuration Dump it and exit # prohibits the display of non-error messages during configuration testing-Q: suppress non-error messages during configuration testing # sends a signal to the main process stop to force exit quit gracefully exit reopen reopen log reload overload configuration-s signal: send signal to a master process: stop, quit, reopen Reload # set the nginx directory $prefix path-p prefix: set prefix path (default: / usr/local/nginx/) # specify the configuration file for startup-c filename: set configuration file (default: conf/nginx.conf) # set the global directive outside the configuration file-g directives: set global directives out of configuration file

Generally speaking, the main use is:

-s parameter control management nginx service-V parameter view nginx open module and compilation parameter-t parameter detect if there are any errors in the configuration file

2. Keepalived to achieve high availability

Business background

The singleton web server can meet the basic business needs and provide web services. However, there is a single point of failure, that is, when the server goes down, the user will not be able to get the service response.

In order to improve the user experience and provide users with high-quality service continuously, when the web server is not available, there can be a backup server to take over the work of the web server and continue to provide responses for users. Among them, there is also a problem to be solved, which requires that the backup server can be quickly and automatically switched over.

The above business requirements are generally referred to as high-availability HA for implementing services.

Demand split

That is, the core of the high-availability implementation:

① redundant server (backup server)

② automatic switching allows users to access services through VIP by binding virtual IP

2.1. Introduction

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 the LVS cluster system. Later, it adds the VRRP function that can achieve high availability. Therefore, in addition to being able to manage LVS software, Keepalived can also be used as highly available solution software for other services such as Nginx, Haproxy, MySQL, and so on.

Keepalived mainly uses three modules, namely, core, check and vrrp.

Core module is the core of keepalived, which is responsible for the startup and maintenance of the main process as well as the loading and parsing of global configuration files.

Check is responsible for health check-ups, including common check-ups.

The vrrp module is used to implement the VRRP protocol.

2.2, installation

Both master and backup need to be installed, that is, server01 and server03 machines

# install keepalived shell > yum-y install keepalived

The directories and files that keepalived needs to use:

2.3, configuration

① backs up the configuration files of the active and standby servers

Shell > cd / etc/keepalived shell > cp keepalived.conf keepalived.conf_bak

② modifies the configuration files of master and slave servers respectively.

Shell > vim keepalived.conf

The sample configuration file states

! Configuration File for keepalived # configuration of sending mail global_defs {notification_email {acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc} notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL} # vrrp protocol configuration vrrp_instance VI_1 {# working mode state MASTER # listening network card interface eth0 # virtual routing id needs to be the same as the standby server virtual_router _ id 51 # weighted priority priority 100 # sending cycle of vrrp packet 1s advert_int 1 # permission verification authentication {auth_type PASS auth_pass 1111} # VIP virtual_ipaddress {192.168.200.16 192.168.200.17 192.168.200.18}}

Primary server

! Configuration File for keepalived global_defs {notification_email {acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc} notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL} vrrp_instance VI_1 {state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication {auth_type PASS auth_pass 1111} # master by default only need to modify the use of VIP But virtual_ipaddress {192.168.17.200}}

Standby server

! Configuration File for keepalived global_defs {notification_email {acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc} notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL} vrrp_instance VI_1 {# modify the working mode to standby state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication {auth_type PASS auth_pass 1111} # Note to modify VIP virtual_ipaddress {192.168.17.200}}

③ starts the keepalived of the primary server and the standby server in sequence.

Shell > service keepalived start

④ to view the Nic information of the master and slave servers

# you need to view shell > ip an in server01 and server03 via ip a command.

2.4, simulated failure

Simulate server failure downtime to see if services can be switched to the standby machine.

Simulate downtime, shut down the server01 master server, and automatically switch VIP to the server03 backup server.

# grab package vrrp shell > yum-y install tcpdump shell > tcpdump vrrp-n after reading this article, I believe you have a certain understanding of "how Nginx+Keepalived makes Web servers highly available". If you want to know more about it, please follow the industry information channel. Thank you for reading!

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