etcd分布式键值存储系统
etcd,分布式的、高可用的、一致性的key-value存储数据库(键值存储系统),基于Go语言实现,主要用于共享配置和服务发现。它是由 CoreOS(后来被 Red Hat 收购)开发的,旨在提供一个可靠的分布式协调服务。
etcd的作用
1、分布式系统中,各种服务配置信息的管理共享和服务发现是一个很基本也是很重要的问题。etcd可集中管理配置信息,服务端将配置信息存储于etcd,客户端通过etcd得到服务配置信息,etcd监听配置信息的改变,发现改变通知客户端。
2、为了防止单点故障,还可启动多个etcd组成集群。etcd集群使用raft一致性算法处理日志复制,保证多节点数据的强一致性。
etcd的raft算法
1、主节点选举,etcd集群中有一个主节点(leader,负责写操作),多个从节点(follower,负责读操作)。主节点会发送心跳包给从节点,从节点进行响应。从节点若超过一定时间(一定范围内的随机值)没有收到主节点的心跳包,则认为主节点已不可用,自身可成为候选主节点(candidate),发起投票,若超过一半节点响应,则可成为新的主节点(可能会有几轮争夺)。在每一轮投票中,参与投票的所有节点,只响应收到的第一个投票请求,对后续请求不作响应。
2.、数据更新:(1)第一阶段:主节点将修改记录到本地日志,并将日志复制给所有从节点,若超过一半节点响应,则认为操作成功,通知客户端。(2)第二阶段:主节点提交本地修改(持久化到磁盘),通知所有从节点也进行数据修改提交。
Windows下集群搭建ETCD
版本:etcd-v3.6.8-windows-amd64.zip
集群信息:
etcd01,客户端端口(listen-client):2379,集群通信端口(listen-peer):2380,数据目录:./etcd01data/;
etcd02,客户端端口(listen-client):3379,集群通信端口(listen-peer):3380,数据目录:./etcd02/data;
etcd03,客户端端口(listen-client):4379 ,集群通信端口(listen-peer):4380,数据目录:./etcd03/data;
启动脚本(bat):
@echo off
title etcd集群
start "" "./etcd01/etcd.exe" ^
--name etcd01 ^
--data-dir ./data/etcd01 ^
--advertise-client-urls http://127.0.0.1:2379 ^
--listen-client-urls http://0.0.0.0:2379 ^
--listen-peer-urls http://0.0.0.0:2380 ^
--initial-advertise-peer-urls http://127.0.0.1:2380 ^
--initial-cluster-token etcd-cluster-2026 ^
--initial-cluster etcd01=http://127.0.0.1:2380,etcd02=http://127.0.0.1:3380,etcd03=http://127.0.0.1:4380 ^
--initial-cluster-state new
start "" "./etcd02/etcd.exe" ^
--name etcd02 ^
--data-dir ./data/etcd02 ^
--advertise-client-urls http://127.0.0.1:3379 ^
--listen-client-urls http://0.0.0.0:3379 ^
--listen-peer-urls http://0.0.0.0:3380 ^
--initial-advertise-peer-urls http://127.0.0.1:3380 ^
--initial-cluster-token etcd-cluster-2026 ^
--initial-cluster etcd01=http://127.0.0.1:2380,etcd02=http://127.0.0.1:3380,etcd03=http://127.0.0.1:4380 ^
--initial-cluster-state new
start "" "./etcd03/etcd.exe" ^
--name etcd03 ^
--data-dir ./data/etcd03 ^
--advertise-client-urls http://127.0.0.1:4379 ^
--listen-client-urls http://0.0.0.0:4379 ^
--listen-peer-urls http://0.0.0.0:4380 ^
--initial-advertise-peer-urls http://127.0.0.1:4380 ^
--initial-cluster-token etcd-cluster-2026 ^
--initial-cluster etcd01=http://127.0.0.1:2380,etcd02=http://127.0.0.1:3380,etcd03=http://127.0.0.1:4380 ^
--initial-cluster-state new
pause
脚本的编辑,VSCode安装Bat插件。
@echo off:Windows批处理文件,关闭命令回显,让脚本运行更干净、更安静。
^符号在Windows 批处理(.bat)脚本中就是续行符,必须是这一行的最后一个字符,后面绝对不能有空格或制表符。
:: 这是标准批处理注释,不会输出;rem 这也是注释,会默认打印出来。
命令参数:
advertise-client-urls---->对外公布的客户端访问地址。
listen-client-urls---->客户端监听地址。
listen-peer-urls---->节点监听地址,etcd进程在本机实际绑定和监听的网卡与端口。
initial-advertise-peer-urls---->对外公布的节点通告地址。
initial-cluster-token---->集群唯一令牌,一个自定义的字符串,用来唯一标识一个etcd集群。
initial-cluster---->初始集群成员列表,格式:etcd01=http://127.0.0.1:2380,etcd02=http://127.0.0.1:3380,etcd03=http://127.0.0.1:4380。
initial-cluster-state---->集群初始状态,new:表示这是第一次创建全新的集群;existing:表示这个集群已经存在了,当前节点是作为新成员加入。
查看集群成员:
etcdctl --endpoints=http://127.0.0.1:2379 member list
成功输出(3 个节点,状态 started)
集群健康检查
etcdctl --endpoints=http://127.0.0.1:2379,http://127.0.0.1:3379,http://127.0.0.1:4379 endpoint health
跨节点读写测试
:: 写(etch01)
etcdctl --endpoints=http://127.0.0.1:2379 put /cluster/test "windows-etcd-cluster-ok"
:: 读(etch02、etch03)
etcdctl --endpoints=http://127.0.0.1:2379 get /cluster/test
etcdctl --endpoints=http://127.0.0.1:3379 get /cluster/test
etcdctl --endpoints=http://127.0.0.1:4379 get /cluster/test