Using Docker and weave to build Nginx reverse proxy under Linux system

Source: Internet
Author: User
Tags nginx reverse proxy docker run

Hi, today we will learn how to use Weave and Docker to build an Nginx reverse proxy/Load balancer server. Weave can create a virtual network that connects Docker containers to each other, enabling cross-host deployment and Autodiscover. It allows us to focus more on the development of the application rather than on the infrastructure. Weave provides such a great environment as if all its containers belong to the same network and do not require configuration of ports/mappings/connections etc. Applications in containers provide services that can easily be accessed by the external world in the 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 and reverse proxy a simple PHP application in a Docker container running on multiple nodes within Amazon Web Services. Here we will introduce Weavedns, which provides a simple way for the container to take advantage of the host name without changing the code, and allows 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 is to use Weave to configure Nginx in a Docker container running on Ubuntu as a load balancer server.

Docker's Weave tool
What is weave? Weave creates a virtual network to connect the Docker containers deployed on multiple machines.

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

Applications use this network as if all the containers are under the same switch network, do not need to configure port mappings, connections, etc., the services in the container can be accessed by the external world in the Weaver network, regardless of where your container is running. Similarly, existing system applications can be exposed to applications in the container without worrying about where the internal application is running.

Weave can penetrate firewalls, traffic is encrypted, allow hosts to connect through an untrusted network, use 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, we're going to deploy one Docker app of the same type on both hosts.

Above the HOST1: Start weave

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

#启动一个容器, a ip,weave run called Docker run-d is set at the command line, so we can use this method to start a container, in the same way that there is a weave start command, which is to invoke the Docker start command to start a container that already exists, If we have more than one container on the HOST1 to deploy, proceed to the second row of commands, as long as the IP of the container set does not conflict, the same network segment IP can be used everywhere
ssh=$ (Weave run 10.1.1.1/24-t-i ubuntu)

Above the HOST2: Start weave


#这一步有点不一样, we told Weave on HOST2 that he had a colleague on the HOST1, you can specify an IP or hostname, and you can specify a port. If you have a firewall directly in HOST1 and HOST2, make sure that the TCP/UDP port 6783 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 to tell them that this is no problem, weave will automatically connect, after their service starts, we can also tell weave to connect multiple peers, you can provide multiple IP addresses, separated by a space.

Above the HOST1:

Docker Attach $ssh

Above the HOST2:

Docker Attach $ssh

Then ping each other, and you'll see that the network is connected.

Here's a look at the specific deployment process
1. Building an AWS instance
First, we need to build a Amzaon Web Service instance so that we can run the Docker container with weave under Ubuntu. We will use the AWS command line to build and configure two instances of AWS EC2. Here, we use the smallest available instance, T1.micro. We need an active Amazon Web Services account to be built and configured using the AWS command line interface. We will first clone the Weave repository on GitHub using the following command under the AWS command line interface.


$ git clone https://github.com/weaveworks/guides
$ CD Weave-gs/aws-nginx-ubuntu-simple

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

$ sudo./demo-aws-setup.sh

Here, we will use the IP addresses of these instances in the future. These addresses are stored in a weavedemo.env file, which is created during the execution of the demo-aws-setup.sh script. In order to obtain these IP addresses, we need to execute the following command, which outputs a message similar to the following.


$ 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 execute the following command under Bash to make the environment variable effective.

. ./weavedemo.env

2. Start Weave and Weavedns
After installing the instance, we will start weave and weavedns on each host. Weave and Weavedns make it easy to deploy containers into a completely new infrastructure and configuration without changing the code or understanding concepts like the Ambassador container and the Link mechanism. Here are the commands to start weave and Weavedns on the first host.


Ssh-i Weavedemo-key.pem [email protected] $WEAVE _aws_demo_host1
$ sudo weave launch
$ sudo weave launch-dns 10.2.1.1/24

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


Ssh-i Weavedemo-key.pem [email protected] $WEAVE _aws_demo_host2
$ sudo weave launch $WEAVE _aws_demo_host1
$ sudo weave launch-dns 10.2.1.2/24

3. Launch the Application container
We are now ready to launch six containers across two hosts, both of which run simple PHP sites with Apache2 Web service instances. In order to run three containers on the first Apache2 Web server instance, we will use the following command.


Ssh-i Weavedemo-key.pem [email protected] $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 launch another three containers on the second instance, using the following command.


Ssh-i Weavedemo-key.pem [email protected] $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 parse the hostname, and H-x.weave.local enables Weavedns to parse the host.

4. Start the Nginx container
After the application container runs as expected, we will launch the Nginx container, which will poll and provide reverse proxy or load balancing between the six application container services. To start the Nginx container, use the following command.


Ssh-i Weavedemo-key.pem [email protected] $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 exposes itself to an HTTP server on the $WEAVEAWSDEMO _host1.

5. Test the Load Balancer server
To test whether our Load Balancer server can work, we execute a script that sends HTTP requests to the Nginx container. We will send 6 requests so that we can see Nginx serving each Web server in a single poll.


$./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 managed to configure Nginx as a reverse proxy/load balancer server by using weave and Docker in the Ubuntu server running in AWS (Amazon Web Service) EC2. From the above step output you can see clearly that we have successfully configured Nginx. We can see that the request was sent to 6 application containers in a single poll, which ran PHP applications on the Apache2 WEB server. Here, we deployed a containerized PHP application, using nginx across multiple hosts on AWS EC2 without changing the code, using Weavedns to make each container join together, only the hostname is enough, the immediate convenience, thanks to weave and weave Dns.

Using Docker and weave to build Nginx reverse proxy under Linux system

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.