Consul installation, deployment, and basic usage are described in the previous section. This section focuses on Consul cluster construction. Three to five servers are recommended because in exception processing, if the Leader is down, as long as more than half of the servers are still active, Consul elects a new Leader to keep the cluster running.
Recommended technology blog: Node.js Technology Stack
The preparatory work
You are advised to set up several VMS on the local PC for commissioning. The following four VMS are deployed in Server mode and Client mode:
-
192.168.6.128 Server mode (initially set to Leader)
-
192.168.6.129 Server mode
-
192.168.6.130 Server mode
-
192.168.6.131 Client mode
Download the Consul of the corresponding platform version and decompress the copy to the /usr/local/bin/(system environment variables) directory. For details about the installation, see -Consul Download installation guide in the preceding part.
Consul_config. json: /usr/ SRC /consul
{
"datacenter": "consul_cluster"."node_name": "consul_1"."server": true."bootstrap_expect": 3."data_dir": "/usr/src/consul/data"."log_level": "DEBUG"."enable_syslog": true."enable_script_checks": true."bind_addr": "192.168.6.128"."client_addr": "192.168.6.128",}Copy the code
- Node_name: indicates the node name, which is the same as -node
- Data_dir: indicates the directory for storing data
- Enable_syslog: Consul Specifies whether the syslog directory on which logs are written to the system is enabled
- Enable_script_checks: Specifies whether to enable the monitoring check script
- Bind_addr: equivalent to -bind
- Client_addr: equal to -client
The Server end deployment
- Deploy the first vm 192.168.6.128
Note: Add -ui to the first node to initialize it only once. Perform the same operation on the other two nodes, but change the node_name, bind_ADDR, and client_ADDR in the configuration file. Keep each machine unique.
$ sudo consul agent -ui -config-file=/usr/src/consul/consul_config.json
-config-file: loads the startup configuration file
- Deploy the second server 192.168.6.129
Modify/usr/SRC/consul/consul_config json:
{
"datacenter": "consul_cluster"."node_name": "consul_2"."server": true."bootstrap_expect": 3."data_dir": "/usr/src/consul/data"."log_level": "DEBUG"."enable_syslog": true."enable_script_checks": true."bind_addr": "192.168.6.129"."client_addr": "192.168.6.129",}Copy the code
Run a command Start a command
$ sudo consul agent -config-file=/usr/src/consul/consul_config.json
Copy the code
- Deploy the third SERVER at 192.168.6.130
Modify/usr/SRC/consul/consul_config json:
{
"datacenter": "consul_cluster"."node_name": "consul_3"."server": true."bootstrap_expect": 3."data_dir": "/usr/src/consul/data"."log_level": "DEBUG"."enable_syslog": true."enable_script_checks": true."bind_addr": "192.168.6.130"."client_addr": "192.168.6.130"
}
Copy the code
Run a command Start a command
$ sudo consul agent -config-file=/usr/src/consul/consul_config.json
Copy the code
Up to now, all servers have been started, but they have not joined the cluster, so it is only a single-node cluster, you can check the member status on a certain machine:
Note: Using Consul members directly generates an error and requires binding IP addresses
$consul members --http-addr 192.168.6.124:8500 Node Address Status Type Build Protocol DC Segment consul_1 Consul_cluster <all>Copy the code
The Server cluster is established
Consul Agent is independent from Consul Agent and is not aware of the presence of other nodes. Add all consul_2 and consul_3 to the same cluster.
- A second 192.168.6.129 has been added to consul_1
$consul join --http-addr 192.168.6.124:8500 192.168.6.128 Successfully joined cluster by contacting 1 nodes.# successfully returned message
Copy the code
- A third, 192.168.6.130, has been added to consul_1
$consul join --http-addr 192.168.6.130:8500 192.168.6.128Copy the code
At present, the cluster on the server has been created. We can take a look at our current cluster members:
Consul members -- HTTP-addr 192.168.6.128:8500 Node Address Status Type Build Protocol DC Segment consul_1 192.168.6.129:8301 alive server 1.4.0 2 Consul_cluster <all> consul_2 192.168.6.129:8301 alive server 1.4.0 2 Consul_cluster <all> 192.168.6.130:8301 alive server 1.4.0 2 Consul_cluster >Copy the code
- View the cluster leader using the HTTP API
$curl 192.168.6.128:8500 / v1 / status/leader"192.168.6.128:8300"
Copy the code
- View cluster members using the HTTP API
$curl 192.168.6.128:8500 / v1 / status/peers ["192.168.6.129:8300"."192.168.6.130:8300"."192.168.6.128:8300"]
Copy the code
The Client side deployment
Now deploy the client side in a different way than the server side
Modify/usr/SRC/consul/consul_config json:
{
"datacenter": "consul_cluster"."node_name": "consul_4".//"server": true, not specified as the server, the default is the client
// "bootstrap_expect": 3, valid only in server mode
"data_dir": "/usr/src/consul/data"."log_level": "DEBUG"."enable_syslog": true."enable_script_checks": true."bind_addr": "192.168.6.131"."client_addr": "192.168.6.131"
}
Copy the code
Execute the startup command:
You can also use the -join parameter to join an already started cluster
$sudo consul-config-file =/usr/ SRC /consul_config.json -join=192.168.6.128Copy the code
When viewing the current cluster members, you can see that there are three Server modes and one Client mode
$consul members --http-addr 192.168.6.124:8500 Node Address Status Type Build Protocol DC Segment consul_1 192.168.6.129:8301 alive server 1.4.0 2 Consul_cluster <all> consul_2 192.168.6.129:8301 alive server 1.4.0 2 Consul_cluster <all> consul_3 192.168.6.130:8301 alive server 1.4.0 2 Consul_cluster <all> consul_4 192.168.6.131:8301 Alive client consul_cluster <default>Copy the code
View it in the management tool
When deploying the first 192.168.6.128 host, Consul agent has a -UI parameter after consul. This parameter is used to start the Web interface, and this parameter is the Web visualization interface provided by Consul. The browser enter http://192.168.6.128:8500 for a visit
This is a series of articles on service registration based on Consul implementation in microservices architecture. The contents are as follows:
- Service deployment, setup and use of Micro-service Consul series
- Cluster setup for micro-service Consul series
- Service registration and service discovery of microservice Consul series
- Summary of issues in the Consul microservice series
Author: you may link: www.imooc.com/article/284… Github: Node.js technology stack