Docker + Nginx + Tomcat 7 Simple Server Load balancer Configuration

Source: Internet
Author: User
This article describes how to configure Simple Server Load balancer on Docker. The host machine is Ubuntu14.04.2LTS, two CentOS containers, Nginx for the host machine, and tomcat 7 for the two containers. The architecture is as follows: the principle of this solution is to map the host machine port and the docker container port (that is, a port accessing the host machine will be mapped to the corresponding port of the docker container ), then, configure Nginx on the host machine to access a port of the host machine, and assign it to the specified service address according to the rules.

This article describes how to configure Simple Server Load balancer on Docker. The host machine is Ubuntu 14.04.2 LTS, two CentOS containers, Nginx installed on the host machine, and tomcat 7 installed on the two containers.

The structure is as follows:

The principle of this solution is to map the host machine port and the docker container port (that is, a port accessing the host machine will be mapped to the corresponding port of the docker container ), then, configure Nginx on the host to access a port on the host machine, and assign the port to the specified service address according to the rules. This completes load balancing.

Procedure

1. Prepare the host machine. The host machine is Ubuntu 14.04.2 LTS and is installed in Vmware. The installation method is not described in detail.

2. Install Nginx on the host machine by running the following command.

Sudo apt-get install nginx

After the installation is complete, you can check whether the Nginx installation is complete. You can run the following command to check the version and print out the Nginx version, which indicates that the installation is successful.

$ Nginx-v

Nginx version: nginx/1.4.6 (Ubuntu)

Then, run the following command to start the Nginx service:

# Start the service

$ Sudo service nginx start

# View service status

$ Sudo service nginx status

* Nginx is running # indicates that the service is started.

Access http: // localhost in a browser and the result is as follows:

3. Download the Docker image and run the docker pull command. For more information about the image operations, see this article.

4. Start a container and set port ing. One of the commands is as follows:

Sudo docker run-t-I-p 3222: 22-p 3280: 80 87e5b6b3ccc1/bin/bash

The above command starts a container in the standard bash output mode, sets port ing, uses the-p parameter,-p host port: docker container port, the preceding command sets port 3222 of the host machine to map to port 22 of the container, and port 3280 of the host machine to port 80 of the container.

5. Install jre and tomcat7.0 in the container, and install jre

Wget-O jre-7u6-linux-x64.rpm? BundleId = 67387.

Yum install jre-7u6-linux-x64.rpm

Check whether jre is successfully installed.

Java-version

Java version "1.7.0 _ 06"

Java (TM) SE Runtime Environment (build 1.7.0 _ 06-b24)

Java HotSpot (TM) 64-Bit Server VM (build 23.2-b09, mixed mode)

Install tomcat7.0

Wget http://apache.fayea.com/tomcat/tomcat-7/v7.0.65/bin/apache-tomcat-7.0.65.tar.gz

Tar-zxvf apache-tomcat-7.0.65.tar.gz

Start tomcat, enter the extract directory, cd to the bin directory, run the command, and enter the following information, indicating that tomcat is successfully started.

Bash startup. sh

Using CATALINA_BASE:/home/apache-tomcat-7.0.65

Using CATALINA_HOME:/home/apache-tomcat-7.0.65

Using CATALINA_TMPDIR:/home/apache-tomcat-7.0.65/temp

Using JRE_HOME:/usr

Using CLASSPATH:/home/apache-tomcat-7.0.65/bin/bootstrap. jar:/home/apache-tomcat-7.0.65/bin/tomcat-juli.jar

Tomcat started.

Because the default tomcat port is 8080, port 80 is set here, you need to modify the default port to 80, enter the conf directory of the tomcat installation directory, and use vi to open the server. xml file.

ConnectionTimeout = "20000"

RedirectPort = "8443" type = "regxph" text = "yourobjectname"/>

Change

ConnectionTimeout = "20000"

RedirectPort = "8443" type = "regxph" text = "yourobjectname"/>

Then, in this docker container, visit http: // localhost to visit the tomcat home page, indicating that tomcat is successfully installed and configured.

6. Go to the webapps/scripts file of tomcat, write "hello this is 172.17.0.2" in the file, and access http: // 172.17.0.2/hello.html On the host. The content is as follows:

7. As shown in the preceding steps, configure another container, but the ing port set during container startup is different. The command is as follows:

Sudo docker run-t-I-p 3322: 22-p 3380: 80 87e5b6b3ccc1/bin/bash

Finally, enter "hello this is 170.17.0.3" in the webapps/scripts file in the tomcat installation directory of this container, and then access http: // 172.17.0.3/hello.html on the host, the following content is displayed:

8. After the container configuration is complete, the rest of the work is to configure the Nginx of the host machine for load balancing.

Go to the/etc/nginx directory, use vim to edit nginx. conf, and add the following content to the http node:

Server {

Listen 80;

Server_name 192.168.1.106;

Location /{

Proxy_pass http: // blance;

}

}

 

Upstream blance {

Server localhost: 3280 weight = 5;

Server localhost: 3380 weight = 5;

}

First, define an upstream, set the port ing and weight of the web server, and then define a server to listen to port 80. server_name is 192.168.1.106, which is the IP address of the host (which can be configured as a domain name ), "location/" means to listen to all requests under port 80 and use the upstream set above as the proxy.

9. after the above configuration is completed and saved, visit http: // 192.168.1.106/hello.html in the browser and refresh the page. Sometimes the page displays "hello this is 172.17.0.3 ", sometimes "hello this is 172.17.0.2" is displayed, indicating that the configuration is successful. this completes the configuration of a Simple Server Load balancer environment.

For more Nginx tutorials, see the following content::

CentOS 6.2 Deployment Nginx + MySQL + PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

Build a WEB server http://www.linuxidc.com/Linux/2013-09/89768.htm with Nginx

Build a Web server http://www.linuxidc.com/Linux/2013-09/89692.htm Based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Nginx Performance Tuning http://www.linuxidc.com/Linux/2013-09/89656.htm in CentOS 6.3

Configure Nginx to load ngx_pagespeed module http://www.linuxidc.com/Linux/2013-09/89657.htm under CentOS 6.3

CentOS 6.4 install and configure Nginx + Pcre + php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx installation configuration use notes http://www.linuxidc.com/Linux/2014-07/104499.htm

3 Virtual Machine Load Balancing http://www.linuxidc.com/Linux/2015-12/125875.htm with Nginx in CentOS Environment

Nginx details: Click here
Nginx: Click here

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-12/125907.htm

Related Article

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.