基于滴滴云服务器搭建 Consul 集群

前言

Consul 是什么

Consul 是用 Go 开发的分布式服务协调管理的工具,它提供了服务发现,健康检查,Key/Value 存储等功能,并且支持跨数据中心的功能。

Consul 基本概念

  • Agent 组成 Consul 集群的每个成员上都要运行一个 Agent,可以通过 Consul Agent 命令来启动。Agent 可以运行在 Server 状态或者 Client 状态。

  • Client 就是客户端模式。是 Consul 节点的一种模式,这种模式下,所有注册到当前节点的服务会被转发到 Server,本身是不持久化这些信息。

  • Server 就是服务端模式,这种模式下,功能和 Client 都一样,唯一不同的是,它会把所有的信息进行持久化。

准备环境

Consul 集群是基于滴滴云虚拟机搭建滴滴云

滴滴云服务器 (DC2): 安全可靠,拥有极高的性价比高,为开发者的需求而设计。适合大中小型用户购买使用。

弹性公网 IP (EIP): 计费灵活,适配各类应用架构。可以满足用户各类应用场景需求。

安装过程

登录机器

滴滴云平台上选择三台 Centos7.3 机器,选择 DC2 默认账户 dc2-user,使用 SSH 登录方式分别登录三台云主机:

server1:ssh dc2-user@{ip1}
server2:ssh dc2-user@{ip2}
server3:ssh dc2-user@{ip3}

由于搭建集群时需要 root 权限,切换到 root 账户:

sudo -i 

建立 Consul 数据目录

mkdir -p /usr/local/consul/data

下载 Consul

进入 /usr/local/consul 目录,Consule 官网下载地址,选择 Linux 平台:

cd /usr/local/consul
sudo wget https://releases.hashicorp.com/consul/1.4.0/consul_1.4.0_linux_amd64.zip

解压

unzip consul_1.4.0_linux_amd64.zip

验证安装

使用 Consul 命令验证:

./consul
Usage: consul [--version] [--help] <command> [<args>]

Available commands are:
    acl            Interact with Consul's ACLs
    agent          Runs a Consul agent
    catalog        Interact with the catalog
    connect        Interact with Consul Connect
    debug          Records a debugging archive for operators
    event          Fire a new event
    exec           Executes a command on Consul nodes
    force-leave    Forces a member of the cluster to enter the "left" state
    info           Provides debugging information for operators.
    intention      Interact with Connect service intentions
    join           Tell Consul agent to join cluster
    keygen         Generates a new encryption key
    keyring        Manages gossip layer encryption keys
    kv             Interact with the key-value store
    leave          Gracefully leaves the Consul cluster and shuts down
    lock           Execute a command holding a lock
    maint          Controls node or service maintenance mode
    members        Lists the members of a Consul cluster
    monitor        Stream logs from a Consul agent
    operator       Provides cluster-level tools for Consul operators
    reload         Triggers the agent to reload configuration files
    rtt            Estimates network round trip time between nodes
    services       Interact with services
    snapshot       Saves, restores and inspects snapshots of Consul server state
    validate       Validate config files/directories
    version        Prints the Consul version
    watch          Watch for changes in Consul

启动 Consul Server

在第一台 Server上启动:

./consul agent -server -ui -bootstrap-expect 2 -data-dir=data -node=n1 -bind={ip1} -client=0.0.0.0 &

启动后,查看 members:

./consul members --http-addr {ip1}:8500
Node  Address             Status  Type    Build  Protocol  DC   Segment
n1    {ip1}:8301  alive   server  1.4.0  2         dc1  <all>

在第二台 Server 上启动:

./consul agent -server -ui -bootstrap-expect 2 -data-dir=data -node=n2 -bind={ip2} -client=0.0.0.0 &

启动后,查看 members:

./consul members --http-addr {ip2}:8500
Node  Address           Status  Type    Build  Protocol  DC   Segment
n2    {ip2}:8301  alive   server  1.4.0  2         dc1  <all>

在第三台 Server 上启动:

./consul agent -server -ui -bootstrap-expect 2 -data-dir=data -node=n3 -bind={ip3} -client=0.0.0.0 &

启动后,查看当前 members:

./consul members --http-addr {ip3}:8500

Node  Address             Status  Type    Build  Protocol  DC   Segment
n3    {ip3}:8301  alive   server  1.4.0  2         dc1  <all>

此时,三台 Server 都是独立的机器,下一步就是将三台机器连接成集群。

连接集群

第二台机器上分别连接第一台机器:

./consul join --http-addr {ip1}:8500 {ip2}

第三台机器上连接第一台机器:

./consul join --http-addr {ip1}:8500 {ip2}

查看集群

./consul members --http-addr {ip1}:8500
Node  Address     Status  Type    Build  Protocol  DC   Segment
n1    {ip1}:8301  alive   server  1.4.0  2         dc1  <all>
n2    {ip2}:8301  alive   server  1.4.0  2         dc1  <all>
n3    {ip3}:8301  alive   server  1.4.0  2         dc1  <all>

查看集群 Leader

可在任意一节点上执行:

curl {ip2}:8500/v1/status/leader
结果
"{ip1}:8300"

查看集群 Peers

可在任意一节点上执行:

curl {ip2}:8500/v1/status/peers
结果
["{ip1}:8300","{ip2}:8300","{ip3}:8300"]

附录

Consul 命令行

- bootstrap-expect 集群期望的节点数,只有节点数量达到这个值才会选举leader
- server 运行在server模式
- data-dir 指定数据目录,其他的节点对于这个目录必须有读的权限
- node 指定节点的名称
- bind 为该节点绑定一个地址
- config-dir 指定配置文件,定义服务的,默认所有一.json结尾的文件都会读
- enable-script-checks=true 设置检查服务为可用
- datacenter 数据中心名称,
- join 加入到已有的集群中
- ui 使用自带的ui
- client 指定web ui、的监听地址,默认127.0.0.1只能本机访问,改为0.0.0.0可外网访问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值