Use Nginx, Consul, Consul template to build load balancing and service discovery services in Windows environments

Source: Internet
Author: User
Tags nginx server what is nginx

The purpose of building load balancing and service discovery Services

As the business of the website continues to improve, the performance of individual servers becomes increasingly difficult to meet the customer's business needs, so in many cases, multiple server instances and load balancers are needed to meet business needs.

Nginx What is Nginx

Nginx is a high-performance Web server that can be used either alone or in combination with other Web servers as a load balancer.

Nginx Installation

We can download the latest version of Windows compressed package from the Nginx website (http://nginx.org/).
The directory structure of the compressed package after decompression is as follows:

Configuration of Nginx
events {    worker_connections 1024;}http {    upstream backend    {        server 127.0.0.1:91 weight=1;        server 127.0.0.1:92 weight=1;    }    server     {        location / {            proxy_pass http://backend;        }    }}

The upstream section defines load balancing for HTTP requests. When the user sends the request to Http://backend, Nginx forwards the request to one of the specified server IP lists. The weight field in the configuration sets the weight of the specified server IP, and the higher the weight, the higher the likelihood of forwarding.

Problems

Nginx will occupy 80 port by default, this has conflict with IIS, so we need to modify nginx default port in nginx.conf.

events {    worker_connections 1024;}http {    upstream backend    {        server 127.0.0.1:92 weight=1;        server 127.0.0.1:93 weight=1;    }    server     {        listen 81;        server_name localhost;        location / {            proxy_pass http://backend;        }    }}
Instance

The current Web site has 2 instances deployed in IIS, SiteA IP 127.0.0.1:92, siteB IP 127.0.0.1:93.

SiteA and SiteB have only one index.html page.
The code is as follows:

SiteA

<!DOCTYPE html>

SiteB

<!DOCTYPE html>

The Nginx server uses port 81. The effect we want to achieve is that when the user accesses http://127.0.0.1:81, Nginx randomly forwards the request to the http://127.0.0.1:92 or http://127.0.0.1:93

The index.html page effect of the SiteA website is as follows

The index.html page effect of the SITEB website is as follows

Implementation steps

A. First we modify the Nginx configuration file

events { worker_connections 1024;}http { upstream 127.0.0.1 { server 127.0.0.1:91 weight=1; server 127.0.0.1:92 weight=1; } server { listen 81; server_name localhost; location / { proxy_pass http://127.0.0.1; } }}

B. Start nginx at the command line

C. Open the browser, enter http://127.0.0.1:81, repeatedly press F5 refresh, the site content between SiteA and SiteB switch, indicating that load balancing has been successfully enabled.

Consul what is consul

Consul is a high performance service registration/service Health Check component

Consul installation

We can get a version of the 32-bit or 64-bit program from the official website (https://www.consul.io/downloads.html). After the download is an executable file Consul.exe

How to start consul

With the command Consul Agent-dev, the consul is started in development mode ( development mode does not enable persistence, and all configurations are lost after the program is closed )

Consul Service Backend

After the consul service is started, we can access, http://localhost:8500 to access the consul management background, the interface is as follows.

In this admin background, you can view all the services and all host nodes registered by Consul.

Registration Services

After the consul service is started, we can invoke some of the appropriate APIs to register the service (related API list https://www.consul.io/api/index.html)

Continue with our example

Now we are trying to register the previous SiteA, SiteB, in consul.

A. First here we go to download a Curl program (https://curl.haxx.se/) to invoke the API (this can be replaced with postman)

B. Create 2 JSON files Sitea.json, Siteb.json. The contents are as follows

Sitea.json

{    "ID":"webA",    "Name":"web",    "Tags":[],    "Address":"127.0.0.1",    "Port":92,    "EnableTagOverride":false}

Siteb.json

{    "ID":"webB",    "Name":"web",    "Tags":[],    "Address":"127.0.0.1",    "Port":93,    "EnableTagOverride":false}

Here, name is the service name, and the ID is an instance ID of the service.

C. Use curl to invoke the registration service API to register SiteA and SiteB in consul
curl --request PUT --data @siteA.json http://localhost:8500/v1/agent/service/register
curl --request PUT --data @siteB.json http://localhost:8500/v1/agent/service/register

D. Go back to the Consul administration page and we'll find a service named Web that appears in the list of services with 2 instances

If you see the results above, it means that 2 Web service instance registrations were successful.

Add a service health check

Once the service is registered with Consul, the first benefit is that each service instance can be checked for health by consul.

Continue to modify our instances

A. Turn off the previously opened consul service, open a new command-line window, and use consul Agent-dev to reopen a new consul service

B. In the SiteA and SiteB Web site Directory, add a healthcheck.html file, respectively, with the following contents

C. Modification of previous Sitea.json and Siteb.json
Sitea.json

{ "ID":"webA", "Name":"web", "Tags":[], "Address":"127.0.0.1", "Port":92, "Checks":[{ "http":"http://127.0.0.1:92/healthcheck.html", "interval":"5s" }], "EnableTagOverride":false}

Siteb.json

{ "ID":"webB", "Name":"web", "Tags":[], "Address":"127.0.0.1", "Port":93, "Checks":[{ "http":"http://127.0.0.1:93/healthcheck.html", "interval":"5s" }], "EnableTagOverride":false}

D. Re-use Curl to invoke the registration service API to register SiteA and SiteB in consul

E. Open the SiteA directory and rename healthcheck.html to healthcheck1.html (this operation is equivalent to simulating the server crash of SiteA, consul request less than healthcheck.html file)

F. Back to the Consul management interface, we'll look at the results and it will show that the Weba check failed

G. Renaming healthcheck1.html will healthcheck.html, wait 5 seconds, return to Consul interface, Weba has changed back to detect through the state.

Consul template what is Consul template

Consul template is an extension component of Consul that can read the service configuration in Consul and generate different profiles based on the specified template.

So here we can use a combination of Consul, Consul template to dynamically generate Nginx configuration files, and automatically restart Nginx.

Installing the consul Template

We can download it from the official website (https://releases.hashicorp.com/consul-template) Consul-template.exe

Start Consul Template

consul-template -consul-addr 127.0.0.1:8500 -template "./2.tpl:./conf/nginx.conf:nginx -s reload"

The -consul-addr parameter here is the address of the specified consul service, which -template is the template used by the specified consul templates, the new file address generated, and the command to execute after the new file is generated.

Continue with our case

Now we add a new Sitec, IP 127.0.0.1:94, Sitec only index.html and HEALTHCHECK.HTML2 files

A. Create 2.TPL, the file content is as follows, here is the template syntax of consul-template (corresponding syntax can be seen https://github.com/hashicorp/consul-template)

events {    worker_connections 1024;}http {   upstream 127.0.0.1{        {{ range service "web" }}    server {{ .Address }}:{{ .Port }} weight=1;{{ end }}     }    server {        listen       81;        server_name  localhost;        location / {           proxy_pass http://127.0.0.1;        }    }}

B. Start consul-template with a command consul-template -consul-addr 127.0.0.1:8500 -template "2.tpl:conf/nginx.conf:nginx -s reload"

C. Create Sitec.json, use curl to register SiteC to consul, Sitec.json content as follows

{    "ID":"webC",    "Name":"web",    "Tags":[],    "Address":"127.0.0.1",    "Port":94,    "Checks":[{        "http":"http://127.0.0.1:94/healthcheck.html",        "interval":"5s"    }],    "EnableTagOverride":false}

D. Open the browser, enter http://localhost:81, repeatedly press F5 refresh, you will find that Sitec has been added to the Nginx load balancer configuration. D

E. Then we perform a test, and we rename the SiteA healthcheck.html to healthcheck1.html

F. Go back to the page just continue to press F5 Refresh, we will find that only the contents of SiteB and Sitec are shown now, this is because SiteA has been consul identified as unhealthy services, so consul-template to regenerate nginx configuration file, Erase the config file in the SiteA and reboot the Nginx.

Use Nginx, Consul, Consul template to build load balancing and service discovery services in Windows environments

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.