1. etcd 客户端

etcdctl 是一个命令行客户端,便于我们进行服务测试或手动修改数据库内容,etcdctl 在两个不同的 etcd 版本(v2 和 v3)下的功能和使用方式也完全不同。

一般通过如下方式来指定使用 etcd 的版本:

export ETCDCTL_API=2
export ETCDCTL_API=3

在前面我们已经在 /tmp/etcd-download-test/ 安装了 etcd,那么就可以将当前的安装路径加到环境变量 PATH 中,如下:

vi ~/.bashrc

在最后加入

export PATH=$PATH:/tmp/etcd-download-test/

然后执行以下命令让其生效:

source ~/.bashrc

此时就可以直接输入 etcdctl 命令进行操作了,如下:

wohu@ubuntu-dev:~/tools$ etcdctl -h
NAME:
	etcdctl - A simple command line client for etcd3.

USAGE:
	etcdctl [flags]

VERSION:
	3.4.15

API VERSION:
	3.4

支持的 COMMANDS 见下图:

grpc etcd服务注册 etcd 客户端_bc

支持的 OPTIONS 见下图:

grpc etcd服务注册 etcd 客户端_grpc etcd服务注册_02

etcdctl 支持的命令大体上分为数据库操作和非数据库操作两类。

  • 数据库操作基本围绕着对键值和目录的 CRUD 操作(即增删改查);
  • 非数据库操作如用户、角色、授权、认证相关;

etcd 在键的组织上采用了类似文件系统中目录的概念,即层次化的空间结构,我们指定的键可以作为键名,实际上,此时键值对放于根目录 / 下面。

我们也可以为键的存储指定目录结构,如 /cluster/node/key,如果不存在 /cluster/node 目录,则 etcd Server 将会创建相应的目录结构。

2 键值对操作

键操作包括最常用的增删改查操作,包括 PUTGETDELETE 等命令。

  • PUT 设置或者更新某个键的值
wohu@ubuntu-dev:~/tools$ etcdctl put /home/wohu/key1 "hello world1"
OK
wohu@ubuntu-dev:~/tools$ etcdctl put /home/wohu/key2 "hello world2"
OK
wohu@ubuntu-dev:~/tools$ etcdctl put /home/wohu/key3 "hello world3"
OK
wohu@ubuntu-dev:~/tools$
  • GET 获取指定键的值
wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/key3 
/home/wohu/key3
hello world3
wohu@ubuntu-dev:~/tools$

加上 --print-value-only 可以读取对应的值。

wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/key3  --print-value-only
hello world3
wohu@ubuntu-dev:~/tools$

GET 通过选项 --hex 获取指定键的对应值的 16 进制格式

wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/key3 --hex
\x2f\x68\x6f\x6d\x65\x2f\x77\x6f\x68\x75\x2f\x6b\x65\x79\x33
\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x33
wohu@ubuntu-dev:~/tools$

十六进制在 etcd 中有多处使用,如租约 ID 也是十六进制。

  • GET 指定键范围内的值
wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/key1 /home/wohu/key3
/home/wohu/key1
hello world1
/home/wohu/key2
hello world2
wohu@ubuntu-dev:~/tools$

上述操作获取了大于等于 /home/wohu/key1,且小于 /home/wohu/key3 的键值对。key3 不在范围之内,因为范围是半开区间 [key1, key3),不包含 key3

  • 通过 --prefix 获取指定键前缀的所有键值对
wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/ --prefix
/home/wohu/key1
hello world1
/home/wohu/key2
hello world2
/home/wohu/key3
hello world3
wohu@ubuntu-dev:~/tools$

这样就能获取所有以 /home/wohu/ 开头的键值对,当前缀获取的结果过多时,还可以通过 --limit=N 限制获取的数量,其中 N 为指定的要返回的个数。

wohu@ubuntu-dev:~/tools$ etcdctl get /home/wohu/ --prefix --limit=2
/home/wohu/key1
hello world1
/home/wohu/key2
hello world2
wohu@ubuntu-dev:~/tools$
  • DELETE 删除一个键或者特定范围的键
  1. 删除单个键
wohu@ubuntu:~$ etcdctl get /home/wohu/key4
/home/wohu/key4
hello world4
wohu@ubuntu:~$ etcdctl del /home/wohu/key4
1
wohu@ubuntu:~$ etcdctl get /home/wohu/key4
wohu@ubuntu:~$
  1. 删除某一范围的键,删除键区间为 [key1 key3),返回已经删除键的个数
wohu@ubuntu:~$ etcdctl del /home/wohu/key1 /home/wohu/key3
2
wohu@ubuntu:~$
  1. --prev-kv 删除键并返回该键的值
