您的位置:首页 > 其它

Consul入门

2017-11-06 08:46 134 查看
参考文档:
https://www.consul.io/intro/index.html
备注: 采用版本Consul v1.0.0

目录

consul介绍

consul安装

运行代理

关闭代理

注册服务

consul集群

健康检查

KV数据

WebUI

gossip协议

一致性协议: https://www.consul.io/docs/internals/consensus.html
一、consul介绍

服务发现

健康检查

KV存储

多数据中心

二、安装

windows系统:


1、下载安装包

     https://www.consul.io/downloads.html
2、配置路径

    解压zip

   执行文件拷贝到C:\\consul\\bin

   配置环境变量:path  C:\TDM-GCC-64\bin;C:\consul\bin;C:\Gradle\gradle-3.5\bin;%JAVA_HOME%\bin;%HADOOP_HOME%\bin;%M2_HOME%\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files
(x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\Intel\WiFi\bin\;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Scripts;C:\ProgramData\Anaconda3\Library\bin;C:\TDM-GCC-64\bin;C:\Program
Files\Git\cmd;%GROOVY_HOME%\bin

3、测试安装

C:\Windows\system32>consul --version

Consul v0.8.1
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

ubuntu系统:

consul直接拷贝到: /usr/local/bin

三、运行代理

启动一个代理


C:\Windows\system32>consul agent -dev

==> Starting Consul agent...

==> Consul agent running!

           Version: 'v0.8.1'

           Node ID: '6edb8e08-d606-4650-a196-fff5632843f9'

         Node name: 'DESKTOP-QHG7SFL'

        Datacenter: 'dc1'

            Server: true (bootstrap: false)

       Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600)

      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)

    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

             Atlas: <disabled>

==> Log data will now stream in as it occurs:

    2017/11/06 09:06:38 [DEBUG] Using unique ID "6edb8e08-d606-4650-a196-fff5632843f9" from host as node ID

查看集群成员

新打卡一个窗口

查看成员信息


C:\Windows\system32>consul members

Node             Address         Status  Type    Build  Protocol  DC

DESKTOP-QHG7SFL  127.0.0.1:8301  alive   server  0.8.1  2         dc1

C:\Windows\system32>curl localhost:8500/v1/catalog/nodes

[

    {

        "ID": "6edb8e08-d606-4650-a196-fff5632843f9",

        "Node": "DESKTOP-QHG7SFL",

        "Address": "127.0.0.1",

        "TaggedAddresses": {

            "lan": "127.0.0.1",

            "wan": "127.0.0.1"

        },

        "Meta": {},

        "CreateIndex": 5,

        "ModifyIndex": 6

    }

]

四、关闭代理

强制关闭: ctrl - c

一致性: 不要强制性关闭

五、注册服务

1、服务定义的方式注册服务

     创建配置文件

    $ sudo mkdir /etc/consul.d

    $echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}'  >     | sudo tee /etc/consul.d/web.json

   重启代理


  $ consul agent -dev -config-dir=/etc/consul.d

    ==> Starting Consul agent...

    ==> Consul agent running!

           Version: 'v1.0.0'

           Node ID: 'b1a78817-513c-1dbd-cf57-d3602f503764'

         Node name: 'slave-01'

        Datacenter: 'dc1' (Segment: '<all>')

            Server: true (Bootstrap: false)

       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 860

 

2、DNS访问服务

   $ dig @127.0.0.1 -p 8600 web.service.consul

; <<>> DiG 9.10.3-P4-Ubuntu <<>> @127.0.0.1 -p 8600 web.service.consul

; (1 server found)

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56299

;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:

; EDNS: version: 0, flags:; udp: 4096

;; QUESTION SECTION:

;web.service.consul.        IN    A

;; ANSWER SECTION:
web.service.consul.    0    IN    A    127.0.0.1

;; Query time: 0 msec

;; SERVER: 127.0.0.1#8600(127.0.0.1)

;; WHEN: Tue Nov 07 05:53:38 CST 2017

;; MSG SIZE  rcvd: 63

use the DNS API to retrieve the entire address/port pair as a SRV record:

$ dig @127.0.0.1 -p 8600 web.service.consul SRV

; <<>> DiG 9.10.3-P4-Ubuntu <<>> @127.0.0.1 -p 8600 web.service.consul SRV

; (1 server found)

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62764

;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 3

;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:

; EDNS: version: 0, flags:; udp: 4096

;; QUESTION SECTION:

;web.service.consul.        IN    SRV

;; ANSWER SECTION:

web.service.consul.    0    IN    SRV    1 1 80 slave-01.node.dc1.consul.

;; ADDITIONAL SECTION:

slave-01.node.dc1.consul. 0    IN    A    127.0.0.1

