The foundation of Consul series one

Source: Internet
Author: User
Tags createindex zookeeper etcd

First, the principle of consul

Features of the consul

• Service Discovery

• Health Checks

• Multi-Data center

Key/value Storage

Consul usage Scenarios

Registration and configuration sharing for Docker instances

Registration and configuration sharing for CoreOS instances

Advantages of Consul

· using the Raft algorithm to ensure consistency is more straightforward than a complex Paxos algorithm. In comparison, Zookeeper uses Paxos, while ETCD uses Raft

· support multi-data center, internal and external network services using different ports for monitoring. A multi-data center cluster avoids single points of failure in a single data center, and its deployment requires consideration of network latency, fragmentation, and so on. Zookeeper and ETCD do not provide support for multi-datacenter functions

· supports health checks. ETCD does not provide this feature

· official web Management interface, ETCD without this function

· supports HTTP and DNS protocol interfaces. Zookeeper integration is more complex, ETCD only supports HTTP protocol

Consul discovery mechanism

When a consul agent is started, it does not know the existence of other nodes, it is an isolated single node cluster , if you want to perceive the existence of other nodes, it must be added to an existing cluster, to join an existing cluster, it is only used to join any of the existing members of the cluster, when joining an existing member, it will quickly discover the other members of the cluster through communication among the members, A Consul agent can join any agent, not just the service node

Consul role

Client: Registering services, health checks, and sending data to the server

Server: Save configuration information, high availability cluster, communicate with local clients in LAN, communicate with other data centers via WAN

Ii. Construction of consul

2.1 Installing consul

[Email protected] ~]# unzip Consul_0.6.3_linux_amd64.zip[[email protected] ~]# mv consul/usr/locao/bin/.

2.2 Single-node boot consul

[[email protected] ~]# consul agent -dev  -bind=192.168.0.149==>  Starting consul agent...==> starting consul agent rpc...==> consul  agent running!         Node name:  ' Consul-1 '         Datacenter:  ' DC1 '              Server: true  (Bootstrap: false)         Client Addr: 127.0.0.1  (http: 8500, https: -1, dns:  8600, rpc: 8400)       Cluster Addr: 192.168.0.149  (LAN:  8301, wan: 8302)     Gossip encrypt: false, RPC-TLS:  false, tls-incoming: false              atlas: <disabled>==> log data will now stream in as it occurs:     2016/03/23 15:17:27 [INFO] serf: EventMemberJoin: consul-1  192.168.0.149    2016/03/23 15:17:27 [info] serf: eventmemberjoin:  consul-1.dc1 192.168.0.149    2016/03/23 15:17:27 [info] raft:  Node at 192.168.0.149:8300 [Follower] entering Follower state     2016/03/23 15:17:27 [info] consul: adding lan server consul-1   (addr: 192.168.0.149:8300)   (DC:&NBSP;DC1)     2016/03/23 15:17:27  [INFO] consul: adding WAN server consul-1.dc1  (addr:  192.168.0.149:8300)   (DC:&NBSP;DC1)     2016/03/23 15:17:27 [err] agent:  failed to sync remote  State: no cluster leader    2016/03/23 15:17:29 [warn] raft : &NBSP;HEARTBEAT&NBSP;TIMEOUT&NBSP;REACHED,&NBSP;STARTING&NBSP;ELECTION&NBSP;&NBSP;&NBSP;&NBSP;2016/03/23  15:17:29 [info] raft: node at 192.168.0.149:8300 [candidate] entering  candidate state    2016/03/23 15:17:29 [debug] raft: votes  needed: 1    2016/03/23 15:17:29 [DEBUG] raft: Vote  Granted from 192.168.0.149:8300. tally: 1    2016/03/23 15:17:29  [INFO] raft: Election won. Tally: 1    2016/03/23  15:17:29 [info] raft: node at 192.168.0.149:8300 [leader] entering  leader state    2016/03/23 15:17:29 [info] raft: disabling  enablesinglenode  (BootStrap)     2016/03/23 15:17:29 [DEBUG] raft: Node  192.168.0.149:8300 updated peer set  (2): [192.168.0.149:8300]     2016/03/23 15:17:29 [info] consul: cluster leadership acquired     2016/03/23 15:17:29 [debug] consul: reset tombstone gc to index  2    2016/03/23 15:17:29 [INFO] consul: member  ' Consul-1 '  joined, marking health alive    2016/03/23 15:17:29 [info]  consul: new leader elected: consul-1    2016/03/23 15:17:29  [INFO] agent: Synced service  ' Consul '     2016/03/23  15:18:46 [debug] agent: service  ' Consul '  in sync

