码迷,mamicode.com
首页 > 其他好文 > 详细

Consul 简介及集群安装

时间:2019-11-29 23:46:22      阅读:420      评论:0      收藏:0      [点我收藏+]

标签:$path   last   lse   image   流言   update   ges   key   注册   

简介

  • Consul是基于GO语言开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理的功能。
  • Consul的功能都很实用,其中包括:服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等特性。
  • Consul本身只是一个二进制的可执行文件,所以安装和部署都非常简单,只需要从官网下载后,在执行对应的启动脚本即可。
  • 官网下载地址: https://www.consul.io/downloads.html

特性

  • 基础特性
    1. 服务注册/发现
    2. 数据强一致型保证
    3. 多数据中心
    4. 健康检查
    5. Key/Value存储
  • 高级特性
    • HTTP API
    • ACL

工作模式

技术图片

  • Consul中包括的3种不同的角色:Client、Server、Server-Leader。

  • 还有一个在图上没有标出来的角色Agent,一共4个角色。

  • Agent

    • 是一个守护线程

    • 跟随Consul应用启动而启动

    • 负责检查、维护节点同步

  • Client

    • 转发所有请求给Server
    • 无状态, 不持久化数据
    • 参与LAN Gossip的健康检查
  • Server

    • 持久化数据
    • 转发请求给Server-Leader
    • 参与Server-Leader选举
    • 通过WAN Gossip(流言算法协议, Cassandra和比特币的底层算法, 符合弱一致性), 与其他数据中心交换数据
  • Server-Leader

    • 响应RPC请求
    • 将服务列表数据同步给Server

