原文 https://www.cnblogs.com/breg/p/5756558.html

比较重要的配置

-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

ectdctl介绍

我们可以使用etcdctl这个官方提供的客户端来对etcd进行操作,可以从github.com/coreos/etcd/releases下载。

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

etcdctl支持下面列出来的命令,基本上可以分为数据库操作和非数据库操作,可以查看etcdctl README.md来了解更多

➜  ~  etcdctl -h
NAME:
etcdctl - A simple command line client for etcd. USAGE:
etcdctl [global options] command [command options] [arguments...] VERSION:
2.2.1 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 or existing directory
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
import import a snapshot to a cluster
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 GLOBAL OPTIONS:
--debug output cURL commands which can be used to reproduce the request
--no-sync don't synchronize cluster information before sending request
--output, -o 'simple' output response in the given format (`simple`, `extended` or `json`)
--discovery-srv, -D domain name to query for SRV records describing cluster endpoints
--peers, -C a comma-delimited list of machine addresses in the cluster (default: "http://127.0.0.1:4001,http://127.0.0.1:2379")
--endpoint a comma-delimited list of machine addresses in the cluster (default: "http://127.0.0.1:4001,http://127.0.0.1:2379")
--cert-file identify HTTPS client using this SSL certificate file
--key-file identify HTTPS client using this SSL key file
--ca-file verify certificates of HTTPS-enabled servers using this CA bundle
--username, -u provide username[:password] and prompt if password is not supplied.
--timeout '1s' connection timeout per request
--total-timeout '5s' timeout for the command execution (except watch)
--help, -h show help
--version, -v print the version

数据库操作

数据库操作围绕对键值和目录的 CRUD (符合 REST 风格的一套操作:Create)完整生命周期的管理。

etcd 在键的组织上采用了层次化的空间结构(类似于文件系统中目录的概念),用户指定的键可以为单独的名字,如 testkey,此时实际上放在根目录 / 下面,也可以为指定目录结构,如 cluster1/node2/testkey,则将创建相应的目录结构。

注:CRUD 即 Create, Read, Update, Delete,是符合 REST 风格的一套 API 操作。

set

指定某个键的值。例如

➜  ~  etcdctl set /testdir/testkey "Hello world"
Hello world

支持的选项包括:

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

get

获取指定键的值。例如

➜  ~  etcdctl get /testdir/testkey
Hello world

当键不存在时,则会报错。例如

➜  ~  etcdctl get /testdir/testkey2
Error: 100: Key not found (/testdir/testkey2) [18]

支持的选项为

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

update

当键存在时,更新值内容。例如

➜  ~  etcdctl update /testdir/testkey "Hello"
Hello

当键不存在时,则会报错。例如

➜  ~  etcdctl update /testdir/testkey2 "Hello"
Error: 100: Key not found (/testdir/testkey2) [19]

支持的选项为

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

rm

删除某个键值。例如

➜  ~  etcdctl rm /testdir/testkey
PrevNode.Value: Hello

当键不存在时,则会报错。例如

➜  ~  etcdctl rm /testdir/testkey
Error: 100: Key not found (/testdir/testkey) [20]

支持的选项为

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

mk

如果给定的键不存在,则创建一个新的键值。例如

➜  ~  etcdctl mk /testdir/testkey "Hello world"
Hello world

当键存在的时候,执行该命令会报错,例如

➜  ~  etcdctl mk /testdir/testkey "Hello world"
Error: 105: Key already exists (/testdir/testkey) [21]

支持的选项为

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

mkdir

如果给定的键目录不存在,则创建一个新的键目录。例如

➜  ~  etcdctl mkdir testdir2

当键目录存在的时候,执行该命令会报错,例如

➜  ~  etcdctl mkdir testdir2
Error: 105: Key already exists (/testdir2) [22]

支持的选项为

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

setdir

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

支持的选项为

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

updatedir

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

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

rmdir

删除一个空目录,或者键值对。

➜  ~  etcdctl setdir dir1
➜ ~ etcdctl rmdir dir1

若目录不空,会报错

➜  ~  etcdctl set /dir/testkey hi
hi
➜ ~ etcdctl rmdir /dir
Error: 108: Directory not empty (/dir) [29]

ls

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

例如

➜  ~  etcdctl ls
/testdir
/testdir2
/dir
➜ ~ etcdctl ls dir
/dir/testkey

支持的选项包括

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

非数据库操作

backup

备份 etcd 的数据。

支持的选项包括

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

watch

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

例如,用户更新 testkey 键值为 Hello watch。

➜  ~  etcdctl get /testdir/testkey
Hello world
➜ ~ etcdctl set /testdir/testkey "Hello watch"
Hello watch
➜  ~  etcdctl watch testdir/testkey
Hello watch

支持的选项包括

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

exec-watch

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

例如,用户更新 testkey 键值。

➜  ~  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 服务实例后,可以用如下命令进行查看。

$ etcdctl member list
ce2a822cea30bfca: name=default peerURLs=http://localhost:2380,http://localhost:7001 clientURLs=http://localhost:2379,http://localhost:4001

命令选项

--debug 输出 cURL 命令,显示执行命令的时候发起的请求
--no-sync 发出请求之前不同步集群信息
--output, -o 'simple' 输出内容的格式 (simple 为原始信息,json 为进行json格式解码,易读性好一些)
--peers, -C 指定集群中的同伴信息,用逗号隔开 (默认为: "127.0.0.1:4001")
--cert-file HTTPS 下客户端使用的 SSL 证书文件
--key-file HTTPS 下客户端使用的 SSL 密钥文件
--ca-file 服务端使用 HTTPS 时,使用 CA 文件进行验证
--help, -h 显示帮助命令信息
--version, -v 打印版本信息

etcd 命令行(转)的更多相关文章

  1. etcd 命令行

    比较重要的配置 -name 节点名称,默认是UUID-data-dir 保存日志和快照的目录,默认为当前工作目录-addr 公布的ip地址和端口. 默认为127.0.0.1:2379-bind-add ...

  2. 浅入深出ETCD之【简介与命令行使用】

    前言 你知道etcd吗?随着k8s的使用广泛之后,etcd被非常多的人所知道,同时又因为它可靠的分布式特性被很多人喜欢.所以,我准备有几篇博文来记录一下,从基本使用到线上部署再到原理分析,做一个系列. ...

  3. CloudFoundry命令行和Kubernetes命令行的Restful API消费方式

    先说CloudFoundry的命令行工具CLI.我们在CloudFoundry环境下工作,第一个使用的命令就是cf login. 如果在环境变量里维护CF_TRACE的值为true: 则我们能发现,诸 ...

  4. 教你用Cobra开发类似docker的命令行

    目录 前言 一.安装 二.初始化应用 gomod初始化 创建入口文件cmd/root.go 创建主程序main.go 三.生成Command 创建hello子命令 创建version子命令 四.如何设 ...

  5. golang常用库:cli命令行/应用程序生成工具-cobra使用

    golang常用库:cli命令行/应用程序生成工具-cobra使用 一.Cobra 介绍 我前面有一篇文章介绍了配置文件解析库 Viper 的使用,这篇介绍 Cobra 的使用,你猜的没错,这 2 个 ...

  6. Go通过cobra快速构建命令行应用

    来自jetbrains Go 语言现状调查报告 显示:在go开发者中使用go开发实用小程序的比例为31%仅次于web,go得益于跨平台.无依赖的特性,用来编写命令行或系统管理这类小程序非常不错. 本文 ...

  7. Cmder--Windows下命令行利器

    cmder cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令. 安装包 安装包链接 下载后,直接解压即用. 修改命令提示符λ为 ...

  8. .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门

    2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...

  9. 让 windows 下的命令行程序 cmd.exe 用起来更顺手

    在 Windows 下使用 Larave 框架做开发,从 Composer 到 artisan 总是避免不了和 cmd.exe 打交道,系统默认的命令行界面却是不怎么好看,且每行显示的字符数是做了限制 ...

随机推荐

  1. mvc core2.1 Identity.EntityFramework Core 用户列表预览 删除 修改 (五)

    用户列表预览 Controllers->AccountController.cs [HttpGet] public IActionResult Index() { return View(_us ...

  2. java依赖注入(injection)

    和SpringSource分别通过其开源项目Guice及Spring Framework提供了依赖注入的功能.然而直到现在开发者也没有一种标准的.独立于供应商的方式从而无需修改其源文件就能在这些框架之 ...

  3. 如何取消noarch.rpm包

    有一次部署zabbix服务器,不小心rpm -ivh zabbix的el7版本的rpm了,但是我的系统是centos6.5的,所以就尴尬了 rpm -ivh http://repo.zabbix.co ...

  4. nginx屏蔽某一ip的访问

    假设我们想禁止访问nginx次数最多的ip访问我们的网站 我们可以先查出那个ip访问次数最多 awk '{print $1}' nginx.access.log |sort |uniq -c|sort ...

  5. 数学的语言 化无形为可见 (Keith Devlin 著)

    第一章 数字为何靠的住 (已看) 第二章 心智的模式 (已看) 第三章 动静有数 (已看) 第四章 当数学成型 (已看) 第五章 数学揭开美之本质 (已看) 第六章 当数学到位 (已看) 第七章 数学 ...

  6. php7 php-fpm 重启

    PHP7中php.ini.php-fpm和www.conf的配置 http://www.tuicool.com/articles/NjmQNj6 php-fpm 关闭:  kill -SIGINT ` ...

  7. oracle-sql系统学习-ddl-dml

    e41084-04 oracle database sql language reference 11g release 2 sql语句类型 ddl alter ...除了alter session和 ...

  8. 深入详解美团点评CAT跨语言服务监控(六)消息分析器与报表(一)

    大众点评CAT微服务监控架构对于消息的具体处理,是由消息分析器完成的,消息分析器会轮训读取PeriodTask中队列的消息来处理,一共有12类消息分析器,处理后的结果就是生成各类报表. 消息分析器的构 ...

  9. python 用type()创建类

    type()可以查看一个类型,也可以查看变量的类型 class Hello1(object): def hello(self, name = 'world'): print('Hello, %s' % ...

  10. C# ASCII与字符串间相互转换 (转)

    引言: 最近开始学习C#,在写串口助手小工具时遇到十六进制发送与字符发送之间转换的问题, 小弟通过网络各路大神的帮助下,终于实现正确显示收发,小弟菜鸟一枚,不足之处还望各位批评指正O(∩_∩)O! 其 ...