Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker and Weave to build Nginx reverse proxy under Linux system

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Docker and Weave to build a Nginx reverse proxy under the Linux system". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Docker and Weave to build a Nginx reverse proxy under the Linux system".

Hi, today we will learn how to use Weave and Docker to build a reverse proxy / load balancing server for Nginx. 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 require port / mapping / connection configuration. The services provided by the applications in the container can be easily accessed by the outside world in the weave network, no matter where your container runs. In this tutorial we will use weave to quickly and simply deploy a nginx web server as a load balancer, reverse proxying a simple php application running in a docker container on multiple nodes in Amazon Web Services. Here we will introduce WeaveDNS, which provides an easy way for containers to find with hostnames without changing the code, and to allow other containers to connect to each other through hostnames.

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 a docker container running on ubuntu as a load balancing server.

Docker's weave tool

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

Let's take a look at the weave application scenario:

Applications use this network just as all containers are in the same switch network. There is no need to configure port mapping, connection, and so on. The services provided by applications in the container can be accessed by the outside world in the weaver network, no matter where your container runs. Similarly, existing system applications can also be exposed to applications in the container to call without worrying about where the internal applications are running.

Weave can penetrate firewalls. Traffic is encrypted, allowing hosts to connect through an untrusted network. With 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 will deploy the same type of docker application on each of these two hosts.

On HOST1: start weave

The code is as follows:

# this step starts weave routing, which needs to be started on each HOST

Weave launch

# start a container and set an ip,weave run to call docker run-d on the command line, so we can use this method to start a container. In the same way, there is a weave start command, which calls the docker start command to start an existing container. If we have multiple containers to deploy on the HOST1, we can continue to execute the command on the second line, as long as we make sure that the ip set by the container does not conflict. Ip of the same network segment can be used everywhere.

Ssh=$ (weave run 10.1.1.1 shock 24-t-I ubuntu)

On HOST2: start weave

The code is as follows:

# this step is a little different. We tell weave on HOST2 that he has a colleague on HOST1. You can specify ip or hostname, and you can also specify a 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 IP is configured differently

Ssh=$ (weave run 10.1.1.2 shock 24-t-I ubuntu)

We can also tell HOST1 to connect to HOST2, or both, there is no problem, weave will automatically connect, after their service is started, we can also tell weave to connect multiple peers, you can provide multiple ip addresses, separated by spaces.

On HOST1:

The code is as follows:

Docker attach $ssh

On HOST2:

The code is as follows:

Docker attach $ssh

Then the two ping each other and will find that the network is connected.

Let's take a look at the specific deployment process.

1. Set up an AWS instance

First, we need to build an 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 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. Let's first clone the weave repository on github using the following command from the AWS command line interface.

The code is as follows:

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

$cd weave-gs/aws-nginx-ubuntu-simple

After cloning the repository, we execute the following script, which will deploy two t1.micro instances, each with ubuntu as the operating system and running the docker container with weave.

The code is as follows:

$sudo. / demo-aws-setup.sh

Here, we will use the IP addresses of these instances later. 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 outputs information similar to the following.

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 will dynamically assign IP addresses to our instances.

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

The code is as follows:

. . / weavedemo.env

two。 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 to a completely new infrastructure and configuration without changing code or understanding concepts such as Ambassador containers and Link mechanisms. Here are the commands to start weave and weavedns on the first host.

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 Compact 24

Next, I'm also going 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 Universe 24

3. Start the application container

Now we are ready to launch six containers across two hosts, both of which run simple php sites using Apache2 Web service instances. To run three containers in the first Apache2 Web server instance, we will use the following command.

The code is as follows:

Ssh-I weavedemo-key.pem ubuntu@$WEAVE_AWS_DEMO_HOST1

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

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

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

After that, we will start three other containers on the second instance, please use 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.4True 24-h ws4.weave.local fintanr/weave-gs-nginx-apache

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

$sudo weave run-- with-dns 10.3.1.6 Plus 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 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.

The code is as follows:

Ssh-I weavedemo-key.pem ubuntu@$WEAVE_AWS_DEMO_HOST1

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

Therefore, our nginx container is publicly exposed as a http server on $WEAVEAWSDEMO_HOST1.

5. Test the load balancing server

To test whether our load balancing server works, we execute a script that sends http requests to the nginx container. We will send six requests so that we can see nginx serving each web server in a single 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"

}

Concluding remarks

We finally successfully configured nginx as a reverse proxy / load balancing server by using weave and docker in the ubuntu server running in the AWS (Amazon Web Service) EC2. From the output of the steps above, it is clear that we have successfully configured nginx. We can see that the request is sent to six application containers in a poll, which are running PHP applications in the Apache2 Web server. Here, we deploy a containerized PHP application, which uses nginx to span multiple hosts on AWS EC2 without changing the code, and uses weavedns to connect each container together, requiring only the hostname. All this convenience can be attributed to weave and weavedns.

Thank you for your reading, the above is the content of "how to use Docker and Weave to build Nginx reverse proxy under the Linux system". After the study of this article, I believe you have a deeper understanding of how to use Docker and Weave to build Nginx reverse proxy under the Linux system. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report