DOCKER+NGINX+TOMCAT7 Configuration Simple Load Balancing

Source: Internet
Author: User

This article describes the configuration of simple load balancing on Docker, the host for Ubuntu 14.04.2 LTS, two CentOS containers, the host installation Nginx, two containers installed TOMCAT7. The structure is as follows:

The idea of this scenario is to map the port of the host and the port of the Docker container (that is, a port that accesses the host is mapped to the port of the Docker container), and then the host is configured with Nginx to reach a port on the host, which is assigned to the specified service address according to the rules. Load balancing is completed.

Configuration steps

1. Prepare the host, the host is Ubuntu 14.04.2 LTS, installed in VMware, specific installation methods do not repeat.

2. Install Nginx in the host, directly with the following command.

sudo apt-get install Nginx

After the installation is complete, you can see if Nginx installation is complete, you can use the following command to view the version, print out the Nginx version, the installation is successful.

$ nginx-v
Nginx version:nginx/1.4.6 (Ubuntu)

After that, you can start the Nginx service with the following command:

#启动服务 $ sudo service nginx start #查看服务状态 $ sudo service nginx status* nginx is running   #说明服务是启动状态

Accessing the http://localhost in the browser displays the following results:

3. Download the Docker image, use the Docker pull command, and see this article for specific image operations

4. Start a container and set the port mappings, with one of the following commands:

sudo docker run-t-i-p 3222:22-p 3280:80 87e5b6b3ccc1/bin/bash

The above command is to start a container in the standard bash output, set the port mapping, with the-p parameter,-p host Port: Docker container port, the above command is to set the host's 3222 port mapped to the container's 22 port, set the host's 3280 port map to the container 80 port.

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

Wget-o jre-7u6-linux-x64.rpm Http://javadl.sun.com/webapps/download/AutoDL? Bundleid=67387yum Install jre-7u6-linux-x64.rpm

To see if the JRE was installed successfully

Java-versionjava 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)

Installing tomcat7.0

wget HTTP://APACHE.FAYEA.COM/TOMCAT/TOMCAT-7/V7.0.65/BIN/APACHE-TOMCAT-7.0.65.TAR.GZTAR-ZXVF  Apache-tomcat-7.0.65.tar.gz

Start Tomcat, go to the Extract directory, CD to Bin directory, execute the command and enter the following information to indicate that Tomcat started successfully

Bash startup.sh Using catalina_base:   /home/apache-tomcat-7.0.65using catalina_home:   /home/ Apache-tomcat-7.0.65using catalina_tmpdir:/home/apache-tomcat-7.0.65/tempusing JRE_HOME:        /usrUsing CLASSPATH:       /home/apache-tomcat-7.0.65/bin/bootstrap.jar:/home/apache-tomcat-7.0.65/bin/tomcat-juli.jartomcat started.

Since the tomcat default port is 8080, this is set to 80 port, so you need to modify the default port to 80, enter the Conf directory of the Tomcat installation directory, use VI to open the Server.xml file, will be configured as follows

<connector port= "8080" protocol= "http/1.1"               connectiontimeout= "20000"               redirectport= "8443"/>

Revision changed to

<connector port= "protocol=" http/1.1 "               connectiontimeout=" 20000 "               redirectport=" 8443 "/>

Later, in this Docker container, access to the http://localhost can be accessed to the Tomcat home page, stating that Tomcat was installed and configured successfully.

6. Enter Tomcat's Webapps/root directory, as this container has an IP of 172.17.0.2, create a hello.html file, and write the following in the file "Hello this is 172.17.0.2", The host then accesses the http://172.17.0.2/hello.html and displays the following:

7. In accordance with the above steps, configure another container, but the container starts with a different mapping port set, the command is as follows:

sudo docker run-t-i-p 3322:22-p 3380:80 87e5b6b3ccc1/bin/bash

Finally, create the hello.html file in the Webapps/root directory of this container tomcat installation directory, because this container has an IP of 172.17.0.3, so the contents of the hello.html file are "Hello this is 170.17.0.3 ", after the host accesses the http://172.17.0.3/hello.html, the following is displayed:

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

Enter the/etc/nginx directory and use vim to edit the nginx.conf, adding the following to the HTTP node:

server {                listen;                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 a upstream, set the Web server's port mappings and weights, then define a server, listen on port 80, server_name 192.168.1.106, which is the host's IP address (configurable as a domain name), "Location/" This means listening for all requests under port 80 and using the upstream set above as the proxy.

9. After completing the above configuration and saving, accessing http://192.168.1.106/hello.html in the browser and refreshing the page, you will find that the page sometimes displays "Hello this is 172.17.0.3" and sometimes "Hello this is 172.17.0.2 ", stating that the configuration was successful so that a simple configuration of the load-balanced environment was completed.

DOCKER+NGINX+TOMCAT7 Configuration Simple Load Balancing

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.