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 build Keepalived+Nginx+Tomcat High availability load balancing Architecture

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to build Keepalived+Nginx+Tomcat high-availability load balancing architecture. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

High availability load balancing Architecture of Nginx

As shown in the following figure: architecture diagram for typical Tomcat service multi-instance deployment

(1) the user requests to DNS through the domain name, and the DNS resolves the domain name and returns the corresponding IP address. The IP and the virtual IP of the Keepalived mapping server

(2) access the corresponding load balancer (Nginx) through the virtual IP, where two Nginx are deployed, and then use Keepalived to ensure the high availability of the NG. Normally, the Keepalived-M forwards the virtual IP mapping to the Nginx-M. If the Nginx-M fails, the Keepalived will switch to the Keepalived-S to work, thus ensuring the single point of failure of the NG.

(3) Route the request to the corresponding Tomcat service through the Nginx load balancer.

Build a highly available load balancing architecture for Keepalived + Nginx + Tomcat

Software that needs to be prepared

(1) apache-tomcat-8.5.16.tar.gz

(2) nginx-1.12.2.tar.gz

(3) keepalived-1.3.9.tar.gz

two。 Server preparation

Two servers, such as 192.168.10.11192.168.10.12

3. Install the required dependency packages

$yum-y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel

4. Installation

4.1 install Tomcat

(1) install Tomcat in two servers, decompress apache-tomcat-8.5.16.tar.gz and complete the installation.

4.2 install Nginx

(1) decompress the installation package: tar-zxvf nginx-1.12.2.tar.gz

(2) enter the nginx-1.12.2 directory: cd nginx-1.12.2

(3) compile:

$. / configure-with-http_stub_status_module-with-http_ssl_module-prefix=/usr/local/nginx

$sudo make & & sudo make install

4.3 install Keepalived

(1) decompress the installation package: tar-zxvf keepalived-1.3.9.tar.gz

(2) enter the keepalived-1.3.9 directory: cd keepalived-1.3.9

(3) perform compilation:

$. / configure-prefix=/usr/local/keepalived-sysconf=/etc

$sudo make & & sudo make install

5 configuration

5.1 configure Nginx for two servers respectively

(1) modify the nginx configuration files of the two servers, vi / usr/local/nginx/conf/nginx.conf

(2) the contents are as follows:

# number of nginx processes

Worker_processes 1

# maximum number of connections per process

Events {

Worker_connections 1024

}

# http server configuration

Http {

Include mime.types

Default_type application/octet-stream

Sendfile on

# long connection timeout (in seconds)

Keepalive_timeout 65

# upstream load balancer configuration, configure the service address and weight routed to tomcat

Upstream localhost {

Server 192.168.10.11:8080 weight=2

Server 192.168.10.12:8080 weight=2

}

# configuration of virtual host

Server {

# listening port

Listen 80

# there can be multiple domain names, separated by spaces

Server_name localhost

Location / {

Root html

Index index.html index.htm

# connection timeout between nginx and backend server (proxy connection timeout)

Proxy_connect_timeout 3

# backend CVM data backhaul time (proxy send timeout)

Proxy_send_timeout 30

# response time of back-end server after successful connection (proxy received timeout)

Proxy_read_timeout 30

Proxy_pass http://localhost;

}

Error_page 500 502 503 504 / 50x.html

Location = / 50x.html {

Root html

}

}

}

5.2 Primary Keepalived configuration

(1) modify the keepalived configuration file of 11 servers, vi / etc/keepalived/keepalived.conf

(2) the contents are as follows:

! Configuration File for keepalived

# Global configuration

Global_defs {

# when keepalived is switched, messages are sent to the specified email, and multiple email can be configured

Notification_email {

Feinik1@foxmail.com

Feinik2@foxmail.com

}

# the address from which the notification email is sent

Notification_email_from feinik@foxmail.com

# smtp address of notification email

Smtp_server smtp.exmail.qq.com

# timeout for connecting to the smtp server (in seconds)

Smtp_connect_timeout 30

# Keepalived machine ID, which is unique within a network

Router_id nginx-master

}

# execute script configuration

Vrrp_script chk_nginx {

# path where the script is located

Script "/ home/project/keepalived/check_nginx.sh"

# interval between script execution, seconds

Interval 2

# priority

Weight 2

}

