Configure Keepalived+nginx+tomcat Load balancer under Ubuntu

Source: Internet
Author: User
Tags nameserver oracle java download nginx load balancing

This article tries to explain the operation steps and simple configuration of using keepalived + Nginx + Tomcat to build a high-availability load balancing environment in Ubuntu Server environment, which does not involve performance tuning. Let's talk about their respective roles:

    • tomcat– Application Server
    • nginx– reverse proxy server, as a load balancer
    • keepalived– to detect Nginx status and ensure high availability

The entire environment is done on a virtual machine in Vsan, and the following are the necessary software to complete this environment:

    • Oracle VM virtual Box 4.3
    • Ubuntu Server 14.04
    • Jdk_1.7.0_60
    • tomcat-7.0.54
    • nginx/1.7,1
    • Keepalived v1.2.7

The following is the overall node planning, 5 nodes, IP allocation is as follows:

    • 192.168.1.50–tomcat node, port 8080
    • 192.168.1.51–tomcat node, port 8080
    • 192.168.1.52–tomcat node, port 8080
    • 192.168.1.40–nginx node, port 80, master node
    • 192.168.1.41–nginx node, port 80, backup node
    • 192.168.1.44– Virtual IP

Install Virtual box and Ubuntu Server

Download the appropriate version on the Oracle website and Ubuntu website, install virtual Box, and install Ubuntu Server with the new VM. The Network Settings in virtual box select "Bridge network card" and after starting Ubuntu Server, configure the IP address to 192.168.1.50.

01 sudovi/etc/network/interfaces

Modify the contents of the file as:

010203040506070809101112131415 # This file describes the network interfaces available on your system# and how to activate them. For more information, see interfaces(5).# The loopback network interfaceauto loiface lo inet loopback # The primary network interfaceauto eth0#iface eth0 inet dhcpiface eth0 inet staticaddress 192.168.1.50netmask 255.255.255.0gateway 192.168.1.1

Set up DNS

01 sudovi/etc/resolvconf/resolv.conf.d/base
0102 nameserver 192.168.1.1nameserver 8.8.8.8

The restart takes effect when the settings are complete.

Installing the JRE and Tomcat

1, first to the Oracle website to download the JRE, the following is the Oracle Java Download home: http://www.oracle.com/technetwork/java/javase/downloads/index.html, and then select the appropriate version to download. The version chosen here is: server-jre-7u60-linux-x64.tar.gz

2, upload to user directory, unzip

01 tar-xvf server-jre-7u60-linux-x64.tar.gz

After completion, folder Jdk1.7.0_60 will appear in the same directory.

3. Create a/usr/local/java and copy the JDK directory.

0102 sodu mkdir/usr/local/javasudo cp-r ./jdk1.7.0_60 /usr/local/java/

4, setting environment variables

010203040506 sudovi /etc/profile # 添加一下内容# javaexport JAVA_HOME=/usr/local/java/jdk1.7.0_60exportPATH=$JAVA_HOME/bin:$PATH

5. Make the environment variable effective

01 source/etc/profile

6, download tomcat:http://tomcat.apache.org/download-70.cgi, select 7.0.54 to download, unzip can.

7, upload app Load-balance to Tomcat's WebApps directory. Load-balance contains only one JSP page: index.jsp, which reads as follows:

01 <%=request.getLocalAddr() %>

REQUEST.GETLOCALADDR () Gets the server native IP address

8, start Tomcat

01 apache-tomcat-7.0.54/bin/startup.sh

View the Address http://192.168.1.50:8080/load-balance/page to display properly and output the IP address of the server.

Create a 51,52,40 node

Using virtual Box's VM copy feature, copy the 192.168.1.50 node to three, and then use the method just above to modify the IP address to 192.168.1.51,192.168.1.51,192.168.1.40 respectively.

192.168.1.40 nodes are reserved as Nginx nodes.

Install Nginx and configure load Balancing

1, perform the installation Nginx on the node 192.168.1.40, the operation is as follows:

010203 sudoapt-add-repository ppa:nginx/developmentsudo apt-get updatesudo apt-get installnginx

Nginx Start command for Sudo/etc/init.d/nginx start or service nginx start

A few more words, the following is a description of the parameters of Nginx

    • start– start
    • stop– stop
    • restart– restart
    • reload– Hot Load configuration file
    • status– View Status

Direct access to http://192.168.1.40 to see if Nginx is installed successfully

2, configuring Nginx load Balancing

Use the command sudo vi/etc/nginx/nginx.conf to edit the Nginx configuration file and add the following information to the HTTP node

