03、Etcd 客户端常用命令

种地  金牌会员 | 2023-5-21 23:33:20 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 962|帖子 962|积分 2886

上一讲我们安装 etcd 服务端,这一讲我们来一起学学如何使用 etcd 客户端常见的命令。文章内容来源于参考资料,如若侵权,请联系删除,谢谢。
etcd可通过客户端命令行工具 etcdctl 对etcd进行请求操作
  1. # 帮助命令,会列出所有的命令和选项,在记不太清命令的时候,可以使用
  2. etcdctl  ‐h
  3. # 对某个命令进行更加详细的介绍
  4. etcdctl put -h
复制代码
1、etcdctl 常见命令

1.1 键操作

键操作包括最常用的增删改查操作,包括PUT、GET、DELETE等命令。注意:etcd 中 PUT 当 key 不存在是新增,当 key 存在时是修改。
  1. # PUT 新增或者修改某个键的值
  2. etcdctl  put  /stu/name  /xiaole
  3. #GET获取指定键的值
  4. etcdctl        get  /stu/name
  5. #以16进制格式返回
  6. etcdctl                get  /stu/name ‐‐hex
  7. #获取key范围内的值 半开区间:左闭右开
  8. etcdctl  get  /stu/name  /xiaole3
  9. #获取指定前缀的值 可以使用选项 ‐‐limit 限制返回的key数量。
  10. etcdctl  get  ‐‐prefix  /stu/name
  11. etcdctl  get  ‐‐prefix  /stu/name  ‐‐limit3
  12. #按key的字典顺序读取
  13. #读取字典顺序大于或等于 name2 的key:
  14. etcdctl  get  ‐‐from‐key  name2
  15. # etcd可通过读取一个key来获取当前etcd服务端的版本号,不管key是否存在
  16. etcdctl  get  /stu  ‐w=json
  17. #访问以前版本的key
  18. etcdctl  get  ‐‐rev=8  /test/name  #访问第8个版本的key
复制代码
返回结果字段解析:

  • cluster_id: 请求的etcd集群ID。
  • member_id: 请求的etcd节点ID。
  • revision: etcd 服务端当前全局数据版本号。对任一 key 的 put 或 delete 操作都会使 revision自增1。revision=1 是 etcd 的保留版本号,因此用户的key版本号将从2开始。
  • raft_term: etcd当前raft主节点任期号。
  • create_revision: 当前 key 创建时全局数据版本号revision的值。
  • mod_revision: 当前key最后一次修改时全局数据版本号revision的值。
  • version: 当前key的数据版本号。key创建时 version为1,对当前key进行put操作会使version自增1,将key删除后,重新创建,version又会从1开始计数。
  1. # 删除一个键或者特定范围的键
  2. etcdctl  del  /aaa/bbb
  3. etcdctl  del  /test/name  /test/name3
  4. #可以通过选项 ‐‐prev‐kv 让命令返回对应的值
  5. etcdctl  del  ‐‐prev‐kv  name1
复制代码
1.2 watch 操作

watch 监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出
  1. etcdctl  watch  /test/name
复制代码
另一个终端输入 etcdctl  put  /test/name fox
1.3 lease(租约)操作

类似 redis 的 TTL,etcd 中的键值对可以绑定到租约上,实现存活周期控制。应用客户端可以 为 etcd 集群里面的键授予租约,一旦租约的TTL到期,租约就会过期并且所有附带的键都将被删除
  1. # 授予租约 TTL 为30s
  2. etcdctl  lease  grant  30
  3. #将 key 与 租约进行绑定
  4. etcdctl  put  ‐‐lease=705b8474d1acd63b   name   fox
复制代码
当然,应用通过租约ID可以撤销租约。撤销租约将删除所有附带的key
  1. #撤销租约
  2. etcdctl  lease  revoke  705b8474d1acd641
复制代码
应用程序可以通过刷新其TTL保持租约存活,因此不会过期
  1. # 刷新租约
  2. etcdctl lease keep-alive 705b8474d1acd63b
复制代码
应用客户端可以查询租约信息,检查续订或租约的状态,是否存在或者是否已过期
  1. # 查询租约
  2. etcdctl lease timetolive 705b8474d1acd647
  3. etcdctl lease timetolive  ‐‐keys  705b8474d1acd647
复制代码
2、etcd权限管理

2.1 权限常用命令

2.1.1 用户管理
  1. # 创建用户
  2. etcdctl user add fox
  3. # 删除用户
  4. etcdctl  user  del  fox
  5. #修改密码
  6. etcdctl  user  passwd  fox
  7. #查看所有用户
  8. etcdctl  user  list
  9. #查看指定用户及绑定角色
  10. etcdctl  user  get  fox
复制代码
2.1.2 角色管理
  1. # 创建角色
  2. etcdctl  role  add  test
  3. # 给角色赋权,可读/可写/可读写
  4. etcdctl  grant‐permission  role_name  [read|write|readwrite] /name
  5. # 回收角色赋权
  6. etcdctl  role  revoke‐permission  role_name  /name
  7. #删除角色
  8. etcdctl  role  del  test
  9. #查询角色列表
  10. etcdctl  role  list
  11. #查询指定角色的权限
  12. etcdctl  role  get  test
  13. #用户绑定角色
  14. etcdctl user grant‐role 用户名 角色名
  15. #回收用户绑定权限
  16. etcdctl user revoke‐role 用户名 角色名
  17. # 开启权限
  18. etcdctl  user  add  root
  19. #root用户存在时才能开启权限控制
  20. etcdctl  auth  enable
  21. # 查看用户列表
  22. etcdctl  user  list  ‐‐user=root
  23. # 权限使用
  24. etcdctl  ‐‐user='用户名'  ‐‐password='密码'  get  /name
复制代码
2.2 集群权限管理

2.2.1  root用户

root        用户是 etcd 默认就有的用户,拥有所有的权限
  1. # root用户自带所有权限,因此只需创建该用户,开启认证即有所有权限
  2. etcdctl  ‐‐endpoints http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379  user  add  root
  3. # 开启身份验证
  4. etcdctl  ‐‐user='root'  ‐‐password='123456'  ‐‐endpoints     http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379  auth  enable
  5. # 操作
  6. etcdctl  ‐‐user='root'  ‐‐password='123456'  put  name  fox
  7. etcdctl  ‐‐user='root'  ‐‐password='123456'  get name
复制代码
2.2.2 普通用户
  1. # 创建普通用户
  2. etcdctl  ‐‐user='root'  ‐‐password='123456'  ‐‐endpoints  http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379   user   add   fox
  3. # 创建角色
  4. etcdctl‐‐user='root'‐‐password='123456'‐‐endpointshttp://127.0.0.1:12 379,http://127.0.0.1:22379,http://127.0.0.1:32379 role add test
  5. # 用户绑定角色
  6. etcdctl  ‐‐user='root'  ‐‐password='123456'  ‐‐endpoints  http://127.0.0.1:12
  7. 379,http://127.0.0.1:22379,http://127.0.0.1:32379  user  grant‐role  fox test
  8. # 给角色赋权
  9. etcdctl  ‐‐user='root'  ‐‐password='123456'  ‐‐endpoints  http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379  role  grant‐permission  test readwrite /name
复制代码
参考资料:
bilibili视频教程

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

种地

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表