2.3 Viewing members in a cluster

[Email protected] ~]# Consul Membersnode Address Status Type Build Protocol DCconsul-1 192.168.0. 149:8301 Alive Server 0.6.4 2 DC1

2.4 Querying nodes

Nodes can be queried through the HTTP API and DNS APIs

If you use the DNS query format:name.node.consul or name.node.datacenter.consul

[[email protected] ~]# dig @127.0.0.1 -p 8600  consul-1.node.consul;  <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.3 <<>> @127.0.0.1  -p 8600 consul-1.node.consul;  (1 server found);  global options: +cmd;;  Got answer:;;  ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6969;;  flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0,  additional: 0;;  WARNING: recursion requested but not available;;  QUESTION SECTION:;consul-1.node.consul.INA;;  ANSWER SECTION:consul-1.node.consul.0INA192.168.0.149;;  Query time: 1 msec;;  server: 127.0.0.1#8600 (127.0.0.1);  WHEN: Wed Mar 23 15:50:19 CST 2016;;  msg size  rcvd: 74

2.5 out -of-node: You can use CTRL + C to smooth exit, or you can use Kill to exit, the difference is to actively inform other nodes themselves to leave, and by other nodes marked as invalid, was found to leave

Third, service registration

Service definition is the most common method of service registration

3.1 Creating a configuration file directory

[Email protected] ~]# mkdir/etc/consul.d/

NOTE:CONSUL.D is the configuration file directory, which indicates that there are several configuration files, which are named specification

3.2 Writing a service definition configuration file

[[email protected] ~]# echo ' {"service": {"name": "Web", "tags": ["rails"], "port": +} ' >/etc/consul.d/web.json

Config file content explanation: There is a service named Web running on port 80, give him another label as an additional way to query the service

3.3 Restart the agent and load the configuration file

[[email protected] ~]# consul agent -dev  -bind=192.168.0.149  - config-dir=/etc/consul.d/==> starting consul agent...==> starting consul  agent rpc...==> consul agent running!          Node name:  ' consul-1 '         Datacenter:  ' DC1 '             Server: true  (Bootstrap:  false)        Client Addr: 127.0.0.1  (http: 8500,  https: -1, dns: 8600, rpc: 8400)       cluster  Addr: 192.168.0.149  (lan: 8301, wan: 8302)     Gossip  encrypt: false, rpc-tls: false, tls-incoming: false           &Nbsp;  atlas: <disabled>==> log data will now stream in  as it occurs:    2016/03/23 16:18:57 [INFO] serf:  Eventmemberjoin: consul-1 192.168.0.149    2016/03/23 16:18:57 [info]  serf: EventMemberJoin: consul-1.dc1 192.168.0.149    2016/03/23  16:18:57 [info] raft: node at 192.168.0.149:8300 [follower] entering  follower state    2016/03/23 16:18:57 [info] consul: adding  lan server consul-1  (addr: 192.168.0.149:8300)   (DC:&NBSP;DC1)      2016/03/23 16:18:57 [info] consul: adding wan server consul-1.dc1  ( addr: 192.168.0.149:8300)   (DC:&NBSP;DC1)     2016/03/23 16:18:57 [err]  agent: failed to sync remote state: no cluster leader    2016/03/ 23 16:18:58 [warn] raft: heartbeat timeout reached, starting election     2016/03/23 16:18:58 [INFO] raft: Node at  192.168.0.149:8300 [candidate] entering candidate state    2016/03/23  16:18:58 [DEBUG] raft: Votes needed: 1    2016/03/23  16:18:58 [debug] raft: vote granted from 192.168.0.149:8300. tally: 1     2016/03/23 16:18:58 [info] raft: election won. tally:  1    2016/03/23 16:18:58 [INFO] raft: Node at  192.168.0.149:8300 [leader] entering leader state    2016/03/23  16:18:58 [info] raft: disabling enablesinglenode  (bootstrap)     2016/03/23 16:18:58 [debug]  raft: Node 192.168.0.149:8300 updated peer set  (2):  [ 192.168.0.149:8300]    2016/03/23 16:18:58 [info] consul: cluster  leadership acquired    2016/03/23 16:18:58 [DEBUG] consul:  reset tombstone gc to index 2    2016/03/23 16:18:58 [ info] consul: member  ' Consul-1 '  joined, marking health alive     2016/03/23 16:18:58 [info] consul: new leader elected: consul-1     2016/03/23 16:19:01 [INFO] agent: Synced service  ' Consul '     2016/03/23 16:19:01 [INFO] agent: Synced service  ' web '

