Docker + nginx + Tomcat 7 Simple Server Load balancer Configuration

Source: Internet
Author: User
Tags docker run

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 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 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. Configuration step 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. After sudo apt-get install nginx is installed, you can check whether Nginx is installed. 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), run the following command to start the Nginx service: # start the service $ sudo service nginx start # view the service status $ sudo service nginx status * nginx is running # It indicates that the service is in the startup status and accesses http: // localhost in the browser, the result is as follows: 3. download the Docker image and run the docker pull command. For more information about the image operations, see 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 above command is Set port 3222 of the host machine to map to port 22 of the container, and set port 3280 of the host machine to port 80 of the container. 5. Install jre and tomcat7.0 in the container and install the jre wget-O jre-7u6-linux-x64.rpm? BundleId = 67387 yum install jre-7u6-linux-x64.rpm check if jre is successfully installed 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) install 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, enter the extract directory, cd to the bin directory, execute the command and enter the following information, tomcat started successfully bash Startup. shUsing 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. 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. in the xml file, configure <Connector Port = "8080" protocol = "HTTP/1.1" connectionTimeout = "20000" redirectPort = "8443"/> to <Connector port = "80" protocol = "HTTP/1.1" after connectionTimeout = "20000" redirectPort = "8443"/>, 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. the preceding steps are the same. to configure another container, only 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 the content entered in the webapps/scripts file in the tomcat installation directory of this container is "hello this is 170.17.0.3", and then access http on the host: // 172.17.0.3/ Hello.html: 8. After the container is configured, configure the Nginx of the host machine for load balancing. Go to the/etc/nginx directory and use vim to edit nginx. conf: 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 machine (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.

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.