What is the Consul
Consul is a service grid (TCP / IP between micro-service network calls between the responsible services, current limiting, fuse and monitoring) solution, which is a distributed, highly available system, and are developed using very simple. It provides a full-featured control plane, the main features are: service discovery, health checks, key storage, security service communication, multiple data centers.
Installation Consul
Here are two Centos: consul1, consul2
Installation:
[root@consul2 ~]# wget https://releases.hashicorp.com/consul/1.2.2/consul_1.2.2_linux_amd64.zip [root@consul2 ~]# unzip consul_1.2.2_linux_amd64.zip [root@consul2 ~]# ./consul Usage: consul [--version] [--help] <command> [<args>] # consul1 同上
Run Agent
Agent must be run after installation Consul, may choose server or client mode. Each data center has at least one server (3-5 recommendation server cluster).
For simplicity, start to develop a model of Agent:
Development model # Agent of [root @ consul2 ~] # ./consul Agent - dev ==> Starting Consul Agent ... ! ==> Consul Agent running # View cluster members [root @ consul2 ~] # ./ Consul Members the Node the Address the Status the Type the Build Protocol the DC Segment consul2 127.0 . 0.1 : 8301 Alive Server 1.2 . 2 2 DC1 <All> # using HTTP API view [the root @ consul2 ~] # curl localhost: 8500 / V1 / Catalog / Nodes [ { " ID " : "796b14fe-1332-4aa0-d96f-8f287a4ccc7e", "Node": "consul2", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "Meta " : { " Consul-Network-segment " : " " }, " the CreateIndex " : . 9 , " ModifyIndex " : 10 } ] # can also use the DNS interface query nodes (default port: 8600 ) [the root @ consul2 ~] # yum install bind- utils [root @ consul2 ~] # @ DIG 127.0 . 0.1 -p 8600 consul2.node.consul ... ;; the SECTION QUESTION: ; consul2.node.consul the IN A. ;; ANSWER the SECTION: consul2.node.consul. 0 IN A 127.0.0.1 ...
Registration Service
1, the definition of a service
[root@consul2 ~]# mkdir /etc/consul.d [root@consul2 ~]# echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' | sudo tee /etc/consul.d/web.json [root@consul2 ~]# ./consul agent -dev -config-dir=/etc/consul.d
2, a service inquiry
# 使用 DNS API [root@consul2 ~]# dig @127.0.0.1 -p 8600 web.service.consul ... ;; QUESTION SECTION: ;web.service.consul. IN A ;; ANSWER SECTION: web.service.consul. 0 IN A 127.0.0.1 # 使用 DNS API 查找 SRV 记录 [root@consul2 ~]# dig @127.0.0.1 -p 8600 web.service.consul SRV ... ;; QUESTION SECTION: ;web.service.consul. IN SRV ;; ANSWER SECTION: web.service.consul. 0 IN SRV 1 1 80 consul2.node.dc1.consul. ;; ADDITIONAL SECTION: . consul2.node.dc1.consul 0 the IN A 127.0 . 0.1 ... # using the HTTP API query [root @ consul2 ~] # curl HTTP: // localhost: 8500 / v1 / Cataog / Service / Web # health check [root consul2 @ ~] # curl ' HTTP: // localhost:? 8500 / v1 / Health / Service / Web passing '
Consul cluster
1, create node1, consul server
[consul1 the root @ ~] # ./consul Agent -bootstrap-Expect = -server. 1 \ -data-the dir = / tmp / Consul \ -node -bind One-Agent = = 192.168.56.112 \ -enable-Script-Checks = = the dir--config to true / etc / consul.d \ -client 0.0.0.0 - UI # - node: name of the node # - the bind: bind an address, the address used for communication between nodes, may be external network , must be accessible to the address # - Server: this is indicates that the node is a SERVER # -bootstrap- the expect: this indicates the number is expected to provide SERVER node, the number of a reach, it will be activated, then that is the LEADER # - DC: name specifies the data center # -client 0.0 . 0.0 -ui: start UI (UI in order to facilitate follow-up visit)
2, create node2, consul client
[root@consul2 ~]# ./consul agent -data-dir=/tmp/consul \
-node=agent-two \
-bind=192.168.56.113 -enable-script-checks=true \
-config-dir=/etc/consul.d \
-ui
3, join the cluster
[root@consul2 ~]# ./consul join 192.168.56.112 Successfully joined cluster by contacting 1 nodes. [root@consul2 ~]# ./consul members Node Address Status Type Build Protocol DC Segment agent-one 192.168.1.13:8301 alive server 1.2.2 2 dc1 <all> agent-two 192.168.1.12:8301 alive client 1.2.2 2 dc1 <default>
4, query node
[root@consul2 ~]# dig @127.0.0.1 -p 8600 agent-two.node.consul ... ;; QUESTION SECTION: ;agent-two.node.consul. IN A ;; ANSWER SECTION: agent-two.node.consul. 0 IN A 192.168.1.12
KV data
Similarly Redis, it is generally used for service configuration.
Learn simple command like:
consul kv put redis/config/minconns 1 consul kv put redis/config/minconns 2 # 更新 consul kv get redis/config/minconns consul kv delete redis/config/minconns consul kv delete -recurse redis # 批量删除
WEB UI
Under Access: http://192.168.56.112:8500/ui
Column Analysis: The above operation is something generated
1, services: placing service
2, nodes: node placed consul
3, key / value: some configuration information is placed
4, dc1: configuration data center