The output indicates a successful registration of the Synced Service ' web ', and if you want to register multiple services, you can create multiple configuration files

3.4 Querying services using the DNS API

You can use the DNS API to view the IP of the service

dig  @127.0.0.1 -p  8600 web.service.consul ;  <<>> dig 9.9.4-redhat-9.9.4-29.el7_2.3 <<>> @127.0.0.1  -p 8600 web.service.consul;  (1 server found);  global options: +cmd;;  Got answer:;;  ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18524;;  flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0,  additional: 0;;  WARNING: recursion requested but not available;;  QUESTION SECTION:;web.service.consul.INA;;  ANSWER SECTION:web.service.consul.0INA192.168.0.149;;  Query time: 0 msec;;  server: 127.0.0.1#8600 (127.0.0.1);  WHEN: Wed Mar 23 16:24:48 CST 2016;;  msg size  rcvd: 70 

The

can also use the DNS API to get the entire address/port information, just add SRV

[[email protected] ~]# dig  @127.0.0.1 -p  8600  web.service.consul srv; <<>> dig 9.9.4-redhat-9.9.4-29.el7_2.3 << >> @127.0.0.1 -p 8600 web.service.consul SRV;  (1 server found);  global options: +cmd;;  Got answer:;;  ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2987;;  flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0,  additional: 1;;  WARNING: recursion requested but not available;;  QUESTION SECTION:;web.service.consul.INSRV;;  ANSWER SECTION:web.service.consul.0INSRV1 1 80 consul-1.node.dc1.consul.;;  ADDITIONAL SECTION:consul-1.node.dc1.consul. 0INA192.168.0.149;;  Query time: 0 msec;;  server: 127.0.0.1#8600 (127.0.0.1);;  WHEN: Wed Mar 23 16:27:17 CST 2016;;  msg size  rcvd: 138

Filtering services using DNS API tags

[[email protected] ~]# dig  @127.0.0.1 -p  8600  Rails.web.service.consul; <<>> dig 9.9.4-redhat-9.9.4-29.el7_2.3 <<> > @127.0.0.1 -p 8600 rails.web.service.consul;  (1 server found);  global options: +cmd;;  Got answer:;;  ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51120;;  flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0,  additional: 0;;  WARNING: recursion requested but not available;;  QUESTION SECTION:;rails.web.service.consul.INA;;  ANSWER SECTION:rails.web.service.consul. 0INA192.168.0.149;;  Query time: 0 msec;;  server: 127.0.0.1#8600 (127.0.0.1);  WHEN: Wed Mar 23 16:29:21 CST 2016;;  msg size  rcvd: 82 

3.5 Querying services using the HTTP API

[Email protected] ~]# Curl http://localhost:8500/v1/catalog/service/web[{"Node": "Consul-1", "Address": " 192.168.0.149 "," ServiceID ":" Web "," ServiceName ":" Web "," servicetags ": [" rails "]," serviceaddress ":" "," Serviceport " : "Serviceenabletagoverride": false, "CreateIndex": 5, "Modifyindex": 5}]

3.6 Querying the health status of a service

[[email protected] ~]# Curl ' http://localhost:8500/v1/health/service/web?passing ' [{"node": {"node": "Consul-1", " Address ":" 192.168.0.149 "," taggedaddresses ": {" wan ":" 192.168.0.149 "}," CreateIndex ": 3," Modifyindex ": 5}," Service ": {"ID": "Web", "Service": "Web", "Tags": ["rails"], "Address": "", "Port": "Enabletagoverride": false, "CreateIndex": 5, "Modifyindex": 5}, "Checks": [{"Node": "Consul-1", "Checkid": "Serfhealth", "Name": "Serf Health Status", "status": " Passing "," Notes ":", "Output": "Agent alive and Reachable", "ServiceID": "", "ServiceName": "", "CreateIndex": 3, " Modifyindex ": 3}]}"



This article is from the "Ming Linux blog" blog, be sure to keep this source http://zhaijunming5.blog.51cto.com/10668883/1754387

The foundation of Consul series one

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.