工作原理

  • 服务注册的方式

    1. HTTP API

      http://{ip}:8500/v1/agent/service/register/:service
    2. JSON 配置文件

      {
          "services": [
                  {
                          "id": "serverId",
                          "name": "serverName",
                          "tags": [
                                  "primary"
                          ],
                          "address": "127.0.0.1",
                          "port": 9003,
                          "checks": [
                                  {
                                          "id": "api-servie",
                                          "name": "Service 'xx' check",
                                          "http": "http://127.0.0.1:9003/public/health",
                                          "method": "GET",
                                          "interval": "10s",
                                          "timeout": "1s"
                                  }
                          ]
                  }
          ]
      • 启动增加参数: -config-dir

        nohup ./consul agent -dev -config-dir=/consul-conf/service.json &
  • 服务发现的方式

    1. HTTP API

      • 获取某个service下健康的服务列表信息

        http://{ip}:8500/v1/health/service/:service
    2. DNS Agent

  • 工作流程

    技术图片

集群搭建

  • 这次我们以二进制包方式搭建, 其实还有docker的方式。

  • 我使用的系统为ubuntu18.04LTS

  • 集群规划:node01, node02, node03, node04, node05上都安装

  • 集群前置准备请参考: https://www.cnblogs.com/ronnieyuan/p/11461377.html

  • 上传二进制包并解压到指定目录

    unzip consul_1.6.2_linux_amd64.zip -d /usr/bin
  • 解压后直接就是一个可执行文件了

  • vim ~/.bashrc 修改环境变量

    # Consul
    export CONSUL_HOME=/usr/bin/consul
    export PATH=$PATH:CONSUL_HOME
  • 验证Consul安装是否成功

    root@node01:/home/ronnie/soft# consul version
    Consul v1.6.2
    Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
    root@node01:/home/ronnie/soft# vim ~/.bashrc 
  • 将二进制文件拷贝至其他几台虚拟机上, 记得修改配置文件

    cd /usr/bin/
    scp consul root@node02:`pwd`
    scp consul root@node03:`pwd`
    scp consul root@node04:`pwd`
    scp consul root@node05:`pwd`
  • 分别在五台虚拟机上输入以下对应的命令

    # node01
    consul agent -server -ui -bootstrap-expect=5 -data-dir=/data/consul -node=agent-1 -client=0.0.0.0 -bind=192.168.180.130 -datacenter=dc1
    
    # node02
    consul agent -server -ui -bootstrap-expect=5 -data-dir=/data/consul -node=agent-2 -client=0.0.0.0 -bind=192.168.180.131 -datacenter=dc1 -join 192.168.180.130
    
    # node03
    consul agent -server -ui -bootstrap-expect=5 -data-dir=/data/consul -node=agent-3 -client=0.0.0.0 -bind=192.168.180.132 -datacenter=dc1 -join 192.168.180.130
    
    # node04
    consul agent -server -ui -bootstrap-expect=5 -data-dir=/data/consul -node=agent-4 -client=0.0.0.0 -bind=192.168.180.133 -datacenter=dc1 -join 192.168.180.130
    
    # node05
    consul agent -server -ui -bootstrap-expect=5 -data-dir=/data/consul -node=agent-5 -client=0.0.0.0 -bind=192.168.180.134 -datacenter=dc1 -join 192.168.180.130
    • 命令解读:

      • -bootstrap-expect=5: 指定了集群有5台
      • -node=agent-x: 指定服务器
      • -client=0.0.0.0: 指定当前主机客户端侦听地址为0.0.0.0
      • -bind: 绑定当前主机的ip地址
      • -datacenter=dc: 指定了一个数据中心的名称
      • -join 192.168.180.130: 后几台服务器在启动的时候加入第一台代理服务器node01(PS, 比较坑的是即使已经改过hostname文件, 还是得敲ip)
      • -ui指定了启用每台服务器的内置 WebUI 服务器组件
      • 当五台服务器都正确运行起来以后,Consul 集群将自动选举 leader,自动进行集群事务,无需干预。
    • 启动成功案例

      ==> bind_addr cannot be empty
      root@node02:~# consul agent -server -ui -bootstrap-expect=5 -data-dir=/data/consul -node=agent-2 -client=0.0.0.0 -bind=192.168.180.131 -datacenter=dc1 -join 192.168.180.130
      bootstrap_expect > 0: expecting 5 servers
      ==> Starting Consul agent...
                 Version: 'v1.6.2'
                 Node ID: '304a1a5a-2987-e7ad-738d-7d21f9dbd887'
               Node name: 'agent-2'
              Datacenter: 'dc1' (Segment: '<all>')
                  Server: true (Bootstrap: false)
             Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, gRPC: -1, DNS: 8600)
            Cluster Addr: 192.168.180.131 (LAN: 8301, WAN: 8302)
                 Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
      
      ==> Log data will now stream in as it occurs:
      
          2019/11/29 22:19:15 [INFO]  raft: Initial configuration (index=0): []
          2019/11/29 22:19:15 [INFO]  raft: Node at 192.168.180.131:8300 [Follower] entering Follower state (Leader: "")
          2019/11/29 22:19:15 [INFO] serf: EventMemberJoin: agent-2.dc1 192.168.180.131
          2019/11/29 22:19:15 [INFO] serf: EventMemberJoin: agent-2 192.168.180.131
          2019/11/29 22:19:15 [INFO] consul: Adding LAN server agent-2 (Addr: tcp/192.168.180.131:8300) (DC: dc1)
          2019/11/29 22:19:15 [INFO] agent: Started DNS server 0.0.0.0:8600 (udp)
          2019/11/29 22:19:15 [INFO] consul: Handled member-join event for server "agent-2.dc1" in area "wan"
          2019/11/29 22:19:15 [INFO] agent: Started DNS server 0.0.0.0:8600 (tcp)
          2019/11/29 22:19:15 [INFO] agent: Started HTTP server on [::]:8500 (tcp)
      ==> Joining cluster...
          2019/11/29 22:19:15 [INFO] agent: (LAN) joining: [192.168.180.130]
          2019/11/29 22:19:15 [INFO] serf: EventMemberJoin: agent-1 192.168.180.130
          2019/11/29 22:19:15 [INFO] agent: (LAN) joined: 1
          Join completed. Synced with 1 initial agents
          2019/11/29 22:19:15 [INFO] agent: started state syncer
      ==> Consul agent running!
          2019/11/29 22:19:15 [INFO] consul: Adding LAN server agent-1 (Addr: tcp/192.168.180.130:8300) (DC: dc1)
          2019/11/29 22:19:15 [INFO] serf: EventMemberJoin: agent-1.dc1 192.168.180.130
          2019/11/29 22:19:15 [INFO] consul: Handled member-join event for server "agent-1.dc1" in area "wan"
          2019/11/29 22:19:17 [INFO] serf: EventMemberJoin: agent-3.dc1 192.168.180.132
          2019/11/29 22:19:17 [INFO] consul: Handled member-join event for server "agent-3.dc1" in area "wan"
          2019/11/29 22:19:17 [INFO] serf: EventMemberJoin: agent-3 192.168.180.132
          2019/11/29 22:19:17 [INFO] consul: Adding LAN server agent-3 (Addr: tcp/192.168.180.132:8300) (DC: dc1)
          2019/11/29 22:19:17 [INFO] serf: EventMemberJoin: agent-4.dc1 192.168.180.133
          2019/11/29 22:19:17 [INFO] consul: Handled member-join event for server "agent-4.dc1" in area "wan"
          2019/11/29 22:19:18 [INFO] serf: EventMemberJoin: agent-4 192.168.180.133
          2019/11/29 22:19:18 [INFO] consul: Adding LAN server agent-4 (Addr: tcp/192.168.180.133:8300) (DC: dc1)
          2019/11/29 22:19:18 [INFO] serf: EventMemberJoin: agent-5 192.168.180.134
          2019/11/29 22:19:18 [INFO] consul: Adding LAN server agent-5 (Addr: tcp/192.168.180.134:8300) (DC: dc1)
          2019/11/29 22:19:18 [INFO] consul: Existing Raft peers reported by agent-5, disabling bootstrap mode
          2019/11/29 22:19:18 [INFO] serf: EventMemberJoin: agent-5.dc1 192.168.180.134
          2019/11/29 22:19:18 [INFO] consul: Handled member-join event for server "agent-5.dc1" in area "wan"
          2019/11/29 22:19:19 [WARN]  raft: Failed to get previous log: 1 log not found (last: 0)
          2019/11/29 22:19:19 [INFO] agent: Synced node info
          2019/11/29 22:19:19 [INFO] consul: New leader elected: agent-1
      ==> Failed to check for updates: Get https://checkpoint-api.hashicorp.com/v1/check/consul?arch=amd64&os=linux&signature=6977bf57-e750-2607-d65d-fa3ab1444055&version=1.6.2: net/http: request canceled (Client.Timeout exceeded while awaiting headers)
  • 在浏览器上访问node01节点的8500端口

    技术图片

    技术图片

    技术图片

  • 那么本次集群的安装就到此为止了

Consul 简介及集群安装

标签:$path   last   lse   image   流言   update   ges   key   注册   

原文地址:https://www.cnblogs.com/ronnieyuan/p/11960696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!