slave-01.node.dc1.consul. 0    IN    TXT    "consul-network-segment="

;; Query time: 0 msec

;; SERVER: 127.0.0.1#8600(127.0.0.1)

;; WHEN: Tue Nov 07 05:55:21 CST 2017

;; MSG SIZE  rcvd: 143

3、Http访问服务

$ curl http://localhost:8500/v1/catalog/service/web
[

    {

        "ID": "b1a78817-513c-1dbd-cf57-d3602f503764",

        "Node": "slave-01",

        "Address": "127.0.0.1",

        "Datacenter": "dc1",

        "TaggedAddresses": {

            "lan": "127.0.0.1",

            "wan": "127.0.0.1"

        },

        "NodeMeta": {

            "consul-network-segment": ""

        },

        "ServiceID": "web",

        "ServiceName": "web",

        "ServiceTags": [

            "rails"

        ],

        "ServiceAddress": "",

        "ServicePort": 80,

        "ServiceEnableTagOverride": false,

        "CreateIndex": 6,

        "ModifyIndex": 6

    }

]

只访问健康实例:

$ curl 'http://localhost:8500/v1/health/service/web?passing'

[

    {

        "Node": {

            "ID": "b1a78817-513c-1dbd-cf57-d3602f503764",

            "Node": "slave-01",

            "Address": "127.0.0.1",

            "Datacenter": "dc1",

            "TaggedAddresses": {

                "lan": "127.0.0.1",

                "wan": "127.0.0.1"

            },

            "Meta": {

                "consul-network-segment": ""

            },

            "CreateIndex": 5,

            "ModifyIndex": 6

        },

        "Service": {

            "ID": "web",

            "Service": "web",

            "Tags": [

                "rails"

            ],

            "Address": "",

            "Port": 80,

            "EnableTagOverride": false,

            "CreateIndex": 6,

            "ModifyIndex": 6

        },

        "Checks": [

            {

                "Node": "slave-01",

                "CheckID": "serfHealth",

                "Name": "Serf Health Status",

                "Status": "passing",

                "Notes": "",

                "Output": "Agent alive and reachable",

                "ServiceID": "",

                "ServiceName": "",

                "ServiceTags": [],

                "CreateIndex": 5,

                "ModifyIndex": 5

            }

        ]

    }

]

4、更新服务

1> consul可以在服务提供的时候动态修改状态

2> 更新服务方法

     修改配置文件,并发送SIGHUP给代理

     HTTP API

六、Consul集群

1、启动server

     注意: -ui参数  打开web ui


      $ consul agent -server -bootstrap-expect=1     -data-dir=/tmp/consul -node=agent-one -bind=192.168.1.190  -ui  -enable-script-checks=true -config-dir=/etc/consul.d

BootstrapExpect is set to 1; this is the same as Bootstrap mode.

bootstrap = true: do not enable unless necessary

==> Starting Consul agent...

==> Consul agent running!

           Version: 'v1.0.0'

           Node ID: '51006970-7f03-de36-12fb-dcd7dd0ecdee'

         Node name: 'agent-one'

        Datacenter: 'dc1' (Segment: '<all>')

            Server: true (Bootstrap: true)

       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)

      Cluster Addr: 192.168.1.190 (LAN: 8301, WAN: 8302)

           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

 

    --node name: 节点名称

   -bootstrap-expect: 期望服务数码

   --data-dir: 节点存储数据的目录

  -enable-script-checks:健康检查

  -config-dir: 配置文件所在目录
2、启动代理

  $ consul agent -data-dir=/tmp/consul -node=agent-two  -bind=192.168.1.191 -enable-script-checks=true -config-dir=/etc/consul.d

==> Starting Consul agent...

==> Consul agent running!

           Version: 'v1.0.0'

           Node ID: 'bdf166e5-9ffd-4f0f-d513-9010b2d318d8'

         Node name: 'agent-two'

        Datacenter: 'dc1' (Segment: '')

            Server: false (Bootstrap: false)

       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)

      Cluster Addr: 192.168.1.191 (LAN: 8301, WAN: 8302)

           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

3、加入集群

   lxq@slave-01:/data$ consul join 192.168.1.191

       Successfully joined cluster by contacting 1 nodes.

   lxq@slave-01:/data$ consul members

       Node       Address             Status  Type    Build  Protocol  DC   Segment

       agent-one  192.168.1.190:8301  alive   server  1.0.0  2         dc1  <all>

       agent-two  192.168.1.191:8301  alive   client  1.0.0  2         dc1  <default>

   注: consul代理加入集群,只需加入一个存在的成员就可以了。

4、自动加入集群

    add the 
retry_join_ec2
retry_join_gce
 or
the 
retry_join_azure
 nested
object to your Consul configuration file

