Consul what is consulConsul is a high performance service registration/service Health Check component
Consul installationWe 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 consulWith 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 BackendAfter 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 ServicesAfter 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 exampleNow 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 checkOnce 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 instancesA. 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 templateConsul 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 TemplateWe can download it from the official website (https://releases.hashicorp.com/consul-template) Consul-template.exe
Start Consul Templateconsul-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 caseNow 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