Use docker and weave to build Nginx reverse proxy under Linux system

Source: Internet
Author: User
Tags http request web services ssh git clone nginx reverse proxy docker run

Hi, today we will learn how to build a Nginx reverse proxy/Load Balancing server using Weave and Docker. Weave can create a virtual network to connect the Docker containers to each other, supporting deployment across hosts and automatic discovery. It allows us to focus more on the development of the application rather than the infrastructure. Weave provides a great environment, as if all of its containers belong to the same network, and no port/map/connection configuration is required. Applications in containers provide services that can be easily accessed by the outside world in a weave network, regardless of where your container is running. In this tutorial we will use weave to quickly and simply deploy the Nginx Web server as a load balancer, reverse proxy a simple PHP application running in a Docker container on multiple nodes in Amazon Web Services. Here we will introduce Weavedns, which provides a simple way for the container to find the host name without changing the code, and enables other containers to connect to each other through the host name.

In this tutorial, we will use Nginx to assign load balancing to a collection of containers running Apache. The simplest and easiest way to do this is to use Weave to configure the Nginx in the Docker container running on Ubuntu as a load-balancing server.

The Weave tool of Docker

What is weave? Weave created a virtual network to connect Docker containers that are deployed on multiple machines.

Here's a look at Weave's application scenario:

Application in the use of the network as all containers are under the same switch network, do not need to configure port mapping, connectivity and so on, the application in the container services provided in the Weaver network can be accessed by the outside world, regardless of where your container runs. Similarly, existing system applications can also be exposed to applications in the container, without worrying about where the internal application is running.

Weave can penetrate firewalls, traffic is encrypted, allowing the host to connect through an untrusted network, using weave you can easily deploy multiple containers to run in different places

If you have a Docker application running on two different hosts HOST1 and HOST2, that is, we're going to deploy one of the same types of Docker applications on both hosts.

Above the HOST1: Start weave

The code is as follows:

#这一步先启动weave路由, it needs to be started on every host

Weave Launch

#启动一个容器, a ip,weave run call Docker run-d is set at the command line, so we can start a container using this method, and there is the Weave start command, which invokes the Docker start command to start the existing container. If we have more than one container on the HOST1 to deploy, proceed to the second line, as long as the IP set by the container does not conflict, the IP of the same network segment can be used everywhere

ssh=$ (Weave run 10.1.1.1/24-t-I. Ubuntu)

Above the HOST2: Start weave

The code is as follows:

#这一步有点不一样, we told Weave on the HOST2 that he had a peer on HOST1, you can specify an IP or host name, and you can specify a port. If you have a firewall directly in HOST1 and HOST2, make sure that the TCP/UDP 6783 port is open

Weave Launch $HOST 1

#和第一步中不同的地方在于, the configured IP is not the same

ssh=$ (Weave run 10.1.1.2/24-t-I. Ubuntu)

We can also tell HOST1 to connect HOST2, or both tell them that there is no problem, weave will automatically connect, after their service startup, we can also tell weave to connect multiple peers, you can provide multiple IP addresses, separated by a space.

Above the HOST1:

The code is as follows:

Docker Attach $ssh

Above the HOST2:

The code is as follows:

Docker Attach $ssh

And then ping each other, and you'll find that the network is going through.

Let's look at the specific deployment process

1. Build AWS Example

First, we need to build a Amzaon Web Service instance so that we can run Docker containers with weave under Ubuntu. We will use the AWS command line to build and configure two AWS EC2 instances. Here, we use the smallest available instance, T1.micro. We need a valid Amazon Web Services account to build and configure using the AWS command line interface. We first use the following command under the AWS command line interface to clone the Weave warehouse on the GitHub.

The code is as follows:

$ git clone https://github.com/weaveworks/guides

$ CD Weave-gs/aws-nginx-ubuntu-simple

After the warehouse is cloned, we execute the following script, which will deploy two T1.micro instances, each of which is Ubuntu as an operating system and weave running Docker container.

Copy Code

The code is as follows:

$ sudo./demo-aws-setup.sh

Here we will use the IP addresses of these instances at a later time. These addresses are stored in a weavedemo.env file that is created during the execution of the demo-aws-setup.sh script. To get these IP addresses, we need to execute the following command, which commands output similar to the following information.

The code is as follows:

$ cat Weavedemo.env

Export weave_aws_demo_host1=52.26.175.175

Export weave_aws_demo_host2=52.26.83.141

Export weave_aws_demo_hostcount=2

Export weave_aws_demo_hosts= (52.26.175.175 52.26.83.141)

Note that these are not fixed IP addresses and AWS dynamically assigns IP addresses to our instances.

