Using docker and weave to build nginx reverse proxy in Linux system

Time:2021-6-16

Hi, today we will learn how to use weave and docker to build nginx‘s reverse proxy / load balancing server. Weave can create a virtual network to connect docker containers to each other, supporting cross host deployment and automatic discovery. It allows us to focus more on application development than on infrastructure. Weave provides such a great environment as if all its containers belong to the same network and do not need port / mapping / connection configuration. The services provided by the application in the container can be easily accessed by the outside world in the web network, no matter where your container is running. In this tutorial, we will use web to quickly and simply deploy nginx web server as a load balancer and reverse proxy a simple PHP application running in the docker container of multiple nodes in Amazon Web services. Here we will introduce weavedns, which provides a simple way for containers to find by using 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 distribute load balancing to a collection of containers running Apache. The easiest way is to use weave to configure nginx in the docker container running on Ubuntu as a load balancing server.

Web tool of docker
What is a weave? Weave creates a virtual network to connect docker containers deployed on multiple machines.

Let’s take a look at the application scenarios of weave


When an application uses the network, it’s like all containers are in the same switch network. It doesn’t need to configure port mapping, connection and so on. The services provided by the application in the container can be accessed by the outside world in the weaver network, no matter where your container is running. Similarly, the existing system applications can also be exposed to the application in the container to call without worrying about the running position of the internal application.


Weave can penetrate the firewall, the 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, we will deploy a docker application of the same type on each of the two hosts.

On host1: start weave

Copy code

The code is as follows:

#In this step, we need to start the weave route on each host
weave launch

#To start a container, an IP is set on the command line. Weave run calls docker run – D, so we can use this method to start a container. Similarly, there is weave start command, which calls docker start command to start an existing container. If we have multiple containers to deploy on host1, we can continue to execute the second line of command, 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)

On host2: start weave

Copy code

The code is as follows:

#This step is a bit different. We tell weave on host2 that he has a peer on host1. We can specify IP or host name and port. If there is a firewall directly in host1 and host2, make sure that port 6783 of TCP / UDP is opened
weave launch $HOST1

#The difference from the first step is that the configured IP is different
ssh=$(weave run 10.1.1.2/24 -t -i ubuntu)

We can also tell host1 to connect to host2, or both. There is no problem. Weave will connect automatically. After their service is started, we can also tell weave to connect to multiple peers. You can provide multiple IP addresses, separated by spaces.

On host1:

Copy code

The code is as follows:

docker attach $ssh

On host2:

Copy code

The code is as follows:

docker attach $ssh

Then the two Ping each other, you will find that the network is connected

Let’s take a look at the specific deployment process
1. Build an example of AWS
First of all, we need to build an instance of amzaon web service so that we can run the docker container 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 an effective Amazon Web services account to build and configure using the AWS command line interface. First, we clone the web repository on GitHub with the following command in the AWS command line interface.

Copy code

The code is as follows:

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

After cloning the warehouse, we execute the following script. This script will deploy two T1. Micro instances. In each instance, Ubuntu is used as the operating system and weave is used to run the docker container.

Copy code

The code is as follows:

$ 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 created during the demo-aws-setup.sh script. In order to obtain these IP addresses, we need to execute the following command, which outputs information similar to the following.

Copy code

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)

Please note that these are not fixed IP addresses. AWS will dynamically assign IP addresses to our instances.

We execute the following command under bash to make the environment variable take effect.

Copy code

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 make it easy for us to deploy containers into a new infrastructure and configuration without changing the code or understanding concepts like the ambassador container and link mechanism. Here are the commands to start weave and weavedns on the first host.

Copy code

The code is as follows:

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 going to start weave and weavedns on the second host.

Copy code

The code is as follows:

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. Start the application container
Now, we are ready to start six containers across two hosts, both of which run simple PHP websites with Apache 2 web service instances. To run three containers in the first instance of apache2 web server, we will use the following command.

Copy code

The code is as follows:

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 start the other three containers on the second instance. Please use the following command.

Copy code

The code is as follows:

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 resolve the host name, and – H x.weave.local enables weavedns to resolve the host.

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

Copy code

The code is as follows:

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

Therefore, our nginx container is in $weaveawsdemo_ Host1 is publicly exposed as an HTTP server.

5. Test the load balancing server
To test whether our load balancing server works, we execute a script that can send HTTP requests to the nginx container. We will send six requests so that we can see nginx serving each web server in one round of polling.

Copy code

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”
}

Concluding remarks
Finally, we successfully configured nginx as a reverse proxy / load balancing server by using weave and docker in Ubuntu server running in AWS (Amazon Web Service) EC2. From the above step output, we can clearly see that we have successfully configured nginx. We can see that requests are sent to six application containers in a single poll, which run PHP applications in the Apache 2 web server. Here, we have deployed a container PHP application, using nginx to span multiple hosts on AWS EC2 without changing the code. Using weavedns to connect each container together, we only need the host name. We owe all the convenience to weave and weavedns.