记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

Docker微服务的ETCD v3.2集群搭建教程详解

etcd是一个高可用的键值存储系统,主要用于共享配置和服务发现。etcd是由CoreOS开发并维护的,灵感来自于 ZooKeeper 和 Doozer,它使用Go语言编写,并通过Raft一致性算法处理日志复制以保证强一致性。Raft是一个来自Stanford的新的一致性算法,适用于分布式系统的日志复制,Raft通过选举的方式来实现一致性,在Raft中,任何一个节点都可能成为Leader。Google的容器集群管理系统Kubernetes、开源PaaS平台Cloud Foundry和CoreOS的Fleet都广泛使用了etcd。

etcd的特性

    简单: curl可访问的用户的API(HTTP+JSON)定义明确,面向用户的API(gRPC)

    安全: 可选的SSL客户端证书认证

    快速: 单实例每秒 1000 次写操作

    可靠: 使用Raft保证一致性

Etcd构建自身高可用集群主要有三种形式

    1)静态发现: 预先已知 Etcd 集群中有哪些节点,在启动时直接指定好Etcd的各个node节点地址
    2)Etcd动态发现: 通过已有的Etcd集群作为数据交互点,然后在扩展新的集群时实现通过已有集群进行服务发现的机制
    3)DNS动态发现: 通过DNS查询方式获取其他节点地址信息

本次搭建的基础环境

底层OS:Centos7
docker版本:Docker version 18.06.1-ce
IP:
    服务器A:192.167.0.168
    服务器B:192.167.0.170
    服务器C:192.167.0.172
首先在各个服务器上下载最新的etcd镜像

1
# docker pull quay.io/coreos/etcd

本人因机器有限,在一台机器配置了3个容器,在机器上创建了子网络,三台容器在一个网络里

1
# docker network create --subnet=192.167.0.0/16 etcdnet

接下来我采用了两种方式来创建集群:1、将三个服务器挨个添加进集群;2、将三个服务器统一添加进集群。以下命令标注A的代表在A机器上执行,同理B、C。

1、将服务器挨个添加进集群

  A  在 容器/服务器 A上运行一个ETCD实例,取名为autumn-client0,注意其状态为new,“-initial-cluster”中只有自己的IP

1
# docker run -d -p 2379:2379 -p 2380:2380 --net etcdnet --ip 192.167.0.168 --name etcd0 quay.io/coreos/etcd /usr/local/bin/etcd --name autumn-client0 -advertise-client-urls http://192.167.0.168:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.167.0.168:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "autumn-client0=http://192.167.0.168:2380" -initial-cluster-state new

参数说明

1
2
3
4
5
6
7
8
—data-dir 指定节点的数据存储目录,这些数据包括节点ID,集群ID,集群初始化配置,Snapshot文件,若未指定—wal-dir,还会存储WAL文件;
—wal-dir 指定节点的was文件的存储目录,若指定了该参数,wal文件会和其他数据文件分开存储。
—name 节点名称
—initial-advertise-peer-urls 告知集群其他节点url.
— listen-peer-urls 监听URL,用于与其他节点通讯
— advertise-client-urls 告知客户端url, 也就是服务的url
— initial-cluster-token 集群的ID
— initial-cluster 集群中所有节点

配置文件说明,例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# [member]
# 节点名称
ETCD_NAME=node1
# 数据存放位置
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR=""
#ETCD_SNAPSHOT_COUNT="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
# 监听其他 Etcd 实例的地址
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
# 监听客户端地址
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#
#[cluster]
# 通知其他 Etcd 实例地址
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://node1:2380"
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
# 初始化集群内节点地址
ETCD_INITIAL_CLUSTER="node1=http://node1:2380,node2=http://node2:2380,etcd2=http://etcd2:2380"
# 初始化集群状态,new 表示新建
ETCD_INITIAL_CLUSTER_STATE="new"
# 初始化集群 token
ETCD_INITIAL_CLUSTER_TOKEN="mritd-etcd-cluster"
# 通知 客户端地址
ETCD_ADVERTISE_CLIENT_URLS=<a rel="external nofollow" href="http://node1:2379,http://node1:4001">http://node1:2379,http://node1:4001</a>

  A  在服务器A的ETCD服务上,通过调用API添加一个新的节点:192.167.0.170

1
# curl http://127.0.0.1:2379/v2/members -XPOST -H "Content-Type: application/json" -d '{"peerURLs": ["http://192.167.0.170:2480"]}'
  

  B  在容器/服务器B上运行一个ETCD实例,取名为autumn-client1,注意其状态为existing,“-initial-cluster”中有前一个IP及自己的IP

1
# docker run -d -p 2479:2479 -p 2480:2480 --name etcd1 quay.io/coreos/etcd /usr/local/bin/etcd --name autumen-client1 -advertise-client-urls http://192.167.0.170:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.167.0.170:2380 -listen-peer-urls http://0.0.0.0:2480 -initial-cluster-token etcd-cluster -initial-cluster "autumn-client0=http://192.167.0.168:2380,autumn-client1=http://192.167.0.170:2480" -initial-cluster-state existing
  

  A  在服务器A的ETCD服务上,通过调用API添加一个新的节点:192.168.7.172

1
# curl http://127.0.0.1:2379/v2/members -XPOST -H "Content-Type: application/json" -d '{"peerURLs": ["http://192.167.0.172:2580"]}'
  

自媒体培训

  C 在服务器C上运行一个ETCD实例,取名为autumn-client2,注意其状态为existing,“-initial-cluster”中有之前所有节点的IP及自己的IP

1
# docker run -d -p 2579:2579 -p 2580:2580 --name etcd quay.io/coreos/etcd -name autumn-client2 -advertise-client-urls http://192.167.0.172:2579 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.167.0.172:2580 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "autumn-client0=http://192.167.0.168:2380,autumn-client1=http://192.167.0.170:2480,autumn-client2=http://192.167.0.172:2580" -initial-cluster-state existing

2、将服务器统一添加进集群

(“-initial-cluster”中包含所有节点的IP,状态均为new)

   A上执行

1
# docker run -d -p 2379:2379 -p 2380:2380 --restart=always --net etcdnet --ip 192.167.0.168 --name etcd0 quay.io/coreos/etcd /usr/local/bin/etcd --name autumn-client0 -advertise-client-urls http://192.167.0.168:2379 -listen-client-urls http://0.0.0.0:2379 -initial-advertise-peer-urls http://192.167.0.168:2380 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster autumn-client0=http://192.167.0.168:2380,autumn-client1=http://192.167.0.170:2480,autumn-client2=http://192.167.0.172:2580 -initial-cluster-state new
  

  B上执行

1
# docker run -d -p 2479:2479 -p 2480:2480 --restart=always --net etcdnet --ip 192.167.0.170  --name etcd1 quay.io/coreos/etcd /usr/local/bin/etcd --name autumn-client1 -advertise-client-urls http://192.167.0.170:2479 -listen-client-urls http://0.0.0.0:2479 -initial-advertise-peer-urls http://192.167.0.170:2480 -listen-peer-urls http://0.0.0.0:2480 -initial-cluster-token etcd-cluster -initial-cluster autumn-client0=http://192.167.0.168:2380,autumn-client1=http://192.167.0.170:2480,autumn-client2=http://192.167.0.172:2580 -initial-cluster-state new
  

  C上执行

1
# docker run -d -p 2579:2579 -p 2580:2580 --restart=always --net etcdnet --ip 192.167.0.172  --name etcd2 quay.io/coreos/etcd /usr/local/bin/etcd --name autumn-client2 -advertise-client-urls http://192.167.0.172:2579 -listen-client-urls http://0.0.0.0:2579 -initial-advertise-peer-urls http://192.167.0.172:2580 -listen-peer-urls http://0.0.0.0:2580 -initial-cluster-token etcd-cluster -initial-cluster autumn-client0=http://192.167.0.168:2380,autumn-client1=http://192.167.0.170:2480,autumn-client2=http://192.167.0.172:2580 -initial-cluster-state new

集群验证。两种方法创建的集群可通过以下方式进行验证

  1、验证集群members。在集群中的每台机器上查看members,得出的结果应该是相同的

1
2
[root@localhost ~]# curl -L http://127.0.0.1:2379/v2/members
{"members":[{"id":"1a661f2b9997ba39","name":"autumn-client0","peerURLs":["http://192.167.0.168:2380"],"clientURLs":["http://192.168.7.168:2379"]},{"id":"4932c8ea462e079c","name":"autumn-client2","peerURLs":["http://192.167.0.172:2580"],"clientURLs":["http://192.167.0.172:2579"]},{"id":"c1dbdde07e61741e","name":"autumn-client1","peerURLs":["http://192.167.0.170:2480"],"clientURLs":[<a rel="external nofollow" href="http://192.167.0.170:2479">http://192.167.0.170:2479</a>]}]}

  2、某台机器上添加数据,其他机器上查看数据,得出的结果应该是相同的
  A上执行

1
2
[root@localhost ~]# curl -L http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello autumn"
{"action":"set","node":{"key":"/message","value":"Hello autumn","modifiedIndex":13,"createdIndex":13},"prevNode":{"key":"/message","value":"Hello world1","modifiedIndex":11,"createdIndex":11}}
  

  B、C上执行

1
2
[root@localhost ~]#  curl -L http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello autumn","modifiedIndex":13,"createdIndex":13}}

etcd api接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
基本操作api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/api.md
 
集群配置api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/members_api.md
 
鉴权认证api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Documentation/v2/auth_api.md
 
配置项:https://github.com/coreos/etcd/blob/master/Documentation/op-guide/configuration.md
 
https://coreos.com/etcd/docs/latest/runtime-configuration.html
https://coreos.com/etcd/docs/latest/clustering.html
https://coreos.com/etcd/docs/latest/runtime-configuration.html
https://coreos.com/etcd/docs/latest/
https://coreos.com/etcd/docs/latest/admin_guide.html#disaster-recovery
采用标准的restful 接口,支持http 和 https 两种协议。

服务注册与发现


传统的服务调用一般通过配置文件读取ip进行调用,这里有诸多限制,如不灵活,无法感知服务的状态,实现服务调用负载均衡复杂等缺点,而引入etcd后,问题将大大化简,这里划分为几个步骤

服务启动后向etcd注册,并上报自己的监听的端口以及当前的权重因子等信息,且对该信息设置ttl值。

服务在ttl的时间内周期性上报权重因子等信息。

client端调用服务时向etcd获取信息,进行调用,同时监听该服务是否变化(通过watch方法实现)。
当新增服务时watch方法监听到变化,将服务加入代用列表,当服务挂掉时ttl失效,client端检测到变化,将服务踢出调用列表,从而实现服务的动态扩展。

另一方面,client端通过每次变化获取到的权重因子来进行client端的加权调用策略,从而保证后端服务的负载均衡。
 

以上就是Docker微服务的ETCD集群搭建教程详解的详细内容



转载请标明出处【Docker微服务的ETCD v3.2集群搭建教程详解】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论