文章转载自公众号:AustinDatabases
.
接着上期说,既然在众多的PostgreSQL的高可用中Patroni是有让他被选择成为PostgreSQL的一种高可用方式的先进性,那下面就有另一个选择点,为什么选择etcd来作为Patroni的分布式协调服务,而不是zookeeper,或其他的方式。
Etcd本身是基于go语言编写的,具有下载后马上可以使用的类型,与zookeeper的结构相比,不需要jvm的支持。这点在配置上就会省去一些精力和时间。

simple,secure,fast,reliable表达的etcd最初的设计初衷。

Etcd is an open source distributed key-value store used to hold and manage the critical information that distributed systems need to keep running.
ZooKeeper is an open source Apache project that provides a centralized service for providing configuration information,naming,synchronization and group services over large clusters in distributed systems.
从上面的定义看etcd主要的工作是存储分布式键值,管理重要的信,保证分布式系统的运行。zookeeper提供的是集中化的服务,为大型分布式的 cluster提供配置信息,命名,同步等服务,zookeeper使用的是zab协议,而etcd使用的是raft协议,raft协议已经被不少的分布式数据库中使用,定义明确简单,实现方便,对比paxos协议(之前写过raft和paxos相关协议的文字),leader,follower,candidate三种服务器的状态,在raft集群中的所有服务器中有体现,一个时刻只能有一个leader其他为follower,当出现问题的情况下,会出现candidate状态,最终选举出new leader。
另外使用etcd作为patroni的搭档,在etcd的官网上有这样一个信息

ETCD整体的内部结构

整体的工作简单的描述,当用户的情况通过客户端或者http协议、https协议机械能访问,首先通过http server接口,将请求转发给store进行具体的事务处理,在有关整体集群的状态变更等问题会交给raft模块处理,最后在本地通过wal模块进行记录,在快速的提交给其他的节点进行信息同步。
关于信息的写入,有以下特点
对于etcd的节点中的信息写在所有节点都可以进行,但如果在follower节点的写会发送到leader节点,leader节点在将这些数据转发到follower节点。
作者:kevinyan
链接:https://juejin.im/post/6844903970461351944
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
安装上比较方便,去官网下载对应的安装包即可
etcd-v3.4.7-linux-amd64.tar.gz以3.4.7版本的etcd解压后可以看到里面有两个文件。
直接在对应账号中将系统变量设置,并且将相关的文件拷贝到你设置的目录。

Etcd通过2379 client request和2380两个端口进行数据的沟通,2379是etcd 与客户进行沟通的断开2380是etcd与另一个etcd之间沟通的通道。

启动单机非常简单,直接运行etcd命令即可。上图为执行命令后的打印信息,明确的相关的端口。
Etcd的配置方法主要分成1变量设置,2配置文件设置,3执行命令中的后缀配置,其中配置文件的优先级最高。
下面通过一个etcd的配置文件来说明一些相关的配置信息:# This is the configuration file for the etcd server.# Human-readable name for this member.name: 'default' 每一个etcd都需要有一个名字,默认使用default# Path to the data directory.# Path to the dedicated wal directory.# Time (in milliseconds) of a heartbeat interval.heartbeat-interval:100心跳间隔,目前默认是100ms# Time (in milliseconds) for an election to timeout.election-timeout:1000选举节点状态的时间# Raise alarms when backend size exceeds the given quota. 0 means use the# List of comma separated urls to listen on for peer traffic.listen-peer-urls:http://localhost:2380此项配置是对于etcd对于多节点之间的信息互通的端口设计,一般希望指定IP定向监听# List of comma separated urls to listen on for client traffic.listen-client-urls:http://localhost:2379# List of this member's peer urls to advertise to the rest of the cluster.# The urls needed to be a comma-separated list.initial-advertise-peer-urls: http://localhost:2380这个位置是设置PostgreSQL的对于集群中自己定位的地址# List of this member's client urls to advertise to the public.# The urls needed to be a comma-separated list.advertise-client-urls:http://localhost:2379# Initial cluster configuration for bootstrapping.初始化集群,类似数据库里面的peer在PXC或MGR中都需要有集群中所有节点的列表# Initial cluster token for the etcd cluster during bootstrap.initial-cluster-token:'etcd-cluster'# Initial cluster state('new' or 'existing').initial-cluster-state:'new'# Accept etcd V2 client requests# Enable runtime profiling data via http server# Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd.下面是其中一个etcd的配置信息,其他节点的配置信息在部分位置有所改变listen-peer-urls:'http://192.168.198.68:2380'listen-client-urls:'http://0.0.0.0:2379'initial-advertise-peer-urls:'http://192.168.198.68:2380'advertise-client-urls:'http://192.168.198.68:2379'initial-cluster: 'etcd1=http://192.168.198.66:2380,etcd2=http://192.168.198.67:2380,etcd3=http://192.168.198.68:2380'initial-cluster-token:'etcd'initial-cluster-state:'new'strict-reconfig-check:falseetcd--config-file/etc/etcd.conf>/etcd/etcd3.log2>&1&依次启动三个机器,启动完毕后,我们可以查看端口2379、2380都已经启动etcdctl member list--write-out=tableETCDCTL_API=3 etcdctl endpoint status --write-out=table --endpoints=http://192.168.198.66:2379 
ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379etcdctl --write-out=table --endpoints=$ENDPOINTS member listENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379ETCDCTL_API=3 etcdctl endpoint status --write-out=table --endpoints=$ENDPOINTS中国开源软件推进联盟PostgreSQL分会(简称:中国PG分会)于2017年成立,由国内多家PostgreSQL生态企业所共同发起,业务上接受工信部中国电子信息产业发展研究院指导。中国PG分会是一个非盈利行业协会组织。我们致力于在中国构建PostgreSQL产业生态,推动PostgreSQL产学研用发展。
做你的舞台,show出自己的才华 。
投稿邮箱:partner@postgresqlchina.com
——愿能安放你不羁的灵魂