Docker + Nginx + Tomcat 7 Simple Server Load balancer Configuration

Source: Internet
Author: User

Docker + Nginx + Tomcat 7 Simple Server Load balancer Configuration

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 <br>

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.

<Connector port = "8080" protocol = "HTTP/1.1"

ConnectionTimeout = "20000"

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

Change

<Connector port = "80" protocol = "HTTP/1.1"

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:

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Load Balancing of three virtual machines with Nginx in CentOS Environment

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.