wohu@ubuntu:~$ etcdctl del /home/wohu/key1 --prev-kv 
1
/home/wohu/key1
hello world1
wohu@ubuntu:~$ etcdctl get /home/wohu/key1
wohu@ubuntu:~$
  1. --prefix 删除指定前缀的键
wohu@ubuntu:~$ etcdctl get /home/wohu/key --prefix
/home/wohu/key1
hello world1
/home/wohu/key2
hello world2
/home/wohu/key3
hello world3
wohu@ubuntu:~$ etcdctl del /home/wohu/key --prefix
3
wohu@ubuntu:~$ etcdctl get /home/wohu/key --prefix
wohu@ubuntu:~$

3 watch 对象

watch 监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。

其中 watch 终端和 put 分别在不同的终端。

  1. 监听单个键
  2. grpc etcd服务注册 etcd 客户端_ubuntu_03

  3. 监听某个范围的键
  4. grpc etcd服务注册 etcd 客户端_grpc etcd服务注册_04

etcd 保存修订版本以便应用客户端可以读取键的历史版本。但是,为了避免积累无限数量的历史数据,需要对历史的修订版本进行压缩 compact 。经过压缩,etcd 删除历史修订版本,释放存储空间,且在压缩修订版本之前的数据将不可访问。

4 lease 对象

lease 意为租约,类似于 Redis 中的 TTL(Time To Live)etcd 中的键值对可以绑定到租约上,实现存活周期控制。在实际应用中,常用来实现服务的心跳,即服务在启动时获取租约,将租约与服务地址绑定,并写入 etcd 服务器,为了保持心跳状态,服务会定时刷新租约。

4.1 授予租约

应用客户端可以为 etcd 集群里面的键授予租约。当键被附加到租约时,它的存活时间被绑定到租约的存活时间,而租约的存活时间相应的被 TTL 管理。在授予租约时,每个租约的最小 TTL 值由应用客户端指定。

一旦租约的 TTL 到期,租约就会过期并且所有附带的键都将被删除。

创建一个租约,时间为 100s

wohu@ubuntu:~$ etcdctl lease grant 100
lease 694d78e3bc12d11a granted with TTL(100s)

将创建的租约绑定到键 /home/wohu/key1 上

wohu@ubuntu:~$ etcdctl put /home/wohu/key1 "hello" --lease=694d78e3bc12d11a
OK

查询键,在租约有效期内,可以获取到键值,租约到期后键值对被删除。

wohu@ubuntu:~$ etcdctl get /home/wohu/key1 
/home/wohu/key1
hello
wohu@ubuntu:~$ etcdctl get /home/wohu/key1 
wohu@ubuntu:~$

4.2 撤销租约

应用通过租约 ID 可以撤销租约。撤销租约将删除所有附带的 key

wohu@ubuntu:~$ etcdctl lease grant 100000
lease 694d78e3bc12d122 granted with TTL(100000s)
wohu@ubuntu:~$ etcdctl put /home/wohu/key1 "hello" --lease=694d78e3bc12d122
OK
wohu@ubuntu:~$ etcdctl lease revoke 694d78e3bc12d122
lease 694d78e3bc12d122 revoked
wohu@ubuntu:~$ etcdctl get /home/wohu/key1

4.3 刷新租期

应用程序可以通过刷新其 TTL 保持租约存活,确保其不会过期。

wohu@ubuntu:~$ etcdctl lease grant 30
lease 694d78e3bc12d134 granted with TTL(30s)
wohu@ubuntu:~$ etcdctl lease keep-alive  694d78e3bc12d134
lease 694d78e3bc12d134 keepalived with TTL(30)
lease 694d78e3bc12d134 keepalived with TTL(30)
lease 694d78e3bc12d134 keepalived with TTL(30)
lease 694d78e3bc12d134 keepalived with TTL(30)
lease 694d78e3bc12d134 keepalived with TTL(30)

4.4 查询租期

客户端可以查询租赁信息,检查续订或租赁的状态,是否存在或者是否已过期。应用客户端还可以查询特定租约绑定的 key

wohu@ubuntu:~$ etcdctl lease grant 300
lease 694d78e3bc12d137 granted with TTL(300s)
wohu@ubuntu:~$ etcdctl put /home/wohu/key1 "hello" --lease=694d78e3bc12d137
OK
wohu@ubuntu:~$ etcdctl lease timetolive 694d78e3bc12d137
lease 694d78e3bc12d137 granted with TTL(300s), remaining(271s)
wohu@ubuntu:~$ etcdctl lease timetolive 694d78e3bc12d137 --keys
lease 694d78e3bc12d137 granted with TTL(300s), remaining(261s), attached keys([/home/wohu/key1])
wohu@ubuntu:~$