5、访问节点

     lxq@slave-01:/data$  dig @127.0.0.1 -p 8600 agent-two.node.consul

; <<>> DiG 9.10.3-P4-Ubuntu <<>> @127.0.0.1 -p 8600 agent-two.node.consul

; (1 server found)

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27975

;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:

; EDNS: version: 0, flags:; udp: 4096

;; QUESTION SECTION:

;agent-two.node.consul.        IN    A

;; ANSWER SECTION:

agent-two.node.consul.    0    IN    A    192.168.1.191

;; Query time: 0 msec

;; SERVER: 127.0.0.1#8600(127.0.0.1)

;; WHEN: Tue Nov 07 07:57:56 CST 2017

;; MSG SIZE  rcvd: 66

6、离开集群

1> 强制离开 Ctrl-C

 2> 优雅离开

     运行节点转为离开状态

七、健康检查——需要研究下,与教程结果不符

1、定义检查


30s运行一次ping操作,检查google网站的健康:

  root@slave-02:/home/lxq# echo '{"check": {"name": "ping",

>   "script": "ping -c1 google.com >/dev/null", "interval": "30s"}}' \

>   >/etc/consul.d/ping.json

root@slave-02:/home/lxq# echo '{"service": {"name": "web", "tags": ["rails"], "port": 80,

>   "check": {"script": "curl localhost >/dev/null 2>&1", "interval": "10s"}}}' \

>   >/etc/consul.d/web.json

10s运行一次检查本地webserver的健康:

root@slave-02:/home/lxq# cat /etc/consul.d/

ping.json  web.json   

root@slave-02:/home/lxq# cat /etc/consul.d/web.json

{"service": {"name": "web", "tags": ["rails"], "port": 80,

  "check": {"script": "curl localhost >/dev/null 2>&1", "interval": "10s"}}}
2、检查健康状态

root@slave-02:/home/lxq#  curl http://localhost:8500/v1/health/state/critical
[]

root@slave-02:/home/lxq# dig @127.0.0.1 -p 8600 web.service.consul

; <<>> DiG 9.10.3-P4-Ubuntu <<>> @127.0.0.1 -p 8600 web.service.consul

; (1 server found)

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8183

;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:

; EDNS: version: 0, flags:; udp: 4096

;; QUESTION SECTION:

;web.service.consul.        IN    A

;; ANSWER SECTION:

web.service.consul.    0    IN    A    192.168.1.191

web.service.consul.    0    IN    A    192.168.1.190

;; Query time: 1 msec

;; SERVER: 127.0.0.1#8600(127.0.0.1)

;; WHEN: Tue Nov 07 08:20:45 CST 2017

;; MSG SIZE  rcvd: 79

八、KV Data

用处: 动态配置,帮助协调服务,领导选取

方式: Consul kv cli,   Consul KV HTTP API

1、写入

root@slave-02:/home/lxq# consul kv get redis/config/minconns

Error! No key exists at: redis/config/minconns

root@slave-02:/home/lxq# consul kv put redis/config/minconns 1

Success! Data written to: redis/config/minconns

root@slave-02:/home/lxq# consul kv put redis/config/maxconns 25

Success! Data written to: redis/config/maxconns

root@slave-02:/home/lxq# consul kv put -flags=42 redis/config/users/admin abcd1234

Success! Data written to: redis/config/users/admin

2、读取

读取一个键值对:

root@slave-02:/home/lxq# consul kv get redis/config/minconns

1

root@slave-02:/home/lxq# consul kv get -detailed redis/config/minconns

CreateIndex      560

Flags            0

Key              redis/config/minconns

LockIndex        0

ModifyIndex      560

Session          -

Value            1

读取所有的键值对:

root@slave-02:/home/lxq#  consul kv get -recurse

redis/config/maxconns:25

redis/config/minconns:1

redis/config/users/admin:abcd1234

3、删除

删除一个键

root@slave-02:/home/lxq# consul kv delete redis/config/minconns

Success! Deleted key: redis/config/minconns

删除所有redis开头的键

root@slave-02:/home/lxq# consul kv delete -recurse redis

Success! Deleted keys with prefix: redis

4、cas设置——与教程不一致

root@slave-02:/home/lxq# consul kv put foo bar

Success! Data written to: foo

root@slave-02:/home/lxq# consul kv get foo

bar

root@slave-02:/home/lxq# consul kv put foo zip

Success! Data written to: foo

root@slave-02:/home/lxq# consul kv get foo

zip

root@slave-02:/home/lxq# consul kv put -cas -modify-index=123 foo bar

Error! Did not write to foo: CAS failed

root@slave-02:/home/lxq#  consul kv put -cas -modify-index=123 foo bar

Error! Did not write to foo: CAS failed

九、Web UI

consul agent -ui
http://localhost:8500/ui
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: