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 deploy High availability load balancing Cluster in Keepalived+Nginx+Tomcat

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "how to deploy a high-availability load balancer cluster in Keepalived+Nginx+Tomcat". In daily operation, I believe many people have doubts about how to deploy a high-availability load balancer cluster in Keepalived+Nginx+Tomcat. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to deploy a high-availability load balancer cluster in Keepalived+Nginx+Tomcat". Next, please follow the editor to study!

The initial application is relatively small, which is generally based on stand-alone deployment, which can meet the needs of the business. with the continuous expansion of business, the mode of stand-alone deployment can not carry such a large amount of business and needs to deploy services in clusters. This paper mainly introduces the multi-instance deployment of server Tomcat, which is equipped with Keepalived + Nginx to achieve the high availability of Web services.

Nginx is a high performance HTTP reverse proxy server

Keepalived is a service high availability scheme based on VRRP protocol, which can be used to avoid single point of failure of service.

Nginx, as a reverse proxy server, provides the ability of load balancing to enable our Web server to expand horizontally. At this time, the reverse proxy server (Nginx) becomes a single point. If you hang up, the integrated Web server cannot be accessed by the outside world. Therefore, it is necessary to combine Keepalived to ensure the high availability of the reverse proxy server (Nginx).

Environmental preparation

CentOS7 system

Keepalived-2.0.15

Nginx-1.14.2

Tomcat8

Prepare 2 servers, each with Nginx+Keepalived and Tomcat deployed and installed, and another VIP (virtual IP). Two servers are omitted here, so it can be practiced in the actual simulation (Tomcat is not deployed on a separate server)

1. Installation of Nginx

2. Installation of Keepalived

3. Configure Keepalived + Nginx to achieve high availability

Of the 2 servers prepared, one as MASTER and the other as BACKUP; need to prepare files as shown in the following figure

Execute the following convention, according to which the above installation script is also executed

1. Both keepalived and nginx are compiled and installed in the / usr/local directory with the names keepalived and nginx, respectively.

2. Ck_ng.sh and keepalived.conf files are placed in the / etc/keepalived/ directory, and ck_ng.sh file permissions are set to 755.

3. Place the nginx.service and keepalived.service files in the / usr/lib/systemd/system directory

3. 1. MASTER configuration

Keepalived.conf configuration

Global_defs {# notification_email {# localhost@163.com #} # notification_email_from Keepalived@localhost # smtp_server 163@smtp.com # smtp_connect_timeout 30 router_id 192.168.1.101 # IP address of the MASTER server} # configure vrrp_script, which is mainly used for health check and the actions performed after the check fails. Vrrp_script chk_nginx {# Health check script. Script "/ etc/keepalived/ck_ng.sh" # check frequency when the returned value of the script is not 0. The following configuration checks interval 2 # every 2 seconds. When the check fails, reduce the priority of vrrp_instance by 5 weight-5 # for 3 consecutive monitoring failures before the real health check fails. And adjust the priority of fall 3 # to monitor twice in a row, it will be considered successful. However, it does not adjust the priority rise 2} # defines the VIP vrrp_instance configuration for providing services vrrp_instance VI_1 {# specifies the initial state of the vrrp_instance, whether MASTER or BackUP mainly depends on the priority. State MASTER # specifies the network card bound to vrrp_instance, and eventually announces the source IP of the heartbeat packet sent by VIP interface ifcfg-eth0 # through the specified network card. You can use the bound network card IP, or you can use other IP mcast_src_ip 192.168.1.101 # on this server, which is equivalent to VRID, and is used to distinguish multicast within a network, which needs to be unique within the multicast domain. Virtual_router_id 55 # priority of this machine, among the machines with the same VRID, the one with the highest priority will be elected as MASTER priority 100# heartbeat interval. In the following configuration, MASTER will send a message every 1 second to other machines in the higher vocational group, and they are still alive. Advert_int 1 # defines the authentication method and password of the master / slave. It generally uses PASS (up to 8 digits, exceeding only the first 8 digits recognized as passwords) authentication {auth_type PASS auth_pass 123456} # VIP virtual IP pool. The settings of both nodes must be the same virtual_ipaddress {192.168.1.100 ash 24} # script configuration referenced in this vrrp_instance The name is the container name track_script {chk_nginx} # status switch defined by vrrp_script. Send an email notification smtp_alert} using the above configuration

Ck_ng.sh configuration

Keepalived.service configuration

Nginx.service configuration

3.2. BACKUP configuration

Keepalived.conf configuration (similar to MASTER, listing differences)

Global_defs {router_id 192.168.1.102} # defines the VIP vrrp_instance configuration for providing services vrrp_instance VI_1 {# specifies the initial state of vrrp_instance, whether it is MASTER or BackUP or depends on priority. State BACKUP # Source IP for sending heartbeat packets. You can use the bound network card IP or other IP mcast_src_ip 192.168.1.102 # on this server. The machine with the same VRID priority will be elected MASTER priority 99}.

Ck_ng.sh configuration (consistent with MASTER)

Keepalived.service configuration (consistent with MASTER)

Nginx.service configuration (consistent with MASTER)

3.3.The Tomcat service provides services through reverse proxy Nginx

The installation and application deployment of Tomcat are no longer described in detail. Let's take a look at how to implement external services through the configuration of Nginx (both Nginx are done through such configuration)

Nginx.conf configuration

# upstream configure upstream myapp {ip_hash; server 192.168.1.101 Tomcat 8080; # Tomcat application server 192.168.1.102 ip_hash; server 8080; # Tomcat application} server {proxy_pass 8888; # suppose an application {proxy_pass proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade under external service port server_name localhost; port_in_redirect on; location / app1 # Tomcat Another application under proxy_set_header Connection "upgrade"; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;} location / app2 # Tomcat {proxy_pass http://myapp/app2; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade" Proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;} location / {proxy_pass http://myapp; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr Proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}} at this point, the study on "how to deploy a highly available load balancing cluster in Keepalived+Nginx+Tomcat" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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