We're under Bash. Execute the following command to make the environment variable effective.

The code is as follows:

. ./weavedemo.env

2. Start Weave and Weavedns

After installing the instance, we will start weave and weavedns on each host. Weave and Weavedns enable us to easily deploy containers into a completely new infrastructure and configuration without changing the code or understanding concepts like the Ambassador container and the Link mechanism. The following is the command to start weave and Weavedns on the first host computer.

The code is as follows:

Ssh-i Weavedemo-key.pem ubuntu@ $WEAVE _aws_demo_host1

$ sudo weave launch

$ sudo weave launch-dns 10.2.1.1/24

Next, I'm also ready to start weave and Weavedns on the second host.

The code is as follows:

Ssh-i Weavedemo-key.pem ubuntu@ $WEAVE _aws_demo_host2

$ sudo weave launch $WEAVE _aws_demo_host1

$ sudo weave launch-dns 10.2.1.2/24

3. Start the application container

We are now ready to start six containers across two hosts, both of which run simple PHP sites with Apache2 Web service instances. To run three containers on the first Apache2 Web server instance, we will use the following command.

Copy Code

The code is as follows:

Ssh-i Weavedemo-key.pem ubuntu@ $WEAVE _aws_demo_host1

$ sudo weave run--with-dns 10.3.1.1/24-h ws1.weave.local Fintanr/weave-gs-nginx-apache

$ sudo weave run--with-dns 10.3.1.2/24-h ws2.weave.local Fintanr/weave-gs-nginx-apache

$ sudo weave run--with-dns 10.3.1.3/24-h ws3.weave.local Fintanr/weave-gs-nginx-apache

After that, we will start another three containers on the second instance, using the following command.

The code is as follows:

Ssh-i Weavedemo-key.pem ubuntu@ $WEAVE _aws_demo_host2

$ sudo weave run--with-dns 10.3.1.4/24-h ws4.weave.local Fintanr/weave-gs-nginx-apache

$ sudo weave run--with-dns 10.3.1.5/24-h ws5.weave.local Fintanr/weave-gs-nginx-apache

$ sudo weave run--with-dns 10.3.1.6/24-h ws6.weave.local Fintanr/weave-gs-nginx-apache

Note: Here, the--with-dns option tells the container to use Weavedns to resolve the hostname, and-H x.weave.local enables Weavedns to resolve the host.

4. Start Nginx Container

After the application container is run as expected, we will start the Nginx container, which will poll and provide a reverse proxy or load balance between the six application container services. To start the Nginx container, use the following command.

Copy Code

The code is as follows:

Ssh-i Weavedemo-key.pem ubuntu@ $WEAVE _aws_demo_host1

$ sudo weave run--with-dns 10.3.1.7/24-ti-h nginx.weave.local-d-P 80:80 fintanr/weave-gs-nginx-simple

As a result, our nginx container is publicly exposed to an HTTP server on the $WEAVEAWSDEMO _host1.

5. Test Load Balancing Server

To test whether our load-balancing server can work, we execute a script that sends an HTTP request to the Nginx container. We will send 6 requests so that we can see Nginx serving each Web server in one poll.

The code is as follows:

$./access-aws-hosts.sh

{

"Message": "Hello Weave-nginx Example",

"hostname": "Ws1.weave.local",

"Date": "2015-06-26 12:24:23"

}

{

"Message": "Hello Weave-nginx Example",

"hostname": "Ws2.weave.local",

"Date": "2015-06-26 12:24:23"

}

{

"Message": "Hello Weave-nginx Example",

"hostname": "Ws3.weave.local",

"Date": "2015-06-26 12:24:23"

}

{

"Message": "Hello Weave-nginx Example",

"hostname": "Ws4.weave.local",

"Date": "2015-06-26 12:24:23"

}

{

"Message": "Hello Weave-nginx Example",

"hostname": "Ws5.weave.local",

"Date": "2015-06-26 12:24:23"

}

{

"Message": "Hello Weave-nginx Example",

"hostname": "Ws6.weave.local",

"Date": "2015-06-26 12:24:23"

}

Conclusion

We finally succeeded in configuring Nginx as a reverse proxy/load Balancing server, using weave and Docker in an Ubuntu server running in the AWS (Amazon Web Service) EC2. From the above steps the output can be clearly seen that we have successfully configured the Nginx. We can see that the request was sent to 6 application containers in a poll that ran PHP applications in the Apache2 Web server. Here, we deployed a container of PHP applications, using nginx across multiple hosts on the AWS EC2 without changing the code, using Weavedns to make each container connected together, only the hostname is required, and the immediate convenience is due to weave and weave Dns.

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.