# keepalived instance configuration

Vrrp_instance VI_1 {

# specify the initial state of the instance, either MASTER or BACKUP, and uppercase is required

State MASTER

# the Nic bound to the instance

Interface ens33

# Virtual routing ID, which is a number that is unique within the entire VRRP. If the keepalived is configured with master / slave, it needs to be the same.

Virtual_router_id 51

# priority. The higher the number, the higher the priority.

Priority 100

# time interval for synchronization checks between MASTER and BACKUP (in seconds)

Advert_int 1

# Communication Verification

Authentication {

Auth_type PASS

Auth_pass feinik

}

# tracking peripheral scripts

Track_script {

# configure the name of vrrp_script here

Chk_nginx

}

# Virtual ip configuration, which can be configured with multiple

Virtual_ipaddress {

192.168.10.200

}

}

5.3 standby Keepalived configuration

(1) modify the keepalived configuration file of 12 servers, vi / etc/keepalived/keepalived.conf

(2) the contents are as follows:

! Configuration File for keepalived

# Global configuration

Global_defs {

# when keepalived is switched, messages are sent to the specified email, and multiple email can be configured

Notification_email {

Feinik1@foxmail.com

Feinik2@foxmail.com

}

# the address from which the notification email is sent

Notification_email_from feinik@foxmail.com

# smtp address of notification email

Smtp_server smtp.exmail.qq.com

# timeout for connecting to the smtp server (in seconds)

Smtp_connect_timeout 30

# Keepalived machine ID, which is unique within a network

Router_id nginx-master

}

# execute script configuration

Vrrp_script chk_nginx {

# path where the script is located

Script "/ home/project/keepalived/check_nginx.sh"

# interval between script execution, seconds

Interval 2

# priority

Weight 2

}

# keepalived instance configuration

Vrrp_instance VI_1 {

# specify the initial state of the instance, either MASTER or BACKUP, and uppercase is required

State BACKUP

# the Nic bound to the instance

Interface ens33

# Virtual routing ID, which is a number that is unique within the entire VRRP. If the keepalived is configured with master / slave, it needs to be the same.

Virtual_router_id 51

# priority. The higher the number, the higher the priority.

Priority 99

# time interval for synchronization checks between MASTER and BACKUP (in seconds)

Advert_int 1

# Communication Verification

Authentication {

Auth_type PASS

Auth_pass feinik

}

# tracking peripheral scripts

Track_script {

# configure the name of vrrp_script here

Chk_nginx

}

# Virtual ip configuration, which can be configured with multiple

Virtual_ipaddress {

192.168.10.200

}

}

5.4 Nginx status check script creation

(1) create a status check script for Nginx: check_nginx.sh

(2) the contents are as follows:

#! / bin/sh

NGINX=/usr/common/nginx/sbin/nginx

PORT=80

Nmap localhost-p $PORT | grep "$PORT/tcp open"

# echo $?

If [$?-ne 0]; then

$NGINX-s stop

# try to start NG again here

$NGINX

Sleep 5

Nmap localhost-p $PORT | grep "$PORT/tcp open"

[$?-ne 0] & & cd / usr/common/keepalived/sbin & & pkill keepalived

Echo "stoped"

Fi

4 run the test

(1) in order to see the effect of keepalived switching more intuitively, the upstream service of nginx in 11 server is only configured with 11 tomcat service address, and the upstream service in 12 server is only configured with 12 tomcat service address. It is only necessary to observe whether the tomcat of 12 server can be accessed by turning off the nginx in 11 server to see if you can access the tomcat of 12 server using virtual ip.

(2) start the tomcat, nginx and keepalived of the two servers respectively, and access the virtual ip:192.168.10.200. You can see that the tomcat of the main keepalived server is accessed.

Image.png

(3) close the nginx,nginx-s stop of the 11 server and access the virtual ip again, as follows: the master keepalived detects that the nginx service of this service is down through the configured script, so it immediately switches to the standby keepalived. At this time, the keepalived of the 12 server is upgraded to the master, so the tomcat of the 12 server is accessed.

This is the end of the article on "how to build Keepalived+Nginx+Tomcat High availability load Balancer Architecture". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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