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.
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.
brother Lian it education original linux OPS engineer / In detail linux tutorials, more information on the official website customer service: http://www.lampbrother.net/linux/
or hooking up with Q2430675018.
Welcome to the Linux Communication Group 478068715
How to use weave and Docker to build Nginx reverse proxy/Load Balancer server