01020304050607080910111213141516171819 upstream myserver {  server 192.168.1.50:8080 weight=1 max_fails=3 fail_timeout=20s;  server 192.168.1.51:8080 weight=1 max_fails=3 fail_timeout=20s;  server 192.168.1.52:8080 weight=1 max_fails=3 fail_timeout=20s;} server{    listen 80;    server_name 192.168.1.40;    index index.htm index.html;    root /etc/wwwroot;        location / {                proxy_pass http://myserver;                proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;                include /etc/nginx/proxy_params;        }}

The upstream node is configured for load balancing, and the rear MyServer must be consistent with the location siblings in the server node, your Proxy_pass http://myserver. Load balancing uses a polling algorithm by default, distributing requests based on weights to different background tomcat nodes. Of course, you can also set other distribution algorithms, such as Ip_hash.

Weight represents the weight of the distribution.

The Servers node defines the server information, and the server_name node is the native IP address.

After modifying the configuration, use the following command as the configuration takes effect

01 sudoservice nginx reload

3, verify

Use Url:http://192.168.1.40/load-balance to verify that the request is distributed to the Tomcat node, and if the configuration succeeds, the page is refreshed and the contents of the page are alternately appearing for the IP addresses of the 3 tomcat nodes.

When you stop a Tomcat node, you find that the request is no longer forwarded to this node. Once the Tomcat node is restarted, it is automatically added to the load balancing team.

Ensure high availability with keepalived

Nginx load balancing improves performance, but an Nginx node can become a problem, which is a "single point of failure." What if the machine hangs out? How do I smoothly switch to a backup machine? The keepalived can automatically help us to switch to the backup node when the main Nginx node is hung out. Here the main use of a virtual IP, that is, the main nginx node and the backup Nginx node are using the same virtual IP 192.168.1.44, when access, we through the virtual IP to access the application, the specific steps please continue down (the following operations are performed on 192.168.1.40 nodes, After the configuration is complete, clone the 192.168.1.41 node directly, only need to modify the IP address and keepalived configuration file.

1, increase virtual IP

01 sudoifconfigeth0:0 192.168.1.44 netmask 255.255.255.0

by Ifconfig or IP A or IP address show eth0 can be seen at this time the machine has two IP, as follows

010203040506070809 [email protected]:~$ ip a show eth02: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000    link/ether 08:00:27:b9:3c:f7 brd ff:ff:ff:ff:ff:ff    inet 192.168.1.40/24 brd 192.168.1.255 scope global eth0       valid_lft forever preferred_lft forever    inet 192.168.1.44/32 scope global eth0       valid_lft forever preferred_lft forever    inet6 fe80::a00:27ff:feb9:3cf7/64 scope link        valid_lft forever preferred_lft forever

2, install keepalived

01 sudoapt-get installkeepalived

After the installation completes keepalived, you need to manually add the configuration file/etc/keepalived/keepalived.conf as follows:

010203040506070809101112131415161718192021222324252627282930 vrrp_script chk_nginx {    script "/etc/keepalived/check_nginx.sh"    interval 2    weight 2}global_defs {    notification_email {            }}vrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 51     mcast_src_ip 192.168.1.40    priority 20    advert_int 1        authentication {        auth_type PASS        auth_pass 123456    }    track_script {        chk_nginx    }    virtual_ipaddress {        192.168.1.44    }}

Let's talk about the difference between the keepalived configuration on two nginx nodes:

    • Different state, MASTER and BACKUP
    • Different priority, MASTER is higher than BACKUP
    • Mcast_src_ip the actual IP of the respective Nginx servers

In addition to the above configuration, the keepalived.conf content on the two Nginx nodes is identical.

There is also a/etc/keepalived/check_nginx.sh is a script to check the status of Nginx, to the effect that if the nginx process does not exist, then start Nginx, if the startup is unsuccessful, then close keepalived. The contents are as follows

010203040506070809 #!/bin/bash  A=`ps -C nginx --no-header |wc -l`  if [ $A -eq 0 ];then /etc/init.d/nginx start  sleep if [ `ps -C nginx --no-header |wc -l`-eq 0 ];then /etc/init.d/keepalived stopfifi

Make configuration effective

01 sudoservice keepalived reload

3, modify the Nginx configuration, change the value of server_name from native IP (192.168.1.40) to virtual IP address (192.168.1.44), and then make the configuration take effect.

01 sudoservice nginx reload

4, verify that http://192.168.1.44/load-balance see if the load can be distributed to different tomcat nodes.

5, copy the 192.168.1.40 VM from virtual box to 192.168.1.41, then modify the IP address to 192.168.1.41, and then modify the keepalived configuration file, state for BACKUP, MCAST_SR C_ip is a small point value for 192.168.1.40,priority. Then make the configuration effective.

6, first verify http://192.168.1.44/load-balance see if you can access, after success, shutdown off node 192.168.1.40, and then access Http://192.168.1.44/load-balance, See if you can access it properly, and if you can access it correctly, it succeeds.

Note: If you modify the IP, it will not be able to access, restart the server to try.

Configure Keepalived+nginx+tomcat Load balancer under Ubuntu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.