1. Introduction
As you know, services are routinely deployed as follows:
Exposed services are deployed in front of nginx to provide reverse proxy and load balancing capabilities
The following describes how to quickly deploy a set of similar services, analyze existing problems and provide corresponding solutions
2. Application related
2.1 Starting the Service
Run boot-cloud-OpenFeign-Provider to start three service instances with ports 8081, 8082, and 8083 respectively
2.2 Service Verification
In your browser, type: http://localhost:8081/index/nginx, http://localhost:8082/index/nginx, http://localhost:8083/index/nginx, to guarantee the three service instance can be accessed
The service runs on Consul in 5.1 and 5.2
3. The nginx
3.1 installation
3.1.1 Searching for mirrors
docker search nginx
Copy the code
3.1.2 Pulling a Mirror
docker pull nginx
Copy the code
3.1.3 Running an Image
docker run --name nginx -p 80:80 -d nginx
Copy the code
3.1.4 Copying an Image Configuration File
Docker cp nginx: / etc/nginx/nginx. Conf path/nginx put configuration file path] [hostingCopy the code
3.1.5 Deleting a Container
docker rm -f nginx
Copy the code
3.1.6 Running an image using a Configuration file
Docker run - name nginx - p 80-80 - v/hosting path/nginx/nginx. Conf: / etc/nginx/nginx. Conf: ro - v [hosting path] / nginx/conf. D: / etc/nginx/conf., d - d nginxCopy the code
3.2 configuration
3.2.1 Configuring load Balancing
Create a load-balancer.conf file in the [host path]/nginx/conf.d directory as follows:
Upstream Backend {server 10.100.40.243:8081; Server 10.100.40.243:8082; Server 10.100.40.243:8083; } server{ listen 80; location / { proxy_pass http://backend; }}Copy the code
3.3 validation
3.3.1 Restarting the Nginx Service
docker exec -it nginx nginx -s reload
Copy the code
3.3.2 Accessing the Nginx Service
Enter http://localhost/index/nginx in the browser, repeated requests, in turn, can see openfeign service: hello nginx from 8081, openfeign service: Hello nginx from 8082, OpenFeign service: hello nginx from 8083 if the result is displayed, the nginx load balancing configuration is normal.
Problem 4.
As you can see, the load balancing capability of the service is achieved by writing the service list dead in the load-balancer.conf configuration file, which also means that whenever the back-end service list changes, the configuration needs to be modified and reloaded using the nginx -s reload command.
5. Plan
If there is a problem, and there is a solution, the solution in this article is Consul and Consul Template
5.1 installation consul
Install Consul according to the Install Consul official tutorial
5.2 run the consul
consul agent -dev
Copy the code
5.3 Installing Consul Template
5.3.1 Downloading the Compressed Package
Wget HTTP: / / https://releases.hashicorp.com/consul-template/0.20.0/consul-template_0.20.0_linux_amd64.zipCopy the code
5.3.2 decompression
Unzip the consul - template_0. 20.0 _linux_amd64. ZipCopy the code
Brew Install Consul-Template can be used for Mac OS installation, which is simple and convenient
5.4 Creating a Load Balancing Configuration File Template
Create load-balancer. CTMPL and edit the following:
upstream backend { {{- range service "openfeign-provider-service" }} server {{ .Address }}:{{ .Port }}; {{- end }} } server { listen 80; location / { proxy_pass http://backend; }}Copy the code
5.5 Deleting the Configuration File
Clear the contents of the load-balancer.conf file in section 3.2.1
5.6 Creating consul Template Configuration
Create the consul-template-config. HCL file and edit the following:
consul { address = "localhost:8500" retry { enabled = true attempts = 12 backoff = "250ms" } } template { source = "[path specified in 5.4]/load-balancer.conf. CTMPL "destination = "[path specified in 5.5]/load-balancer.conf" perms = 0600 command = "sh -c docker exec -it nginx nginx -s reload" }Copy the code
Address: specifies the Consul address. Source: specifies the path of the load balancing configuration template file. Destination: path for generating load balancing configuration files. Command: Specifies the command to be executed. Consul template pulls the service address list from the consul address, generates a load balancing configuration from the source template file to the destination file, and runs the command to load the nginx again
5.7 Running Consul Template
consul-template -config=consul-template-config.hcl
Copy the code
After consul template is run, the load-balancer.conf file contains a load balancing configuration
5.8 Shutting down or starting services
At this point, you can see that the contents of the load-Balancer.conf configuration file are constantly changing as the service is shut down and started, enabling dynamic service scaling.