最简单的安装方法是直接去etcd GitHub的Release页下载预编译好的二进制文件。etcd官方为各个系统提供了不一样的二进制文件,供开发者根据本身的系统去下载。git
下载地址:github.com/etcd-io/etc…github
下载完成解压后,目录中有两个二进制文件,etcd
以及etcdctl
。其中etcd
就是运行etcd服务的二进制文件,etcdctl
是官方提供的命令行etcd客户端,使用etcdctl
能够在命令行中访问etcd服务。数据库
将etcd
和etcdctl
这两个文件软链到系统环境变量$PATH
对应的目录下,方便服务启动,固然试验目的直接把工做目录切换到刚才下载的目录直接运行两个文件便可。bash
我从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
服务ui
......
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 的称呼)。spa
启动命令时比较重要的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
。code
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
复制代码
这些操做命令基本上分为键值库操做命令和行为控制命令。
设置键(或者叫主题)的值
➜ 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' 若该键如今的索引值是指定索引,则进行设置操做
复制代码
获取给定键的值
➜ 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 将请求发给主节点,保证获取内容的一致性
复制代码
更新给定键中存储的值
➜ 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)则永不超时
复制代码
删除给定的键,若是命令参数中给定的键不存在则会报错
➜ 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 是否匹配
复制代码
建立一个目录,不管存在与否。
支持的选项为
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时
复制代码
更新一个已经存在的目录。 支持的选项为
--ttl '0' 超时时间(单位为秒),不配置(默认为 0)则永不超时
复制代码
列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。
支持的选项包括
--sort 将输出结果排序
--recursive 若是目录下有子目录,则递归输出其中的内容
-p 对于输出为目录,在最后添加 `/` 进行区分
复制代码
备份 etcd 的数据。
支持的选项包括
--data-dir etcd 的数据目录
--backup-dir 备份到指定路径
复制代码
监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。
例如
➜ 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 返回全部的键值和子键值
复制代码
监测一个键值的变化,一旦键值发生更新,就执行给定命令。
例如,用户更新 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 返回全部的键值和子键值
复制代码
经过 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
复制代码