Home  >  Article  >  Operation and Maintenance  >  How to use Consul-template+Nginx to implement Thrift Consul load balancing

How to use Consul-template+Nginx to implement Thrift Consul load balancing

王林
王林forward
2023-05-15 08:19:10796browse

Overall Architecture

Let’s first take a look at what the architecture of the entire framework looks like. Here we have three service providers and three service callers, which pass Consul and Nginx, and Consul-template to achieve load balancing.

Explanation This example is for RPC load balancing. RPC is a tcp protocol, so Nginx needs to configure the tcp module to support tcp load balancing.

  1. Consul Cluster is used for service registration, registering multiple service instances, and providing RPC services to the outside world.

  2. Consul-template is used to monitor the status of services in Consul in real time, and generates Nginx## with its own template file. # configuration file.

  3. Nginx Use your own configuration file and the configuration file generated in the second step for load balancing.

Nginx installation

  1. Install the latest version of

    Nginx, ensure that the Nginx version is 1.9.0 The above

  2. 1.9.0 version or above only supports

    TCP forwarding. It is said that this module is not installed by default. You can check it after the installation is completed. If there is -- The with-stream parameter indicates that TCP is already supported. If not, recompile and add parameters for installation.


  3. My Nginx is installed in the

    /etc/nginx directory

  4. After the installation is completed, use

    nginx -t to monitor whether it is successful.

Consul-template

This article aims at load balancing and does not introduce the construction of Consul cluster.

1. Download the corresponding system version file

2. Unzip it and copy it to the

PATH path

[silence@centos145 ~]$ tar xzvf consul-template_0.19.4_linux_amd64.tgz
[silence@centos145 ~]$ mv ./consul-template /usr/sbin/consul-template
3. Find a place Create a new folder and create three files

4.

config.hcl is mainly used to configure the startup parameters of consul-template, including consul The address of the server, the location of the template file, the location of the generated configuration file, etc. Except for consul and template blocks, other parameters are optional.

5.

ConsulBlock configurationConsulServer address and port

consul {
  auth {
    enabled  = false
    username = "test"
    password = "test"
  }


  address = "172.20.132.196:8500"
  retry {
    enabled = true
    attempts = 12
    backoff = "250ms"
    max_backoff = "1m"
  }


}
6.

templateThe path and sum of the block configuration template The location of the generated file, and the commands that need to be executed after generating the file. Here we need nginx to reload the configuration file, so the set command is nginx -s reload

template {
  source = "/etc/nginx/consul-template/template.ctmpl"
  destination = "/etc/nginx/consul-template/nginx.conf"
  create_dest_dirs = true
  command = "/usr/sbin/nginx -s reload"
  command_timeout = "30s"
  error_on_missing_key = false
  perms = 0600
  backup = true
  left_delimiter  = "{{"
  right_delimiter = "}}"
  wait {
    min = "2s"
    max = "10s"
  }
}
7.

template.ctmpl Write, because only the server address and port number are needed here, the template file is as follows:

[root@centos145 consul-template]# cat template.ctmpl
stream {


    log_format main '$remote_addr - [$time_local] '
      '$status';


    access_log /var/log/nginx/tcp_access.log main;


    upstream cloudsocket {
 \{\{range service "ad-rpc-device-server"}}server \{\{.Address}}:\{\{.Port}};{{end}}
    }


    server {
 listen 8888;
 proxy_pass cloudsocket;
    }
}
8. Start

consul-template consul-template -config=./config .hcl

The config.hcl configuration file is used to simplify the command consul-template -consul-addr=172.20.132.196:8500 -template=./template.ctmpl:./nginx. conf

9. The initial

nignx.conf file is empty, and after startup the content is

[root@centos145 consul-template]# cat nginx.conf
stream {


    log_format main '$remote_addr - [$time_local] '
      '$status';


    access_log /var/log/nginx/tcp_access.log main;


    upstream cloudsocket {
 server 172.20.139.77:8183;
    }


    server {
 listen 8888;
 proxy_pass cloudsocket;
    }
}
Make sure the service has been successfully registered to Consul , you can see that the server address and port have been configured.

10. Introduce the configuration file generated by

consul-template into nginx.conf of the nginx installation directory include /etc/nginx/consul-template/nginx.conf;

Note that the generated configuration file cannot duplicate the content in nginx’s own configuration file! ! !

11. Start a service instance and check the generated

nginx.conf file. You will find that the service list will be dynamically added in upstream cloudsocket{}, and Dynamic changes as services join and leave.

[root@centos145 consul-template]# cat nginx.conf
stream {


    log_format main '$remote_addr - [$time_local] '
      '$status';


    access_log /var/log/nginx/tcp_access.log main;


    upstream cloudsocket {
 server 172.20.139.77:8183;
    }


    server {
 listen 8888;
 proxy_pass cloudsocket;
    }
}
Start one more and the service list becomes two

[root@centos145 consul-template]# cat nginx.conf
stream {


    log_format main '$remote_addr - [$time_local] '
      '$status';


    access_log /var/log/nginx/tcp_access.log main;


    upstream cloudsocket {
 server 172.20.139.77:8183;server 172.20.139.77:8184;
    }


    server {
 listen 8888;
 proxy_pass cloudsocket;
    }
}
12.

thriftThe client only needs to configure when callingNginx The address and port are sufficient. There is no need to configure the address and port of the service. Nginx will automatically forward it.

The above is the detailed content of How to use Consul-template+Nginx to implement Thrift Consul load balancing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete