概述:对etcd及安装版本的选择进行了简介,介绍了集群部署流程,部署完成后进行了简单测试。
coreos 基于go语言开发的分布式服务系统,内部采用 raft协议作为一致性算法。作为服务发现系统,有以下的特点:
· 简单:安装配置简单,而且提供了 HTTP API 进行交互,使用也很简单
· 安全:支持 SSL 证书验证
· 快速:根据官方提供的 benchmark 数据,单实例支持每秒 2k+ 读操作
· 可靠:采用 raft 算法,实现分布式系统数据的可用性和一致性
etcd 也支持单点部署,但是在生产环境中推荐集群方式部署,一般 etcd 节点数会选择 3、5、7。etcd 会保证所有的节点都会保存数据,并保证数据的一致性和正确性。
· 事务:ETCD V3提供了多键条件事务(multi-key conditional transactions),应用各种需要使用事务代替原来的Compare-And-Swap操作。
· 平键空间(Flat keyspace):ETCD V3不再使用目录结构,只保留键。例如:”/a/b/c/“是一个键,而不是目录。V3中提供了前缀查询,来获取符合前缀条件的所有键值,这变向实现了V2中查询一个目录下所有子目录和节点的功能。
· 简洁的响应:像DELETE这类操作成功后将不再返回操作前的值。如果希望获得删除前的值,可以使用事务,来实现一个原子操作,先获取键值,然后再删除。
· 租约:租约代替了V2中的TTL实现,TTL绑定到一个租约上,键再附加到这个租约上。当TTL过期时,租约将被销毁,同时附加到这个租约上的键也被删除。
注意:v2与v3 不兼容,采用了不同的数据存储结构,实际上2已结被放弃了。
但是如果不设置 ETCDCTL_API=3,则默认是的API版本是2(将ETCDCTL_API=3加入到path中,避免每次都得export)
环境:
node |
IP |
OS |
etcd_version |
etcd0 |
192.168.169.36 |
CentOS7.0 |
etcd3.2.4 |
etcd1 |
192.168.169.37 |
centos7.0 |
etcd3.2.4 |
etcd2 |
192.168.169.38 |
centos7.0 |
etcd3.2.4 |
3.1 etcd在各个节点上的安装
1.下载etcd源码包:https://github.com/coreos/etcd/releases,这里用的是etcd-v3.2.4-Linux-amd64.tar.gz,解压并添加etcd命令至环境变量。
2.Etcd属于server ,etcdctl 属于client,二者之间通过http协议进行通信。
有各种语言的客户端封装包,java/go等。Etcdctl 参数:
COMMANDS:
1. backup backup an etcddirectory
2. cluster-health check the health of the etcd cluster
3. mk make a new key with a given value
4. mkdir make a new directory
5. rm remove a key or a directory
6. rmdir removes the key if it is an emptydirectory or a key-value pair
7. get retrieve the value of a key
8. ls retrieve adirectory
9. set set thevalue of a key
10. setdir create a new directory or update anexisting directory TTL
11. update update an existing keywith a given value
12. updatedir update an existing directory
13. watch watch a key for changes
14. exec-watch watch a key for changes and exec anexecutable
15. member member add, remove andlist subcommands
16. import import asnapshot to a cluster
17. user user add, grant and revokesubcommands
18. role role add, grant and revoke subcommands
19. auth overall auth controls
3.2 etcd集群部署
简述:在每个节点上写配置文件,节点之间通过配置文件找到彼此的位置(ip、端口),然后通过心跳消息、raft协议构建集群。
3.2.1 配置项说明
配置方式有多种:静态(已知各个几点的情况下,推荐静态配置)、动态配置(节点间彼此自我发现)
静态配置用于在配置前已经明确各种信息的情况,比如集群的大小,各member的ip,端口等信息。各member依靠配置得知其他member的联系地址。当然ip等信息可以通过环境变量传进去,不一定要写死。
静态配置参数:
--name
etcd集群中的节点名,这里可以随意,可区分且不重复就行
--listen-peer-urls
监听的用于节点之间通信的url,可监听多个,集群内部将通过这些url进行数据交互(如选举,数据同步等)
--initial-advertise-peer-urls
建议用于节点之间通信的url,节点间将以该值进行通信。
--listen-client-urls
监听的用于客户端通信的url,同样可以监听多个。
--advertise-client-urls
建议使用的客户端通信url,该值用于etcd代理或etcd成员与etcd节点通信。
--initial-cluster-token etcd-cluster-1
节点的token值,设置该值后集群将生成唯一id,并为每个节点也生成唯一id,当使用相同配置文件再启动一个集群时,只要该token值不一样,etcd集群就不会相互影响。
--initial-cluster
也就是集群中所有的initial-advertise-peer-urls的合集
--initial-cluster-state new
新建集群的标志
关闭防火墙关闭防火墙关闭防火墙
systemctl stopfirewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
集群建立阶段
在第一个节点上执行:
etcd --name etcd0--initial-advertise-peer-urls http://192.168.169.36:2380 \
--listen-peer-urls http://0.0.0.0:2380 \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://192.168.169.36:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster etcd0=http://192.168.169.36:2380,etcd1=http://192.168.169.37:2380,etcd2=http://192.168.169.38:2380 \
--initial-cluster-state new
在第二个节点上执行:
etcd --name etcd1--initial-advertise-peer-urls http://192.168.169.37:2380 \
--listen-peer-urls http://0.0.0.0:2380 \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://192.168.169.37:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster etcd0=http://192.168.169.36:2380,etcd1=http://192.168.169.37:2380,etcd2=http://192.168.169.38:2380 \
--initial-cluster-state new
在第三个节点上执行:
etcd --name etcd2 --initial-advertise-peer-urls http://192.168.169.38:2380 \
--listen-peer-urls http://0.0.0.0:2380 \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://192.168.169.38:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster etcd0=http://192.168.169.36:2380,etcd1=http://192.168.169.37:2380,etcd2=http://192.168.169.38:2380 \
--initial-cluster-state new
集群构建完成,检测集群是否部署成功:
查看集群节点:
数据一致性测试:
在一个节点写入数据后其他节点能够感知,但是是收到写入的,时效性无法保证。
PS:
目前没有进行读写性能、稳定性、数据一致性、异常处理(主
/
从
宕机)等测试。