Run the ASPDOTNETCOREMVC program on Docker-part4: Load Balancing

Source: Internet
Author: User
Tags docker ps docker run haproxy

in the previous part " Run the ASPDOTNETCOREMVC program on Docker-PART3: Use a separate storage container , we use MySQL containers and volume to realize the true meaning of data storage. The whole structure is very simple, it is a Web container and a database container, is a simple application. Now it is popular to support high concurrency, clustering, or at least a number of Web servers, so often use load balancing technology, such as Haproxy,nginx and so on. In this part, we then use Docker technology to load balance, in fact, the strict or the use of old technology, but packaged in a Docker container only.

Customizing the network configuration

Docker has a default network configuration built in, and the default network configuration is automatically created to be used for network access between containers. In the previous part we used this default network configuration, the MySQL container is accessed through the default network configuration.

Use the following command to view the network configuration created by Docker by default

Docker Network LS

NETWORK ID NAME DRIVER SCOPE
13d5e19c447e Bridge Bridge Local
FB4CE1A7BFCF Host Host Local
ee50b1dc443b None Null Local

After the MySQL container has been created, the network allocated by the system is bridge, and we can see which container the bridge is using with the following command

"Containers": {
"d4e5cf975ad5e3ff11620c02f9b626fa4d0042faab83fa9d0ea86801d2cce452": {
"Name": "MySQL",
"EndpointId": "5fe1c90310938704323ba4cbb7c156a962ca07cd49845d12ea142dff40021212",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},

From the Containers node, you can see the container that is currently configured with the network and the assigned IP address, which can be accessed through this IP.

So the default network configuration seems to suffice, but it has his limitations. First, this IP is allocated by Docker and needs to be manually checked to see which IP address is used. Another important issue is that if you have multiple containers that use the same network, you can't differentiate and manage them based on functionality, especially the load balancing feature that we're going to talk about, which is more flexible with custom network configurations.

Create a custom network configuration

Create Frontend and backend two custom networks with the following commands, and the names can be changed to others.

Docker Network Create Frontend

Docker Network Create backend

Frontend will be used for our website container, backend will be used for MySQL container.

View the networks you have created

Docker Network LS

NETWORK ID NAME DRIVER SCOPE
f18bd38d350f Backend Bridge local
13d5e19c447e Bridge Bridge Local
1318f2d56587 frontend Bridge Local
FB4CE1A7BFCF Host Host Local
ee50b1dc443b None Null Local

Once you have a custom network, you can assign it to a container

To facilitate the demonstration, first delete the previously created container with the following command

Docker Rm-f $ (Docker Ps-aq)

Create a MySQL container and specify to use the backend network

Docker run-p 3306:3306-d--name mysql-v productdata:/var/lib/mysql--network=backend-e mysql_root_password=password-e bind-address=0.0.0.0 mysql:8.0.0

Compared to the previous part, here is just one more-network parameter

A useful feature of using a custom network is that inside Docker you can use the name of the container as the network access address, for example, we created the MySQL container, the container name is MySQL, so actually inside Docker, MySQL can be the host name, Then, inside docker, MySQL resolves to its assigned IP address. In this case, we can directly use the container name in the Docker to configure the network parameters, such as the database host address, next we create the site container will be used.

Create an MVC site container

or using the image SHENBA/ASPDOTNETCOREMVC we defined earlier

Here we create three containers

Docker Create--name app1-e dbhost=mysql-e message= "1st Server"--network backend Shenba/aspdotnetcoremvc

Docker Create--name app2-e dbhost=mysql-e message= "2nd Server"--network backend Shenba/aspdotnetcoremvc

Docker Create--name app3-e dbhost=mysql-e message= "3rd Server"--network backend Shenba/aspdotnetcoremvc

Here the Create command is just a message, the message is an environment variable, as the site content output, to identify the current in which container.

The network parameter is specified in the same way as the MySQL container, which only specifies the need to connect to the net, all three containers need to connect to the backend network to access the MySQL container.

Also, there is no port mapping for the specified host when it is created here.

We need to further connect these three containers to the Frontend network and then access the three containers through the Frontend network.

Connecting to the Frontend network

Docker Network Connect frontend App1

Docker Network Connect frontend APP2

Docker Network Connect frontend APP3

Start these three MVC site containers

Docker start App1 app2 app3

Using the Haproxy container

After starting the three MVC site container, it cannot be accessed by the host because there is no public access to the IP and port, so they can only be accessed by the network within Docker. This is where we typically use load balancing scenarios where the application servers are deployed in the intranet and then externally accessed through the reverse proxy to the internal application server. So next we need a reverse proxy server, which is, of course, a container with a reverse proxy service installed in Docker.

We use the Hapoxy container for load balancing, first creating a haproxy.cfg configuration file on the server where Docker resides (or the host, which uses CentOS)

Defaults
Timeout Connect 5000
Timeout client 50000
Timeout server 50000

Frontend localnodes
Bind *:80
Mode http
Default_backend MVC

Backend MVC
Mode http
Balance Roundrobin
Server MVC1 app1:80
Server MVC2 app2:80
Server MVC3 app3:80

Simply put, the 80 port of the load Balancer server is exposed, and the request for this port is assigned to three different MVC sites.

Next, execute the following command in the same directory as the CFG file

Docker run-d--name loadbalancer--network frontend-v "$ (PWD)/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg"-p 3000:80 haproxy:1.7.0

parameters are the same as creating other containers , provides name (LoadBalancer), Network (frontend), port Mapping (3,000:80), mirror-based (haproxy:1.7.0)

The more specific is the-v parameter, which maps the host's haproxy.cfg to the haproxy.cfg file in the container.

All right, all of the configuration for load balancing has been completed, open the browser input http://192.168.115.136:3000, repeat the refresh many times then you will see the content of the site title will change, as shown in

The current structure diagram is as follows



Run the ASPDOTNETCOREMVC program on Docker-part4: Load Balancing

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.