菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
0
0

etcd 常用操作介绍

原创
05/13 14:22
阅读数 401

安装

最简单的安装方法是直接去 etcd GitHub 的 Release 页下载预编译好的二进制文件。etcd 官方为各个系统提供了不同的二进制文件,供开发者根据自己的系统去下载。

下载地址:https://github.com/etcd-io/etcd/releases

下载完成解压后,目录中有两个二进制文件,etcd 以及 etcdctl。其中 etcd 就是运行 etcd 服务的二进制文件,etcdctl 是官方提供的命令行 etcd 客户端,使用 etcdctl 可以在命令行中访问 etcd 服务。

将 etcd 和 etcdctl 这两个文件软链到系统环境变量 $PATH 对应的目录下,方便服务启动,当然试验目的直接把工作目录切换到刚才下载的目录直接运行两个文件即可。

我从 GitHub 上下载了 MacOS 对应的 etcd 文件,执行下面的命令可以看到 etcd 的版本

➜  etcd-v3.3.17-darwin-amd64 ./etcd --version
etcd Version: 3.3.17
Git SHA: 6d8052314
Go Version: go1.12.9
Go OS/Arch: darwin/amd64
➜  etcd-v3.3.17-darwin-amd64 

运行

直接运行 etcd 指令在电脑上启动和运行 etcd 服务

......
2019-10-22 13:15:32.244300 I | embed: listening for peers on http://localhost:2380
2019-10-22 13:15:32.244466 I | embed: listening for client requests on localhost:2379
......

通过启动命令的输出日志中可以找到两行关键的信息,etcd 服务启动后提供给外部客户端通信的端口是 2379,而 etcd 服务中成员间的通信端口是 2380(Peer 是对同一个 etcd 集群中另外一个 Member 的称呼)。

启动命令时比较重要的 options:

-name 节点名称,默认是 UUID
-data-dir 保存日志和快照的目录,默认为当前工作目录
-addr 公布的 ip 地址和端口。 默认为 127.0.0.1:2379
-bind-addr 用于客户端连接的监听地址,默认为 - addr 配置
-peers 集群成员逗号分隔的列表,例如 127.0.0.1:2380,127.0.0.1:2381
-peer-addr 集群服务通讯的公布的 IP 地址,默认为 127.0.0.1:2380.
-peer-bind-addr 集群服务通讯的监听地址,默认为 - peer-addr 配置

上述配置也可以设置配置文件,默认为 /etc/etcd/etcd.conf。

使用 etcd

etcdctl 是一个命令行的客户端,它提供了一下简洁的命令,可以方便我们在对服务进行测试或者手动修改数据库内容。建议刚刚接触 etcd 的同学可以先通过 etcdctl 来熟悉相关操作。这些操作跟 etcd 提供的 HTTP API 是对应的。

通过 -h 选项可以看到 etcdctl 支持的操作。

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl -h
NAME:
   etcdctl - A simple command line client for etcd.
......

VERSION:
   3.3.17

COMMANDS:
     backup          backup an etcd directory
     cluster-health  check the health of the etcd cluster
     mk              make a new key with a given value
     mkdir           make a new directory
     rm              remove a key or a directory
     rmdir           removes the key if it is an empty directory or a key-value pair
     get             retrieve the value of a key
     ls              retrieve a directory
     set             set the value of a key
     setdir          create a new directory or update an existing directory TTL
     update          update an existing key with a given value
     updatedir       update an existing directory
     watch           watch a key for changes
     exec-watch      watch a key for changes and exec an executable
     member          member add, remove and list subcommands
     user            user add, grant and revoke subcommands
     role            role add, grant and revoke subcommands
     auth            overall auth controls
     help, h         Shows a list of commands or help for one command

这些操作命令基本上分为键值库操作命令和行为控制命令。

键值库操作

set

设置键(或者叫主题)的值

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl set /root/test/keyOne "Hello etcd"
Hello etcd
➜  etcd-v3.3.17-darwin-amd64 

支持的选项包括:

--ttl '0'            该键值的超时时间(单位为秒),不配置(默认为 0)则永不超时
--swap-with-value value 若该键现在的值是 value,则进行设置操作
--swap-with-index '0'    若该键现在的索引值是指定索引,则进行设置操作

get

获取给定键的值

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl get /root/test/keyOne          
Hello etcd
➜  etcd-v3.3.17-darwin-amd64 

当尝试获取不存的值时会报错

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl get /root/test/keyTwo
Error:  100: Key not found (/root/test/keyTwo) [11]
➜  etcd-v3.3.17-darwin-amd64 

支持的选项为

--sort    对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性

update

更新给定键中存储的值

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl update /root/test/keyOne "Hello World"
Hello World
➜  etcd-v3.3.17-darwin-amd64 

同样尝试更新不存在的值时会报错

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl update /root/test/keyTwo "Hello World"
Error:  100: Key not found (/root/test/keyTwo) [11]

支持的选项为

--ttl '0'    超时时间(单位为秒),不配置(默认为 0)则永不超时

rm

删除给定的键,如果命令参数中给定的键不存在则会报错

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl rm /root/test/keyOne              
PrevNode.Value: Hello World
➜  etcd-v3.3.17-darwin-amd64 

--dir        如果键是个空目录或者键值对则删除
--recursive        删除目录和所有子键
--with-value     检查现有的值是否匹配
--with-index '0'    检查现有的 index 是否匹配

setdir

创建一个目录,无论存在与否。

支持的选项为

--ttl '0'    超时时间(单位为秒),不配置(默认为 0)则永不超时

updatedir

更新一个已经存在的目录。 支持的选项为

--ttl '0'    超时时间(单位为秒),不配置(默认为 0)则永不超时

ls

列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。

支持的选项包括

--sort    将输出结果排序
--recursive    如果目录下有子目录,则递归输出其中的内容
-p        对于输出为目录,在最后添加 `/` 进行区分

行为操作

backup

备份 etcd 的数据。

支持的选项包括

--data-dir         etcd 的数据目录
--backup-dir     备份到指定路径

watch

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

例如

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl set root/test/KeyThree "Hello etcd"
Hello etcd // 设置root/test/KeyThree的值
// 监控root/test/KeyThree,在其他会话里将它的值改为"Hello World" 这里就能收到更新后的结果
➜  etcd-v3.3.17-darwin-amd64 ./etcdctl watch  root/test/KeyThree --forever
Hello World

支持的选项包括

--forever        一直监测,直到用户按 `CTRL+C` 退出
--after-index '0'    在指定 index 之前一直监测
--recursive        返回所有的键值和子键值

exec-watch

监测一个键值的变化,一旦键值发生更新,就执行给定命令。

例如,用户更新 testkey 键值。

➜ etcd-v3.3.17-darwin-amd64 ./etcdctl exec-watch testkey -- sh -c 'ls'
default.etcd
Documentation
etcd
etcdctl
etcd-migrate
README-etcdctl.md
README.md

支持的选项包括

--after-index '0'    在指定 index 之前一直监测
--recursive        返回所有的键值和子键值

member

通过 list、add、remove 命令列出、添加、删除 etcd 实例到 etcd 集群中。

例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。

➜  etcd-v3.3.17-darwin-amd64 ./etcdctl member list
8e9e05c52164694d: name=default peerURLs=http://localhost:2380 clientURLs=http://localhost:2379 isLeader=true
➜  etcd-v3.3.17-darwin-amd64 

发表评论

0/200
0 点赞
0 评论
收藏
为你推荐 换一批