Etcd

Table of Contents

1. Etcd 简介

etcd 是一个分布式的键值对数据存储系统,常用于实现配置的共享、服务的注册和发现。 etcd 采用 raft 一致性算法,基于 Go 语言实现。

"Etcd" stands for "/etc distributed"; it is meant to be a highly reliable configuration mechanism that provides a uniform view across a cluster of machines.

1.1. Etcd 安装

Redhat 中安装 etcd:

$ sudo yum install etcd

启动 etcd:

$ systemctl start etcd
$ systemctl status etcd

默认,etcd 为客户端提供服务的端口为 2379,集群之间同步数据的端口为 2380。

使用 etcdctl --version 查看 etcd 版本:

$ etcdctl --version
etcdctl version: 3.3.11
API version: 2

使用 curl 命令查看 etcd 版本:

$ curl localhost:2379/version
{"etcdserver":"3.3.11","etcdcluster":"3.3.0"}

2. Ectd 使用实例

下面例子主要摘自:
Getting started with etcd

2.1. 增删改查 key

使用 etcdctl set 可以设置一个 key:

$ etcdctl set /message Hello
Hello

或者,直接使用 curl 命令来设置 key:

$ curl -X PUT http://127.0.0.1:2379/v2/keys/message -d value="Hello"
{"action":"set","node":{"key":"/message","value":"Hello","modifiedIndex":4,"createdIndex":4}}

使用 etcdctl get 可以读取指定 key 的值

$ etcdctl get /message
Hello

或者,直接使用 curl 命令来读取 key 的值:

$ curl http://127.0.0.1:2379/v2/keys/message
{"action":"get","node":{"key":"/message","value":"Hello","modifiedIndex":4,"createdIndex":4}}

使用 etcdctl rm 可以删除指定 key:

$ etcdctl rm /message

或者,直接使用 curl 命令来删除 key 的值:

$ curl -X DELETE http://127.0.0.1:2379/v2/keys/message
{"action":"delete","node":{"key":"/message","modifiedIndex":19,"createdIndex":4}}

使用 etcdctl update 可以更新 key 为指定内容,如果 key 不存在则会报错。
使用 etcdctl mk 可以创建 key 并设置为指定内容,如果 key 存在则会报错。

2.2. 目录相关操作

etcdctl 目录相关操作如表 1 所示。

Table 1: etcdctl 目录相关操作
命令 说明
mkdir 创建新目录,如果目录存在会报错
setdir 创建目录,无论存在与否
updatedir 更新已存在的目录,如过期时间
rmdir 删除目录
ls 查看目录中的 key 和子目录

一般我们不用显式地创建目录,创建 key 时(如 key 名为/d1/d2/d3/k1),如果目录不存在会自动创建目录。

常用命令 etcdctl ls 支持 4 个选项:

$ etcdctl help ls
NAME:
   etcdctl ls - retrieve a directory

USAGE:
   etcdctl ls [command options] [key]

OPTIONS:
   --sort           returns result in sorted order
   --recursive, -r  returns all key names recursively for the given path
   -p               append slash (/) to directories
   --quorum, -q     require quorum for get request

2.3. Test and set(--swap-with-value)

etcd 通过 set 命令的 --swap-with-value 选项支持 TestAndSet 功能。如下面命令仅当“/message”的当前值为“Hello”时,才会设置为“Hi”。

$ etcdctl set /message "Hi" --swap-with-value "Hello"
Hi

2.4. TTL

修改命令(如 set/update/mk/setdir/updatedir/mkdir 等)都支持 --ttl 选项来设置过期时间(单位为秒)。如:

$ etcdctl set /foo "Expiring Soon" --ttl 20     # 设置 /foo 20秒后过期
Expiring Soon

2.5. 监控 key(或者目录)的变化

ectdctl watch 可以监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。
ectdctl exec-watch 可以监测一个键值的变化,一旦键值发生更新,就执行给定命令。

注:要监控目录变化可以加上 --recursive 选项。

下面是使用 etcdctl exec-watch --recursive 监控目录“/foo-service”的例子(这个目录中有 key 发生变化就执行后面指定的 shell 命令):

$ etcdctl exec-watch --recursive /foo-service -- sh -c 'echo "\"$ETCD_WATCH_KEY\" key was updated to \"$ETCD_WATCH_VALUE\" value by \"$ETCD_WATCH_ACTION\" action"'

我们,打开另外一个窗口,对目录“/foo-service”中添加信息:

$ etcdctl set /foo-service/node1 localhost:1111
localhost:1111
$ etcdctl set /foo-service/node2 localhost:2222
localhost:2222

这时,我们会发现之前的窗口监控到了“/foo-service”的变化,会输出:

$ etcdctl exec-watch --recursive /foo-service -- sh -c 'echo "\"$ETCD_WATCH_KEY\" key was updated to \"$ETCD_WATCH_VALUE\" value by \"$ETCD_WATCH_ACTION\" action"'
"/foo-service/node1" key was updated to "localhost:1111" value by "set" action
"/foo-service/node2" key was updated to "localhost:2222" value by "set" action

2.6. 集群操作(member)

通过 member 命令的参数 list、add、remove 可以分别列出、添加、删除 etcd 实例到 etcd 集群中。

下面是查看集群( etcdctl member list )的例子:

$ etcdctl member list
8e9e05c52164694d: name=default peerURLs=http://localhost:2380 clientURLs=http://localhost:2379 isLeader=true

Author: cig01

Created: <2018-10-05 Fri>

Last updated: <2019-04-20 Sat>

Creator: Emacs 27.1 (Org mode 9.4)